human_attribute_value 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8086c5af79f3b998473cd1721c12640c486fb59f
4
+ data.tar.gz: f6ea7f866376e75609bdd2c49e6b257bcb35df13
5
+ SHA512:
6
+ metadata.gz: 9fcadcb54c3162c6ed6753ab6a16e930c7e0490f270651f8d472b29eb576e279be73af98a734c38dc1e7161bd1c3c4e09127381ff22c1fe453984eb8b8eda301
7
+ data.tar.gz: fc1a5e3b7bcd51590b316a3811dc2e11cdc751f021a57853b7596e6f49626c95fc14231b057d4db18ae86ce49f6f922fd9462fe6de1078605b90b7e5c75bb312
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -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
data/Gemfile ADDED
@@ -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
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -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).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -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__)
data/bin/setup ADDED
@@ -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
@@ -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,0 +1,51 @@
1
+ require "active_support/concern"
2
+ require "human_attribute_value/version"
3
+
4
+ module HumanAttributeValue
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ extend ActiveModel::Translation unless self.is_a?(ActiveModel::Translation)
9
+ end
10
+
11
+ class_methods do
12
+ # adapted from activemodel-5.0.0/lib/active_model/translation.rb
13
+ def human_attribute_value(attribute, value, options = {})
14
+ options = { count: 1 }.merge!(options)
15
+ parts = attribute.to_s.split(".")
16
+ attribute = parts.pop
17
+ namespace = parts.join("/") unless parts.empty?
18
+ values_scope = "#{self.i18n_scope}.values"
19
+
20
+ if namespace
21
+ defaults = lookup_ancestors.map do |klass|
22
+ :"#{values_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}.#{value}"
23
+ end
24
+ defaults << :"#{values_scope}.#{namespace}.#{attribute}.#{value}"
25
+ else
26
+ defaults = lookup_ancestors.map do |klass|
27
+ :"#{values_scope}.#{klass.model_name.i18n_key}.#{attribute}.#{value}"
28
+ end
29
+ end
30
+
31
+ defaults << :"values.#{attribute}.#{value}"
32
+ defaults << options.delete(:default) if options[:default]
33
+ defaults << value.try(:humanize) || value
34
+
35
+ options[:default] = defaults
36
+ I18n.translate(defaults.shift, options)
37
+ end
38
+ end
39
+
40
+ def human_attribute_value(attribute, options = {})
41
+ value = public_send(attribute)
42
+
43
+ if value.present?
44
+ if value.is_a?(Array)
45
+ value.map { |el| self.class.human_attribute_value(attribute, el, options) }
46
+ else
47
+ self.class.human_attribute_value(attribute, value, options)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module HumanAttributeValue
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_attribute_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Sievers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - msievers@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - human_attribute_value.gemspec
58
+ - lib/human_attribute_value.rb
59
+ - lib/human_attribute_value/version.rb
60
+ homepage: https://github.com/nerdgeschoss/human_attribute_value
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.6.11
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Translate rails models attribute values just like attribute names.
84
+ test_files: []