Sha256: 427a5edc7831c0963681a7b7ba06eab1fc13013a4b377684084fb149190fd303

Contents?: true

Size: 862 Bytes

Versions: 9

Compression:

Stored size: 862 Bytes

Contents

require 'active_support/concern'

module ActiveHouse
  module Scopeable
    extend ActiveSupport::Concern

    included do
      private

      def apply_scope(name, *args)
        scope = model_class._scopes.fetch(name.to_sym)
        instance_exec(*args, &scope)
      end

      def scope?(name)
        model_class._scopes.key?(name.to_sym)
      end

      def apply_default_scope
        return if model_class._default_scope.nil?
        apply_scope(model_class._default_scope)
      end
    end

    def initialize(*)
      super
      with_current_query { apply_default_scope }
    end

    def respond_to_missing?(method_name, *_args)
      scope?(method_name) || super
    end

    def method_missing(method_name, *args, &_block)
      if scope?(method_name)
        apply_scope(method_name, *args)
      else
        super
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_house-0.4.0 lib/active_house/scopeable.rb
active_house-0.3.2 lib/active_house/scopeable.rb
active_house-0.3.1 lib/active_house/scopeable.rb
active_house-0.3.0 lib/active_house/scopeable.rb
active_house-0.2.2 lib/active_house/scopeable.rb
active_house-0.2.1 lib/active_house/scopeable.rb
active_house-0.2.0 lib/active_house/scopeable.rb
active_house-0.1.5 lib/active_house/scopeable.rb
active_house-0.1.4 lib/active_house/scopeable.rb