Class: ActionView::Partials::PartialRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/render/partials.rb

Constant Summary collapse

PARTIAL_NAMES =
Hash.new {|h,k| h[k] = {} }

Instance Method Summary collapse

Constructor Details

#initialize(view_context, options, block) ⇒ PartialRenderer

Returns a new instance of PartialRenderer.



219
220
221
222
223
224
# File 'lib/action_view/render/partials.rb', line 219

def initialize(view_context, options, block)
  @view           = view_context
  @partial_names  = PARTIAL_NAMES[@view.controller.class.name]

  setup(options, block)
end

Instance Method Details

#collection_with_template(template = @template) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/action_view/render/partials.rb', line 284

def collection_with_template(template = @template)
  segments, locals, template = [], @locals, @template

  if @options[:as]
    as = @options[:as]
    counter = "#{as}_counter".to_sym
  else
    as = template.variable_name
    counter = template.counter_name
  end

  locals[counter] = -1

  @collection.each do |object|
    locals[counter] += 1
    locals[as] = object
    segments << template.render(@view, locals)
  end

  segments
end

#collection_without_template(collection_paths = @collection_paths) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/action_view/render/partials.rb', line 306

def collection_without_template(collection_paths = @collection_paths)
  segments, locals = [], @locals
  index, template  = -1, nil

  if @options[:as]
    as = @options[:as]
    counter = "#{as}_counter"
  end

  @collection.each_with_index do |object, i|
    template = find_template(collection_paths[i])
    locals[as || template.variable_name] = object
    locals[counter || template.counter_name] = (index += 1)

    segments << template.render(@view, locals)
  end

  @template = template
  segments
end

#renderObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/action_view/render/partials.rb', line 249

def render
  identifier = ((@template = find_template) ? @template.identifier : @path)

  if @collection
    ActiveSupport::Notifications.instrument("render_collection.action_view",
      :identifier => identifier || "collection", :count => @collection.size) do
      render_collection
    end
  else
    block, options, locals = @block, @options, @locals

    content = ActiveSupport::Notifications.instrument("render_partial.action_view",
      :identifier => identifier) do
      render_partial
    end

    if !block && (layout = options[:layout])
      content = @view._render_layout(find_template(layout), locals){ content }
    end

    content
  end
end

#render_collectionObject



273
274
275
276
277
278
279
280
281
282
# File 'lib/action_view/render/partials.rb', line 273

def render_collection
  return nil if @collection.blank?

  if @options.key?(:spacer_template)
    spacer = find_template(@options[:spacer_template]).render(@view, @locals)
  end

  result = @template ? collection_with_template : collection_without_template
  result.join(spacer).html_safe
end

#render_partial(object = @object) ⇒ Object



327
328
329
330
331
332
333
334
335
336
# File 'lib/action_view/render/partials.rb', line 327

def render_partial(object = @object)
  locals, view, template, block = @locals, @view, @template, @block

  object ||= locals[template.variable_name]
  locals[@options[:as] || template.variable_name] = object

  template.render(view, locals) do |*name|
    view._layout_for(*name, &block)
  end
end

#setup(options, block) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/action_view/render/partials.rb', line 226

def setup(options, block)
  partial = options[:partial]

  @options = options
  @locals  = options[:locals] || {}
  @block   = block

  if String === partial
    @object     = options[:object]
    @path       = partial
    @collection = collection
  else
    @object = partial

    if @collection = collection_from_object || collection
      paths = @collection_paths = @collection.map { |o| partial_path(o) }
      @path = paths.uniq.size == 1 ? paths.first : nil
    else
      @path = partial_path
    end
  end
end