module AttributesHistory::HasAttributesHistory::ClassMethods

Public Instance Methods

has_attributes_history(options) click to toggle source

The options should include the keys :attributes and :version_class

# File lib/attributes_history/has_attributes_history.rb, line 17
def has_attributes_history(options)
  history_model = options[:with_model]
  history_attributes = options[:for].map(&:to_s)
  history_association = history_model.name.underscore.pluralize.to_sym

  has_many history_association

  store_history_options(history_model, history_attributes, history_association)
  setup_history_callback(history_attributes, history_model)
  define_attribute_on_date_methods(history_attributes)
end
history_association(attribute) click to toggle source
# File lib/attributes_history/has_attributes_history.rb, line 33
def history_association(attribute)
  self.history_associations[attribute]
end
history_model(attribute) click to toggle source
# File lib/attributes_history/has_attributes_history.rb, line 29
def history_model(attribute)
  self.history_models[attribute]
end

Private Instance Methods

define_attribute_on_date_methods(history_attributes) click to toggle source
# File lib/attributes_history/has_attributes_history.rb, line 53
def define_attribute_on_date_methods(history_attributes)
  define_method :attribute_on_date do |attribute, date|
    @history_retriever ||= HistoryRetriever.new(self)
    @history_retriever.attribute_on_date(attribute, date)
  end

  history_attributes.each do |attribute|
    define_method "#{attribute}_on_date" do |date|
      attribute_on_date(attribute, date)
    end
  end
end
setup_history_callback(history_attributes, history_model) click to toggle source
# File lib/attributes_history/has_attributes_history.rb, line 46
def setup_history_callback(history_attributes, history_model)
  after_update do
    HistorySaver.new(self, history_attributes, history_model)
      .save_if_needed
  end
end
store_history_options(history_model, history_attributes, history_association) click to toggle source
# File lib/attributes_history/has_attributes_history.rb, line 39
def store_history_options(history_model, history_attributes, history_association)
  history_attributes.each do |attribute|
    self.history_associations[attribute] = history_association
    self.history_models[attribute] = history_model
  end
end