Class: ActiveRecord::NamedScope::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/named_scope.rb

Constant Summary collapse

NON_DELEGATE_METHODS =
%w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?).to_set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_scope, options, &block) ⇒ Scope

Returns a new instance of Scope.



114
115
116
117
118
119
120
121
122
# File 'lib/active_record/named_scope.rb', line 114

def initialize(proxy_scope, options, &block)
  options ||= {}
  [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
  extend Module.new(&block) if block_given?
  unless Scope === proxy_scope
    @current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods)
  end
  @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/active_record/named_scope.rb', line 170

def method_missing(method, *args, &block)
  if scopes.include?(method)
    scopes[method].call(self, *args)
  else
    with_scope({:find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ?  proxy_options[:conditions] : {}}, :reverse_merge) do
      method = :new if method == :build
      if current_scoped_methods_when_defined && !scoped_methods.include?(current_scoped_methods_when_defined)
        with_scope current_scoped_methods_when_defined do
          proxy_scope.send(method, *args, &block)
        end
      else
        proxy_scope.send(method, *args, &block)
      end
    end
  end
end

Instance Attribute Details

#current_scoped_methods_when_definedObject (readonly)

Returns the value of attribute current_scoped_methods_when_defined.



104
105
106
# File 'lib/active_record/named_scope.rb', line 104

def current_scoped_methods_when_defined
  @current_scoped_methods_when_defined
end

#proxy_optionsObject (readonly)

Returns the value of attribute proxy_options.



104
105
106
# File 'lib/active_record/named_scope.rb', line 104

def proxy_options
  @proxy_options
end

#proxy_scopeObject (readonly)

Returns the value of attribute proxy_scope.



104
105
106
# File 'lib/active_record/named_scope.rb', line 104

def proxy_scope
  @proxy_scope
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
# File 'lib/active_record/named_scope.rb', line 156

def any?
  if block_given?
    proxy_found.any? { |*block_args| yield(*block_args) }
  else
    !empty?
  end
end

#empty?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/active_record/named_scope.rb', line 148

def empty?
  @found ? @found.empty? : count.zero?
end

#first(*args) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/active_record/named_scope.rb', line 128

def first(*args)
  if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
    proxy_found.first(*args)
  else
    find(:first, *args)
  end
end

#last(*args) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/active_record/named_scope.rb', line 136

def last(*args)
  if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
    proxy_found.last(*args)
  else
    find(:last, *args)
  end
end

#reloadObject



124
125
126
# File 'lib/active_record/named_scope.rb', line 124

def reload
  load_found; self
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/active_record/named_scope.rb', line 152

def respond_to?(method, include_private = false)
  super || @proxy_scope.respond_to?(method, include_private)
end

#sizeObject



144
145
146
# File 'lib/active_record/named_scope.rb', line 144

def size
  @found ? @found.length : count
end