Module: Ddb::Userstamp::Stampable::ClassMethods
- Defined in:
- lib/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.
-
#stamper_class ⇒ Object
:nodoc:.
-
#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
:deleter => true,
:optional => true
end
The method will automatically setup all the associations, and create before_save and before_create filters for doing the stamping. By default, the deleter association and before filter are not defined unless you are using acts_as_paranoid or you set the :deleter_attribute or set the :deleter option to true.
75 76 77 78 79 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 |
# File 'lib/stampable.rb', line 75 def stampable( = {}) compatability = Ddb::Userstamp.compatibility_mode defaults = { :stamper_class_name => :user, :creator_attribute => (compatability ? :created_by : :creator_id), :updater_attribute => (compatability ? :updated_by : :updater_id), :deleter_attribute => (compatability ? :deleted_by : :deleter_id), :optional => true, :deleter => !!(.has_key?(:deleter_attribute) or defined?(Caboose::Acts::Paranoid)) }.merge() self.stamper_class_name = defaults[:stamper_class_name].to_sym self.creator_attribute = defaults[:creator_attribute].to_sym self.updater_attribute = defaults[:updater_attribute].to_sym self.deleter_attribute = defaults[:deleter_attribute].to_sym self.userstamp_optional = !!defaults[:optional] class_eval do klass = "::#{stamper_class_name.to_s.singularize.camelize}" belongs_to :creator, :class_name => klass, :foreign_key => creator_attribute, optional: userstamp_optional belongs_to :updater, :class_name => klass, :foreign_key => updater_attribute, optional: userstamp_optional before_validation :set_updater_attribute before_validation :set_creator_attribute, :on => :create if defaults[:deleter] belongs_to :deleter, :class_name => klass, :foreign_key => deleter_attribute, optional: userstamp_optional before_destroy :set_deleter_attribute end end end |
#stamper_class ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/stampable.rb', line 122 def stamper_class #:nodoc: stamper_class_name.to_s.camelize.constantize rescue nil 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
114 115 116 117 118 119 120 |
# File 'lib/stampable.rb', line 114 def without_stamps original_value = self.record_userstamp self.record_userstamp = false yield ensure self.record_userstamp = original_value end |