Module: Erector::Rails::WidgetExtensions::ClassMethods

Defined in:
lib/erector/rails2/extensions/rails_widget.rb

Instance Method Summary collapse

Instance Method Details

#controller_assigns_propagate_to_partialsObject


82
83
84
85
86
87
# 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
end

#controller_assigns_propagate_to_partials=(new_value) ⇒ Object

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.


102
103
104
# 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)
end

#ignore_extra_controller_assignsObject


58
59
60
61
62
63
# 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
end

#ignore_extra_controller_assigns=(new_value) ⇒ Object

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.


78
79
80
# 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)
end