Skip to content

Commit

Permalink
Make sure respond_to? and method_missing works in abstract Railties
Browse files Browse the repository at this point in the history
Those methods were raising an confusing exception when trying to call
`Rails::Railtie.respond_to?(:something)`. Now they just work.

Fixes #44761
  • Loading branch information
rafaelfranca committed Mar 25, 2022
1 parent eeb2cfb commit af0733a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion railties/lib/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ def generate_railtie_name(string)
end

def respond_to_missing?(name, _)
return super if abstract_railtie?

instance.respond_to?(name) || super
end

# If the class method does not have a method, then send the method call
# to the Railtie instance.
def method_missing(name, *args, &block)
if instance.respond_to?(name)
if !abstract_railtie? && instance.respond_to?(name)
instance.public_send(name, *args, &block)
else
super
Expand Down
10 changes: 9 additions & 1 deletion railties/test/railties/railtie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ def app
end

test "cannot instantiate a Railtie object" do
assert_raise(RuntimeError) { Rails::Railtie.new }
assert_raise(RuntimeError) { Rails::Railtie.send(:new) }
end

test "respond_to? works in the abstract railties" do
assert_not_respond_to Rails::Railtie, :something_nice
end

test "method_missing works in the abstract railties" do
assert_raise(NoMethodError) { Rails::Railtie.something_nice }
end

test "Railtie provides railtie_name" do
Expand Down

0 comments on commit af0733a

Please sign in to comment.