lib/jbuilder/jbuilder_template.rb in jbuilder-2.2.16 vs lib/jbuilder/jbuilder_template.rb in jbuilder-2.3.0

- old
+ new

@@ -9,44 +9,21 @@ self.template_lookup_options = { handlers: [:jbuilder] } def initialize(context, *args) @context = context - super(*args) + super *args end - def partial!(name_or_options, locals = {}) - case name_or_options - when ::Hash - # partial! partial: 'name', foo: 'bar' - options = name_or_options + def partial!(*args) + if args.one? && _is_active_model?(args.first) + _render_active_model_partial args.first else - # partial! 'name', locals: {foo: 'bar'} - if locals.one? && (locals.keys.first == :locals) - options = locals.merge(partial: name_or_options) - else - options = { partial: name_or_options, locals: locals } - end - # partial! 'name', foo: 'bar' - as = locals.delete(:as) - options[:as] = as if as.present? - options[:collection] = locals[:collection] if locals.key?(:collection) + _render_explicit_partial *args end - - _render_partial_with_options options end - def array!(collection = [], *attributes) - options = attributes.extract_options! - - if options.key?(:partial) - partial! options[:partial], options.merge(collection: collection) - else - super - end - end - # Caches the json constructed within the block passed. Has the same signature as the `cache` helper # method in `ActionView::Helpers::CacheHelper` and so can be used in the same way. # # Example: # @@ -76,12 +53,32 @@ # end def cache_if!(condition, *args) condition ? cache!(*args, &::Proc.new) : yield end - protected + def array!(collection = [], *args) + options = args.first + if args.one? && _partial_options?(options) + partial! options.merge(collection: collection) + else + super + end + end + + def set!(name, object = BLANK, *args) + options = args.first + + if args.one? && _partial_options?(options) + _set_inline_partial name, object, options + else + super + end + end + + private + def _render_partial_with_options(options) options.reverse_merge! locals: {} options.reverse_merge! ::JbuilderTemplate.template_lookup_options as = options[:as] @@ -109,12 +106,10 @@ key = _fragment_name_with_digest(key, options) key = url_for(key).split('://', 2).last if ::Hash === key ::ActiveSupport::Cache.expand_cache_key(key, :jbuilder) end - private - def _fragment_name_with_digest(key, options) if @context.respond_to?(:cache_fragment_name) # Current compatibility, fragment_name_with_digest is private again and cache_fragment_name # should be used instead. @context.cache_fragment_name(key, options) @@ -124,13 +119,53 @@ else key end end - def _mapable_arguments?(value, *args) - return true if super - options = args.last - ::Hash === options && options.key?(:as) + def _partial_options?(options) + ::Hash === options && options.key?(:as) && options.key?(:partial) + end + + def _is_active_model?(object) + object.class.respond_to?(:model_name) && object.respond_to?(:to_partial_path) + end + + def _set_inline_partial(name, object, options) + value = if object.nil? + [] + elsif _is_collection?(object) + _scope{ _render_partial_with_options options.merge(collection: object) } + else + locals = ::Hash[options[:as], object] + _scope{ _render_partial options.merge(locals: locals) } + end + + set! name, value + end + + def _render_explicit_partial(name_or_options, locals = {}) + case name_or_options + when ::Hash + # partial! partial: 'name', foo: 'bar' + options = name_or_options + else + # partial! 'name', locals: {foo: 'bar'} + if locals.one? && (locals.keys.first == :locals) + options = locals.merge(partial: name_or_options) + else + options = { partial: name_or_options, locals: locals } + end + # partial! 'name', foo: 'bar' + as = locals.delete(:as) + options[:as] = as if as.present? + options[:collection] = locals[:collection] if locals.key?(:collection) + end + + _render_partial_with_options options + end + + def _render_active_model_partial(object) + @context.render object, json: self end end class JbuilderHandler cattr_accessor :default_format