Module: RecordHistory::Model::InstanceMethods

Defined in:
lib/record_history/has_record_history.rb

Instance Method Summary collapse

Instance Method Details

#attr_ignoreObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/record_history/has_record_history.rb', line 84

def attr_ignore
  attr_ignore = []
  self.record_history_polymorphic_group.each do |attr|
    if !self.reflections[attr.to_sym].options[:polymorphic]
      raise "Тип связи должен быть polymorphic"
    else
      attr_ignore += ["#{attr}_id", "#{attr}_type"]
    end
  end
  attr_ignore
end

#build_history_on_updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/record_history/has_record_history.rb', line 50

def build_history_on_update
  return if self.new_record?
  self.record_history_obj = []
  ignore = self.attr_ignore
  self.class.new.attributes.keys.each do |attr_name|
    next unless (self.send("#{attr_name}_changed?"))

    next if ignore.include?(attr_name)
    next if !self.class.record_history_only.blank? && !self.class.record_history_only.include?(attr_name)
    next if !self.class.record_history_ignore.blank? && self.record_history_ignore.include?(attr_name)
    self.record_history_obj << RecordHistoryModel.new(
              :item_type => self.class.name,
              :item_id => self.id,
              :attr_name => attr_name,
              :old_value => self.send("#{attr_name}_was"),
              :new_value => self.send("#{attr_name}"),
              :author => RecordHistory.author,
              :transaction_id => Time.now.to_f
    )
  end
  self.record_history_polymorphic_group.each do |attr|
    next if !self.send("#{attr}_type_changed?") && !self.send("#{attr}_id_changed?")
    self.record_history_obj << RecordHistoryModel.new(
                :item_type => self.class.name,
                :item_id => self.id,
                :attr_name => attr,
                :old_value => {"#{attr}_type".to_sym => self.send("#{attr}_type_was"), "#{attr}_id".to_sym => self.send("#{attr}_id_was")},
                :new_value => {"#{attr}_type".to_sym => self.send("#{attr}_type"), "#{attr}_id".to_sym => self.send("#{attr}_id")},
                :author => RecordHistory.author,
                :transaction_id => Time.now.to_f
      )
  end
end

#save_history_on_createObject



100
101
102
103
104
105
106
107
# File 'lib/record_history/has_record_history.rb', line 100

def save_history_on_create
  RecordHistoryModel.create!(
    :item_type => self.class.name,
    :item_id => self.id,
    :author => RecordHistory.author,
    :transaction_id => Time.now.to_f
  )
end

#save_history_on_updateObject



96
97
98
# File 'lib/record_history/has_record_history.rb', line 96

def save_history_on_update
  self.record_history_obj.each{|item| item.save}
end