diff options
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view.rb | 1 | ||||
-rw-r--r-- | actionview/lib/action_view/view_paths.rb | 96 | ||||
-rw-r--r-- | actionview/test/fixtures/override/test/hello_world.erb | 1 | ||||
-rw-r--r-- | actionview/test/fixtures/override2/layouts/test/sub.erb | 1 | ||||
-rw-r--r-- | actionview/test/lib/controller/view_paths_test.rb | 174 |
5 files changed, 273 insertions, 0 deletions
diff --git a/actionview/lib/action_view.rb b/actionview/lib/action_view.rb index 8def4ba7c5..7adf19cc7a 100644 --- a/actionview/lib/action_view.rb +++ b/actionview/lib/action_view.rb @@ -38,6 +38,7 @@ module ActionView autoload :RecordIdentifier autoload :RoutingUrlFor autoload :Template + autoload :ViewPaths autoload_under "renderer" do autoload :Renderer diff --git a/actionview/lib/action_view/view_paths.rb b/actionview/lib/action_view/view_paths.rb new file mode 100644 index 0000000000..6c349feb1d --- /dev/null +++ b/actionview/lib/action_view/view_paths.rb @@ -0,0 +1,96 @@ +require 'action_view/base' + +module ActionView + module ViewPaths + extend ActiveSupport::Concern + + included do + class_attribute :_view_paths + self._view_paths = ActionView::PathSet.new + self._view_paths.freeze + end + + delegate :template_exists?, :view_paths, :formats, :formats=, + :locale, :locale=, :to => :lookup_context + + module ClassMethods + def parent_prefixes + @parent_prefixes ||= begin + parent_controller = superclass + prefixes = [] + + until parent_controller.abstract? + prefixes << parent_controller.controller_path + parent_controller = parent_controller.superclass + end + + prefixes + end + end + end + + # The prefixes used in render "foo" shortcuts. + def _prefixes + @_prefixes ||= begin + parent_prefixes = self.class.parent_prefixes + parent_prefixes.dup.unshift(controller_path) + end + end + + # LookupContext is the object responsible to hold all information required to lookup + # templates, i.e. view paths and details. Check ActionView::LookupContext for more + # information. + def lookup_context + @_lookup_context ||= + ActionView::LookupContext.new(self.class._view_paths, details_for_lookup, _prefixes) + end + + def details_for_lookup + { } + end + + def append_view_path(path) + lookup_context.view_paths.push(*path) + end + + def prepend_view_path(path) + lookup_context.view_paths.unshift(*path) + end + + module ClassMethods + # Append a path to the list of view paths for this controller. + # + # ==== Parameters + # * <tt>path</tt> - If a String is provided, it gets converted into + # the default view path. You may also provide a custom view path + # (see ActionView::PathSet for more information) + def append_view_path(path) + self._view_paths = view_paths + Array(path) + end + + # Prepend a path to the list of view paths for this controller. + # + # ==== Parameters + # * <tt>path</tt> - If a String is provided, it gets converted into + # the default view path. You may also provide a custom view path + # (see ActionView::PathSet for more information) + def prepend_view_path(path) + self._view_paths = ActionView::PathSet.new(Array(path) + view_paths) + end + + # A list of all of the default view paths for this controller. + def view_paths + _view_paths + end + + # Set the view paths. + # + # ==== Parameters + # * <tt>paths</tt> - If a PathSet is provided, use that; + # otherwise, process the parameter into a PathSet. + def view_paths=(paths) + self._view_paths = ActionView::PathSet.new(Array(paths)) + end + end + end +end diff --git a/actionview/test/fixtures/override/test/hello_world.erb b/actionview/test/fixtures/override/test/hello_world.erb new file mode 100644 index 0000000000..3e308d3d86 --- /dev/null +++ b/actionview/test/fixtures/override/test/hello_world.erb @@ -0,0 +1 @@ +Hello overridden world!
\ No newline at end of file diff --git a/actionview/test/fixtures/override2/layouts/test/sub.erb b/actionview/test/fixtures/override2/layouts/test/sub.erb new file mode 100644 index 0000000000..3863d5a8ef --- /dev/null +++ b/actionview/test/fixtures/override2/layouts/test/sub.erb @@ -0,0 +1 @@ +layout: <%= yield %>
\ No newline at end of file diff --git a/actionview/test/lib/controller/view_paths_test.rb b/actionview/test/lib/controller/view_paths_test.rb new file mode 100644 index 0000000000..c6e7a523b9 --- /dev/null +++ b/actionview/test/lib/controller/view_paths_test.rb @@ -0,0 +1,174 @@ +require 'abstract_unit' + +class ViewLoadPathsTest < ActionController::TestCase + class TestController < ActionController::Base + def self.controller_path() "test" end + + before_action :add_view_path, only: :hello_world_at_request_time + + def hello_world() end + def hello_world_at_request_time() render(:action => 'hello_world') end + + private + def add_view_path + prepend_view_path "#{FIXTURE_LOAD_PATH}/override" + end + end + + module Test + class SubController < ActionController::Base + layout 'test/sub' + def hello_world; render(:template => 'test/hello_world'); end + end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @controller = TestController.new + @paths = TestController.view_paths + end + + def teardown + TestController.view_paths = @paths + end + + def expand(array) + array.map {|x| File.expand_path(x.to_s)} + end + + def assert_paths(*paths) + controller = paths.first.is_a?(Class) ? paths.shift : @controller + assert_equal expand(paths), controller.view_paths.map { |p| p.to_s } + end + + def test_template_load_path_was_set_correctly + assert_paths FIXTURE_LOAD_PATH + end + + def test_controller_appends_view_path_correctly + @controller.append_view_path 'foo' + assert_paths(FIXTURE_LOAD_PATH, "foo") + + @controller.append_view_path(%w(bar baz)) + assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz") + + @controller.append_view_path(FIXTURE_LOAD_PATH) + assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH) + end + + def test_controller_prepends_view_path_correctly + @controller.prepend_view_path 'baz' + assert_paths("baz", FIXTURE_LOAD_PATH) + + @controller.prepend_view_path(%w(foo bar)) + assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH + + @controller.prepend_view_path(FIXTURE_LOAD_PATH) + assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH + end + + def test_template_appends_view_path_correctly + @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) + class_view_paths = TestController.view_paths + + @controller.append_view_path 'foo' + assert_paths FIXTURE_LOAD_PATH, "foo" + + @controller.append_view_path(%w(bar baz)) + assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz" + assert_paths TestController, *class_view_paths + end + + def test_template_prepends_view_path_correctly + @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) + class_view_paths = TestController.view_paths + + @controller.prepend_view_path 'baz' + assert_paths "baz", FIXTURE_LOAD_PATH + + @controller.prepend_view_path(%w(foo bar)) + assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH + assert_paths TestController, *class_view_paths + end + + def test_view_paths + get :hello_world + assert_response :success + assert_equal "Hello world!", @response.body + end + + def test_view_paths_override + TestController.prepend_view_path "#{FIXTURE_LOAD_PATH}/override" + get :hello_world + assert_response :success + assert_equal "Hello overridden world!", @response.body + end + + def test_view_paths_override_for_layouts_in_controllers_with_a_module + @controller = Test::SubController.new + Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ] + get :hello_world + assert_response :success + assert_equal "layout: Hello overridden world!", @response.body + end + + def test_view_paths_override_at_request_time + get :hello_world_at_request_time + assert_response :success + assert_equal "Hello overridden world!", @response.body + end + + def test_decorate_view_paths_with_custom_resolver + decorator_class = Class.new(ActionView::PathResolver) do + def initialize(path_set) + @path_set = path_set + end + + def find_all(*args) + @path_set.find_all(*args).collect do |template| + ::ActionView::Template.new( + "Decorated body", + template.identifier, + template.handler, + { + :virtual_path => template.virtual_path, + :format => template.formats + } + ) + end + end + end + + decorator = decorator_class.new(TestController.view_paths) + TestController.view_paths = ActionView::PathSet.new.push(decorator) + + get :hello_world + assert_response :success + assert_equal "Decorated body", @response.body + end + + def test_inheritance + original_load_paths = ActionController::Base.view_paths + + self.class.class_eval %{ + class A < ActionController::Base; end + class B < A; end + class C < ActionController::Base; end + } + + A.view_paths = ['a/path'] + + assert_paths A, "a/path" + assert_paths A, *B.view_paths + assert_paths C, *original_load_paths + + C.view_paths = [] + assert_nothing_raised { C.append_view_path 'c/path' } + assert_paths C, "c/path" + end + + def test_lookup_context_accessor + assert_equal ["test"], TestController.new.lookup_context.prefixes + end +end |