translateable_attributes 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c3f4b9d3b35070a71a8495ded176171b3de558d
4
- data.tar.gz: 434f55f169fa3b844f84d4c82106ac998664b455
3
+ metadata.gz: 740b0c6982156887b62d119cf65305bf677ddd1f
4
+ data.tar.gz: 64fdc39fa6d8683a9bcaa5910112b5c033f8715f
5
5
  SHA512:
6
- metadata.gz: 494d31020c4a2e2f7987e3c37c91018990caf3350c8c2255f06becb0485131c8eaeacda657fdd51a1db0ae88b43202a7bdfc8ddc774ebf8d682857bea68a8241
7
- data.tar.gz: 4330f27b819394806216312213dbca2a44371f654edbc0fad7de550b06622f5ba1324d886c66012f07032ddab7ebcb3c10cafef2aa23c7b67376212a5d1e232f
6
+ metadata.gz: 4b44a6b217d009a71b630c0ab68461df3146fbe990bad43693e36515b4e8b2b0f3871d50b1b80ead7999fc04df4100a8c6708670153d04cffa48ee260dcd6523
7
+ data.tar.gz: 6bcaf33ef250d98d032a1dbe1d40e282b36509f5589e6ca112fe15c7ee9316e303af0654a0e7ae64755de9526a6cf336f920003f2ddb0321c3a78d2b313e11a3
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # TranslateableAttributes ![Travis CI](https://travis-ci.org/tmikoss/translateable_attributes.svg)
2
2
 
3
- Helper methods to display translated values of object attributes (stored in database, plain object properties, ...)
3
+ Helper methods to display translated values of object attributes.
4
4
 
5
5
  ## Installation
6
6
 
@@ -29,7 +29,7 @@ en:
29
29
  completed: 'Done'
30
30
  ```
31
31
 
32
- Extend a class with `TranslateableAttributes::Methods` and call `translate_attributes` method on it, passing a hash of `attribute_name: 'translation.namespace'` to it:
32
+ Extend a class with `TranslateableAttributes::Methods` and call `translate_attributes` method on it:
33
33
 
34
34
  ```ruby
35
35
  class TranslatedClass
@@ -55,6 +55,10 @@ In addition, `attributes_for_select` class method is defined that returns all av
55
55
  TranslatedClass.states_for_select #=> [['Initiated', 'new'], ['Done', 'completed']]
56
56
  ```
57
57
 
58
+ `translate_attributes` class method accepts a hash of `attribute_name: 'translation.namespace'`. Translations are looked up by appending attribute value to provided namespace and passing that to `I18n.t` method.
59
+
60
+ For example, given `Klass.translate_attributes state: 'some.nested.translation.namespace'`, calling `Klass.translated_state 'new'` equals to `I18n.t 'some.nested.translation.namespace.new'`.
61
+
58
62
  ## Development
59
63
 
60
64
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,13 +1,16 @@
1
1
  module TranslateableAttributes
2
2
  module Methods
3
+ # rubocop:disable Metrics/AbcSize
3
4
  def translate_attributes(options = {})
4
5
  options.each do |attribute, namespace|
6
+ plural_attribute = attribute.to_s.pluralize
7
+
5
8
  define_method "translated_#{attribute}" do
6
9
  value = send(attribute)
7
10
  value.present? ? I18n.t("#{namespace}.#{value}", default: value) : value
8
11
  end
9
12
 
10
- define_singleton_method "#{attribute.to_s.pluralize}_for_select" do
13
+ define_singleton_method "#{plural_attribute}_for_select" do
11
14
  I18n.t(namespace).each_with_object([]) do |(value, translation), collection|
12
15
  collection << [translation, value]
13
16
  end
@@ -16,6 +19,10 @@ module TranslateableAttributes
16
19
  define_singleton_method "translated_#{attribute}" do |value|
17
20
  I18n.t([namespace, value].join '.')
18
21
  end
22
+
23
+ define_singleton_method "possible_#{plural_attribute}" do
24
+ I18n.t(namespace).keys
25
+ end
19
26
  end
20
27
  end
21
28
  end
@@ -1,3 +1,3 @@
1
1
  module TranslateableAttributes
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translateable_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toms Mikoss
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-07 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport