Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
human_attribute_values (1.2.1)
human_attribute_values (1.2.2)
rails (>= 4.2.10)

GEM
Expand Down Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions lib/human_attribute_values/human_attribute_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,3 @@ def human_attribute_value(attribute, value, options = {})
end
end
end

ActiveRecord::Base.include HumanAttributeValues
ActiveModel::Model.include HumanAttributeValues
1 change: 1 addition & 0 deletions test/dummy/app/models/active_model_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class ActiveModelModel
include ActiveModel::Model
include HumanAttributeValues

attr_accessor :boolean_attr, :decimal_attr, :integer_attr, :float_attr, :string_attr
end
1 change: 1 addition & 0 deletions test/dummy/app/models/active_model_parent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class ActiveModelParent
include ActiveModel::Model
include HumanAttributeValues

attr_accessor :inherited_attr
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
include HumanAttributeValues

self.abstract_class = true
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/boolean_model.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

class BooleanModel < ActiveRecord::Base
class BooleanModel < ApplicationRecord
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/enum_model.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/dummy/app/models/lexicon.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Lexicon < ActiveRecord::Base
class Lexicon < ApplicationRecord
has_many :the_answers
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/numeric_model.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

class NumericModel < ActiveRecord::Base
class NumericModel < ApplicationRecord
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/parent.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

class Parent < ActiveRecord::Base
class Parent < ApplicationRecord
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/the_answer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class TheAnswer < ActiveRecord::Base
class TheAnswer < ApplicationRecord
belongs_to :lexicon
end