Module: MagicUserstamp::Stampable::ClassMethods
- Defined in:
- lib/magic_userstamp/stampable.rb
Instance Method Summary collapse
-
#stampable(options = {}) ⇒ Object
This method is automatically called on for all classes that inherit from ActiveRecord, but if you need to customize how the plug-in functions, this is the method to use.
- #stampable_on(event_name, options = {}) ⇒ Object
-
#without_stamps ⇒ Object
Temporarily allows you to turn stamping off.
Instance Method Details
#stampable(options = {}) ⇒ Object
This method is automatically called on for all classes that inherit from ActiveRecord, but if you need to customize how the plug-in functions, this is the method to use. Here’s an example:
class Post < ActiveRecord::Base
stampable :stamper_class_name => :person,
:creator_attribute => :create_user,
:updater_attribute => :update_user,
:deleter_attribute => :delete_user
end
The method will automatically setup all the associations, and create before_save
and before_create
filters for doing the stamping.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/magic_userstamp/stampable.rb', line 61 def stampable( = {}) reader_name = MagicUserstamp.compatibility_mode ? :default_attribute_compatible : :default_attribute = { :stamper_class_name => "User", :creator_attribute => Event[:create ].send(reader_name), :updater_attribute => Event[:update ].send(reader_name), :deleter_attribute => Event[:destroy].send(reader_name) }.update( || {}) stamper_class_name = [:stamper_class_name].to_s stamper_class_name = stamper_class_name.camelize unless stamper_class_name =~ /^[A-Z]/ (:stamper_class_name => stamper_class_name) do |s| s.stampable_on(:create , :attribute => [:creator_attribute]) s.stampable_on(:update , :attribute => [:updater_attribute]) s.stampable_on(:destroy, :attribute => [:deleter_attribute]) if defined?(Caboose::Acts::Paranoid) end end |
#stampable_on(event_name, options = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/magic_userstamp/stampable.rb', line 80 def stampable_on(event_name, = {}) MagicUserstamp::Stampable.() event = Event[event_name] reader_name = MagicUserstamp.compatibility_mode ? :default_attribute_compatible : :default_attribute = { :stamper_name => event.actor, :stamper_class_name => "User", # :stamper_attribute => nil :attribute => event.send(reader_name), :actual_hook => nil }.update( || {}) belongs_to_class_name = [:stamper_class_name].to_s belongs_to_class_name = belongs_to_class_name.singularize.camelize unless belongs_to_class_name =~ /^[A-Z]/ callback_method_name = "set_#{[:attribute]}_on_#{event.name}" line_no = __LINE__ + 2 method_definitions = <<-"EOS" belongs_to(:#{[:stamper_name]}, :class_name => '#{belongs_to_class_name}', :foreign_key => '#{[:attribute].to_s}') #{[:actual_hook] || event.actual_hook} :#{callback_method_name} def #{callback_method_name} if MagicUserstamp.config.verbose?(self.class, "#{[:attribute]}") && !self.record_userstamp logger.debug("aborting #{self.name}.#{callback_method_name} cause of record_userstamp is #{self.record_userstamp.inspect}") end return unless self.record_userstamp if RAILS_ENV == 'development' @@#{[:attribute]}_stamper_class = "#{[:stamper_class_name]}".constantize else @@#{[:attribute]}_stamper_class ||= "#{[:stamper_class_name]}".constantize end stamper_class = @@#{[:attribute]}_stamper_class stamper_class.model_stamper if stamper_class stamper = stamper_class.stamper if stamper_class send("#{[:attribute]}=", stamper) if stamper #{event.after_callback} end EOS if MagicUserstamp.config.verbose?(self, [:attribute]) ActiveRecord::Base.logger.debug "=" * 100 ActiveRecord::Base.logger.debug self.name ActiveRecord::Base.logger.debug method_definitions end module_eval(method_definitions, __FILE__, line_no) end |
#without_stamps ⇒ Object
Temporarily allows you to turn stamping off. For example:
Post.without_stamps do
post = Post.find(params[:id])
post.update_attributes(params[:post])
post.save
end
136 137 138 139 140 141 142 143 144 |
# File 'lib/magic_userstamp/stampable.rb', line 136 def without_stamps original_value = self.record_userstamp self.record_userstamp = false begin yield ensure self.record_userstamp = original_value end end |