Skip to content

Commit

Permalink
Fix sorting of helpers from different paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drogus committed May 27, 2012
1 parent 1f2ee5d commit 26ddf2f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/metal/helpers.rb
Expand Up @@ -96,9 +96,9 @@ def all_helpers_from_path(path)
helpers = []
Array.wrap(path).each do |_path|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/
helpers += Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
names = Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
helpers += names.sort
end
helpers.sort!
helpers.uniq!
helpers
end
Expand Down
30 changes: 30 additions & 0 deletions actionpack/test/controller/helper_test.rb
Expand Up @@ -50,12 +50,42 @@ def lib
class MeTooController < JustMeController
end

class HelpersPathsController < ActionController::Base
paths = ["helpers1_pack", "helpers2_pack"].map do |path|
File.join(File.expand_path('../../fixtures', __FILE__), path)
end
$:.unshift(*paths)

self.helpers_path = paths
helper :all

def index
render :inline => "<%= conflicting_helper %>"
end
end

module LocalAbcHelper
def a() end
def b() end
def c() end
end

class HelperPathsTest < ActiveSupport::TestCase
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_helpers_paths_priority
request = ActionController::TestRequest.new
responses = HelpersPathsController.action(:index).call(request.env)

# helpers2_pack was given as a second path, so pack2_helper should be
# included as the second one
assert_equal "pack2", responses.last.body
end
end

class HelperTest < ActiveSupport::TestCase
class TestController < ActionController::Base
attr_accessor :delegate_attr
Expand Down
5 changes: 5 additions & 0 deletions actionpack/test/fixtures/helpers1_pack/pack1_helper.rb
@@ -0,0 +1,5 @@
module Pack1Helper
def conflicting_helper
"pack1"
end
end
5 changes: 5 additions & 0 deletions actionpack/test/fixtures/helpers2_pack/pack2_helper.rb
@@ -0,0 +1,5 @@
module Pack2Helper
def conflicting_helper
"pack2"
end
end

0 comments on commit 26ddf2f

Please sign in to comment.