module Erector::Rails

Public Class Methods

assigns_for(widget_class, view, local_assigns, is_partial) click to toggle source
# 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) click to toggle source
# File lib/erector/rails.rb, line 68
      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) click to toggle source

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

# File lib/erector/rails.rb, line 60
      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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

capture(&block) click to toggle source

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.

Calls superclass method
# 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) click to toggle source

Rails content_for is output if and only if no block given

# 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
method_missing(name, *args, &block) click to toggle source

Delegate to both Rails and custom helpers via method_missing, and output return values that are html_safe

Calls superclass method
# 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
render(*args, &block) click to toggle source

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

# 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) click to toggle source

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

Calls superclass method
# File lib/erector/rails.rb, line 157
def respond_to?(name)
  super || helpers.respond_to?(name)
end