From bdf78797fc24ef3afa9b86040a2c5878e631d279 Mon Sep 17 00:00:00 2001 From: Ian White Date: Sat, 7 Feb 2009 18:48:25 +0000 Subject: [PATCH] Extract InheritViews::PathsContainer extension, revert 17993be --- init.rb | 2 +- lib/inherit_views.rb | 78 ++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/init.rb b/init.rb index 8a55e3d..3eba5ed 100644 --- a/init.rb +++ b/init.rb @@ -1,4 +1,4 @@ require 'inherit_views' -ActionController::Base.send :extend, InheritViews::ActionController +ActionController::Base.send :extend, InheritViews::ClassMethods ActionView::Base.send :include, InheritViews::ActionView \ No newline at end of file diff --git a/lib/inherit_views.rb b/lib/inherit_views.rb index 1e39a27..29dc2ce 100644 --- a/lib/inherit_views.rb +++ b/lib/inherit_views.rb @@ -44,51 +44,55 @@ module InheritViews # extension for ActionController::Base which enables inherit_views, this module is extended into # ActionController::Base - module ActionController + module ClassMethods # Specify this to have your controller inherit its views from the specified path # or the current controller's default path if no argument is given def inherit_views(*paths) class_eval do - unless respond_to?(:inherit_views?) - extend ClassMethods - delegate :inherit_views?, :inherit_view_paths, :to => 'self.class' - end + extend PathsContainer unless respond_to?(:inherit_views_paths) self.inherit_views = true self.inherit_view_paths = paths if paths.any? end end - - module ClassMethods - # Return true if the controller is inheriting views - def inherit_views? - read_inheritable_attribute('inherit_views') ? true : false - end - - # Instruct the controller that it is, or is not, inheriting views - def inherit_views=(bool) - write_inheritable_attribute('inherit_views', bool) - end - - # Return the inherit view paths, in order of self to ancestor - # - # Takes inherit_view_paths from the superclass when first read, and prepends the current controller_path - def inherit_view_paths - instance_variable_get('@inherit_view_paths') || instance_variable_set('@inherit_view_paths', [controller_path] + (superclass.inherit_view_paths rescue [])) - end + end - # Set the inherit view paths, in order of self to ancestor. - # - # The controller_path for self is always prepended to the front, no matter what the arguments. - def inherit_view_paths=(new_paths) - new_paths -= [controller_path] - old_paths = inherit_view_paths - [controller_path] - new_paths - instance_variable_set('@inherit_view_paths', [controller_path] + new_paths + old_paths) + # class extension that enables inherit_view_paths to be calculated/set + # + # requires a class method called 'controller_path' + module PathsContainer + def self.extended(base) + base.class_eval do + delegate :inherit_views?, :inherit_view_paths, :to => 'self.class' end end - end + + # Return true if the class is inheriting views + def inherit_views? + read_inheritable_attribute('inherit_views') ? true : false + end + + # Instruct the class that it is, or is not, inheriting views + def inherit_views=(bool) + write_inheritable_attribute('inherit_views', bool) + end + + # Return the inherit view paths, in order of self to ancestor + # Takes inherit_view_paths from the superclass when first read, and prepends the current controller_path + def inherit_view_paths + instance_variable_get('@inherit_view_paths') || instance_variable_set('@inherit_view_paths', [controller_path] + (superclass.inherit_view_paths rescue [])) + end - # Mixin for ActionView to enable inherit views functionality. This module is - # included into ActionView::Base + # Set the inherit view paths, in order of self to ancestor. + # + # The controller_path for self is always prepended to the front, no matter what the arguments. + def inherit_view_paths=(new_paths) + new_paths -= [controller_path] + old_paths = inherit_view_paths - [controller_path] - new_paths + instance_variable_set('@inherit_view_paths', [controller_path] + new_paths + old_paths) + end + end + + # Mixin for ActionView::Base to enable inherit views functionality module ActionView def self.included(base) base.class_eval do @@ -105,18 +109,14 @@ def view_paths=(value) # which will be used to look for a matching template if the original template is missing class PathSet < ::ActionView::PathSet extend ActiveSupport::Memoizable - attr_reader :inherit_view_paths - - def inherit_view_paths=(paths) - @inherit_view_paths = Array(paths) - end + attr_accessor :inherit_view_paths alias_method :orig_find_template, :find_template def find_template(original_template_path, format = nil) super rescue ::ActionView::MissingTemplate => e - find_template_from_inherit_view_paths(original_template_path, format) || raise(e) + (inherit_view_paths && find_template_from_inherit_view_paths(original_template_path, format)) || raise(e) end protected