Skip to content

Commit de01f82

Browse files
authored
Merge pull request #335 from moznion/active_model_naming
Fix `model_name` availability by explicitly extending ActiveModel::Naming
2 parents c2582a7 + 9c4af39 commit de01f82

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/active_hash/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Base
2424
class_attribute :_data, :dirty, :default_attributes, :scopes
2525

2626
if Object.const_defined?(:ActiveModel)
27+
extend ActiveModel::Naming
2728
extend ActiveModel::Translation
2829
include ActiveModel::Conversion
2930
else

spec/active_hash/base_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,17 @@ class Book < ActiveRecord::Base
17321732

17331733
end
17341734

1735+
describe "#model_name" do
1736+
# This test reproduces the issue reported in https://github.com/active-hash/active_hash/pull/335,
1737+
# regarding whether ActiveHash (and its derived models) expose `#model_name`.
1738+
# Some serializers (e.g., okuramasafumi/alba) use the `#model_name` method internally,
1739+
# so this test ensures that a NoMethodError is not raised.
1740+
it "exposes model_name correctly" do
1741+
instance = Country.new(:id => 1)
1742+
expect { instance.model_name }.not_to raise_error
1743+
end
1744+
end
1745+
17351746
describe ".transaction" do
17361747

17371748
it "execute the block given to it" do

0 commit comments

Comments
 (0)