magic_user 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in magic_user.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ = Magic User -- A little Active Record extension
2
+
3
+ Active Record automatically userstamps create and update operations if the
4
+ table has fields named created_by or updated_by.
5
+
6
+ Userstamping can be turned off by setting:
7
+
8
+ config.active_record.record_userstamps = false
9
+
10
+ == Download and installation
11
+
12
+ Use Bundler to install Magic User:
13
+
14
+ gem 'magic_user'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+
3
+ module MagicUser
4
+ module Userstamp
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :record_userstamps, :instance_writer => false
9
+ self.record_userstamps = true
10
+ end
11
+
12
+ private
13
+
14
+ def create
15
+ if self.record_userstamps
16
+ magic_user = User.current # TODO: Should be more flexible
17
+
18
+ all_userstamp_attributes.each do |column|
19
+ write_attribute(column.to_s, magic_user.present? ? magic_user.id : magic_user) if respond_to?(column) && self.send(column).nil?
20
+ end
21
+ end
22
+
23
+ super
24
+ end
25
+
26
+ def update(*args)
27
+ if should_record_userstamps?
28
+ magic_user = User.current # TODO: Should be more flexible
29
+
30
+ userstamp_attributes_for_update_in_model.each do |column|
31
+ column = column.to_s
32
+ next if attribute_changed?(column)
33
+ write_attribute(column, magic_user.present? ? magic_user.id : magic_user)
34
+ end
35
+ end
36
+ super
37
+ end
38
+
39
+ def should_record_userstamps?
40
+ self.record_userstamps && (!partial_updates? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
41
+ end
42
+
43
+ def userstamp_attributes_for_create_in_model
44
+ userstamp_attributes_for_create.select { |c| self.class.column_names.include?(c.to_s) }
45
+ end
46
+
47
+ def userstamp_attributes_for_update_in_model
48
+ userstamp_attributes_for_update.select { |c| self.class.column_names.include?(c.to_s) }
49
+ end
50
+
51
+ def all_userstamp_attributes_in_model
52
+ userstamp_attributes_for_create_in_model + userstamp_attributes_for_update_in_model
53
+ end
54
+
55
+ def userstamp_attributes_for_update
56
+ [:updated_by]
57
+ end
58
+
59
+ def userstamp_attributes_for_create
60
+ [:created_by]
61
+ end
62
+
63
+ def all_userstamp_attributes
64
+ userstamp_attributes_for_create + userstamp_attributes_for_update
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module MagicUser
2
+ VERSION = "0.0.1"
3
+ end
data/lib/magic_user.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "magic_user/version"
2
+
3
+ module MagicUser
4
+ extend ActiveSupport::Autoload
5
+ autoload :Userstamp
6
+
7
+ ActiveRecord::Base.class_eval do
8
+ include Userstamp
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "magic_user/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "magic_user"
7
+ s.version = MagicUser::VERSION
8
+ s.authors = ["Michael Balsiger"]
9
+ s.email = ["michael.balsiger@swisscom.com"]
10
+ s.homepage = "https://github.com/innovative-office/magic_user"
11
+ s.summary = %q{ActiveRecord extension}
12
+ s.description = %q{Active Record automatically userstamps create and update operations if the table has fields named created_by or updated_by.}
13
+
14
+ s.required_ruby_version = '>= 1.8.7'
15
+
16
+ s.rubyforge_project = "magic_user"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_user
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Balsiger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Active Record automatically userstamps create and update operations if
15
+ the table has fields named created_by or updated_by.
16
+ email:
17
+ - michael.balsiger@swisscom.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.rdoc
25
+ - Rakefile
26
+ - lib/magic_user.rb
27
+ - lib/magic_user/userstamp.rb
28
+ - lib/magic_user/version.rb
29
+ - magic_user.gemspec
30
+ homepage: https://github.com/innovative-office/magic_user
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 1.8.7
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: magic_user
50
+ rubygems_version: 1.8.10
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: ActiveRecord extension
54
+ test_files: []