module RailsRbs::Rules::DateRange

Used by the date_range default rule type to ensure an object's observed_field falls in a certain range of two dates. Assumes the rule model supports a start_date and end_date field

Public Instance Methods

filter_objects(association) click to toggle source

Return an association of active record objects that fall in the date range of the rule using the provided association as a base to query from. @param association Active record association, relation or collection proxy that supports the where interface.

# File lib/rails_rbs/rules/date_range.rb, line 30
def filter_objects(association)
  start_date, end_date = self.start_date, self.end_date
  # Attempt to coerce the types if we can
  if self.respond_to?(:enforced_type) && self.respond_to?(:force_type)
    start_date = force_type(start_date)
    end_date = force_type(end_date)
  end
  association.where("#{self.observed_field} >= ? AND #{self.observed_field} <= ?", start_date, end_date)
end
follows_rule?(*objects) click to toggle source

Check one or more object's observed_field to ensure it falls between a start_date and end_date @param objects [Array<ActiveRecord::Base>] active record objects, or objects that respond to the observed_field value of the including rule. @return true if the provided object(s) follow the current rule

# File lib/rails_rbs/rules/date_range.rb, line 12
def follows_rule?(*objects)
  objects.all? do |object|
    source_date = object.send(self.observed_field.to_sym)
    range_start, range_end = self.start_date, self.end_date
    # Attempt to coerce the types if we can
    if self.respond_to?(:enforced_type) && self.respond_to?(:force_type)
      source_date = force_type(source_date)
      range_start = force_type(range_start)
      range_end = force_type(range_end)
    end
    # Was having mixed results with .between method
    source_date >= range_start && source_date <= range_end
  end
end