# File lib/erector/rails2/extensions/rails_widget.rb, line 3 def self.assigns_for(widget_class, view, local_assigns, is_partial) assigns = {} instance_variables = view.instance_variables_for_widget_assignment if is_partial || widget_class.ignore_extra_controller_assigns instance_variables = remove_unneeded_assigns(widget_class, instance_variables) end assigns.merge!(instance_variables) unless is_partial && (! widget_class.controller_assigns_propagate_to_partials) if is_partial assigns.merge!(filter_local_assigns_for_partial(widget_class, local_assigns || { })) end assigns end
# File lib/erector/rails3.rb, line 56 def def_rails_form_helper(method_name) module_eval " def #{method_name}(*args, &block) options = args.extract_options! args << options.merge(:builder => FormBuilder.wrapping(options[:builder])) text helpers.#{method_name}(*args, &block) end ", __FILE__, __LINE__+1 end
Wrappers for rails helpers that produce markup. Erector needs to manually emit their result.
# File lib/erector/rails3.rb, line 48 def def_simple_rails_helper(method_name) module_eval " def #{method_name}(*args, &block) text helpers.#{method_name}(*args, &block) end ", __FILE__, __LINE__+1 end
# File lib/erector/rails2/extensions/rails_widget.rb, line 29 def self.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
# 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
# File lib/erector/rails2/extensions/rails_widget.rb, line 38 def self.render(widget, view, assigns = nil, options = {}) if widget.is_a?(Class) assigns ||= assigns_for(widget, view, nil, false) widget = widget.new(assigns) end view.send(:_evaluate_assigns_and_ivars) output_buffer = view.with_output_buffer do # Set parent to the view and use Rails's output buffer. new_output = Output.new :buffer => lambda { view.output_buffer } widget.to_html(options.merge(:parent => view, :output => new_output)) end output_buffer end
# 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
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.
# 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
Rails #content_for is output if and only if no block given
# File lib/erector/rails3.rb, line 124 def content_for(*args,&block) if block helpers.content_for(*args,&block) else rawtext(helpers.content_for(*args)) '' end end
Delegate to non-markup producing helpers via #method_missing, returning their result directly.
# File lib/erector/rails3.rb, line 135 def method_missing(name, *args, &block) if helpers.respond_to?(name) helpers.send(name, *args, &block) else super end end
Wrap Rails' render method, to capture output from partials etc.
# 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
Since we delegate #method_missing to helpers, we need to delegate respond_to? as well.
# File lib/erector/rails3.rb, line 145 def respond_to?(name) super || helpers.respond_to?(name) end