Module: ActiveRecord::Scoping::Named::ClassMethods

Defined in:
lib/active_record/scope_names.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scope_namesObject (readonly)

Returns the value of attribute scope_names.



5
6
7
# File 'lib/active_record/scope_names.rb', line 5

def scope_names
  @scope_names
end

Instance Method Details

#scope(name, body, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/scope_names.rb', line 7

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