module ActiveRecord::Userstamp::Stampable::ClassMethods

Public Instance Methods

inherited(klass) click to toggle source
Calls superclass method
# File lib/active_record/userstamp/stampable.rb, line 22
def inherited(klass)
  super

  klass.class_eval do
    add_userstamp_associations({})
  end
end
stampable(options = {}) click to toggle source

This method customizes how the gem functions. For example:

class Post < ActiveRecord::Base
  stampable stamper_class_name: Person.name,
            with_deleted:       true
end

The method will set up all the associations. Extra arguments (like :with_deleted) will be propagated to the associations.

By default, the deleter association is not defined unless the :deleter_attribute is set in the gem configuration.

# File lib/active_record/userstamp/stampable.rb, line 42
def stampable(options = {})
  self.stamper_class_name = options.delete(:stamper_class_name) if options.key?(:stamper_class_name)

  add_userstamp_associations(options)
end
without_stamps() { || ... } click to toggle source

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
# File lib/active_record/userstamp/stampable.rb, line 55
def without_stamps
  original_value = self.record_userstamp
  self.record_userstamp = false
  yield
ensure
  self.record_userstamp = original_value
end

Private Instance Methods

add_userstamp_associations(options) click to toggle source

Defines the associations for Userstamp.

# File lib/active_record/userstamp/stampable.rb, line 70
def add_userstamp_associations(options)
  ActiveRecord::Userstamp::Utilities.remove_association(self, :creator)
  ActiveRecord::Userstamp::Utilities.remove_association(self, :updater)
  ActiveRecord::Userstamp::Utilities.remove_association(self, :deleter)

  associations = ActiveRecord::Userstamp::Utilities.available_association_columns(self)
  return if associations.nil?

  config = ActiveRecord::Userstamp.config
  klass = stamper_class.try(:name)
  relation_options = options.reverse_merge(class_name: klass)

  belongs_to :creator, relation_options.reverse_merge(foreign_key: config.creator_attribute) if
    associations.first
  belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute) if
    associations.second
  belongs_to :deleter, relation_options.reverse_merge(foreign_key: config.deleter_attribute) if
    associations.third
end