activerecord-userstamp 3.0.2 → 3.0.3

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -5
  3. data/.rspec +2 -2
  4. data/CHANGELOG.md +103 -99
  5. data/Gemfile +3 -3
  6. data/LICENSE +21 -21
  7. data/README.md +189 -189
  8. data/Rakefile +17 -17
  9. data/activerecord-userstamp.gemspec +34 -34
  10. data/lib/active_record/userstamp.rb +30 -30
  11. data/lib/active_record/userstamp/configuration.rb +49 -49
  12. data/lib/active_record/userstamp/controller_additions.rb +50 -50
  13. data/lib/active_record/userstamp/migration_additions.rb +14 -14
  14. data/lib/active_record/userstamp/model_additions.rb +10 -10
  15. data/lib/active_record/userstamp/stampable.rb +123 -123
  16. data/lib/active_record/userstamp/stamper.rb +54 -54
  17. data/lib/active_record/userstamp/utilities.rb +55 -54
  18. data/lib/active_record/userstamp/version.rb +4 -4
  19. data/lib/activerecord/userstamp.rb +1 -1
  20. data/spec/controllers/posts_controller_spec.rb +44 -44
  21. data/spec/controllers/users_controller_spec.rb +50 -50
  22. data/spec/dummy/README.rdoc +28 -28
  23. data/spec/dummy/Rakefile +6 -6
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -13
  25. data/spec/dummy/app/assets/stylesheets/application.css +15 -15
  26. data/spec/dummy/app/controllers/application_controller.rb +13 -13
  27. data/spec/dummy/app/controllers/posts_controller.rb +36 -36
  28. data/spec/dummy/app/controllers/users_controller.rb +22 -22
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -2
  30. data/spec/dummy/app/models/comment.rb +5 -5
  31. data/spec/dummy/app/models/person.rb +3 -3
  32. data/spec/dummy/app/models/post.rb +14 -14
  33. data/spec/dummy/app/models/tag.rb +3 -0
  34. data/spec/dummy/app/models/user.rb +3 -3
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -14
  36. data/spec/dummy/bin/bundle +3 -3
  37. data/spec/dummy/bin/rails +4 -4
  38. data/spec/dummy/bin/rake +4 -4
  39. data/spec/dummy/bin/setup +29 -29
  40. data/spec/dummy/config.ru +4 -4
  41. data/spec/dummy/config/application.rb +30 -30
  42. data/spec/dummy/config/boot.rb +5 -5
  43. data/spec/dummy/config/database.yml +23 -23
  44. data/spec/dummy/config/environment.rb +5 -5
  45. data/spec/dummy/config/environments/development.rb +41 -41
  46. data/spec/dummy/config/environments/production.rb +79 -79
  47. data/spec/dummy/config/environments/test.rb +37 -37
  48. data/spec/dummy/config/initializers/assets.rb +11 -11
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
  50. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -3
  51. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -4
  52. data/spec/dummy/config/initializers/inflections.rb +16 -16
  53. data/spec/dummy/config/initializers/mime_types.rb +4 -4
  54. data/spec/dummy/config/initializers/session_store.rb +3 -3
  55. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
  56. data/spec/dummy/config/locales/en.yml +23 -23
  57. data/spec/dummy/config/routes.rb +56 -56
  58. data/spec/dummy/config/secrets.yml +22 -22
  59. data/spec/dummy/db/schema.rb +54 -45
  60. data/spec/dummy/public/404.html +67 -67
  61. data/spec/dummy/public/422.html +67 -67
  62. data/spec/dummy/public/500.html +66 -66
  63. data/spec/lib/configuration_spec.rb +20 -20
  64. data/spec/lib/migration_spec.rb +71 -71
  65. data/spec/lib/stamper_spec.rb +66 -66
  66. data/spec/lib/stamping_spec.rb +250 -238
  67. data/spec/lib/userstamp_spec.rb +7 -7
  68. data/spec/rails_helper.rb +7 -7
  69. data/spec/spec_helper.rb +97 -97
  70. data/spec/support/database_helpers.rb +22 -22
  71. data/spec/support/with_temporary_table.rb +51 -51
  72. metadata +3 -2
data/Rakefile CHANGED
@@ -1,17 +1,17 @@
1
- require 'bundler/setup'
2
- Bundler::GemHelper.install_tasks
3
-
4
- require 'rspec/core/rake_task'
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task :default => :spec
8
-
9
- require 'rdoc/task'
10
- desc 'Generate documentation for the userstamp plugin.'
11
- Rake::RDocTask.new(:rdoc) do |rdoc|
12
- rdoc.rdoc_dir = 'rdoc'
13
- rdoc.title = 'Userstamp'
14
- rdoc.options << '--line-numbers' << '--inline-source'
15
- rdoc.rdoc_files.include('Readme.rdoc', 'CHANGELOG', 'LICENSE')
16
- rdoc.rdoc_files.include('lib/**/*.rb')
17
- end
1
+ require 'bundler/setup'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ require 'rdoc/task'
10
+ desc 'Generate documentation for the userstamp plugin.'
11
+ Rake::RDocTask.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Userstamp'
14
+ rdoc.options << '--line-numbers' << '--inline-source'
15
+ rdoc.rdoc_files.include('Readme.rdoc', 'CHANGELOG', 'LICENSE')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
@@ -1,34 +1,34 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'active_record/userstamp/version'
5
-
6
- Gem::Specification.new do |s|
7
- s.name = 'activerecord-userstamp'
8
- s.version = ActiveRecord::Userstamp::VERSION
9
- s.authors = ['Joel Low']
10
- s.email = ['joel@joelsplace.sg']
11
-
12
- s.summary = 'Adds magic creator and updater attributes to your ActiveRecord models.'
13
- s.description = 'This gem extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.'
14
- s.homepage = 'https://github.com/lowjoel/activerecord-userstamp'
15
- s.license = 'MIT'
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ['lib']
21
-
22
- s.add_dependency 'activesupport', '~> 4.2'
23
- s.add_dependency 'activerecord', '~> 4.2'
24
-
25
- s.add_development_dependency 'actionview', '~> 4.2'
26
- s.add_development_dependency 'tzinfo-data'
27
- s.add_development_dependency 'rake'
28
- s.add_development_dependency 'rdoc'
29
- s.add_development_dependency 'rspec-rails', '~> 3.3'
30
- s.add_development_dependency 'simplecov'
31
- s.add_development_dependency 'coveralls'
32
- s.add_development_dependency 'codeclimate-test-reporter'
33
- s.add_development_dependency 'sqlite3'
34
- end
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/userstamp/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'activerecord-userstamp'
8
+ s.version = ActiveRecord::Userstamp::VERSION
9
+ s.authors = ['Joel Low']
10
+ s.email = ['joel@joelsplace.sg']
11
+
12
+ s.summary = 'Adds magic creator and updater attributes to your ActiveRecord models.'
13
+ s.description = 'This gem extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.'
14
+ s.homepage = 'https://github.com/lowjoel/activerecord-userstamp'
15
+ s.license = 'MIT'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_dependency 'activesupport', '~> 4.2'
23
+ s.add_dependency 'activerecord', '~> 4.2'
24
+
25
+ s.add_development_dependency 'actionview', '~> 4.2'
26
+ s.add_development_dependency 'tzinfo-data'
27
+ s.add_development_dependency 'rake'
28
+ s.add_development_dependency 'rdoc'
29
+ s.add_development_dependency 'rspec-rails', '~> 3.3'
30
+ s.add_development_dependency 'simplecov'
31
+ s.add_development_dependency 'coveralls'
32
+ s.add_development_dependency 'codeclimate-test-reporter'
33
+ s.add_development_dependency 'sqlite3'
34
+ end
@@ -1,30 +1,30 @@
1
- module ActiveRecord::Userstamp
2
- extend ActiveSupport::Autoload
3
-
4
- autoload :Configuration
5
- autoload :Stampable
6
- autoload :Stamper
7
- autoload :Utilities
8
-
9
- eager_autoload do
10
- autoload :ControllerAdditions
11
- autoload :MigrationAdditions
12
- autoload :ModelAdditions
13
- end
14
-
15
- # Retrieves the configuration for the userstamp gem.
16
- #
17
- # @return [ActiveRecord::Userstamp::Configuration]
18
- def self.config
19
- Configuration
20
- end
21
-
22
- # Configures the gem.
23
- #
24
- # @yield [ActiveRecord::Userstamp::Configuration] The configuration for the gem.
25
- def self.configure
26
- yield config
27
- end
28
-
29
- eager_load!
30
- end
1
+ module ActiveRecord::Userstamp
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Configuration
5
+ autoload :Stampable
6
+ autoload :Stamper
7
+ autoload :Utilities
8
+
9
+ eager_autoload do
10
+ autoload :ControllerAdditions
11
+ autoload :MigrationAdditions
12
+ autoload :ModelAdditions
13
+ end
14
+
15
+ # Retrieves the configuration for the userstamp gem.
16
+ #
17
+ # @return [ActiveRecord::Userstamp::Configuration]
18
+ def self.config
19
+ Configuration
20
+ end
21
+
22
+ # Configures the gem.
23
+ #
24
+ # @yield [ActiveRecord::Userstamp::Configuration] The configuration for the gem.
25
+ def self.configure
26
+ yield config
27
+ end
28
+
29
+ eager_load!
30
+ end
@@ -1,49 +1,49 @@
1
- module ActiveRecord::Userstamp::Configuration
2
- # !@attribute [r] default_stamper
3
- # Determines the default model used to stamp other models.
4
- #
5
- # By default, this is set to +'User'+.
6
- def self.default_stamper
7
- ActiveRecord::Base.stamper_class_name
8
- end
9
-
10
- # !@attribute [rw] default_stamper
11
- # @see {.default_stamper}
12
- def self.default_stamper=(stamper)
13
- ActiveRecord::Base.stamper_class_name = stamper
14
- end
15
- self.default_stamper = 'User'.freeze
16
-
17
- # @!attribute [r] default_stamper_class
18
- # Determines the default model used to stamp other models.
19
- def self.default_stamper_class
20
- ActiveRecord::Base.stamper_class
21
- end
22
-
23
- # !@attribute [rw] creator_attribute
24
- # Determines the name of the column in the database which stores the name of the creator.
25
- #
26
- # Override the attribute by using the stampable class method within a model.
27
- #
28
- # By default, this is set to +:creator_id+.
29
- mattr_accessor :creator_attribute
30
- self.creator_attribute = :creator_id
31
-
32
- # !@attribute [rw] updater_attribute
33
- # Determines the name of the column in the database which stores the name of the updater.
34
- #
35
- # Override the attribute by using the stampable class method within a model.
36
- #
37
- # By default, this is set to +:updater_id+.
38
- mattr_accessor :updater_attribute
39
- self.updater_attribute = :updater_id
40
-
41
- # !@attribute [rw] deleter_attribute
42
- # Determines the name of the column in the database which stores the name of the deleter.
43
- #
44
- # Override the attribute by using the stampable class method within a model.
45
- #
46
- # By default, this is set to +:deleter_id+.
47
- mattr_accessor :deleter_attribute
48
- self.deleter_attribute = :deleter_id
49
- end
1
+ module ActiveRecord::Userstamp::Configuration
2
+ # !@attribute [r] default_stamper
3
+ # Determines the default model used to stamp other models.
4
+ #
5
+ # By default, this is set to +'User'+.
6
+ def self.default_stamper
7
+ ActiveRecord::Base.stamper_class_name
8
+ end
9
+
10
+ # !@attribute [rw] default_stamper
11
+ # @see {.default_stamper}
12
+ def self.default_stamper=(stamper)
13
+ ActiveRecord::Base.stamper_class_name = stamper
14
+ end
15
+ self.default_stamper = 'User'.freeze
16
+
17
+ # @!attribute [r] default_stamper_class
18
+ # Determines the default model used to stamp other models.
19
+ def self.default_stamper_class
20
+ ActiveRecord::Base.stamper_class
21
+ end
22
+
23
+ # !@attribute [rw] creator_attribute
24
+ # Determines the name of the column in the database which stores the name of the creator.
25
+ #
26
+ # Override the attribute by using the stampable class method within a model.
27
+ #
28
+ # By default, this is set to +:creator_id+.
29
+ mattr_accessor :creator_attribute
30
+ self.creator_attribute = :creator_id
31
+
32
+ # !@attribute [rw] updater_attribute
33
+ # Determines the name of the column in the database which stores the name of the updater.
34
+ #
35
+ # Override the attribute by using the stampable class method within a model.
36
+ #
37
+ # By default, this is set to +:updater_id+.
38
+ mattr_accessor :updater_attribute
39
+ self.updater_attribute = :updater_id
40
+
41
+ # !@attribute [rw] deleter_attribute
42
+ # Determines the name of the column in the database which stores the name of the deleter.
43
+ #
44
+ # Override the attribute by using the stampable class method within a model.
45
+ #
46
+ # By default, this is set to +:deleter_id+.
47
+ mattr_accessor :deleter_attribute
48
+ self.deleter_attribute = :deleter_id
49
+ end
@@ -1,50 +1,50 @@
1
- # The +ControllerAdditions+ module, when included into a controller, adds an +before_action+
2
- # callback (named +set_stamper+) and an +after_action+ callback (named +reset_stamper+). These
3
- # methods assume a couple of things, but can be re-implemented in your controller to better suit
4
- # your application.
5
- #
6
- # See the documentation for `set_stamper` and `reset_stamper` for specific implementation details.
7
- module ActiveRecord::Userstamp::ControllerAdditions
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- around_action :with_stamper
12
- end
13
-
14
- private
15
-
16
- # This {#with_stamper} method sets the stamper for the duration of the action. This ensures
17
- # that exceptions raised within the controller action would properly restore the previous stamper.
18
- #
19
- # TODO: Remove set_stamper/reset_stamper
20
- def with_stamper
21
- set_stamper
22
- yield
23
- ensure
24
- reset_stamper
25
- end
26
-
27
- # The {#set_stamper} method as implemented here assumes a couple of things. First, that you are
28
- # using a +User+ model as the stamper and second that your controller has a +current_user+
29
- # method that contains the currently logged in stamper. If either of these are not the case in
30
- # your application you will want to manually add your own implementation of this method to the
31
- # private section of your +ApplicationController+
32
- def set_stamper
33
- @_userstamp_stamper = ActiveRecord::Userstamp.config.default_stamper_class.stamper
34
- ActiveRecord::Userstamp.config.default_stamper_class.stamper = current_user
35
- end
36
-
37
- # The {#reset_stamper} method as implemented here assumes that a +User+ model is being used as
38
- # the stamper. If this is not the case then you will need to manually add your own
39
- # implementation of this method to the private section of your +ApplicationController+
40
- def reset_stamper
41
- ActiveRecord::Userstamp.config.default_stamper_class.stamper = @_userstamp_stamper
42
- @_userstamp_stamper = nil
43
- end
44
- end
45
-
46
- if defined?(ActionController)
47
- ActionController::Base.class_eval do
48
- include ActiveRecord::Userstamp::ControllerAdditions
49
- end
50
- end
1
+ # The +ControllerAdditions+ module, when included into a controller, adds an +before_action+
2
+ # callback (named +set_stamper+) and an +after_action+ callback (named +reset_stamper+). These
3
+ # methods assume a couple of things, but can be re-implemented in your controller to better suit
4
+ # your application.
5
+ #
6
+ # See the documentation for `set_stamper` and `reset_stamper` for specific implementation details.
7
+ module ActiveRecord::Userstamp::ControllerAdditions
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ around_action :with_stamper
12
+ end
13
+
14
+ private
15
+
16
+ # This {#with_stamper} method sets the stamper for the duration of the action. This ensures
17
+ # that exceptions raised within the controller action would properly restore the previous stamper.
18
+ #
19
+ # TODO: Remove set_stamper/reset_stamper
20
+ def with_stamper
21
+ set_stamper
22
+ yield
23
+ ensure
24
+ reset_stamper
25
+ end
26
+
27
+ # The {#set_stamper} method as implemented here assumes a couple of things. First, that you are
28
+ # using a +User+ model as the stamper and second that your controller has a +current_user+
29
+ # method that contains the currently logged in stamper. If either of these are not the case in
30
+ # your application you will want to manually add your own implementation of this method to the
31
+ # private section of your +ApplicationController+
32
+ def set_stamper
33
+ @_userstamp_stamper = ActiveRecord::Userstamp.config.default_stamper_class.stamper
34
+ ActiveRecord::Userstamp.config.default_stamper_class.stamper = current_user
35
+ end
36
+
37
+ # The {#reset_stamper} method as implemented here assumes that a +User+ model is being used as
38
+ # the stamper. If this is not the case then you will need to manually add your own
39
+ # implementation of this method to the private section of your +ApplicationController+
40
+ def reset_stamper
41
+ ActiveRecord::Userstamp.config.default_stamper_class.stamper = @_userstamp_stamper
42
+ @_userstamp_stamper = nil
43
+ end
44
+ end
45
+
46
+ if defined?(ActionController)
47
+ ActionController::Base.class_eval do
48
+ include ActiveRecord::Userstamp::ControllerAdditions
49
+ end
50
+ end
@@ -1,14 +1,14 @@
1
- module ActiveRecord::Userstamp::MigrationHelper
2
- extend ActiveSupport::Concern
3
-
4
- def userstamps(*args)
5
- config = ActiveRecord::Userstamp.config
6
- column(config.creator_attribute, :integer, *args)
7
- column(config.updater_attribute, :integer, *args)
8
- column(config.deleter_attribute, :integer, *args) if config.deleter_attribute.present?
9
- end
10
- end
11
-
12
- ActiveRecord::ConnectionAdapters::TableDefinition.class_eval do
13
- include ActiveRecord::Userstamp::MigrationHelper
14
- end
1
+ module ActiveRecord::Userstamp::MigrationHelper
2
+ extend ActiveSupport::Concern
3
+
4
+ def userstamps(*args)
5
+ config = ActiveRecord::Userstamp.config
6
+ column(config.creator_attribute, :integer, *args)
7
+ column(config.updater_attribute, :integer, *args)
8
+ column(config.deleter_attribute, :integer, *args) if config.deleter_attribute.present?
9
+ end
10
+ end
11
+
12
+ ActiveRecord::ConnectionAdapters::TableDefinition.class_eval do
13
+ include ActiveRecord::Userstamp::MigrationHelper
14
+ end
@@ -1,10 +1,10 @@
1
- module ActiveRecord::Userstamp::ModelAdditions
2
- extend ActiveSupport::Concern
3
-
4
- include ActiveRecord::Userstamp::Stampable
5
- include ActiveRecord::Userstamp::Stamper
6
- end
7
-
8
- ActiveRecord::Base.class_eval do
9
- include ActiveRecord::Userstamp::ModelAdditions
10
- end
1
+ module ActiveRecord::Userstamp::ModelAdditions
2
+ extend ActiveSupport::Concern
3
+
4
+ include ActiveRecord::Userstamp::Stampable
5
+ include ActiveRecord::Userstamp::Stamper
6
+ end
7
+
8
+ ActiveRecord::Base.class_eval do
9
+ include ActiveRecord::Userstamp::ModelAdditions
10
+ end
@@ -1,123 +1,123 @@
1
- # Extends the stamping functionality of ActiveRecord by automatically recording the model
2
- # responsible for creating, updating, and deleting the current object. See the +Stamper+ and
3
- # +ControllerAdditions+ modules for further documentation on how the entire process works.
4
- module ActiveRecord::Userstamp::Stampable
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- # Should ActiveRecord record userstamps? Defaults to true.
9
- class_attribute :record_userstamp
10
- self.record_userstamp = true
11
-
12
- class_attribute :stamper_class_name
13
-
14
- before_validation :set_updater_attribute, if: :record_userstamp
15
- before_validation :set_creator_attribute, on: :create, if: :record_userstamp
16
- before_save :set_updater_attribute, if: :record_userstamp
17
- before_save :set_creator_attribute, on: :create, if: :record_userstamp
18
- before_destroy :set_deleter_attribute, if: :record_userstamp
19
- end
20
-
21
- module ClassMethods
22
- def inherited(klass)
23
- super
24
-
25
- klass.class_eval do
26
- add_userstamp_associations({})
27
- end
28
- end
29
-
30
- # This method customizes how the gem functions. For example:
31
- #
32
- # class Post < ActiveRecord::Base
33
- # stampable stamper_class_name: Person.name,
34
- # with_deleted: true
35
- # end
36
- #
37
- # The method will set up all the associations. Extra arguments (like +:with_deleted+) will be
38
- # propagated to the associations.
39
- #
40
- # By default, the deleter association is not defined unless the :deleter_attribute is set in
41
- # the gem configuration.
42
- def stampable(options = {})
43
- self.stamper_class_name = options.delete(:stamper_class_name) if options.key?(:stamper_class_name)
44
-
45
- add_userstamp_associations(options)
46
- end
47
-
48
- # Temporarily allows you to turn stamping off. For example:
49
- #
50
- # Post.without_stamps do
51
- # post = Post.find(params[:id])
52
- # post.update_attributes(params[:post])
53
- # post.save
54
- # end
55
- def without_stamps
56
- original_value = self.record_userstamp
57
- self.record_userstamp = false
58
- yield
59
- ensure
60
- self.record_userstamp = original_value
61
- end
62
-
63
- def stamper_class #:nodoc:
64
- stamper_class_name.to_s.camelize.constantize rescue nil
65
- end
66
-
67
- private
68
-
69
- # Defines the associations for Userstamp.
70
- def add_userstamp_associations(options)
71
- ActiveRecord::Userstamp::Utilities.remove_association(self, :creator)
72
- ActiveRecord::Userstamp::Utilities.remove_association(self, :updater)
73
- ActiveRecord::Userstamp::Utilities.remove_association(self, :deleter)
74
-
75
- associations = ActiveRecord::Userstamp::Utilities.available_association_columns(self)
76
- return if associations.nil?
77
-
78
- config = ActiveRecord::Userstamp.config
79
- klass = stamper_class.try(:name)
80
- relation_options = options.reverse_merge(class_name: klass)
81
-
82
- belongs_to :creator, relation_options.reverse_merge(foreign_key: config.creator_attribute) if
83
- associations.first
84
- belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute) if
85
- associations.second
86
- belongs_to :deleter, relation_options.reverse_merge(foreign_key: config.deleter_attribute) if
87
- associations.third
88
- end
89
- end
90
-
91
- private
92
-
93
- def has_stamper?
94
- !self.class.stamper_class.nil? && !self.class.stamper_class.stamper.nil?
95
- end
96
-
97
- def set_creator_attribute
98
- attribute = ActiveRecord::Userstamp.config.creator_attribute
99
- return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
100
-
101
- current_attribute_value = send(attribute)
102
- return if current_attribute_value.present?
103
-
104
- ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
105
- end
106
-
107
- def set_updater_attribute
108
- attribute = ActiveRecord::Userstamp.config.updater_attribute
109
- return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
110
-
111
- return if !self.new_record? && !self.changed?
112
-
113
- ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
114
- end
115
-
116
- def set_deleter_attribute
117
- attribute = ActiveRecord::Userstamp.config.deleter_attribute
118
- return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
119
-
120
- ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
121
- save
122
- end
123
- end
1
+ # Extends the stamping functionality of ActiveRecord by automatically recording the model
2
+ # responsible for creating, updating, and deleting the current object. See the +Stamper+ and
3
+ # +ControllerAdditions+ modules for further documentation on how the entire process works.
4
+ module ActiveRecord::Userstamp::Stampable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ # Should ActiveRecord record userstamps? Defaults to true.
9
+ class_attribute :record_userstamp
10
+ self.record_userstamp = true
11
+
12
+ class_attribute :stamper_class_name
13
+
14
+ before_validation :set_updater_attribute, if: :record_userstamp
15
+ before_validation :set_creator_attribute, on: :create, if: :record_userstamp
16
+ before_save :set_updater_attribute, if: :record_userstamp
17
+ before_save :set_creator_attribute, on: :create, if: :record_userstamp
18
+ before_destroy :set_deleter_attribute, if: :record_userstamp
19
+ end
20
+
21
+ module ClassMethods
22
+ def inherited(klass)
23
+ super
24
+
25
+ klass.class_eval do
26
+ add_userstamp_associations({})
27
+ end
28
+ end
29
+
30
+ # This method customizes how the gem functions. For example:
31
+ #
32
+ # class Post < ActiveRecord::Base
33
+ # stampable stamper_class_name: Person.name,
34
+ # with_deleted: true
35
+ # end
36
+ #
37
+ # The method will set up all the associations. Extra arguments (like +:with_deleted+) will be
38
+ # propagated to the associations.
39
+ #
40
+ # By default, the deleter association is not defined unless the :deleter_attribute is set in
41
+ # the gem configuration.
42
+ def stampable(options = {})
43
+ self.stamper_class_name = options.delete(:stamper_class_name) if options.key?(:stamper_class_name)
44
+
45
+ add_userstamp_associations(options)
46
+ end
47
+
48
+ # Temporarily allows you to turn stamping off. For example:
49
+ #
50
+ # Post.without_stamps do
51
+ # post = Post.find(params[:id])
52
+ # post.update_attributes(params[:post])
53
+ # post.save
54
+ # end
55
+ def without_stamps
56
+ original_value = self.record_userstamp
57
+ self.record_userstamp = false
58
+ yield
59
+ ensure
60
+ self.record_userstamp = original_value
61
+ end
62
+
63
+ def stamper_class #:nodoc:
64
+ stamper_class_name.to_s.camelize.constantize rescue nil
65
+ end
66
+
67
+ private
68
+
69
+ # Defines the associations for Userstamp.
70
+ def add_userstamp_associations(options)
71
+ ActiveRecord::Userstamp::Utilities.remove_association(self, :creator)
72
+ ActiveRecord::Userstamp::Utilities.remove_association(self, :updater)
73
+ ActiveRecord::Userstamp::Utilities.remove_association(self, :deleter)
74
+
75
+ associations = ActiveRecord::Userstamp::Utilities.available_association_columns(self)
76
+ return if associations.nil?
77
+
78
+ config = ActiveRecord::Userstamp.config
79
+ klass = stamper_class.try(:name)
80
+ relation_options = options.reverse_merge(class_name: klass)
81
+
82
+ belongs_to :creator, relation_options.reverse_merge(foreign_key: config.creator_attribute) if
83
+ associations.first
84
+ belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute) if
85
+ associations.second
86
+ belongs_to :deleter, relation_options.reverse_merge(foreign_key: config.deleter_attribute) if
87
+ associations.third
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def has_stamper?
94
+ !self.class.stamper_class.nil? && !self.class.stamper_class.stamper.nil?
95
+ end
96
+
97
+ def set_creator_attribute
98
+ attribute = ActiveRecord::Userstamp.config.creator_attribute
99
+ return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
100
+
101
+ current_attribute_value = send(attribute)
102
+ return if current_attribute_value.present?
103
+
104
+ ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
105
+ end
106
+
107
+ def set_updater_attribute
108
+ attribute = ActiveRecord::Userstamp.config.updater_attribute
109
+ return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
110
+
111
+ return if !self.new_record? && !self.changed?
112
+
113
+ ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
114
+ end
115
+
116
+ def set_deleter_attribute
117
+ attribute = ActiveRecord::Userstamp.config.deleter_attribute
118
+ return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
119
+
120
+ ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
121
+ save
122
+ end
123
+ end