module StateOfTheNation

Constants

ConfigurationError
VERSION

Private Instance Methods

bad_configuration?() click to toggle source
# File lib/state_of_the_nation.rb, line 174
def bad_configuration?
  [start_key, finish_key].any?(&:blank?)
end
ensure_finishes_after_starts() click to toggle source
# File lib/state_of_the_nation.rb, line 123
def ensure_finishes_after_starts
  return if finish.blank?
  return if finish >= start
  errors.add(finish_key, "must be after #{start_key.to_s.humanize}")
end
finish() click to toggle source
# File lib/state_of_the_nation.rb, line 169
def finish
  return unless finish_key.present?
  round_if_should(self.send(finish_key))
end
finish_key() click to toggle source
# File lib/state_of_the_nation.rb, line 192
def finish_key
  self.class.finish_key
end
ignore_empty() click to toggle source
# File lib/state_of_the_nation.rb, line 196
def ignore_empty
  self.class.ignore_empty
end
model() click to toggle source
# File lib/state_of_the_nation.rb, line 159
def model
  return unless parent_association.present?
  self.send(parent_association)
end
other_records_active_in_range() click to toggle source
# File lib/state_of_the_nation.rb, line 137
def other_records_active_in_range
  records = self.class.where(parent_association => model)
  # all records scoped to the model (e.g. all subscriptions for a customer)

  records = records.where("id != ?", id) if id.present?
  # excluding the current record

  records = records.where(QueryString.query_for(:less_than, self.class), finish) if finish.present?
  # find competing records which *start* being active BEFORE the current record *finishes* being active
  # (if the current record is set to finish being active)

  records = records.where(QueryString.query_for(:greater_than_or_null, self.class), start)
  # find competing records which *finish* being active AFTER this record *starts* being active
  # (or ones which are not set to finish being active)

  records = records.where(QueryString.query_for(:start_and_finish_not_equal_or_are_null, self.class)) if ignore_empty
  # exclude records where there is no difference between start and finish dates
  # (we need to deliberately not filter out records with null value keys with this comparison as not equal comparisons to null are always deemed null/false)

  records
end
parent_association() click to toggle source
# File lib/state_of_the_nation.rb, line 184
def parent_association
  self.class.parent_association
end
prevent_active_collisions() click to toggle source
# File lib/state_of_the_nation.rb, line 129
def prevent_active_collisions
  return unless prevent_multiple_active
  raise ConfigurationError if bad_configuration?
  return unless model.present?

  raise ConflictError.new(self, other_records_active_in_range) if other_records_active_in_range.any?
end
prevent_multiple_active() click to toggle source
# File lib/state_of_the_nation.rb, line 180
def prevent_multiple_active
  self.class.prevent_multiple_active
end
round_if_should(time) click to toggle source
# File lib/state_of_the_nation.rb, line 204
def round_if_should(time)
  return time if time.nil?
  self.class.send(:round_if_should, time)
end
should_round_timestamps?() click to toggle source
# File lib/state_of_the_nation.rb, line 200
def should_round_timestamps?
  self.class.send(:should_round_timestamps?)
end
start() click to toggle source
# File lib/state_of_the_nation.rb, line 164
def start
  return unless start_key.present?
  return round_if_should(self.send(start_key) || Time.now.utc)
end
start_key() click to toggle source
# File lib/state_of_the_nation.rb, line 188
def start_key
  self.class.start_key
end