module RailsRbs::FollowsRuleSet

Public Instance Methods

apply_rule_set(*objects) click to toggle source

Apply the rule_set to all matching objects in the provided collection. Application simply means the enforced_field will be set to the enforced_value for any objects that follow all rules in the set. @param objects [Array<ActiveRecord::BAse> | ActiveRecord::Base] a single collection of active record objects to check this apply this rule_set to. Only objects that follow all rules in the set will have the rule_set applied.

# File lib/rails_rbs/follows_rule_set.rb, line 33
def apply_rule_set(*objects)
  follows_rules = filter_objects(objects)
  return if follows_rules.empty?
  self.rule_actions.map { |rule_action| rule_action.apply(follows_rules.flatten) }
end
filter_objects(*objects) click to toggle source

Return a collection of active record objects that follow all rules in the rule_set using the provided collection or association as a base to query from. @param objects collection of objects or an association object that supports the where interface @return Array collection of objects that follow all rules in the rule_set

# File lib/rails_rbs/follows_rule_set.rb, line 42
def filter_objects(*objects)
  if objects.count == 1 && objects.first.respond_to?(:where)
    follows_rules = objects.first
    self.rules.each { |rule| follows_rules = rule.filter_objects(follows_rules) }
  else
    follows_rules = objects.select { |obj| self.follows_rule_set?(obj) }
  end
  follows_rules
end
follows_rule_set?(*objects) click to toggle source

Check if an object or collection of objects follows all rules in this rule_set @param objects [Array<ActiveRecord::Base> | ActiveRecord::Base] a single or collection of active record objects to check this rule_set against. @return true if the object or objects follows all rules in the rule_set

# File lib/rails_rbs/follows_rule_set.rb, line 24
def follows_rule_set?(*objects)
  self.rules.all? { |rule| rule.follows_rule?(*objects.flatten) }
end