module Erector::Rails::WidgetExtensions::ClassMethods

Public Instance Methods

controller_assigns_propagate_to_partials() click to toggle source
# File lib/erector/rails2/extensions/rails_widget.rb, line 82
def controller_assigns_propagate_to_partials
  out = @controller_assigns_propagate_to_partials
  out ||= (superclass.controller_assigns_propagate_to_partials ? :true : :false) if superclass.respond_to?(:controller_assigns_propagate_to_partials)
  out ||= :true
  out == :true
controller_assigns_propagate_to_partials=(new_value) click to toggle source

In ERb templates, controller instance variables are available to any partial that gets rendered by the view, no matter how deeply-nested. This effectively makes controller instance variables “globals”. In small view hierarchies this probably isn’t an issue, but in large ones it can make debugging and reasoning about the code very difficult.

If you set this to true (and it’s inherited through to subclasses), then any widget that’s getting rendered as a partial will only have access to locals explicitly passed to it (render :partial => …, :locals => …). (This doesn’t change the behavior of widgets that are explicitly rendered, as they don’t have this issue.) This can allow for cleaner encapsulation of partials, as they must be passed everything they use and can’t rely on controller instance variables.

# File lib/erector/rails2/extensions/rails_widget.rb, line 102
def controller_assigns_propagate_to_partials=(new_value)
  @controller_assigns_propagate_to_partials = (new_value ? :true : :false)
ignore_extra_controller_assigns() click to toggle source
# File lib/erector/rails2/extensions/rails_widget.rb, line 58
def ignore_extra_controller_assigns
  out = @ignore_extra_controller_assigns
  out ||= (superclass.ignore_extra_controller_assigns ? :true : :false) if superclass.respond_to?(:ignore_extra_controller_assigns)
  out ||= :true
  out == :true
ignore_extra_controller_assigns=(new_value) click to toggle source

Often, large Rails applications will assign many controller instance variables. Sometimes these aren’t used by a view: ApplicationController might assign variables that are used by many, but not all, views; and various other things may accumulate, especially if you’ve been using templating systems that are more forgiving than Erector. If you then migrate to Erector, you’re stuck using no “needs” declaration at all, because it needs to contain every assigned variable, or Erector will raise an exception.

If you set this to true (and it’s inherited through to subclasses), however, then “needs” declarations on the widget will cause excess controller variables to be ignored – they’ll be unavailable to the widget (so ‘needs’ still means something), but they won’t cause widget instantiation to fail, either. This can let a large Rails project transition to Erector more smoothly.

# File lib/erector/rails2/extensions/rails_widget.rb, line 78
def ignore_extra_controller_assigns=(new_value)
  @ignore_extra_controller_assigns = (new_value ? :true : :false)