Skip to content

Commit fd86a1b

Browse files
committed
Rely on a public contract between railties instead of accessing railtie methods directly.
1 parent 3ee0116 commit fd86a1b

File tree

12 files changed

+94
-54
lines changed

12 files changed

+94
-54
lines changed

actionpack/lib/abstract_controller/railties/routes_helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ def self.with(routes)
55
Module.new do
66
define_method(:inherited) do |klass|
77
super(klass)
8-
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
9-
klass.send(:include, namespace._railtie.routes.url_helpers)
8+
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_routes_url_helpers) }
9+
klass.send(:include, namespace.railtie_routes_url_helpers)
1010
else
1111
klass.send(:include, routes.url_helpers)
1212
end

actionpack/lib/action_controller/railties/paths.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ def self.with(app)
66
define_method(:inherited) do |klass|
77
super(klass)
88

9-
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
10-
paths = namespace._railtie.paths["app/helpers"].existent
9+
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_helpers_paths) }
10+
paths = namespace.railtie_helpers_paths
1111
else
12-
paths = app.config.helpers_paths
12+
paths = app.helpers_paths
1313
end
1414

1515
klass.helpers_path = paths
16+
1617
if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers
1718
klass.helper :all
1819
end

actionpack/test/activerecord/polymorphic_routes_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ class Blog < ActiveRecord::Base
3434
set_table_name 'projects'
3535
end
3636

37-
def self._railtie
38-
o = Object.new
39-
def o.railtie_name; "blog" end
40-
o
37+
def self.use_relative_model_naming?
38+
true
4139
end
4240
end
4341

actionpack/test/lib/controller/fake_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def name
175175
end
176176

177177
module Blog
178-
def self._railtie
179-
self
178+
def self.use_relative_model_naming?
179+
true
180180
end
181181

182182
class Post < Struct.new(:title, :id)

actionpack/test/template/form_helper_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def test_form_for_with_format
740740
assert_dom_equal expected, output_buffer
741741
end
742742

743-
def test_form_for_with_isolated_namespaced_model
743+
def test_form_for_with_model_using_relative_model_naming
744744
form_for(@blog_post) do |f|
745745
concat f.text_field :title
746746
concat f.submit('Edit post')

activemodel/lib/active_model/naming.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ class Name < String
1313
def initialize(klass, namespace = nil, name = nil)
1414
name ||= klass.name
1515
super(name)
16-
@unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
1716

18-
@klass = klass
19-
@singular = _singularize(self).freeze
20-
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
21-
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
22-
@human = ActiveSupport::Inflector.humanize(@element).freeze
23-
@collection = ActiveSupport::Inflector.tableize(self).freeze
17+
@unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
18+
@klass = klass
19+
@singular = _singularize(self).freeze
20+
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
21+
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
22+
@human = ActiveSupport::Inflector.humanize(@element).freeze
23+
@collection = ActiveSupport::Inflector.tableize(self).freeze
2424
@partial_path = "#{@collection}/#{@element}".freeze
25-
@param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
26-
@route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
27-
@i18n_key = self.underscore.to_sym
25+
@param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
26+
@route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
27+
@i18n_key = self.underscore.to_sym
2828
end
2929

3030
# Transform the model name into a more humane format, using I18n. By default,
@@ -79,7 +79,9 @@ module Naming
7979
# used to retrieve all kinds of naming-related information.
8080
def model_name
8181
@_model_name ||= begin
82-
namespace = self.parents.detect { |n| n.respond_to?(:_railtie) }
82+
namespace = self.parents.detect do |n|
83+
n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
84+
end
8385
ActiveModel::Name.new(self, namespace)
8486
end
8587
end

activemodel/test/cases/naming_test.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ def test_route_key
7474
def test_param_key
7575
assert_equal 'post', @model_name.param_key
7676
end
77-
78-
def test_recognizing_namespace
79-
assert_equal 'Post', Blog::Post.model_name.instance_variable_get("@unnamespaced")
80-
end
8177
end
8278

8379
class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
@@ -160,6 +156,40 @@ def test_param_key
160156
end
161157
end
162158

159+
class NamingUsingRelativeModelNameTest < ActiveModel::TestCase
160+
def setup
161+
@model_name = Blog::Post.model_name
162+
end
163+
164+
def test_singular
165+
assert_equal 'blog_post', @model_name.singular
166+
end
167+
168+
def test_plural
169+
assert_equal 'blog_posts', @model_name.plural
170+
end
171+
172+
def test_element
173+
assert_equal 'post', @model_name.element
174+
end
175+
176+
def test_collection
177+
assert_equal 'blog/posts', @model_name.collection
178+
end
179+
180+
def test_human
181+
assert_equal 'Post', @model_name.human
182+
end
183+
184+
def test_route_key
185+
assert_equal 'posts', @model_name.route_key
186+
end
187+
188+
def test_param_key
189+
assert_equal 'post', @model_name.param_key
190+
end
191+
end
192+
163193
class NamingHelpersTest < Test::Unit::TestCase
164194
def setup
165195
@klass = Contact

activemodel/test/models/blog_post.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
module Blog
2-
def self._railtie
3-
Object.new
4-
end
5-
6-
def self.table_name_prefix
7-
"blog_"
2+
def self.use_relative_model_naming?
3+
true
84
end
95

106
class Post

railties/lib/rails/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ def to_app
141141
self
142142
end
143143

144+
def helpers_paths
145+
config.helpers_paths
146+
end
147+
144148
protected
145149

146150
alias :build_middleware_stack :app

railties/lib/rails/engine.rb

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -371,20 +371,28 @@ def isolate_namespace(mod)
371371
self.routes.default_scope = { :module => ActiveSupport::Inflector.underscore(mod.name) }
372372
self.isolated = true
373373

374-
unless mod.respond_to?(:_railtie)
375-
name = engine_name
376-
_railtie = self
374+
unless mod.respond_to?(:railtie_namespace)
375+
name, railtie = engine_name, self
376+
377377
mod.singleton_class.instance_eval do
378-
define_method(:_railtie) do
379-
_railtie
380-
end
378+
define_method(:railtie_namespace) { railtie }
381379

382380
unless mod.respond_to?(:table_name_prefix)
383-
define_method(:table_name_prefix) do
384-
"#{name}_"
385-
end
381+
define_method(:table_name_prefix) { "#{name}_" }
382+
end
383+
384+
unless mod.respond_to?(:use_relative_model_naming?)
385+
class_eval "def use_relative_model_naming?; true; end", __FILE__, __LINE__
386+
end
387+
388+
unless mod.respond_to?(:railtie_helpers_paths)
389+
define_method(:railtie_helpers_paths) { railtie.helpers_paths }
390+
end
391+
392+
unless mod.respond_to?(:railtie_routes_url_helpers)
393+
define_method(:railtie_routes_url_helpers) { railtie.routes_url_helpers }
386394
end
387-
end
395+
end
388396
end
389397
end
390398

@@ -429,13 +437,6 @@ def railties
429437
def helpers
430438
@helpers ||= begin
431439
helpers = Module.new
432-
433-
helpers_paths = if config.respond_to?(:helpers_paths)
434-
config.helpers_paths
435-
else
436-
paths["app/helpers"].existent
437-
end
438-
439440
all = ActionController::Base.all_helpers_from_path(helpers_paths)
440441
ActionController::Base.modules_for_helpers(all).each do |mod|
441442
helpers.send(:include, mod)
@@ -444,6 +445,14 @@ def helpers
444445
end
445446
end
446447

448+
def helpers_paths
449+
paths["app/helpers"].existent
450+
end
451+
452+
def routes_url_helpers
453+
routes.url_helpers
454+
end
455+
447456
def app
448457
@app ||= begin
449458
config.middleware = config.middleware.merge_into(default_middleware_stack)

railties/lib/rails/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def load_generators(app=self)
196196
end
197197

198198
def railtie_namespace
199-
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:_railtie) }
199+
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
200200
end
201201
end
202202
end

railties/test/railties/engine_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class MyMailer < ActionMailer::Base
323323

324324
assert_equal "bukkits_", Bukkits.table_name_prefix
325325
assert_equal "bukkits", Bukkits::Engine.engine_name
326-
assert_equal Bukkits._railtie, Bukkits::Engine
326+
assert_equal Bukkits.railtie_namespace, Bukkits::Engine
327327
assert ::Bukkits::MyMailer.method_defined?(:foo_path)
328328
assert !::Bukkits::MyMailer.method_defined?(:bar_path)
329329

@@ -467,7 +467,7 @@ def index
467467
assert_nil Rails.application.load_seed
468468
end
469469

470-
test "using namespace more than once on one module should not overwrite _railtie method" do
470+
test "using namespace more than once on one module should not overwrite railtie_namespace method" do
471471
@plugin.write "lib/bukkits.rb", <<-RUBY
472472
module AppTemplate
473473
class Engine < ::Rails::Engine
@@ -484,7 +484,7 @@ class Engine < ::Rails::Engine
484484

485485
boot_rails
486486

487-
assert_equal AppTemplate._railtie, AppTemplate::Engine
487+
assert_equal AppTemplate.railtie_namespace, AppTemplate::Engine
488488
end
489489

490490
test "properly reload routes" do

0 commit comments

Comments
 (0)