Sha256: 6a0fe155169a823fac33d019ec7ac478657f0f364114387ba346664f057e7459

Contents?: true

Size: 1.17 KB

Versions: 30

Compression:

Stored size: 1.17 KB

Contents

module ActiveZuora
  module Scoping

    extend ActiveSupport::Concern

    included do
      class << self

        # Delegate to :scoped
        delegate :find, :all, :find_each, :to => :scoped
        delegate :select, :where, :and, :or, :order, :to => :scoped
        delegate :first, :last, :each, :map, :any?, :empty?, :blank?, :present?, :size, :count, :to => :scoped

        # Keep track of a current scope.
        attr_accessor :current_scope

      end
    end

    module ClassMethods

      def scoped
        current_scope || relation
      end

      def unscoped
        block_given? ? relation.scoped { yield } : relation
      end

      def exclude_from_queries(*field_names)
        (@excluded_from_queries ||= []).concat field_names.map(&:to_sym)
      end

      def relation
        query_field_names = field_names - (@excluded_from_queries ||= [])
        Relation.new(self, query_field_names)
      end

      def scope(name, body)
        # Body can be a Relation or a lambda that returns a relation.
        define_singleton_method(name) do |*args|
          body.respond_to?(:call) ? body.call(*args) : scoped.merge(body)
        end
      end

    end

  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
active_zuora-2.6.0 lib/active_zuora/scoping.rb
active_zuora-2.5.4 lib/active_zuora/scoping.rb
active_zuora-2.5.3 lib/active_zuora/scoping.rb
active_zuora-2.5.2 lib/active_zuora/scoping.rb
active_zuora-2.5.1 lib/active_zuora/scoping.rb
active_zuora-2.5.0 lib/active_zuora/scoping.rb
active_zuora-2.4.1 lib/active_zuora/scoping.rb
active_zuora-2.4.0 lib/active_zuora/scoping.rb
active_zuora-2.3.1 lib/active_zuora/scoping.rb
active_zuora-2.3.0 lib/active_zuora/scoping.rb
active_zuora-2.2.7 lib/active_zuora/scoping.rb
active_zuora-2.2.6 lib/active_zuora/scoping.rb
active_zuora-2.2.5 lib/active_zuora/scoping.rb
active_zuora-2.2.4 lib/active_zuora/scoping.rb
active_zuora-2.2.3 lib/active_zuora/scoping.rb
active_zuora-2.2.2 lib/active_zuora/scoping.rb
active_zuora-2.2.1 lib/active_zuora/scoping.rb
active_zuora-2.2.0 lib/active_zuora/scoping.rb
active_zuora-2.1.4 lib/active_zuora/scoping.rb
active_zuora-2.1.3 lib/active_zuora/scoping.rb