Module: Ddb::Userstamp::Stampable::ClassMethods

Defined in:
lib/ddb/userstamp/stampable.rb

Instance Method Summary collapse

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ddb/userstamp/stampable.rb', line 68

def stampable(options = {})
  defaults  = {
                :stamper_class_name => :user,
                :creator_attribute  => Ddb::Userstamp.compatibility_mode ? :created_by : :creator_id,
                :updater_attribute  => Ddb::Userstamp.compatibility_mode ? :updated_by : :updater_id,
                :deleter_attribute  => Ddb::Userstamp.compatibility_mode ? :deleted_by : :deleter_id
              }.merge(options)

  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

  class_eval do
    belongs_to :creator, :class_name => self.stamper_class_name.to_s.singularize.camelize,
                         :foreign_key => self.creator_attribute
                         
    belongs_to :updater, :class_name => self.stamper_class_name.to_s.singularize.camelize,
                         :foreign_key => self.updater_attribute
                         
    before_save     :set_updater_attribute
    before_create   :set_creator_attribute
                         
    if defined?(Caboose::Acts::Paranoid)
      belongs_to :deleter, :class_name => self.stamper_class_name.to_s.singularize.camelize,
                           :foreign_key => self.deleter_attribute
      before_destroy  :set_deleter_attribute
    end
  end
end

#stamper_classObject

:nodoc:



113
114
115
# File 'lib/ddb/userstamp/stampable.rb', line 113

def stamper_class #:nodoc:
  stamper_class_name.to_s.capitalize.constantize rescue nil
end

#without_stampsObject

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


106
107
108
109
110
111
# File 'lib/ddb/userstamp/stampable.rb', line 106

def without_stamps
  original_value = self.record_userstamp
  self.record_userstamp = false
  yield
  self.record_userstamp = original_value
end