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.



182
183
184
185
186
187
# File 'lib/action_view/render/partials.rb', line 182

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/action_view/render/partials.rb', line 245

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/action_view/render/partials.rb', line 267

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/action_view/render/partials.rb', line 212

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
    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



234
235
236
237
238
239
240
241
242
243
# File 'lib/action_view/render/partials.rb', line 234

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



288
289
290
291
292
293
294
295
296
297
# File 'lib/action_view/render/partials.rb', line 288

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

  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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/action_view/render/partials.rb', line 189

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
      paths = @collection_paths = @collection.map { |o| partial_path(o) }
      @path = paths.uniq.size == 1 ? paths.first : nil
    else
      @path = partial_path
    end
  end
end