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.



119
120
121
122
123
124
125
126
127
# File 'lib/active_record/named_scope.rb', line 119

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 || ActiveRecord::Associations::AssociationCollection === 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)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_record/named_scope.rb', line 175

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.



109
110
111
# File 'lib/active_record/named_scope.rb', line 109

def current_scoped_methods_when_defined
  @current_scoped_methods_when_defined
end

#proxy_optionsObject (readonly)

Returns the value of attribute proxy_options.



109
110
111
# File 'lib/active_record/named_scope.rb', line 109

def proxy_options
  @proxy_options
end

#proxy_scopeObject (readonly)

Returns the value of attribute proxy_scope.



109
110
111
# File 'lib/active_record/named_scope.rb', line 109

def proxy_scope
  @proxy_scope
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
# File 'lib/active_record/named_scope.rb', line 161

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#first(*args) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/active_record/named_scope.rb', line 133

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



141
142
143
144
145
146
147
# File 'lib/active_record/named_scope.rb', line 141

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



129
130
131
# File 'lib/active_record/named_scope.rb', line 129

def reload
  load_found; self
end

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

Returns:

  • (Boolean)


157
158
159
# File 'lib/active_record/named_scope.rb', line 157

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

#sizeObject



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

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