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



147
148
149
150
151
152
153
# File 'lib/erector/rails.rb', line 147

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



68
69
70
71
72
73
74
75
76
# File 'lib/erector/rails.rb', line 68

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

.def_simple_rails_helper(method_name) ⇒ Object

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



60
61
62
63
64
65
66
# File 'lib/erector/rails.rb', line 60

def def_simple_rails_helper(method_name)
  module_eval "    def \#{method_name}(*args, &block)\n      text helpers.\#{method_name}(*args, &block)\n    end\n  METHOD_DEF\nend\n", __FILE__, __LINE__+1

.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 && value.nil?)
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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)

  if options[:pathname]
    widget.instance_variable_set(:@virtual_path, options[:pathname])
  end

  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.



118
119
120
121
122
123
124
# File 'lib/erector/rails.rb', line 118

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



136
137
138
139
140
141
142
143
# File 'lib/erector/rails.rb', line 136

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.



127
128
129
130
131
132
133
# File 'lib/erector/rails.rb', line 127

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)


157
158
159
# File 'lib/erector/rails.rb', line 157

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