acts_as_translated 0.0.2
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/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/acts_as_translated.gemspec +22 -0
- data/lib/acts_as_translated.rb +57 -0
- data/lib/acts_as_translated/version.rb +3 -0
- data/tests/countries.yml +17 -0
- data/tests/translated_test.rb +94 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e4d64595d0a9517ba19cc3a8265e5a9e2fe24b43
|
4
|
+
data.tar.gz: 3fb8bafa1b863462ac85296e23a4cae29d330794
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 73632959c1a3838f70486ef1b177f85393405554344a22570659d5b8ce3447e45f8d18213961f87fcc4c8786ea214f3b035b16f0a4e0c159c4c06ce2ef864894
|
7
|
+
data.tar.gz: ca3a0156fab58f916eeb33626cd892a089257b36613864ee0c544f2b8a983d8013da77c971984629722cd72ab963606113ef091087a5a6ad0bab040dc29e5cb5
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Stijn Mathysen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# ActsAsTranslated
|
2
|
+
|
3
|
+
This acts_as extension allows easy attribute translation.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
Your migration file should look like this;
|
8
|
+
|
9
|
+
create_table :countries do |t|
|
10
|
+
t.string :name_nl
|
11
|
+
t.string :name_fr
|
12
|
+
t.string :name_en
|
13
|
+
|
14
|
+
t.timestamps
|
15
|
+
end
|
16
|
+
|
17
|
+
Create a class;
|
18
|
+
|
19
|
+
class Country < ActiveRecord::Base
|
20
|
+
acts_as_translated :name #, :default => :nl
|
21
|
+
end
|
22
|
+
|
23
|
+
Use it in your views, controllers, ...
|
24
|
+
|
25
|
+
<% Country.all.each do |country| %>
|
26
|
+
<%= country.name %>
|
27
|
+
<% end %>
|
28
|
+
|
29
|
+
|
30
|
+
# Form Example
|
31
|
+
|
32
|
+
Build a dynamic form that extends, based on the number of languages available;
|
33
|
+
|
34
|
+
<% form_for @country do %>
|
35
|
+
...
|
36
|
+
<% @country.translated_fields_to_attributes.each do |attribute| %>
|
37
|
+
<p><%= f.label attribute %><br/>
|
38
|
+
<%= f.text_field attribute %></p>
|
39
|
+
<% end %>
|
40
|
+
...
|
41
|
+
<% end %>
|
42
|
+
|
43
|
+
|
44
|
+
# Install
|
45
|
+
|
46
|
+
In your bundler Gemfile
|
47
|
+
|
48
|
+
gem 'acts_as_translated'
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'acts_as_translated/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "acts_as_translated"
|
8
|
+
spec.version = ActsAsTranslated::VERSION
|
9
|
+
spec.authors = ["Stijn Mathysen"]
|
10
|
+
spec.email = ["stijn@skylight.be"]
|
11
|
+
spec.summary = %q{This acts_as extension allows easy attribute translation.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "acts_as_translated/version"
|
2
|
+
|
3
|
+
module ActsAsTranslated
|
4
|
+
def self.included(base)
|
5
|
+
class << base
|
6
|
+
attr_accessor :language
|
7
|
+
attr_accessor :translated_fields
|
8
|
+
end
|
9
|
+
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def acts_as_translated(fields, options = {})
|
15
|
+
self.language = options[:default] || 'en'
|
16
|
+
self.translated_fields = Array.new if self.translated_fields.blank?
|
17
|
+
self.translated_fields << fields
|
18
|
+
self.translated_fields.flatten!
|
19
|
+
class_eval do
|
20
|
+
include InstanceMethods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def translated_field_exists(field)
|
27
|
+
self.class.translated_fields.member?(field.to_sym) && attributes.member?("#{field}_#{self.class.language}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def translated_fields_to_attributes
|
31
|
+
result = Array.new
|
32
|
+
self.class.translated_fields.each do |field|
|
33
|
+
attributes.each do |attribute, value|
|
34
|
+
if attribute =~ /\A#{field}_\w{2}\Z/
|
35
|
+
result << attribute
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
result.sort
|
41
|
+
end
|
42
|
+
|
43
|
+
def respond_to?(field, include_priv = false)
|
44
|
+
return true if translated_field_exists(field)
|
45
|
+
super
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(field, *args)
|
49
|
+
return self["#{field}_#{self.class.language}".to_sym] if translated_field_exists(field)
|
50
|
+
super
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class ActiveRecord::Base
|
56
|
+
include ActsAsTranslated
|
57
|
+
end
|
data/tests/countries.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
belgium:
|
2
|
+
name_nl: België
|
3
|
+
name_fr: Belgique
|
4
|
+
name_en: Belgium
|
5
|
+
description_nl: Koninkrijk België
|
6
|
+
description_fr: Royaume de Belgique
|
7
|
+
description_en: Kingdom of Belgium
|
8
|
+
position: 1
|
9
|
+
|
10
|
+
holland:
|
11
|
+
name_nl: Nederland
|
12
|
+
name_fr: Pays-Bas
|
13
|
+
name_en: Netherlands
|
14
|
+
description_nl: Koninkrijk der Nederlanden
|
15
|
+
description_fr: Royaume des Pays-Bas
|
16
|
+
description_en: Kingdom of the Netherlands
|
17
|
+
position: 2
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'activerecord', '>= 1.15.4.7794'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
require "#{File.dirname(__FILE__)}/../init"
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
10
|
+
|
11
|
+
def setup_db
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
13
|
+
create_table :countries do |t|
|
14
|
+
t.column :name_nl, :string
|
15
|
+
t.column :name_fr, :string
|
16
|
+
t.column :name_en, :string
|
17
|
+
t.column :description_nl, :text
|
18
|
+
t.column :description_fr, :text
|
19
|
+
t.column :description_en, :text
|
20
|
+
t.column :created_at, :datetime
|
21
|
+
t.column :updated_at, :datetime
|
22
|
+
t.column :position, :integer
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def fixtures(filename)
|
28
|
+
records = YAML.load_file("#{filename}.yml")
|
29
|
+
records.each do |key, record|
|
30
|
+
Country.create!(record)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def teardown_db
|
35
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
36
|
+
ActiveRecord::Base.connection.drop_table(table)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Country < ActiveRecord::Base
|
41
|
+
acts_as_translated :name
|
42
|
+
acts_as_translated :description
|
43
|
+
|
44
|
+
default_scope :order => 'position'
|
45
|
+
end
|
46
|
+
|
47
|
+
class TranslatedTest < Test::Unit::TestCase
|
48
|
+
def setup
|
49
|
+
setup_db
|
50
|
+
fixtures :countries
|
51
|
+
end
|
52
|
+
|
53
|
+
def teardown
|
54
|
+
teardown_db
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_fixtures
|
58
|
+
assert_equal 2, Country.count
|
59
|
+
assert_equal 'Belgium', Country.first.name_en
|
60
|
+
assert_equal 'Netherlands', Country.last.name_en
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_translated_name
|
64
|
+
@belgium = Country.first
|
65
|
+
@netherlands = Country.last
|
66
|
+
|
67
|
+
# test default language
|
68
|
+
assert_equal 'Belgium', @belgium.name
|
69
|
+
assert_equal 'Netherlands', @netherlands.name
|
70
|
+
assert_equal 'Kingdom of Belgium', @belgium.description
|
71
|
+
assert_equal 'Kingdom of the Netherlands', @netherlands.description
|
72
|
+
|
73
|
+
# change language to dutch
|
74
|
+
Country.language = 'nl'
|
75
|
+
assert_equal 'België', @belgium.name
|
76
|
+
assert_equal 'Nederland', @netherlands.name
|
77
|
+
assert_equal 'Koninkrijk België', @belgium.description
|
78
|
+
assert_equal 'Koninkrijk der Nederlanden', @netherlands.description
|
79
|
+
|
80
|
+
# change language to french
|
81
|
+
Country.language = 'fr'
|
82
|
+
assert_equal 'Belgique', @belgium.name
|
83
|
+
assert_equal 'Pays-Bas', @netherlands.name
|
84
|
+
assert_equal 'Royaume de Belgique', @belgium.description
|
85
|
+
assert_equal 'Royaume des Pays-Bas', @netherlands.description
|
86
|
+
|
87
|
+
# change language to english
|
88
|
+
Country.language = 'en'
|
89
|
+
assert_equal 'Belgium', @belgium.name
|
90
|
+
assert_equal 'Netherlands', @netherlands.name
|
91
|
+
assert_equal 'Kingdom of Belgium', @belgium.description
|
92
|
+
assert_equal 'Kingdom of the Netherlands', @netherlands.description
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_translated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stijn Mathysen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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
|
+
- stijn@skylight.be
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- acts_as_translated.gemspec
|
54
|
+
- lib/acts_as_translated.rb
|
55
|
+
- lib/acts_as_translated/version.rb
|
56
|
+
- tests/countries.yml
|
57
|
+
- tests/translated_test.rb
|
58
|
+
homepage: ''
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: This acts_as extension allows easy attribute translation.
|
82
|
+
test_files: []
|