lib/rabl/builder.rb in rabl-0.0.4 vs lib/rabl/builder.rb in rabl-0.0.5
- old
+ new
@@ -30,11 +30,11 @@
@options[:extends].each do |settings|
extends(settings[:file], settings[:options], &settings[:block])
end if @options.has_key?(:extends)
@_root_name ||= @_object.class.model_name.element
- options[:root] ? { @_root_name => @_result } : @_result
+ (@options[:root] || options[:root]) ? { @_root_name => @_result } : @_result
end
# Indicates an attribute or method should be included in the json output
# attribute :foo, :as => "bar"
# attribute :foo => :bar
@@ -72,22 +72,15 @@
object = data_object(data)
glued_attributes = self.object_to_hash(object, &block)
@_result.merge!(glued_attributes)
end
- # Renders a partial hash based on another rabl template
- # partial("users/show", :object => @user)
- def partial(file, options={}, &block)
- source = File.read(Rails.root.join("app/views/" + file + ".json.rabl"))
- self.object_to_hash(options[:object], source, &block)
- end
-
# Extends an existing rabl template with additional attributes in the block
# extends("users/show") { attribute :full_name }
def extends(file, options={}, &block)
options = options.merge!(:object => @_object)
- result = partial(file, options, &block)
+ result = @options[:engine].partial(file, options, &block)
@_result.merge!(result)
end
protected
@@ -108,10 +101,10 @@
# data_name(data) => "user"
# data_name(@user => :person) => :person
# data_name(@users) => :user
def data_name(data)
return data.values.first if data.is_a?(Hash)
- return data.first.class.model_name.element.pluralize if data.first.is_a?(ActiveRecord::Base)
+ return data.first.class.model_name.element.pluralize if data.respond_to?(:first) && data.first.respond_to?(:valid?)
data.class.model_name.element
end
end
end
\ No newline at end of file