lib/api_resource/finders.rb in api_resource-0.6.18 vs lib/api_resource/finders.rb in api_resource-0.6.19
- old
+ new
@@ -11,13 +11,13 @@
autoload :SingleObjectAssociationFinder
autoload :MultiObjectAssociationFinder
module ClassMethods
- # This decides which finder method to call.
+ # This decides which finder method to call.
# It accepts arguments of the form "scope", "options={}"
- # where options can be standard rails options or :expires_in.
+ # where options can be standard rails options or :expires_in.
# If :expires_in is set, it caches it for expires_in seconds.
# Need to support the following cases
# => 1) Klass.find(1)
# => 2) Klass.find(:all, :params => {a => b})
@@ -34,44 +34,37 @@
# TODO: Make this into a class attribute properly (if it isn't already)
# this is a little bit of a hack because options can sometimes be a Condition
expiry = @expiry
ApiResource.with_ttl(expiry.to_f) do
if numeric_find
- if single_find && (@conditions.blank_conditions? || include_associations_only?)
+ if single_find && (@conditions.blank_conditions? || nested_find_only?)
# If we have no conditions or they are only prefixes or
- # includes, and only one argument (not a word) then we
+ # includes, and only one argument (not a word) then we
# only have a single item to find.
# e.g. Class.includes(:association).find(1)
# Class.find(1)
- @scope = @scope.first if @scope.is_a?(Array)
final_cond = @conditions.merge!(ApiResource::Conditions::ScopeCondition.new({:id => @scope}, self))
ApiResource::Finders::SingleFinder.new(self, final_cond).load
else
# e.g. Class.scope(1).find(1)
# Class.includes(:association).find(1,2)
# Class.find(1,2)
# Class.active.find(1)
- if Array.wrap(@scope).size == 1 && @scope.is_a?(Array)
- @scope = @scope.first
- end
-
fnd = @conditions.merge!(ApiResource::Conditions::ScopeCondition.new({:find => {:ids => @scope}}, self))
fnd.send(:all)
end
else
# e.g. Class.scope(1).first
# Class.first
- @scope = @scope.first if @scope.is_a?(Array)
new_condition = @scope == :all ? {} : {@scope => true}
final_cond = @conditions.merge!ApiResource::Conditions::ScopeCondition.new(new_condition, self)
fnd = ApiResource::Finders::ResourceFinder.new(self, final_cond)
fnd.send(@scope)
end
-
end
end
# A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass
@@ -93,11 +86,11 @@
def all(*args)
find(:all, *args)
end
def instantiate_collection(collection)
- collection.collect{|record|
+ collection.collect{|record|
instantiate_record(record)
}
end
def instantiate_record(record)
@@ -149,27 +142,33 @@
args = Array.wrap(args)
# Conditions sometimes call find, passing themselves as the last arg.
if args.last.is_a?(ApiResource::Conditions::AbstractCondition)
- cond = args.slice!(args.length - 1)
+ cond = args.slice!(args.length - 1)
else
- cond = nil
+ cond = nil
end
# Support options being passed in as a hash.
- options = args.extract_options!
+ options = args.extract_options!
if options.blank?
options = nil
end
- @expiry = (options.is_a?(Hash) ? options.delete(:expires_in) : nil) || ApiResource::Base.ttl || 0
+ @expiry = (options.is_a?(Hash) ? options.delete(:expires_in) : nil) || ApiResource::Base.ttl || 0
combine_conditions(options, cond)
# Remaining args are the scope.
- @scope = args
+ @scope = args
+
+ if Array.wrap(@scope).size == 1 && @scope.is_a?(Array)
+ @scope = @scope.first
+ end
+
+ true
end
def combine_conditions(options, condition)
# Convert options hash to scope condition.
if options.is_a?(Hash)
@@ -190,10 +189,10 @@
end
@conditions = final_cond
end
- def include_associations_only?
+ def nested_find_only?
if @conditions.blank_conditions?
return false
else
return @conditions.conditions.include?(:foreign_key_id) &&
@conditions.conditions.size == 1
\ No newline at end of file