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
Defined Under Namespace
Classes: FormBuilder, TemplateHandler
Class Method Summary collapse
- .assigns_for(widget_class, view, local_assigns, is_partial) ⇒ Object
- .def_rails_form_helper(method_name, explicit_builder = nil) ⇒ Object
-
.def_simple_rails_helper(method_name) ⇒ Object
Wrappers for rails helpers that produce markup.
- .filter_local_assigns_for_partial(widget_class, local_assigns) ⇒ Object
- .render(widget, view, local_assigns = {}, is_partial = false, options = {}) ⇒ Object
- .should_assign?(name, widget_class, is_partial) ⇒ Boolean
Instance Method Summary collapse
-
#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.
-
#content_for(*args, &block) ⇒ Object
Rails content_for is output if and only if no block given.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate to both Rails and custom helpers via method_missing, and output return values that are html_safe.
-
#render(*args, &block) ⇒ Object
Wrap Rails’ render method, to capture output from partials etc.
-
#respond_to?(name) ⇒ Boolean
Since we delegate method_missing to helpers, we need to delegate respond_to? as well.
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
136 137 138 139 140 141 142 |
# File 'lib/erector/rails.rb', line 136 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
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/erector/rails.rb', line 15 def assigns_for(, view, local_assigns, is_partial) assigns = {} view.assigns.each do |name, value| name = name.to_sym assigns[name] = value if should_assign?(name, , is_partial) end assigns.merge!(filter_local_assigns_for_partial(, local_assigns)) if is_partial assigns end |
.def_rails_form_helper(method_name, explicit_builder = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/erector/rails.rb', line 57 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.
49 50 51 52 53 54 55 |
# File 'lib/erector/rails.rb', line 49 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
28 29 30 31 32 33 34 35 |
# File 'lib/erector/rails.rb', line 28 def filter_local_assigns_for_partial(, local_assigns) = .name.underscore = $1 if =~ %r{.*/(.*?)$} local_assigns.reject do |name, value| name == :object || name == .to_sym end end |
.render(widget, view, local_assigns = {}, is_partial = false, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/erector/rails.rb', line 37 def render(, view, local_assigns = {}, is_partial = false, = {}) = .new(assigns_for(, view, local_assigns, is_partial)) if .is_a?(Class) view.with_output_buffer do # Set parent and helpers to the view and use Rails's output buffer. .to_html(.merge(:helpers => view, :parent => view, :output => Output.new(:buffer => lambda { view.output_buffer }))) end end |
.should_assign?(name, widget_class, is_partial) ⇒ Boolean
10 11 12 13 |
# File 'lib/erector/rails.rb', line 10 def should_assign?(name, , is_partial) (!.ignore_extra_controller_assigns || .needs?(name)) && (!is_partial || .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.
107 108 109 110 111 112 113 |
# File 'lib/erector/rails.rb', line 107 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
125 126 127 128 129 130 131 132 |
# File 'lib/erector/rails.rb', line 125 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.
116 117 118 119 120 121 122 |
# File 'lib/erector/rails.rb', line 116 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.
146 147 148 |
# File 'lib/erector/rails.rb', line 146 def respond_to?(name) super || helpers.respond_to?(name) end |