lib/jbuilder/jbuilder_template.rb in jbuilder-2.0.7 vs lib/jbuilder/jbuilder_template.rb in jbuilder-2.0.8

- old
+ new

@@ -1,6 +1,6 @@ -require 'jbuilder' +require 'jbuilder/jbuilder' require 'action_dispatch/http/mime_type' require 'active_support/cache' class JbuilderTemplate < Jbuilder class << self @@ -72,51 +72,58 @@ def cache_if!(condition, *args, &block) condition ? cache!(*args, &block) : yield end protected - def _handle_partial_options(options) - options.reverse_merge! locals: {} - options.reverse_merge! ::JbuilderTemplate.template_lookup_options - as = options[:as] - if as && options.key?(:collection) - collection = options.delete(:collection) || [] - array!(collection) do |member| - options[:locals].merge! as => member - options[:locals].merge! collection: collection - _render_partial options - end - else + def _handle_partial_options(options) + options.reverse_merge! locals: {} + options.reverse_merge! ::JbuilderTemplate.template_lookup_options + as = options[:as] + + if as && options.key?(:collection) + collection = options.delete(:collection) || [] + array!(collection) do |member| + 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 - @context.render options - end + def _render_partial(options) + options[:locals].merge! json: self + @context.render options + end - def _cache_key(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) - elsif @context.respond_to?(:fragment_name_with_digest) - # Backwards compatibility for period of time when fragment_name_with_digest was made public. - @context.fragment_name_with_digest(key) - else - ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder) - end - end + def _cache_key(key, options) + 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 _mapable_arguments?(value, *args) - return true if super - options = args.last - ::Hash === options && options.key?(:as) + 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) + elsif @context.respond_to?(:fragment_name_with_digest) + # Backwards compatibility for period of time when fragment_name_with_digest was made public. + @context.fragment_name_with_digest(key) + else + key end + end + + def _mapable_arguments?(value, *args) + return true if super + options = args.last + ::Hash === options && options.key?(:as) + end end class JbuilderHandler cattr_accessor :default_format self.default_format = Mime::JSON