module Userstamper::Stampable::ClassMethods

Public Instance Methods

columns(*) click to toggle source
Calls superclass method
# File lib/userstamper/stampable.rb, line 21
def columns(*)
  columns = super
  return columns if defined?(@stamper_initialized) && @stamper_initialized

  add_userstamp_associations({})
  columns
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
end

the gem configuration.

# File lib/userstamper/stampable.rb, line 36
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/userstamper/stampable.rb, line 49
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 Userstamper.

# File lib/userstamper/stampable.rb, line 64
def add_userstamp_associations(options)
  @stamper_initialized = true
  Userstamper::Utilities.remove_association(self, :creator)
  Userstamper::Utilities.remove_association(self, :updater)

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

  config = Userstamper.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, required: false) if associations.first
  belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute, required: false) if associations.second
end