lib/jbuilder/jbuilder_template.rb in jbuilder-1.5.3 vs lib/jbuilder/jbuilder_template.rb in jbuilder-2.0.0.beta
- old
+ new
@@ -1,14 +1,15 @@
require 'jbuilder'
require 'action_dispatch/http/mime_type'
+require 'active_support/cache'
class JbuilderTemplate < Jbuilder
class << self
attr_accessor :template_lookup_options
end
- self.template_lookup_options = { :handlers => [:jbuilder] }
+ self.template_lookup_options = { handlers: [:jbuilder] }
def initialize(context, *args, &block)
@context = context
super(*args, &block)
end
@@ -18,26 +19,24 @@
when ::Hash
# partial! partial: 'name', locals: { foo: 'bar' }
options = name_or_options
else
# partial! 'name', foo: 'bar'
- options = { :partial => name_or_options, :locals => locals }
+ options = { partial: name_or_options, locals: locals }
as = locals.delete(:as)
options[:as] = as if as.present?
options[:collection] = locals[:collection] if locals.key?(:collection)
end
- options[:collection] ||= [] if options.key?(:collection)
-
_handle_partial_options options
end
def array!(collection = [], *attributes, &block)
options = attributes.extract_options!
if options.key?(:partial)
- partial! options[:partial], options.merge(:collection => collection)
+ partial! options[:partial], options.merge(collection: collection)
else
super
end
end
@@ -61,26 +60,27 @@
end
end
protected
def _handle_partial_options(options)
- options.reverse_merge! :locals => {}
+ options.reverse_merge! locals: {}
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
- collection = options.delete(:collection)
as = options[:as]
- if collection && as
+ if as && options.key?(:collection)
+ collection = options.delete(:collection) || []
array!(collection) do |member|
- options[:locals].merge!(as => member, :collection => collection)
+ options[:locals].merge! as => member
+ options[:locals].merge! collection: collection
_render_partial options
end
else
_render_partial options
end
end
def _render_partial(options)
- options[:locals].merge!(:json => self)
+ options[:locals].merge! json: self
@context.render options
end
def _cache_key(key)
if @context.respond_to?(:cache_fragment_name)