Module: MagicUserstamp::Stampable::ClassMethods

Defined in:
lib/magic_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.



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(options = {})
  reader_name = MagicUserstamp.compatibility_mode ? :default_attribute_compatible : :default_attribute
  options  = {
    :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(options || {})

  stamper_class_name = options[:stamper_class_name].to_s
  stamper_class_name = stamper_class_name.camelize unless stamper_class_name =~ /^[A-Z]/

  with_options(:stamper_class_name => stamper_class_name) do |s|
    s.stampable_on(:create , :attribute => options[:creator_attribute])
    s.stampable_on(:update , :attribute => options[:updater_attribute])
    s.stampable_on(:destroy, :attribute => options[: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, options = {})
  MagicUserstamp::Stampable.raise_unless_valid_options_for_stampable_on(options)
  event = Event[event_name]
  reader_name = MagicUserstamp.compatibility_mode ? :default_attribute_compatible : :default_attribute
  options = {
    :stamper_name => event.actor,
    :stamper_class_name => "User",
    # :stamper_attribute => nil
    :attribute => event.send(reader_name),
    :actual_hook => nil
  }.update(options || {})

  belongs_to_class_name = options[: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_#{options[:attribute]}_on_#{event.name}"

  line_no = __LINE__ + 2
  method_definitions = "    belongs_to(:\#{options[:stamper_name]},\n      :class_name => '\#{belongs_to_class_name}',\n      :foreign_key => '\#{options[:attribute].to_s}')\n\n    \#{options[:actual_hook] || event.actual_hook} :\#{callback_method_name}\n\n    def \#{callback_method_name}\n      if MagicUserstamp.config.verbose?(self.class, \"\#{options[:attribute]}\") && !self.record_userstamp\n        logger.debug(\"aborting \#{self.name}.\#{callback_method_name} cause of record_userstamp is \#{self.record_userstamp.inspect}\")\n      end\n      return unless self.record_userstamp\n      if RAILS_ENV == 'development'\n        @@\#{options[:attribute]}_stamper_class = \"\#{options[:stamper_class_name]}\".constantize\n      else\n        @@\#{options[:attribute]}_stamper_class ||= \"\#{options[:stamper_class_name]}\".constantize\n      end\n      stamper_class = @@\#{options[:attribute]}_stamper_class\n      stamper_class.model_stamper if stamper_class\n      stamper = stamper_class.stamper if stamper_class\n      send(\"\#{options[:attribute]}=\", stamper) if stamper\n      \#{event.after_callback}\n    end\n  EOS\n  if MagicUserstamp.config.verbose?(self, options[:attribute])\n    ActiveRecord::Base.logger.debug \"=\" * 100\n    ActiveRecord::Base.logger.debug self.name\n    ActiveRecord::Base.logger.debug method_definitions\n  end\n  module_eval(method_definitions, __FILE__, line_no)\nend\n"

#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


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