Sha256: db8f9ff0e61fc17f7db543a1893fbedda012769a5bfcb76b1e54187d1bb880ba

Contents?: true

Size: 950 Bytes

Versions: 10

Compression:

Stored size: 950 Bytes

Contents

module ActiveRecord
  module Scoping
    module Named
      module ClassMethods
        attr_reader :scope_names

        def scope(name, body, &block)
          @scope_names ||= []
          unless body.respond_to?(:call)
            raise ArgumentError, 'The scope body needs to be callable.'
          end

          if dangerous_class_method?(name)
            raise ArgumentError, "You tried to define a scope named \"#{name}\" " \
              "on the model \"#{self.name}\", but Active Record already defined " \
              "a class method with the same name."
          end

          extension = Module.new(&block) if block

          singleton_class.send(:define_method, name) do |*args|
            scope = all.scoping { body.call(*args) }
            scope = scope.extending(extension) if extension

            scope || all
          end


          @scope_names << name if body.arity.eql?(0)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_hash_relation-1.1.0 lib/active_record/scope_names.rb
active_hash_relation-1.0.5 lib/active_record/scope_names.rb
active_hash_relation-1.0.4 lib/active_record/scope_names.rb
active_hash_relation-1.0.3 lib/active_record/scope_names.rb
active_hash_relation-1.0.2 lib/active_record/scope_names.rb
active_hash_relation-1.0.1 lib/active_record/scope_names.rb
active_hash_relation-1.0.0 lib/active_record/scope_names.rb
active_hash_relation-0.0.3 lib/active_record/scope_names.rb
active_hash_relation-0.0.2 lib/active_record/scope_names.rb
active_hash_relation-0.0.1 lib/active_record/scope_names.rb