lib/flare/collection.rb in flare-1.4.4 vs lib/flare/collection.rb in flare-1.6.1
- old
+ new
@@ -18,15 +18,19 @@
def self.create_from_response(response, page, per_page, options)
# raise response.inspect
collection = self.new(page, per_page, response['response']['numFound'] || 0)
collection.response = response
- collection.replace(instantiate_objects(response))
+ collection.replace(instantiate_objects(response, options))
return collection
end
- def previous_page
+ def max_pages=(value)
+ @total_pages = value if value < @total_pages
+ end
+
+ def previous_page
current_page > 1 ? (current_page - 1) : nil
end
def next_page
current_page < total_pages ? (current_page + 1): nil
@@ -35,13 +39,27 @@
def offset
(current_page - 1) * @per_page
end
private
- def self.instantiate_objects(response)
+ def self.instantiate_objects(response, options = {})
+ types = Hash.new{|h, k| h[k] = []}
+ results = Hash.new{|h, k| h[k] = {}}
response['response']['docs'].map do |doc|
type, id = doc['id'].split(':')
- type.constantize.find(id)
+ types[type] << id
end
+
+ types.each do |type, ids|
+ includes = options[:include]
+ type.constantize.find_all_by_id(ids, :include => includes).each do |item|
+ results[type][item.id] = item
+ end
+ end
+
+ response['response']['docs'].map do |doc|
+ type, id = doc['id'].split(':')
+ results[type][id.to_i]
+ end.compact
end
end
end