translated_attribute_value 0.0.2.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -7
- data/db/schema.rb +4 -0
- data/lib/translated_attribute_value.rb +9 -0
- data/lib/translated_attribute_value/base.rb +30 -14
- data/lib/translated_attribute_value/version.rb +1 -1
- data/spec/spec_helper.rb +10 -1
- data/spec/translated_attribute_value/base_spec.rb +90 -31
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31a6be9205a66efb82e4bf15e9b6877cf8404c46
|
4
|
+
data.tar.gz: 3a98796b26dc5986a4211754bffe986a5c31c153
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aceaabdae2f290eda9ef5f65b9a388792c0f23a9f5042d88e03425f86769af1cf0f5a0651f2368b1b526591264366db2f8d10525bcc26f2126df053144b920e
|
7
|
+
data.tar.gz: d001ceb8cd5298343e75df32b504e021d2dc66d0e04ed276a24d36c12d8217ef8e39d3bdc8aca13b0a718961fb555955b6ca50652169175dad891741d1476886
|
data/README.md
CHANGED
@@ -1,15 +1,10 @@
|
|
1
1
|
# How to use
|
2
|
-
|
2
|
+
If you are using ActiveRecord or Mongoid, translated attribute value will be included by default, else you have to include TranslatedAttributeValue::Base in your model class:
|
3
3
|
|
4
4
|
```ruby
|
5
5
|
class User
|
6
6
|
include TranslatedAttributeValue::Base
|
7
|
-
|
8
|
-
# if you have a field called status
|
9
|
-
translate_value_for :status
|
10
|
-
|
11
|
-
# more field
|
12
|
-
translate_value_for :payments_status, :rule_status
|
7
|
+
# model related code
|
13
8
|
end
|
14
9
|
```
|
15
10
|
|
@@ -52,4 +47,5 @@ Anywhere in your code you can call
|
|
52
47
|
user = User.new
|
53
48
|
user.status = 'value1'
|
54
49
|
user.status_translated
|
50
|
+
#=> 'Translation for value1'
|
55
51
|
```
|
data/db/schema.rb
ADDED
@@ -3,3 +3,12 @@ require "translated_attribute_value/version"
|
|
3
3
|
|
4
4
|
module TranslatedAttributeValue
|
5
5
|
end
|
6
|
+
|
7
|
+
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
ActiveRecord::Base.send(:include, TranslatedAttributeValue::Base)
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveSupport.on_load(:mongoid) do
|
13
|
+
Mongoid::Document.send(:include, TranslatedAttributeValue::Base)
|
14
|
+
end
|
@@ -1,32 +1,48 @@
|
|
1
|
-
require 'active_support/inflector'
|
2
|
-
|
3
1
|
module TranslatedAttributeValue
|
4
2
|
module Base
|
5
3
|
def self.included(base)
|
6
|
-
base.
|
4
|
+
if defined?(Mongoid::Document) && base.ancestors.include?(Mongoid::Document)
|
5
|
+
Mongoid::Document.send(:include, InstanceMethods)
|
6
|
+
Mongoid::Document::ClassMethods.send(:include, ClassMethods)
|
7
|
+
else
|
8
|
+
base.send(:include, InstanceMethods)
|
9
|
+
base.send(:extend, ClassMethods)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def method_missing(name, *args, &block)
|
15
|
+
attribute_name = name.to_s.gsub!(/_translated$/, '') # this will return nil or the modified string
|
16
|
+
if (attribute_name) && self.respond_to?(attribute_name)
|
17
|
+
self.class.translated_value_for(attribute_name)
|
18
|
+
self.send(name)
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
7
23
|
end
|
8
24
|
|
9
25
|
module ClassMethods
|
10
26
|
def translated_value_for(*args)
|
11
27
|
args.each do |attribute_name|
|
12
|
-
if defined?(ActiveRecord::Base)
|
28
|
+
if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
|
13
29
|
self.send(:define_method, "#{attribute_name}_translated") do
|
14
30
|
attribute_value = self.send(attribute_name)
|
15
|
-
I18n.t("activerecord.attributes.#{self.class.to_s.
|
31
|
+
I18n.t("activerecord.attributes.#{self.class.to_s.downcase}.#{attribute_name}_translation.#{attribute_value}")
|
16
32
|
end
|
17
|
-
elsif defined?(Mongoid::Document)
|
33
|
+
elsif defined?(Mongoid::Document) && self.ancestors.include?(Mongoid::Document)
|
18
34
|
self.send(:define_method, "#{attribute_name}_translated") do
|
19
35
|
attribute_value = self.send(attribute_name)
|
20
|
-
I18n.t("mongoid.attributes.#{self.class.to_s.
|
36
|
+
I18n.t("mongoid.attributes.#{self.class.to_s.downcase}.#{attribute_name}_translation.#{attribute_value}")
|
21
37
|
end
|
22
38
|
else
|
23
39
|
self.send(:define_method, "#{attribute_name}_translated") do
|
24
40
|
attribute_value = self.send(attribute_name)
|
25
|
-
I18n.t("translated_attribute_value.#{self.class.to_s.
|
41
|
+
I18n.t("translated_attribute_value.#{self.class.to_s.downcase}.#{attribute_name}_translation.#{attribute_value}")
|
26
42
|
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
43
|
+
end # of if chain
|
44
|
+
end # of each
|
45
|
+
end # of translated_value_for
|
46
|
+
end # of ClassMethods
|
47
|
+
end # of Base module
|
48
|
+
end # of TranslatedAttributeValue module
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
|
-
ENV["RAILS_ENV"]
|
1
|
+
ENV["RAILS_ENV"] = 'test'
|
2
2
|
require 'rubygems'
|
3
3
|
require 'bundler/setup'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_record'
|
6
|
+
require 'mongoid'
|
7
|
+
require 'nulldb_rspec'
|
4
8
|
require 'translated_attribute_value'
|
9
|
+
require 'debugger'
|
10
|
+
include NullDB::RSpec::NullifiedDatabase
|
5
11
|
|
6
12
|
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
|
13
|
+
|
14
|
+
NullDB.configure {|ndb| def ndb.project_root;'./';end}
|
15
|
+
ActiveRecord::Base.establish_connection :adapter => :nulldb
|
@@ -2,57 +2,116 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe TranslatedAttributeValue::Base do
|
5
|
-
let(:test_model) { Class.new do
|
6
|
-
include TranslatedAttributeValue::Base
|
7
|
-
translated_value_for :status
|
8
|
-
def self.to_s
|
9
|
-
"NomeClasse"
|
10
|
-
end
|
11
|
-
end.new
|
12
|
-
}
|
13
|
-
|
14
5
|
before(:each) do
|
15
|
-
|
6
|
+
stub_i18n = Object.new
|
7
|
+
stub_i18n.stub("t")
|
8
|
+
stub_const("I18n", stub_i18n)
|
16
9
|
end
|
17
10
|
|
18
11
|
describe "translated_value_for" do
|
19
12
|
describe 'ActiveRecord' do
|
20
|
-
|
21
|
-
|
22
|
-
|
13
|
+
let(:test_model_instance) {
|
14
|
+
Class.new ActiveRecord::Base do
|
15
|
+
|
16
|
+
def status
|
17
|
+
'my_value'
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.to_s
|
21
|
+
"nome_classe"
|
22
|
+
end
|
23
|
+
|
24
|
+
end.new
|
25
|
+
}
|
26
|
+
|
27
|
+
specify "I can call translated_value_for in the model" do
|
28
|
+
expect(test_model_instance.class).to respond_to(:translated_value_for)
|
23
29
|
end
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
31
|
+
describe "I can call a attribute with attribute_translated without defining it" do
|
32
|
+
specify "the translation must be called" do
|
33
|
+
expect(I18n).to receive(:t).with("activerecord.attributes.nome_classe.status_translation.my_value")
|
34
|
+
test_model_instance.status_translated
|
35
|
+
end
|
36
|
+
|
37
|
+
specify "the method should be defined" do
|
38
|
+
test_model_instance.status_translated
|
39
|
+
expect(test_model_instance).to respond_to(:status_translated)
|
40
|
+
end
|
29
41
|
end
|
30
42
|
|
31
43
|
end
|
44
|
+
|
32
45
|
describe 'Mongoid' do
|
33
|
-
|
34
|
-
|
35
|
-
|
46
|
+
let(:test_model_instance) {
|
47
|
+
Class.new do
|
48
|
+
include Mongoid::Document
|
49
|
+
|
50
|
+
def status
|
51
|
+
'my_value'
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.to_s
|
55
|
+
"nome_classe"
|
56
|
+
end
|
57
|
+
|
58
|
+
end.new
|
59
|
+
}
|
60
|
+
|
61
|
+
specify "I can call translated_value_for in the model" do
|
62
|
+
expect(test_model_instance.class).to respond_to(:translated_value_for)
|
36
63
|
end
|
37
64
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
65
|
+
|
66
|
+
describe "I can call a attribute with attribute_translated without defining it" do
|
67
|
+
specify "the translation must be called" do
|
68
|
+
expect(I18n).to receive(:t).with("mongoid.attributes.nome_classe.status_translation.my_value")
|
69
|
+
test_model_instance.status_translated
|
70
|
+
end
|
71
|
+
|
72
|
+
specify "the method should be defined" do
|
73
|
+
test_model_instance.status_translated
|
74
|
+
expect(test_model_instance).to respond_to(:status_translated)
|
75
|
+
end
|
42
76
|
end
|
43
77
|
|
78
|
+
|
44
79
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
80
|
+
|
81
|
+
describe 'without ActiveRecord or Mongoid' do
|
82
|
+
let(:test_model_instance) {
|
83
|
+
Class.new do
|
84
|
+
include TranslatedAttributeValue::Base
|
85
|
+
|
86
|
+
def status
|
87
|
+
'my_value'
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.to_s
|
91
|
+
"nome_classe"
|
92
|
+
end
|
93
|
+
|
94
|
+
end.new
|
95
|
+
}
|
96
|
+
|
97
|
+
specify "I can call translated_value_for in the model" do
|
98
|
+
expect(test_model_instance.class).to respond_to(:translated_value_for)
|
48
99
|
end
|
49
100
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
101
|
+
|
102
|
+
describe "I can call a attribute with attribute_translated without defining it" do
|
103
|
+
specify "the translation must be called" do
|
104
|
+
expect(I18n).to receive(:t).with("translated_attribute_value.nome_classe.status_translation.my_value")
|
105
|
+
test_model_instance.status_translated
|
106
|
+
end
|
107
|
+
|
108
|
+
specify "the method should be defined" do
|
109
|
+
test_model_instance.status_translated
|
110
|
+
expect(test_model_instance).to respond_to(:status_translated)
|
111
|
+
end
|
54
112
|
end
|
55
113
|
|
114
|
+
|
56
115
|
end
|
57
116
|
|
58
117
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: translated_attribute_value
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vinícius Oyama
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -32,6 +32,7 @@ extra_rdoc_files: []
|
|
32
32
|
files:
|
33
33
|
- README.md
|
34
34
|
- Rakefile
|
35
|
+
- db/schema.rb
|
35
36
|
- lib/translated_attribute_value.rb
|
36
37
|
- lib/translated_attribute_value/base.rb
|
37
38
|
- lib/translated_attribute_value/version.rb
|