translated_attribute_value 0.0.1
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 +7 -0
- data/README.md +55 -0
- data/Rakefile +23 -0
- data/lib/translated_attribute_value.rb +5 -0
- data/lib/translated_attribute_value/base.rb +30 -0
- data/lib/translated_attribute_value/version.rb +3 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/translated_attribute_value/base_spec.rb +57 -0
- data/vendor/assets/javascripts/yojs.js +70 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 32a73c61415386ebc9b3d3a1cf40c78e0d898371
|
4
|
+
data.tar.gz: e1ab310f9b93d6a1c6217647a3aa7835e3c8b3bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0983d7ff7328f60e892ef8f9e0822965e5c8c06f1f933cf9c20d84f8e98772dad73cc2a2bd2adf9881ea5c3b25fdd1505211f908cef452da39c77b514842d19
|
7
|
+
data.tar.gz: c321d7e4c0390754c844de31494b234ad92f168c55b695fd969a6600fb358d1bd8e225c659ec9b2c9607d8fe720e8e3051abf68f759b287a66a452408de2e256
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# How to use
|
2
|
+
In your model (activerecord or mongo):
|
3
|
+
|
4
|
+
```ruby
|
5
|
+
class User
|
6
|
+
include NamespacedAssetsRails::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
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
And then your can translate the attribute as following:
|
17
|
+
|
18
|
+
## Activerecord
|
19
|
+
```yaml
|
20
|
+
pt-BR:
|
21
|
+
activerecord:
|
22
|
+
attributes:
|
23
|
+
user:
|
24
|
+
status_translation:
|
25
|
+
value1: 'Translation for value1'
|
26
|
+
value2: 'Translation for value2'
|
27
|
+
```
|
28
|
+
|
29
|
+
## Mongoid
|
30
|
+
```yaml
|
31
|
+
pt-BR:
|
32
|
+
mongoid:
|
33
|
+
attributes:
|
34
|
+
user:
|
35
|
+
status_translation:
|
36
|
+
value1: 'Translation for value1'
|
37
|
+
value2: 'Translation for value2'
|
38
|
+
```
|
39
|
+
|
40
|
+
## Otherwise
|
41
|
+
```yaml
|
42
|
+
pt-BR:
|
43
|
+
translated_attribute_value:
|
44
|
+
user:
|
45
|
+
status_translation:
|
46
|
+
value1: 'Translation for value1'
|
47
|
+
value2: 'Translation for value2'
|
48
|
+
```
|
49
|
+
|
50
|
+
Anywhere in your code you can call
|
51
|
+
```ruby
|
52
|
+
user = User.new
|
53
|
+
user.status = 'value1'
|
54
|
+
user.status_translated
|
55
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'CodusTemplates'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rspec/core'
|
20
|
+
require 'rspec/core/rake_task'
|
21
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
22
|
+
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
|
23
|
+
task :default => :spec
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module TranslatedAttributeValue
|
2
|
+
module Base
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:extend, ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def translated_value_for(*args)
|
9
|
+
args.each do |attribute_name|
|
10
|
+
if defined?(ActiveRecord::Base)
|
11
|
+
self.send(:define_method, "#{attribute_name}_translated") do
|
12
|
+
attribute_value = self.send(attribute_name)
|
13
|
+
I18n.t("activerecord.attributes.user.status_translation.#{attribute_name}.#{attribute_value}")
|
14
|
+
end
|
15
|
+
elsif defined?(Mongoid::Document)
|
16
|
+
self.send(:define_method, "#{attribute_name}_translated") do
|
17
|
+
attribute_value = self.send(attribute_name)
|
18
|
+
I18n.t("mongoid.attributes.user.status_translation.#{attribute_name}.#{attribute_value}")
|
19
|
+
end
|
20
|
+
else
|
21
|
+
self.send(:define_method, "#{attribute_name}_translated") do
|
22
|
+
attribute_value = self.send(attribute_name)
|
23
|
+
I18n.t("translated_attribute_value.user.status_translation.#{attribute_name}.#{attribute_value}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe TranslatedAttributeValue::Base do
|
5
|
+
let(:test_model) { Class.new do
|
6
|
+
include TranslatedAttributeValue::Base
|
7
|
+
translated_value_for :status
|
8
|
+
end.new
|
9
|
+
}
|
10
|
+
|
11
|
+
before(:each) do
|
12
|
+
stub_const("I18n", double)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "translated_value_for" do
|
16
|
+
describe 'ActiveRecord' do
|
17
|
+
before(:each) do
|
18
|
+
stub_const("I18n", Object.new)
|
19
|
+
stub_const("ActiveRecord::Base", Object.new)
|
20
|
+
end
|
21
|
+
|
22
|
+
specify do
|
23
|
+
test_model.stub(:status).and_return('my_value')
|
24
|
+
expect(I18n).to receive(:t).with("activerecord.attributes.user.status_translation.status.my_value")
|
25
|
+
test_model.status_translated
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
describe 'Mongoid' do
|
30
|
+
before(:each) do
|
31
|
+
stub_const("I18n", Object.new)
|
32
|
+
stub_const("Mongoid::Document", Object.new)
|
33
|
+
end
|
34
|
+
|
35
|
+
specify do
|
36
|
+
test_model.stub(:status).and_return('my_value')
|
37
|
+
expect(I18n).to receive(:t).with("mongoid.attributes.user.status_translation.status.my_value")
|
38
|
+
test_model.status_translated
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
describe 'Mongoid' do
|
43
|
+
before(:each) do
|
44
|
+
stub_const("I18n", Object.new)
|
45
|
+
end
|
46
|
+
|
47
|
+
specify do
|
48
|
+
test_model.stub(:status).and_return('my_value')
|
49
|
+
expect(I18n).to receive(:t).with("translated_attribute_value.user.status_translation.status.my_value")
|
50
|
+
test_model.status_translated
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
// git@github.com:codus/yojs.git
|
2
|
+
// Version: 0.0.1
|
3
|
+
|
4
|
+
if(!Array.prototype.last) {
|
5
|
+
Array.prototype.last = function() {
|
6
|
+
return this[this.length - 1];
|
7
|
+
}
|
8
|
+
}
|
9
|
+
|
10
|
+
var yojs = (function(){
|
11
|
+
var nameFunctionHash = {};
|
12
|
+
var parentsNamespaces = [];
|
13
|
+
|
14
|
+
var getNameSpaceResult = function(namespace, arguments) {
|
15
|
+
if (typeof(nameFunctionHash[namespace]) == 'function') {
|
16
|
+
return nameFunctionHash[namespace].apply(this, arguments);
|
17
|
+
} else {
|
18
|
+
return nameFunctionHash[namespace];
|
19
|
+
}
|
20
|
+
}
|
21
|
+
return {
|
22
|
+
define: function(namespace, value) {
|
23
|
+
if (this.isDefined(namespace)) {
|
24
|
+
var errorMessage = "Error: trying to define namespace '" + namespace + "'' twice";
|
25
|
+
throw errorMessage;
|
26
|
+
} else {
|
27
|
+
nameFunctionHash[namespace] = value;
|
28
|
+
}
|
29
|
+
return;
|
30
|
+
},
|
31
|
+
|
32
|
+
set: function(namespace, value) {
|
33
|
+
nameFunctionHash[namespace] = value;
|
34
|
+
},
|
35
|
+
|
36
|
+
call: function(namespace){
|
37
|
+
var parentNamespaceArray = namespace.split(".");
|
38
|
+
var lastNameCalled = parentNamespaceArray.pop();
|
39
|
+
var currentParentNamespace = parentNamespaceArray.join(".");
|
40
|
+
var returnValue = null;
|
41
|
+
var lastParentNamespace = parentsNamespaces.last();
|
42
|
+
var functionWithLastParentClassNamespace = lastParentNamespace + "." + lastNameCalled;
|
43
|
+
var errorMessage = "";
|
44
|
+
var calledFunctionArguments = Array.prototype.slice.apply(arguments, [1]);
|
45
|
+
|
46
|
+
parentsNamespaces.push(currentParentNamespace);
|
47
|
+
|
48
|
+
if (this.isDefined(namespace)) {
|
49
|
+
returnValue = getNameSpaceResult(namespace, calledFunctionArguments);
|
50
|
+
} else if (this.isDefined(functionWithLastParentClassNamespace)) {
|
51
|
+
returnValue = getNameSpaceResult(functionWithLastParentClassNamespace, calledFunctionArguments);
|
52
|
+
} else {
|
53
|
+
var errorMessage = "Error: called namespace '" + namespace + "'. But it doesn't exist.";
|
54
|
+
throw errorMessage;
|
55
|
+
}
|
56
|
+
|
57
|
+
parentsNamespaces.pop();
|
58
|
+
|
59
|
+
return returnValue;
|
60
|
+
},
|
61
|
+
|
62
|
+
get: function(namespace) {
|
63
|
+
return this.call(namespace);
|
64
|
+
},
|
65
|
+
|
66
|
+
isDefined: function(namespace) {
|
67
|
+
return nameFunctionHash.hasOwnProperty(namespace);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}());
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: translated_attribute_value
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vinícius Oyama
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
description: Translate attribute values
|
28
|
+
email: vinicius.oyama@gmail.com.br
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- lib/translated_attribute_value.rb
|
36
|
+
- lib/translated_attribute_value/base.rb
|
37
|
+
- lib/translated_attribute_value/version.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/translated_attribute_value/base_spec.rb
|
40
|
+
- vendor/assets/javascripts/yojs.js
|
41
|
+
homepage: https://github.com/viniciusoyama/translated_attribute_value
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.2.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Translate attribute values
|
65
|
+
test_files:
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/translated_attribute_value/base_spec.rb
|