Skip to content

Commit 26ddf2f

Browse files
committed
Fix sorting of helpers from different paths
When more than one directory for helpers is provided to a controller, it should preserver the order of directories. Given 2 paths: MyController.helpers_paths = ["dir1/helpers", "dir2/helpers"] helpers from dir1 should be loaded first. Before this commit, all helpers were mixed and then sorted alphabetically, which essentially would require to rename helpers to get desired order. This is a problem especially for engines, where you would like to be able to predict accurately which engine helpers will load first.
1 parent 1f2ee5d commit 26ddf2f

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

actionpack/lib/action_controller/metal/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def all_helpers_from_path(path)
9696
helpers = []
9797
Array.wrap(path).each do |_path|
9898
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/
99-
helpers += Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
99+
names = Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
100+
helpers += names.sort
100101
end
101-
helpers.sort!
102102
helpers.uniq!
103103
helpers
104104
end

actionpack/test/controller/helper_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,42 @@ def lib
5050
class MeTooController < JustMeController
5151
end
5252

53+
class HelpersPathsController < ActionController::Base
54+
paths = ["helpers1_pack", "helpers2_pack"].map do |path|
55+
File.join(File.expand_path('../../fixtures', __FILE__), path)
56+
end
57+
$:.unshift(*paths)
58+
59+
self.helpers_path = paths
60+
helper :all
61+
62+
def index
63+
render :inline => "<%= conflicting_helper %>"
64+
end
65+
end
66+
5367
module LocalAbcHelper
5468
def a() end
5569
def b() end
5670
def c() end
5771
end
5872

73+
class HelperPathsTest < ActiveSupport::TestCase
74+
def setup
75+
@request = ActionController::TestRequest.new
76+
@response = ActionController::TestResponse.new
77+
end
78+
79+
def test_helpers_paths_priority
80+
request = ActionController::TestRequest.new
81+
responses = HelpersPathsController.action(:index).call(request.env)
82+
83+
# helpers2_pack was given as a second path, so pack2_helper should be
84+
# included as the second one
85+
assert_equal "pack2", responses.last.body
86+
end
87+
end
88+
5989
class HelperTest < ActiveSupport::TestCase
6090
class TestController < ActionController::Base
6191
attr_accessor :delegate_attr
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Pack1Helper
2+
def conflicting_helper
3+
"pack1"
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Pack2Helper
2+
def conflicting_helper
3+
"pack2"
4+
end
5+
end

0 commit comments

Comments
 (0)