Sha256: 2ea5820b5702ba9553b46776657cfde5e26249f18defacf569e64d7f5d2a9792

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'active_support'
module Forcast
  module Model
    module RuleEngine
      module RuleScope
        extend ActiveSupport::Concern

        included do
          scope :actives, -> { where(active?: true) }
          scope :for_attributes, ->(attr_names) { where(attr_comparable: attr_names) }
          scope :for_class, ->(class_name) { where(class_rule?: true, rule_model: class_name) }
          scope :for_object, ->(object) {
            where(class_rule?: false, rule_model: object.class.name, rule_model_id: object.id)
          }
          scope :for_object_and_class, ->(object) {
            where(self._for_class(object.class.name).or(self._for_object(object)))
          }
        end

        class_methods do
          def _for_class(class_name)
            arel_table[:class_rule?].eq(true).and(arel_table[:rule_model].eq(class_name))
          end

          def _for_object(object)
            arel_table[:class_rule?]
              .eq(false)
              .and(arel_table[:rule_model].eq(object.class.name))
              .and(arel_table[:rule_model_id].eq(object.id))
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
forcast-0.0.110 lib/forcast/models/rule_engine/rule_scope.rb