module PeriodicRecords::Model
Constants
- MAX
- MIN
Public Instance Methods
adjust_overlapping_records()
click to toggle source
# File lib/periodic_records/model.rb, line 55 def adjust_overlapping_records overlapping_records.each do |overlapping_record| if overlapping_record.start_at >= start_at && overlapping_record.end_at <= end_at destroy_overlapping_record(overlapping_record) elsif overlapping_record.start_at < start_at && overlapping_record.end_at > end_at split_overlapping_record(overlapping_record) elsif overlapping_record.start_at < start_at adjust_overlapping_record_end_at(overlapping_record) elsif overlapping_record.end_at > end_at adjust_overlapping_record_start_at(overlapping_record) end end end
current?()
click to toggle source
# File lib/periodic_records/model.rb, line 38 def current? date = Date.current within_interval?(date, date) end
overlapping_records()
click to toggle source
# File lib/periodic_records/model.rb, line 51 def overlapping_records siblings.within_interval(start_at, end_at) end
periodic_dup()
click to toggle source
# File lib/periodic_records/model.rb, line 75 def periodic_dup dup end
record_split_step()
click to toggle source
# File lib/periodic_records/model.rb, line 79 def record_split_step @record_split_step ||= begin column = self.class.column_for_attribute(:start_at) if column.type == :datetime precision = column.precision || 6 Float("1.0e-#{precision}").seconds else 1.day end end end
set_default_period_after_initialize?()
click to toggle source
# File lib/periodic_records/model.rb, line 71 def set_default_period_after_initialize? new_record? end
siblings()
click to toggle source
# File lib/periodic_records/model.rb, line 47 def siblings raise NotImplementedError end
within_interval?(start_date, end_date)
click to toggle source
# File lib/periodic_records/model.rb, line 43 def within_interval?(start_date, end_date) start_at && end_at && start_at <= end_date && end_at >= start_date end
Private Instance Methods
adjust_overlapping_record_end_at(overlapping_record)
click to toggle source
# File lib/periodic_records/model.rb, line 114 def adjust_overlapping_record_end_at(overlapping_record) overlapping_record.end_at = start_at - record_split_step overlapping_record.save(validate: false) end
adjust_overlapping_record_start_at(overlapping_record)
click to toggle source
# File lib/periodic_records/model.rb, line 119 def adjust_overlapping_record_start_at(overlapping_record) overlapping_record.start_at = end_at + record_split_step overlapping_record.save(validate: false) end
destroy_overlapping_record(overlapping_record)
click to toggle source
# File lib/periodic_records/model.rb, line 98 def destroy_overlapping_record(overlapping_record) overlapping_record.destroy end
set_default_period()
click to toggle source
# File lib/periodic_records/model.rb, line 93 def set_default_period self.start_at ||= Date.current self.end_at ||= MAX end
split_overlapping_record(overlapping_record)
click to toggle source
# File lib/periodic_records/model.rb, line 102 def split_overlapping_record(overlapping_record) overlapping_record_end = overlapping_record.periodic_dup overlapping_record_end.start_at = end_at + record_split_step overlapping_record_end.end_at = overlapping_record.end_at overlapping_record_start = overlapping_record overlapping_record_start.end_at = start_at - record_split_step overlapping_record_start.save(validate: false) overlapping_record_end.save(validate: false) end
validate_dates()
click to toggle source
# File lib/periodic_records/model.rb, line 124 def validate_dates if start_at && end_at && end_at < start_at errors.add :end_at, :invalid end end