Module: UpdateHelper
- Included in:
- TimeTravel, Timeline
- Defined in:
- lib/time_travel/update_helper.rb
Instance Method Summary collapse
- #construct_corrected_records(new_record, affected_timeframes, affected_records, attributes) ⇒ Object
- #fetch_history_for_correction(record) ⇒ Object
- #get_affected_timeframes(record, affected_records) ⇒ Object
- #ignored_copy_attributes ⇒ Object
- #squish_record_history(corrected_records) ⇒ Object
Instance Method Details
#construct_corrected_records(new_record, affected_timeframes, affected_records, attributes) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/time_travel/update_helper.rb', line 23 def construct_corrected_records(new_record, affected_timeframes, affected_records, attributes) affected_timeframes.map do |timeframe| matched_record = affected_records.find do |record| record.effective_from <= timeframe[:from] && record.effective_till >= timeframe[:till] end if matched_record attrs = matched_record.attributes.except(*ignored_copy_attributes) if timeframe[:from] >= new_record.effective_from && timeframe[:till] <= new_record.effective_till attrs.merge!(attributes) end else attrs = new_record.attributes.except(*ignored_copy_attributes) end attrs.merge!( **@timeline_identifiers, effective_from: timeframe[:from], effective_till: timeframe[:till]).symbolize_keys end end |
#fetch_history_for_correction(record) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/time_travel/update_helper.rb', line 2 def fetch_history_for_correction(record) correction_head = self.effective_history .where("effective_from <= ?", record.effective_from) .where("effective_till > ?", record.effective_from).first correction_tail = self.effective_history .where("effective_from < ?", record.effective_till) .where("effective_till >= ?", record.effective_till).first correction_range = self.effective_history .where("effective_from > ?", record.effective_from) .where("effective_till < ?", record.effective_till) [correction_head, correction_range.to_a, correction_tail].flatten.compact.uniq end |
#get_affected_timeframes(record, affected_records) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/time_travel/update_helper.rb', line 16 def get_affected_timeframes(record,affected_records) affected_timeframes = affected_records.map { |record| [record.effective_from, record.effective_till] } affected_timeframes << [record.effective_from, record.effective_till] affected_timeframes = affected_timeframes.flatten.uniq.sort affected_timeframes.each_with_index.map{|time, i| {from: time, till: affected_timeframes[i+1]} }[0..-2] end |
#ignored_copy_attributes ⇒ Object
69 70 71 |
# File 'lib/time_travel/update_helper.rb', line 69 def ignored_copy_attributes ["id", "created_at", "updated_at", "valid_from", "valid_till"] end |
#squish_record_history(corrected_records) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/time_travel/update_helper.rb', line 45 def squish_record_history(corrected_records) squished = [] corrected_records.each do |current| # fetch and compare last vs current record last_squished = squished.last effective_attr = [:effective_from, :effective_till] if last_squished && if last_squished.except(*effective_attr) == current.except(*effective_attr) && last_squished[:effective_till] == current[:effective_from] # remove last_squished and push squished attributes squished = squished[0..-2] squished << last_squished.merge(effective_from: last_squished[:effective_from], effective_till: current[:effective_till]) end else squished << current end end squished.compact end |