Skip to content

Commit 26477e1

Browse files
committed
Initial commit
0 parents  commit 26477e1

File tree

14 files changed

+278
-0
lines changed

14 files changed

+278
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sudo: false
2+
language: ruby
3+
rvm:
4+
- 2.3.1
5+
before_install: gem install bundler -v 1.13.6

Gemfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
source "https://rubygems.org"
2+
3+
# Specify your gem's dependencies in zstandard.gemspec
4+
gemspec
5+
6+
group :development, :test do
7+
gem "benchmark-ips"
8+
gem "bundler"
9+
gem "rake"
10+
gem "rubocop", "~> 0.41", require: false
11+
gem "rspec", "~> 3.0"
12+
gem "simplecov"
13+
14+
if !ENV["CI"] && RUBY_ENGINE == "ruby"
15+
gem "pry"
16+
17+
if RUBY_VERSION < "2.0.0"
18+
gem "pry-nav"
19+
else
20+
gem "pry-byebug"
21+
end
22+
end
23+
end

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Michael Sievers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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).

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "human_attribute_value"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start(__FILE__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

human_attribute_value.gemspec

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# coding: utf-8
2+
lib = File.expand_path("../lib", __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require "human_attribute_value/version"
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "human_attribute_value"
8+
spec.version = HumanAttributeValue::VERSION
9+
spec.authors = ["Michael Sievers"]
10+
spec.email = ["msievers@users.noreply.github.com"]
11+
12+
spec.summary = %q{Translate rails models attribute values just like attribute names.}
13+
spec.homepage = "https://github.com/nerdgeschoss/human_attribute_value"
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
17+
f.match(%r{^(test|spec|features)/})
18+
end
19+
20+
spec.bindir = "exe"
21+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22+
spec.require_paths = ["lib"]
23+
24+
spec.add_dependency "activemodel"
25+
spec.add_dependency "activesupport"
26+
end

0 commit comments

Comments
 (0)