Module: ActiveHistory::Adapter::ActiveRecord
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/activehistory/adapters/active_record.rb
Instance Method Summary collapse
- #activehistory_association_udpated(reflection, id, added: [], removed: [], timestamp: nil, type: :update) ⇒ Object
- #activehistory_complete ⇒ Object
- #activehistory_event ⇒ Object
- #activehistory_id ⇒ Object
- #activehistory_start ⇒ Object
- #activehistory_timestamp ⇒ Object
- #activehistory_track(type) ⇒ Object
- #activehistory_tracking ⇒ Object
Instance Method Details
#activehistory_association_udpated(reflection, id, added: [], removed: [], timestamp: nil, type: :update) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/activehistory/adapters/active_record.rb', line 169 def activehistory_association_udpated(reflection, id, added: [], removed: [], timestamp: nil, type: :update) if inverse_of = activehistory_tracking.dig(:habtm_model, reflection.name, :inverse_of) inverse_of = reflection.klass.reflect_on_association(inverse_of) else inverse_of = reflection.inverse_of end if inverse_of.nil? puts "NO INVERSE for #{self.class}.#{reflection.name}!!!" return end model_name = reflection.klass.base_class.model_name.name action = activehistory_event.action_for("#{model_name}/#{id}") || activehistory_event.action!({ type: type, subject: "#{model_name}/#{id}", timestamp: # || Time.now }) action.diff ||= {} if inverse_of.collection? || activehistory_tracking[:habtm_model] diff_key = "#{inverse_of.name.to_s.singularize}_ids" action.diff[diff_key] ||= [[], []] action.diff[diff_key][0] |= removed action.diff[diff_key][1] |= added else diff_key = "#{inverse_of.name.to_s.singularize}_id" action.diff[diff_key] ||= [removed.first, added.first] end end |
#activehistory_complete ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/activehistory/adapters/active_record.rb', line 70 def activehistory_complete @activehistory_timestamp = nil if instance_variable_defined?(:@activehistory_finish) && @activehistory_finish && activehistory_tracking activehistory_event.save! if activehistory_event Thread.current[:activehistory_event] = nil @activehistory_timestamp = nil end end |
#activehistory_event ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/activehistory/adapters/active_record.rb', line 85 def activehistory_event case Thread.current[:activehistory_event] when ActiveHistory::Event Thread.current[:activehistory_event] when Hash Thread.current[:activehistory_event][:timestamp] ||= @activehistory_timestamp Thread.current[:activehistory_event] = ActiveHistory::Event.new(Thread.current[:activehistory_event]) when Fixnum else Thread.current[:activehistory_event] = ActiveHistory::Event.new(timestamp: @activehistory_timestamp) end end |
#activehistory_id ⇒ Object
55 56 57 |
# File 'lib/activehistory/adapters/active_record.rb', line 55 def activehistory_id "#{self.class.name}/#{id}" end |
#activehistory_start ⇒ Object
63 64 65 66 67 68 |
# File 'lib/activehistory/adapters/active_record.rb', line 63 def activehistory_start if activehistory_tracking && !instance_variable_defined?(:@activehistory_finish) @activehistory_finish = !Thread.current[:activehistory_event] end @activehistory_timestamp = Time.now.utc end |
#activehistory_timestamp ⇒ Object
59 60 61 |
# File 'lib/activehistory/adapters/active_record.rb', line 59 def @activehistory_timestamp ||= Time.now.utc end |
#activehistory_track(type) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/activehistory/adapters/active_record.rb', line 98 def activehistory_track(type) return if !activehistory_tracking if type == :create || type == :update diff = self.changes.select { |k,v| !activehistory_tracking[:exclude].include?(k.to_sym) } if type == :create self.class.columns.each do |column| if !diff[column.name] && !activehistory_tracking[:exclude].include?(column.name.to_sym) && column.default != self.attributes[column.name] diff[column.name] = [nil, self.attributes[column.name]] end end end elsif type == :destroy diff = self.attributes.select { |k| !activehistory_tracking[:exclude].include?(k.to_sym) }.map { |k, i| [k, [i, nil]] }.to_h end return if type == :update && diff.size == 0 if !activehistory_tracking[:habtm_model] activehistory_event.action!({ type: type, subject: self.activehistory_id, diff: diff, timestamp: @activehistory_timestamp }) end self._reflections.each do |key, reflection| foreign_key = activehistory_tracking.dig(:habtm_model, reflection.name, :foreign_key) || reflection.foreign_key if areflection = self.class.reflect_on_association(reflection.name) if areflection.macro == :has_and_belongs_to_many && type == :create self.send("#{areflection.name.to_s.singularize}_ids").each do |fid| next unless fid activehistory_association_udpated(areflection, fid, added: [diff['id'][1]], timestamp: ) activehistory_association_udpated(areflection.klass.reflect_on_association(areflection.[:inverse_of]), diff['id'][1], added: [fid], timestamp: , type: :create) end elsif areflection.macro == :has_and_belongs_to_many && type == :destroy self.send("#{areflection.name.to_s.singularize}_ids").each do |fid| activehistory_association_udpated(areflection, fid, removed: [diff['id'][0]], timestamp: , type: :update) activehistory_association_udpated(areflection.klass.reflect_on_association(areflection.[:inverse_of]), diff['id'][0], removed: [fid], timestamp: , type: :update) end end end next unless reflection.macro == :belongs_to && (type == :destroy || diff.has_key?(foreign_key)) case type when :create old_id = nil new_id = diff[foreign_key][1] when :destroy old_id = diff[foreign_key][0] new_id = nil else old_id = diff[foreign_key][0] new_id = diff[foreign_key][1] end relation_id = self.id || diff.find { |k, v| k != foreign_key }[1][1] if reflection.polymorphic? else activehistory_association_udpated(reflection, old_id, removed: [relation_id], timestamp: ) if old_id activehistory_association_udpated(reflection, new_id, added: [relation_id], timestamp: ) if new_id end end end |
#activehistory_tracking ⇒ Object
79 80 81 82 83 |
# File 'lib/activehistory/adapters/active_record.rb', line 79 def activehistory_tracking if ActiveHistory.configured? && self.class.instance_variable_defined?(:@activehistory) self.class.instance_variable_get(:@activehistory) end end |