Module: DirtyHistory::Mixin::ObjectInstanceMethods

Defined in:
lib/dirty_history/dirty_history_mixin.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_dirty_history_record(column_name, old_value, new_value, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dirty_history/dirty_history_mixin.rb', line 101

def add_dirty_history_record column_name, old_value, new_value, options={}    
  creator = options[:creator] || self.creator_for_dirty_history
  
  dhr_attributes = { 
    :object       => self,
    :column_name  => column_name,
    :column_type  => self.class.columns_hash[column_name.to_s].type,
    :old_value    => old_value,
    :new_value    => new_value,
    :creator      => creator
  }  

  dhr = DirtyHistoryRecord.new(dhr_attributes)
  
  # attributes for manual updates
  [:revised_created_at, :performing_manual_update].each do |attribute|
    dhr.send("#{attribute}=", options[attribute]) if options[attribute]
  end
  
  self.dirty_history_records << dhr
end

#history_for_column(column, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/dirty_history/dirty_history_mixin.rb', line 123

def history_for_column column, options={}
  options[:sort] = true if options[:sort].blank?
  
  records = dirty_history_records.for_column(column)
  records = records.send(*options[:scope]) if options[:scope]
  records = records.order_asc if options[:sort]

  options[:return_objects] ? records : records.map { |s| s.new_value }
end

#save_dirty_historyObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dirty_history/dirty_history_mixin.rb', line 89

def save_dirty_history     
  return true unless self.dirty_history_changes.present?
  
  self.dirty_history_changes.each do |column_name,vals|       
    add_dirty_history_record column_name, vals[0], vals[1], :creator => self.creator_for_dirty_history
  end 

  self.dirty_history_changes = nil

  return true
end

#set_dirty_history_changesObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dirty_history/dirty_history_mixin.rb', line 76

def set_dirty_history_changes                    
  return true unless self.new_record? || self.changed? 
  
  self.dirty_history_changes    = self.class.dirty_history_columns.inject({}) do |changes_hash, column_name| 
    changes_hash[column_name]   = self.send("#{column_name}_change") if self.send("#{column_name}_changed?")  
    changes_hash[column_name] ||= [nil, self.send(column_name)] if self.new_record? && self.send(column_name).present?
    changes_hash
  end  

  self.initialize_dirty_history = self.new_record?
  return true
end