Module: Erector::Rails

Extended by:
ActiveSupport::Concern
Defined in:
lib/erector/rails3.rb,
lib/erector/rails/form_builder.rb,
lib/erector/rails2/rails_version.rb,
lib/erector/rails/template_handler.rb,
lib/erector/rails2/extensions/rails_widget.rb,
lib/erector/rails2/extensions/rails_helpers.rb

Defined Under Namespace

Modules: Helpers, WidgetExtensions Classes: FormBuilder, TemplateHandler

Constant Summary collapse

RAILS_VERSION =
"2.3.11"
RAILS_VERSION_TAG =
"v2.3.11"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate to non-markup producing helpers via method_missing, returning their result directly.


125
126
127
128
129
130
131
# File 'lib/erector/rails3.rb', line 125

def method_missing(name, *args, &block)
  if helpers.respond_to?(name)
    helpers.send(name, *args, &block)
  else
    super
  end
end

Class Method Details

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


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erector/rails3.rb', line 14

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

  view.assigns.each do |name, value|
    name = name.to_sym
    assigns[name] = value if should_assign?(name, widget_class, is_partial)
  end

  assigns.merge!(filter_local_assigns_for_partial(widget_class, local_assigns)) if is_partial

  assigns
end

.def_rails_form_helper(method_name) ⇒ Object


56
57
58
59
60
61
62
63
64
# File 'lib/erector/rails3.rb', line 56

def def_rails_form_helper(method_name)
  module_eval <<-METHOD_DEF, __FILE__, __LINE__+1
    def #{method_name}(*args, &block)
      options = args.extract_options!
      args << options.merge(:builder => FormBuilder.wrapping(options[:builder]))
      text helpers.#{method_name}(*args, &block)
    end
  METHOD_DEF
end

.def_simple_rails_helper(method_name) ⇒ Object

Wrappers for rails helpers that produce markup. Erector needs to manually emit their result.


48
49
50
51
52
53
54
# File 'lib/erector/rails3.rb', line 48

def def_simple_rails_helper(method_name)
  module_eval <<-METHOD_DEF, __FILE__, __LINE__+1
    def #{method_name}(*args, &block)
      text helpers.#{method_name}(*args, &block)
    end
  METHOD_DEF
end

.filter_local_assigns_for_partial(widget_class, local_assigns) ⇒ Object


27
28
29
30
31
32
33
34
# File 'lib/erector/rails3.rb', line 27

def 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/rails2/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


36
37
38
39
40
41
42
43
44
# File 'lib/erector/rails3.rb', line 36

def render(widget, view, local_assigns = {}, is_partial = false, options = {})
  widget = widget.new(assigns_for(widget, view, local_assigns, is_partial)) if widget.is_a?(Class)
  view.with_output_buffer do
    # Set parent and helpers to the view and use Rails's output buffer.
    widget.to_html(options.merge(:helpers => view,
                                 :parent  => view,
                                 :output  => Output.new(:buffer => lambda { view.output_buffer })))
  end
end

.should_assign?(name, widget_class, is_partial) ⇒ Boolean

Returns:

  • (Boolean)

9
10
11
12
# File 'lib/erector/rails3.rb', line 9

def should_assign?(name, widget_class, is_partial)
  (!widget_class.ignore_extra_controller_assigns || widget_class.needs?(name)) &&
    (!is_partial || widget_class.controller_assigns_propagate_to_partials)
end

Instance Method Details

#capture(&block) ⇒ Object

We need to delegate #capture to helpers.capture, so that when the captured block is executed, both erector and Rails output from within the block go to the appropriate buffer.


106
107
108
109
110
111
112
# File 'lib/erector/rails3.rb', line 106

def capture(&block)
  if helpers.respond_to?(:capture)
    raw(helpers.capture(&block).to_s)
  else
    super
  end
end

#render(*args, &block) ⇒ Object

Wrap Rails' render method, to capture output from partials etc.


115
116
117
118
119
120
121
# File 'lib/erector/rails3.rb', line 115

def render(*args, &block)
  captured = helpers.capture do
    helpers.concat(helpers.render(*args, &block))
    helpers.output_buffer.to_s
  end
  rawtext(captured)
end

#respond_to?(name) ⇒ Boolean

Since we delegate method_missing to helpers, we need to delegate respond_to? as well.

Returns:

  • (Boolean)

135
136
137
# File 'lib/erector/rails3.rb', line 135

def respond_to?(name)
  super || helpers.respond_to?(name)
end