Module: DirtyHistory::Mixin::ClassMethods

Defined in:
lib/dirty_history/dirty_history_mixin.rb

Instance Method Summary collapse

Instance Method Details

#creates_dirty_historyObject

has_dirty_history



63
64
65
66
67
68
69
70
71
# File 'lib/dirty_history/dirty_history_mixin.rb', line 63

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

  has_many :dirty_history_records, :as => :creator
  
  include DirtyHistory::Mixin::CreatorInstanceMethods
end

#has_dirty_history(*args) ⇒ Object

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

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

end



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
61
# File 'lib/dirty_history/dirty_history_mixin.rb', line 27

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

  has_many        :dirty_history_records, :as => :object, :dependent => :destroy
  attr_accessor   :dirty_history_changes, :initialize_dirty_history
  cattr_accessor  :dirty_history_columns  


  self.dirty_history_columns ||= []
    
  if args.present?
    
    before_save     :set_dirty_history_changes
    after_save      :save_dirty_history

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