Created
July 17, 2014 02:18
-
-
Save kenkeiter/28d615da0b432e521c5a to your computer and use it in GitHub Desktop.
If you're using Rabl with DataMapper, lazy collection queries won't be evaluated correctly, and you'll end up SELECTing each record from the database. This patch fixes that issue (albeit, in an ugly way).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Rabl | |
class Engine | |
def collection(data, options={}) | |
self.object(data_object(data)) | |
end | |
end | |
module Helpers | |
def data_name(data_token) | |
return unless data_token # nil or false | |
return data_token.values.first if data_token.is_a?(Hash) # @user => :user | |
data = data_object(data_token) | |
if is_collection?(data) and data.respond_to?(:first) and not data.kind_of?(DataMapper::Collection) # data is a collection | |
object_name = data_name(data.first).to_s.pluralize if data.first.present? | |
object_name ||= data_token if data_token.is_a?(Symbol) | |
object_name | |
elsif is_object?(data) # data is an object | |
object_name = object_root_name if object_root_name | |
object_name ||= data if data.is_a?(Symbol) | |
object_name ||= collection_root_name.to_s.singularize if collection_root_name | |
object_name ||= data.class.respond_to?(:model_name) ? data.class.model_name.element : data.class.to_s.downcase | |
object_name | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment