diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index ab43762..4bffad2 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -24,6 +24,7 @@ class Base class_attribute :_data, :dirty, :default_attributes, :scopes if Object.const_defined?(:ActiveModel) + extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Conversion else diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 4ff3099..c268815 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -1732,6 +1732,17 @@ class Book < ActiveRecord::Base end + describe "#model_name" do + # This test reproduces the issue reported in https://github.com/active-hash/active_hash/pull/335, + # regarding whether ActiveHash (and its derived models) expose `#model_name`. + # Some serializers (e.g., okuramasafumi/alba) use the `#model_name` method internally, + # so this test ensures that a NoMethodError is not raised. + it "exposes model_name correctly" do + instance = Country.new(:id => 1) + expect { instance.model_name }.not_to raise_error + end + end + describe ".transaction" do it "execute the block given to it" do