diff --git a/Gemfile.lock b/Gemfile.lock index ac2c4ed..307644f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - human_attribute_values (1.2.1) + human_attribute_values (1.2.2) rails (>= 4.2.10) GEM @@ -135,7 +135,7 @@ GEM websocket-driver (0.7.3) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - zeitwerk (2.4.0) + zeitwerk (2.4.1) PLATFORMS ruby diff --git a/README.md b/README.md index ccf61b3..b250d42 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,36 @@ gem install human_attribute_values * Ruby: MRI >= 2.3.0 ## Usage -The gem defines ``human_attribute_value`` as instance and class method on ``ActiveRecord::Base`` and ``ActiveModel::Model``. -To translate a value it uses the I18n API. The translations are looked up from the current locale file under the key ``'activerecord.values.model_name.attribute_name.value'`` respectively ``'activemodel.values.model_name.attribute_name.value'`` by default. + +The module `HumanAttributeValues` defines `human_attribute_value` as both an instance and class method. + +### ActiveRecord + +You can include the module in any specific models you wish to translate directly, or include it in `ApplicationRecord` globally. + +```ruby +class ApplicationRecord < ActiveRecord::Base + include HumanAttributeValues + self.abstract_class = true +end +``` + +To translate a value it uses the I18n API. The translations are looked up from the current locale file under the key `'activerecord.values.{model_name}.attribute_name.value'` by default. + +### ActiveModel + +You can include the module in any specific models you wish to translate directly, or include it in `ApplicationRecord` globally. + +```ruby +class Model + include ActiveModel::Model + include HumanAttributeValues + + attr_accessor ... +end +``` + +To translate a value it uses the I18n API. The translations are looked up from the current locale file under the key `'activemodel.values.{model_name}.attribute_name.value'` by default. ### Locale: ```yml diff --git a/lib/human_attribute_values/human_attribute_value.rb b/lib/human_attribute_values/human_attribute_value.rb index 4bb3b59..f8e2fc0 100644 --- a/lib/human_attribute_values/human_attribute_value.rb +++ b/lib/human_attribute_values/human_attribute_value.rb @@ -41,6 +41,3 @@ def human_attribute_value(attribute, value, options = {}) end end end - -ActiveRecord::Base.include HumanAttributeValues -ActiveModel::Model.include HumanAttributeValues diff --git a/test/dummy/app/models/active_model_model.rb b/test/dummy/app/models/active_model_model.rb index 583311d..a67453a 100644 --- a/test/dummy/app/models/active_model_model.rb +++ b/test/dummy/app/models/active_model_model.rb @@ -2,6 +2,7 @@ class ActiveModelModel include ActiveModel::Model + include HumanAttributeValues attr_accessor :boolean_attr, :decimal_attr, :integer_attr, :float_attr, :string_attr end diff --git a/test/dummy/app/models/active_model_parent.rb b/test/dummy/app/models/active_model_parent.rb index ee6bb01..35ce687 100644 --- a/test/dummy/app/models/active_model_parent.rb +++ b/test/dummy/app/models/active_model_parent.rb @@ -2,6 +2,7 @@ class ActiveModelParent include ActiveModel::Model + include HumanAttributeValues attr_accessor :inherited_attr end diff --git a/test/dummy/app/models/application_record.rb b/test/dummy/app/models/application_record.rb new file mode 100644 index 0000000..f721ab6 --- /dev/null +++ b/test/dummy/app/models/application_record.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ApplicationRecord < ActiveRecord::Base + include HumanAttributeValues + + self.abstract_class = true +end diff --git a/test/dummy/app/models/boolean_model.rb b/test/dummy/app/models/boolean_model.rb index 77f0caf..99a70bb 100644 --- a/test/dummy/app/models/boolean_model.rb +++ b/test/dummy/app/models/boolean_model.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true -class BooleanModel < ActiveRecord::Base +class BooleanModel < ApplicationRecord end diff --git a/test/dummy/app/models/enum_model.rb b/test/dummy/app/models/enum_model.rb index 163c895..e970f2c 100644 --- a/test/dummy/app/models/enum_model.rb +++ b/test/dummy/app/models/enum_model.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class EnumModel < ActiveRecord::Base +class EnumModel < ApplicationRecord enum status: {dead_and_alive: 0, alive: 1, dead: 2} end diff --git a/test/dummy/app/models/lexicon.rb b/test/dummy/app/models/lexicon.rb index f1bb583..00d75bf 100644 --- a/test/dummy/app/models/lexicon.rb +++ b/test/dummy/app/models/lexicon.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class Lexicon < ActiveRecord::Base +class Lexicon < ApplicationRecord has_many :the_answers end diff --git a/test/dummy/app/models/numeric_model.rb b/test/dummy/app/models/numeric_model.rb index 3c1ed65..785e3af 100644 --- a/test/dummy/app/models/numeric_model.rb +++ b/test/dummy/app/models/numeric_model.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true -class NumericModel < ActiveRecord::Base +class NumericModel < ApplicationRecord end diff --git a/test/dummy/app/models/parent.rb b/test/dummy/app/models/parent.rb index 94c7b9f..b0b5740 100644 --- a/test/dummy/app/models/parent.rb +++ b/test/dummy/app/models/parent.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true -class Parent < ActiveRecord::Base +class Parent < ApplicationRecord end diff --git a/test/dummy/app/models/the_answer.rb b/test/dummy/app/models/the_answer.rb index eb87c08..1a1ab71 100644 --- a/test/dummy/app/models/the_answer.rb +++ b/test/dummy/app/models/the_answer.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class TheAnswer < ActiveRecord::Base +class TheAnswer < ApplicationRecord belongs_to :lexicon end