|
| 1 | +# HumanAttributeValue |
| 2 | + |
| 3 | +Rails provides `.human_attribute_name`, for you to translate model names. But it lacks the same functionality when it comes to attribute values. This gems adds the missing piece, while being as close as possible to the present conventions. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add this line to your application's Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +gem "human_attribute_value" |
| 11 | +``` |
| 12 | + |
| 13 | +And then execute: |
| 14 | + |
| 15 | + $ bundle |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Rails defines some conventions for model (attributes) translations, for example `activerecord.models` and `activerecord.attributes`. This gem adds `activerecord.values`. |
| 20 | + |
| 21 | +`config/locales/de.yml` |
| 22 | + |
| 23 | +```yml |
| 24 | +--- |
| 25 | +de: |
| 26 | + activerecord: |
| 27 | + models: |
| 28 | + company: Firma |
| 29 | + attributes: |
| 30 | + company: |
| 31 | + name: Name |
| 32 | + city: Stadt |
| 33 | + size: Größe |
| 34 | + # |
| 35 | + # This gem adds activerecord.values |
| 36 | + # |
| 37 | + values: |
| 38 | + company: |
| 39 | + city: |
| 40 | + size: |
| 41 | + small: klein |
| 42 | + medium: mittel |
| 43 | + large: groß |
| 44 | + tags: |
| 45 | + company: Firma |
| 46 | + important: wichtig |
| 47 | + green: ökologisch |
| 48 | +``` |
| 49 | +
|
| 50 | +In order to use `human_attribute_value` it needs to be included into your model or your `ApplicationRecord`. |
| 51 | + |
| 52 | +```ruby |
| 53 | +class ApplicationRecord < ActiveRecord::Base |
| 54 | + include HumanAttributeValue |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +Now you can do the following. |
| 59 | + |
| 60 | +```ruby |
| 61 | +company = Company.create(name: "ACME Inc.", city: "Berlin", size: "large") |
| 62 | +
|
| 63 | +# assuming your current locale is :de |
| 64 | +company.human_attribute_value(:size) # => "groß" |
| 65 | +``` |
| 66 | + |
| 67 | +It respects serialized arrays as well. Let's assume you have an serialized `tags` array. |
| 68 | + |
| 69 | +```ruby |
| 70 | +company = Company.create(name: "ACME Inc.", tags: ["company". "important", "green"]) |
| 71 | +
|
| 72 | +# assuming your current locale is :de |
| 73 | +company.human_attribute_value(:tags) # => ["Firma", "wichtig", "ökologisch"] |
| 74 | +``` |
| 75 | + |
| 76 | +## Development |
| 77 | + |
| 78 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 79 | + |
| 80 | +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 81 | + |
| 82 | +## Contributing |
| 83 | + |
| 84 | +Bug reports and pull requests are welcome on GitHub at https://github.com/nerdgeschoss/human_attribute_value. |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
0 commit comments