rails_translates_value 0.0.1 → 0.0.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6de8539d338beeab9ec678a5dd9fac404539689e
|
4
|
+
data.tar.gz: 5ffb19020d1d3e4d8ac4a7c23d2ed8284a5e6f63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c46b2ce67c78069cea74db917ef8c9ed09269031abc57b6546ab01dd5402aa7377be6bd1a10048964837398f1758bc5d130e61d869234b4f6bb754f5d7a9b9e
|
7
|
+
data.tar.gz: b4386d5d371c2a2c646380cebb2fa64453e1011559e5154f177029a573289e7ca3c5289e706082d4951258a5352b9428bb4666f11dada3f4d3ae80da078b6d23
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Concerns
|
4
|
+
module Model
|
5
|
+
module TranslatedAttrAccessor
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
def translated_attr_accessor(*args)
|
10
|
+
_ = args.extract_options!
|
11
|
+
|
12
|
+
attributes = args
|
13
|
+
|
14
|
+
attributes.each do |attribute|
|
15
|
+
I18n.available_locales.each do |locale|
|
16
|
+
reader_method_name = "#{attribute}_#{locale}"
|
17
|
+
writer_method_name = "#{attribute}_#{locale}="
|
18
|
+
|
19
|
+
define_method attribute do |locale = I18n.locale|
|
20
|
+
name = __method__
|
21
|
+
key = read_attribute(:identifier)
|
22
|
+
read_translated_attribute(name, key, locale)
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method reader_method_name do
|
26
|
+
locale = __method__.to_s.split("_").last
|
27
|
+
name = __method__.to_s.gsub("_#{locale}", '')
|
28
|
+
key = read_attribute(:identifier)
|
29
|
+
read_translated_attribute(name, key, locale)
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method writer_method_name do |value|
|
33
|
+
locale = __method__.to_s.split("_").last.gsub("=", '')
|
34
|
+
name = __method__.to_s.gsub("_#{locale}=", "")
|
35
|
+
key = read_attribute(:identifier)
|
36
|
+
write_translated_attribute(name, key, value, locale)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# private
|
44
|
+
|
45
|
+
def write_translated_attribute(name, key, value, locale)
|
46
|
+
key = "activerecord.values.#{self.class.name.underscore}.#{name}.#{key}"
|
47
|
+
Ecm::Translations::Translation.where(locale: locale, key: key).first_or_initialize.update_attribute(:value, value)
|
48
|
+
I18n.reload!
|
49
|
+
value
|
50
|
+
end
|
51
|
+
|
52
|
+
def read_translated_attribute(name, key, locale)
|
53
|
+
I18n.with_locale(locale) do
|
54
|
+
I18n.t("activerecord.values.#{self.class.name.underscore}.#{name}.#{key}", default: '')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_translates_value
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Vasquez Angel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- MIT-LICENSE
|
77
77
|
- README.rdoc
|
78
78
|
- Rakefile
|
79
|
+
- app/models/concerns/model/translated_attr_accessor.rb
|
79
80
|
- app/models/concerns/model/translates_value.rb
|
80
81
|
- lib/rails_translates_value.rb
|
81
82
|
- lib/rails_translates_value/engine.rb
|
@@ -106,3 +107,4 @@ signing_key:
|
|
106
107
|
specification_version: 4
|
107
108
|
summary: Translate rails active model values with i18n.
|
108
109
|
test_files: []
|
110
|
+
has_rdoc:
|