Module: Erector::Rails

Defined in:
lib/erector/rails/rails_version.rb,
lib/erector/rails/extensions/rails_widget.rb,
lib/erector/rails/extensions/rails_helpers.rb

Defined Under Namespace

Modules: Helpers, WidgetExtensions

Constant Summary collapse

RAILS_VERSION =
"2.3.8"
RAILS_VERSION_TAG =
"v2.3.8"

Class Method Summary collapse

Class Method Details

.assigns_for(widget_class, view, local_assigns, is_partial) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/erector/rails/extensions/rails_widget.rb', line 3

def self.assigns_for(widget_class, view, local_assigns, is_partial)
  assigns = {}

  instance_variables = view.instance_variables_for_widget_assignment
  if is_partial || widget_class.ignore_extra_controller_assigns
    instance_variables = remove_unneeded_assigns(widget_class, instance_variables)
  end

  assigns.merge!(instance_variables) unless is_partial && (! widget_class.controller_assigns_propagate_to_partials)

  if is_partial
    assigns.merge!(filter_local_assigns_for_partial(widget_class, local_assigns || { }))
  end

  assigns
end

.filter_local_assigns_for_partial(widget_class, local_assigns) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/erector/rails/extensions/rails_widget.rb', line 29

def self.filter_local_assigns_for_partial(widget_class, local_assigns)
  widget_class_variable_name = widget_class.name.underscore
  widget_class_variable_name = $1 if widget_class_variable_name =~ %r{.*/(.*?)$}

  local_assigns.reject do |name, value|
    name == :object || name == widget_class_variable_name.to_sym
  end
end

.remove_unneeded_assigns(widget_class, assigns) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/erector/rails/extensions/rails_widget.rb', line 20

def self.remove_unneeded_assigns(widget_class, assigns)
  needs = widget_class.needed_variables
  if needs.empty?
    assigns
  else
    assigns.reject { |key, value| ! needs.include?(key) }
  end
end

.render(widget, view, assigns = nil, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/erector/rails/extensions/rails_widget.rb', line 38

def self.render(widget, view, assigns = nil, options = {})
  if widget.is_a?(Class)
    assigns ||= assigns_for(widget, view, nil, false)
    widget = widget.new(assigns)
  end

  view.send(:_evaluate_assigns_and_ivars)

  view.with_output_buffer do
    # Set parent to the view and use Rails's output buffer.
    widget.to_html(options.merge(:parent => view,
                              :output => Output.new { view.output_buffer }))
  end
end