Module: Erector::Rails

Extended by:
ActiveSupport::Concern
Defined in:
lib/erector/rails.rb,
lib/erector/rails/form_builder.rb,
lib/erector/rails/template_handler.rb,
lib/erector/rails/form_builder_rails_3.rb

Defined Under Namespace

Classes: FormBuilder, TemplateHandler

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 both Rails and custom helpers via method_missing, and output return values that are html_safe



142
143
144
145
146
147
148
# File 'lib/erector/rails.rb', line 142

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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/erector/rails.rb', line 21

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, explicit_builder = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/erector/rails.rb', line 63

def def_rails_form_helper(method_name, explicit_builder = nil)
  module_eval <<-METHOD_DEF, __FILE__, __LINE__+1
    def #{method_name}(*args, &block)
      options = args.extract_options!
      args << options.merge(:builder => FormBuilder.wrapping(#{explicit_builder || '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.



55
56
57
58
59
60
61
# File 'lib/erector/rails.rb', line 55

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



34
35
36
37
38
39
40
41
# File 'lib/erector/rails.rb', line 34

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

.render(widget, view, local_assigns = {}, is_partial = false, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/erector/rails.rb', line 43

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)


16
17
18
19
# File 'lib/erector/rails.rb', line 16

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.



113
114
115
116
117
118
119
# File 'lib/erector/rails.rb', line 113

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

#content_for(*args, &block) ⇒ Object

Rails content_for is output if and only if no block given



131
132
133
134
135
136
137
138
# File 'lib/erector/rails.rb', line 131

def content_for(*args,&block)
  if block
    helpers.content_for(*args,&block)
  else
    rawtext(helpers.content_for(*args))
    ''
  end
end

#render(*args, &block) ⇒ Object

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



122
123
124
125
126
127
128
# File 'lib/erector/rails.rb', line 122

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)


152
153
154
# File 'lib/erector/rails.rb', line 152

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