Class: Tilt::Jbuilder

Inherits:
Jbuilder
  • Object
show all
Defined in:
lib/tilt/jbuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope, *args, &block) ⇒ Jbuilder

Returns a new instance of Jbuilder.



6
7
8
9
# File 'lib/tilt/jbuilder.rb', line 6

def initialize(scope, *args, &block)
  @scope = scope
  super(*args, &block)
end

Instance Method Details

#array!(collection = [], *attributes, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/tilt/jbuilder.rb', line 29

def array!(collection = [], *attributes, &block)
  options = attributes.extract_options!

  if options.key?(:partial)
    partial! options[:partial], options.merge(collection: collection)
  else
    super
  end
end

#cache!(key = nil, options = {}, &block) ⇒ Object

for now caching is not supported, but at least we can transparently ignore it



40
41
42
# File 'lib/tilt/jbuilder.rb', line 40

def cache!(key=nil, options={}, &block)
  yield
end

#partial!(name_or_options, locals = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tilt/jbuilder.rb', line 11

def partial!(name_or_options, locals = {})
  case name_or_options
  when ::Hash
    # partial! partial: 'name', locals: { foo: 'bar' }
    options = name_or_options
  else
    # partial! 'name', foo: 'bar'
    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

  view_path = @scope.instance_variable_get('@_jbuilder_view_path')
  template = ::Tilt::JbuilderTemplate.new(fetch_partial_path(options[:partial].to_s, view_path), nil, view_path: view_path)
  render_partial_with_options template, options
end