Module: ModelHistory::Mixin::ClassMethods

Defined in:
lib/model_history/model_history_mixin.rb

Instance Method Summary collapse

Instance Method Details

#creates_model_historyObject

has_model_history



62
63
64
65
66
67
68
69
70
# File 'lib/model_history/model_history_mixin.rb', line 62

def creates_model_history 
  # Mix in the module, but ensure to do so just once.
  metaclass = (class << self; self; end)
  return if metaclass.included_modules.include?(ModelHistory::Mixin::CreatorInstanceMethods)

  has_many :model_history_records, :as => :creator
  
  include ModelHistory::Mixin::CreatorInstanceMethods
end

#has_model_history(*args) ⇒ Object

pass an optional proc to assign a creator to the model_history object example usage: class User < ActiveRecord::Base

has_model_history :email, :first_name, :last_name, :creator => proc { User.current_user }

end



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/model_history/model_history_mixin.rb', line 25

def has_model_history *args 
  # Mix in the module, but ensure to do so just once.
  metaclass = (class << self; self; end)
  return if metaclass.included_modules.include?(ModelHistory::Mixin::ObjectInstanceMethods)

  has_many        :model_history_records, :as => :model, :dependent => :destroy
  attr_accessor   :model_history_changes, :initialize_model_history
  cattr_accessor  :model_history_columns  


  self.model_history_columns ||= []
    
  if args.present?
    
    before_save     :set_model_history_changes
    after_save      :save_model_history

    args.each do |arg| 
      if [String,Symbol].include?(arg.class)     
        arg = arg.to_sym
        self.model_history_columns << arg unless self.model_history_columns.include?(arg)
        define_method "creator_for_model_history" do end
      elsif arg.is_a?(Hash)
        creator_proc = arg.delete(:creator)
        send :define_method, "creator_for_model_history" do
          begin
            creator_proc.is_a?(Proc) ? creator_proc.call : nil
          rescue
            raise ModelHistory::Mixin::CreatorProcError
          end
        end
      end
    end
    include ModelHistory::Mixin::ObjectInstanceMethods
  end
end