simple_model_translations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +52 -0
- data/LICENSE +20 -0
- data/README.rdoc +55 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/lib/simple_model_translations.rb +10 -0
- data/lib/simple_model_translations/attributes.rb +21 -0
- data/lib/simple_model_translations/base.rb +60 -0
- data/lib/simple_model_translations/class_methods.rb +2 -0
- data/lib/simple_model_translations/instance_methods.rb +23 -0
- data/lib/simple_model_translations/validations.rb +23 -0
- data/simple_model_translations.gemspec +87 -0
- data/spec/attributes_spec.rb +97 -0
- data/spec/data/models.rb +16 -0
- data/spec/data/schema.rb +26 -0
- data/spec/simple_model_translations_spec.rb +31 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/validations_spec.rb +14 -0
- metadata +194 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.0)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
builder (2.1.2)
|
17
|
+
columnize (0.3.1)
|
18
|
+
database_cleaner (0.5.2)
|
19
|
+
diff-lcs (1.1.2)
|
20
|
+
factory_girl (1.3.2)
|
21
|
+
i18n (0.4.1)
|
22
|
+
linecache (0.43)
|
23
|
+
rspec (2.0.0)
|
24
|
+
rspec-core (= 2.0.0)
|
25
|
+
rspec-expectations (= 2.0.0)
|
26
|
+
rspec-mocks (= 2.0.0)
|
27
|
+
rspec-core (2.0.0)
|
28
|
+
rspec-expectations (2.0.0)
|
29
|
+
diff-lcs (>= 1.1.2)
|
30
|
+
rspec-mocks (2.0.0)
|
31
|
+
rspec-core (= 2.0.0)
|
32
|
+
rspec-expectations (= 2.0.0)
|
33
|
+
ruby-debug (0.10.3)
|
34
|
+
columnize (>= 0.1)
|
35
|
+
ruby-debug-base (~> 0.10.3.0)
|
36
|
+
ruby-debug-base (0.10.3)
|
37
|
+
linecache (>= 0.3)
|
38
|
+
shoulda (2.11.3)
|
39
|
+
sqlite3-ruby (1.3.1)
|
40
|
+
tzinfo (0.3.23)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
activerecord (>= 3.0.0)
|
47
|
+
database_cleaner
|
48
|
+
factory_girl
|
49
|
+
rspec (>= 2.0.0)
|
50
|
+
ruby-debug
|
51
|
+
shoulda
|
52
|
+
sqlite3-ruby
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Pavel Forkert
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
= SimpleModelTranslations
|
2
|
+
|
3
|
+
Yet another implementation of ActiveRecord translations. Created for zn.ua, because globalize3 was too unconfortable to use.
|
4
|
+
|
5
|
+
== Basic Usage
|
6
|
+
|
7
|
+
For example, you are dealing with a website for some magazine, and you want your articles to be translated.
|
8
|
+
So, you'll need the following models to achieve this behaviour:
|
9
|
+
|
10
|
+
class Article < ActiveRecord::Base
|
11
|
+
translates :name, :content
|
12
|
+
end
|
13
|
+
|
14
|
+
class ArticleTranslation < ActiveRecord::Base
|
15
|
+
translation_for :article
|
16
|
+
end
|
17
|
+
|
18
|
+
Also, you'll need a migration:
|
19
|
+
|
20
|
+
create_table(:article_translations) do |t|
|
21
|
+
t.references :article
|
22
|
+
t.string :locale
|
23
|
+
|
24
|
+
t.string :name
|
25
|
+
t.text :content
|
26
|
+
end
|
27
|
+
add_index :article_translations, [:article_id, :locale], :unique => true
|
28
|
+
|
29
|
+
Now you are able to translate values for the attributes :title and :description per locale:
|
30
|
+
|
31
|
+
I18n.locale = :en
|
32
|
+
article = Article.new(:name => 'Translations are so simple!')
|
33
|
+
I18n.locale = :uk
|
34
|
+
article.name = 'Hello, from Ukraine!'
|
35
|
+
|
36
|
+
I18n.locale = :en
|
37
|
+
article.name #=> 'Translations are so simple!'
|
38
|
+
I18n.locale = :uk
|
39
|
+
article.name #=> 'Hello, from Ukraine!'
|
40
|
+
|
41
|
+
Additional features can be discovered by searching the code and specs. Documentation is not available yet. I hope, it'll be done when releasing zn.ua. :)
|
42
|
+
|
43
|
+
== Note on Patches/Pull Requests
|
44
|
+
|
45
|
+
* Fork the project.
|
46
|
+
* Make your feature addition or bug fix.
|
47
|
+
* Add tests for it. This is important so I don't break it in a
|
48
|
+
future version unintentionally.
|
49
|
+
* Commit, do not mess with rakefile, version, or history.
|
50
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
51
|
+
* Send me a pull request. Bonus points for topic branches.
|
52
|
+
|
53
|
+
== Copyright
|
54
|
+
|
55
|
+
Copyright (c) 2010 Pavel Forkert. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "simple_model_translations"
|
8
|
+
gem.summary = %Q{Simple ActiveRecord translations for Rails 3}
|
9
|
+
gem.description = %Q{Simple ActiveRecord translations for Rails 3}
|
10
|
+
gem.email = "fxposter@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/fxposter/simple_model_translations"
|
12
|
+
gem.authors = ["Pavel Forkert"]
|
13
|
+
gem.add_dependency "activerecord", '>= 3.0.0'
|
14
|
+
|
15
|
+
gem.add_development_dependency 'database_cleaner'
|
16
|
+
gem.add_development_dependency 'ruby-debug'
|
17
|
+
gem.add_development_dependency 'sqlite3-ruby'
|
18
|
+
gem.add_development_dependency 'shoulda'
|
19
|
+
gem.add_development_dependency 'factory_girl'
|
20
|
+
gem.add_development_dependency 'rspec', '>= 2.0.0'
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "simple_model_translations #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'simple_model_translations/base'
|
2
|
+
require 'simple_model_translations/class_methods'
|
3
|
+
require 'simple_model_translations/instance_methods'
|
4
|
+
require 'simple_model_translations/attributes'
|
5
|
+
require 'simple_model_translations/validations'
|
6
|
+
|
7
|
+
ActiveRecord::Base.extend SimpleModelTranslations::Base
|
8
|
+
ActiveRecord::Base.extend SimpleModelTranslations::Validations
|
9
|
+
|
10
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SimpleModelTranslations
|
2
|
+
module Attributes
|
3
|
+
def attributes=(attributes)
|
4
|
+
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
5
|
+
super(attributes)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def update_attributes!(attributes)
|
10
|
+
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
11
|
+
super(attributes)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def update_attributes(attributes)
|
16
|
+
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
17
|
+
super(attributes)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module SimpleModelTranslations
|
2
|
+
|
3
|
+
module Base
|
4
|
+
# Configure translation model dependency.
|
5
|
+
# Eg:
|
6
|
+
# class PostTranslation < ActiveRecord::Base
|
7
|
+
# translation_for :post
|
8
|
+
# end
|
9
|
+
def translation_for(model)
|
10
|
+
belongs_to model
|
11
|
+
validates_presence_of :locale
|
12
|
+
validates_uniqueness_of :locale, :scope => "#{model}_id"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Configure translated attributes.
|
16
|
+
# Eg:
|
17
|
+
# class Post < ActiveRecord::Base
|
18
|
+
# translates :title, :description
|
19
|
+
# end
|
20
|
+
def translates(*attributes)
|
21
|
+
configure_translations(attributes.extract_options!)
|
22
|
+
|
23
|
+
attributes.each do |attribute|
|
24
|
+
# attribute setter
|
25
|
+
define_method "#{attribute}=" do |value|
|
26
|
+
translation = find_or_build_translation_by_locale(I18n.locale)
|
27
|
+
translation.send("#{attribute}=", value)
|
28
|
+
end
|
29
|
+
|
30
|
+
# attribute getter
|
31
|
+
define_method attribute do
|
32
|
+
translation = find_translation_by_locale(current_locale_for_translation) ||
|
33
|
+
find_translation_by_locale(default_locale_for_translation)
|
34
|
+
|
35
|
+
translation ? translation.send(attribute) : nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
# configure model
|
42
|
+
def configure_translations(options)
|
43
|
+
raise 'You can call #translates method only once per model!' if included_modules.include?(SimpleModelTranslations::InstanceMethods)
|
44
|
+
|
45
|
+
include SimpleModelTranslations::InstanceMethods
|
46
|
+
include SimpleModelTranslations::Attributes
|
47
|
+
|
48
|
+
class_name = options[:class_name] || "#{self.name}Translation"
|
49
|
+
# unless Kernel.const_get(class_name)
|
50
|
+
# klass = Kernel.const_set(class_name.to_sym, Class.new(ActiveRecord::Base))
|
51
|
+
# klass.translation_for(self.name.underscore.to_sym)
|
52
|
+
# end
|
53
|
+
has_many :translations, :class_name => class_name, :dependent => :destroy, :order => "created_at DESC", :autosave => true
|
54
|
+
|
55
|
+
if options[:accepts_nested_attributes]
|
56
|
+
accepts_nested_attributes_for :translations, :allow_destroy => true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SimpleModelTranslations
|
2
|
+
module InstanceMethods
|
3
|
+
def find_translation_by_locale(locale)
|
4
|
+
translations.detect { |t| t.locale.to_sym == locale }
|
5
|
+
end
|
6
|
+
|
7
|
+
def find_or_build_translation_by_locale(locale)
|
8
|
+
find_translation_by_locale(locale) || build_translation_for_locale(locale)
|
9
|
+
end
|
10
|
+
|
11
|
+
def build_translation_for_locale(locale)
|
12
|
+
translations.build(:locale => locale)
|
13
|
+
end
|
14
|
+
|
15
|
+
def current_locale_for_translation
|
16
|
+
I18n.locale
|
17
|
+
end
|
18
|
+
|
19
|
+
def default_locale_for_translation
|
20
|
+
I18n.default_locale
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SimpleModelTranslations
|
2
|
+
module Validations
|
3
|
+
class TranslationsValidator < ActiveModel::Validator
|
4
|
+
def validate(record)
|
5
|
+
locales = options[:locales]
|
6
|
+
locales = [locales] unless locales.respond_to?(:each)
|
7
|
+
locales.each do |locale|
|
8
|
+
unless record.find_translation_by_locale(locale)
|
9
|
+
record.errors.add(:translations, "miss #{locale} translation")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_translations(*locales)
|
16
|
+
validates_with TranslationsValidator, :locales => locales
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_translation(locale)
|
20
|
+
validate_translations locale
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simple_model_translations}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Pavel Forkert"]
|
12
|
+
s.date = %q{2010-10-14}
|
13
|
+
s.description = %q{Simple ActiveRecord translations for Rails 3}
|
14
|
+
s.email = %q{fxposter@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/simple_model_translations.rb",
|
29
|
+
"lib/simple_model_translations/attributes.rb",
|
30
|
+
"lib/simple_model_translations/base.rb",
|
31
|
+
"lib/simple_model_translations/class_methods.rb",
|
32
|
+
"lib/simple_model_translations/instance_methods.rb",
|
33
|
+
"lib/simple_model_translations/validations.rb",
|
34
|
+
"simple_model_translations.gemspec",
|
35
|
+
"spec/attributes_spec.rb",
|
36
|
+
"spec/data/models.rb",
|
37
|
+
"spec/data/schema.rb",
|
38
|
+
"spec/simple_model_translations_spec.rb",
|
39
|
+
"spec/spec_helper.rb",
|
40
|
+
"spec/validations_spec.rb"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/fxposter/simple_model_translations}
|
43
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.7}
|
46
|
+
s.summary = %q{Simple ActiveRecord translations for Rails 3}
|
47
|
+
s.test_files = [
|
48
|
+
"spec/attributes_spec.rb",
|
49
|
+
"spec/data/models.rb",
|
50
|
+
"spec/data/schema.rb",
|
51
|
+
"spec/simple_model_translations_spec.rb",
|
52
|
+
"spec/spec_helper.rb",
|
53
|
+
"spec/validations_spec.rb"
|
54
|
+
]
|
55
|
+
|
56
|
+
if s.respond_to? :specification_version then
|
57
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
61
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
62
|
+
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
63
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
64
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
65
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
66
|
+
s.add_development_dependency(%q<factory_girl>, [">= 0"])
|
67
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
70
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
71
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
72
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
73
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
74
|
+
s.add_dependency(%q<factory_girl>, [">= 0"])
|
75
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
76
|
+
end
|
77
|
+
else
|
78
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
79
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
80
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
81
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
82
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
83
|
+
s.add_dependency(%q<factory_girl>, [">= 0"])
|
84
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Article do
|
4
|
+
describe '#create with locale' do
|
5
|
+
before do
|
6
|
+
@article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World', :locale => :en)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should create corresponding translation' do
|
10
|
+
@article.should have_translation(:en)
|
11
|
+
@article.translations.count.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should assign attributes to translation' do
|
15
|
+
@article.should have_translated_attribute(:en, :name, 'Hello')
|
16
|
+
@article.should have_translated_attribute(:en, :content, 'World')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should assign non-translated attributes to model itself' do
|
20
|
+
@article.slug.should == '__hello__'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#create without locale' do
|
25
|
+
before do
|
26
|
+
I18n.locale = :ru
|
27
|
+
@article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should create corresponding translation' do
|
31
|
+
@article.should have_translation(:ru)
|
32
|
+
@article.translations.count.should == 1
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should assign attributes to translation' do
|
36
|
+
@article.should have_translated_attribute(:ru, :name, 'Hello')
|
37
|
+
@article.should have_translated_attribute(:ru, :content, 'World')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should assign non-translated attributes to model itself' do
|
41
|
+
@article.slug.should == '__hello__'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#update_attributes' do
|
46
|
+
before do
|
47
|
+
I18n.locale = :ru
|
48
|
+
@article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
49
|
+
@article.update_attributes! :locale => :en, :name => 'Hello in English'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should create corresponding translation' do
|
53
|
+
@article.should have_translation(:ru)
|
54
|
+
@article.should have_translation(:en)
|
55
|
+
@article.translations.length.should == 2
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should assign attributes to translation' do
|
59
|
+
@article.should have_translated_attribute(:ru, :name, 'Hello')
|
60
|
+
@article.should have_translated_attribute(:en, :name, 'Hello in English')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#attributes=' do
|
65
|
+
before do
|
66
|
+
I18n.locale = :ru
|
67
|
+
@article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
68
|
+
@article.attributes = { :locale => :en, :name => 'Hello in English' }
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should create corresponding translation' do
|
72
|
+
@article.should have_translation(:ru)
|
73
|
+
@article.should have_translation(:en)
|
74
|
+
@article.translations.length.should == 2
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should assign attributes to translation' do
|
78
|
+
@article.should have_translated_attribute(:ru, :name, 'Hello')
|
79
|
+
@article.should have_translated_attribute(:en, :name, 'Hello in English')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should proxy #attribute= methods to translations' do
|
84
|
+
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
85
|
+
article.name = 'Hi'
|
86
|
+
article.should have_translated_attribute(:ru, :name, 'Hi')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should proxy #attribute methods to translations' do
|
90
|
+
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
91
|
+
article.name.should == 'Hello'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe ArticleTranslation do
|
96
|
+
it { should belong_to :article }
|
97
|
+
end
|
data/spec/data/models.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class Article < ActiveRecord::Base
|
2
|
+
translates :name, :content, :class_name => 'ArticleTranslation'
|
3
|
+
end
|
4
|
+
|
5
|
+
class ArticleTranslation < ActiveRecord::Base
|
6
|
+
translation_for :article
|
7
|
+
end
|
8
|
+
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
translates :name, :content
|
11
|
+
validate_translations :en, :ru
|
12
|
+
end
|
13
|
+
|
14
|
+
class PostTranslation < ActiveRecord::Base
|
15
|
+
translation_for :post
|
16
|
+
end
|
data/spec/data/schema.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
ActiveRecord::Migration.verbose = false
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define do
|
4
|
+
create_table :articles, :force => true do |t|
|
5
|
+
t.string :slug
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :article_translations, :force => true do |t|
|
9
|
+
t.string :locale
|
10
|
+
t.references :article
|
11
|
+
t.string :name
|
12
|
+
t.text :content
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :posts, :force => true do |t|
|
16
|
+
t.string :slug
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :post_translations, :force => true do |t|
|
20
|
+
t.string :locale
|
21
|
+
t.references :post
|
22
|
+
t.string :name
|
23
|
+
t.text :content
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Article do
|
4
|
+
it { should have_many :translations }
|
5
|
+
|
6
|
+
it "should have empty translations for a new record" do
|
7
|
+
Article.new.translations.should be_empty
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should destroy dependent locales' do
|
11
|
+
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
12
|
+
expect do
|
13
|
+
article.destroy
|
14
|
+
end.to change(ArticleTranslation, :count).by(-1)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should use I18n fallbacks' do
|
18
|
+
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
19
|
+
article.update_attributes! :locale => :en, :name => 'Hello in English'
|
20
|
+
I18n.locale = :en
|
21
|
+
article.name.should == 'Hello in English'
|
22
|
+
I18n.locale = :ru
|
23
|
+
article.name.should == 'Hello'
|
24
|
+
I18n.locale = :de
|
25
|
+
article.name.should == 'Hello'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ArticleTranslation do
|
30
|
+
it { should belong_to :article }
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.require(:default, :test)
|
5
|
+
|
6
|
+
require 'rspec'
|
7
|
+
require 'shoulda'
|
8
|
+
require 'factory_girl'
|
9
|
+
require 'active_record'
|
10
|
+
require 'simple_model_translations'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
13
|
+
DatabaseCleaner.strategy = :transaction
|
14
|
+
|
15
|
+
require 'spec/data/schema'
|
16
|
+
require 'spec/data/models'
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.before do
|
20
|
+
I18n.locale = :ru
|
21
|
+
I18n.default_locale = :ru
|
22
|
+
DatabaseCleaner.start
|
23
|
+
end
|
24
|
+
config.after do
|
25
|
+
DatabaseCleaner.clean
|
26
|
+
end
|
27
|
+
|
28
|
+
config.mock_with :rspec
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec::Matchers.define :have_translation do |locale|
|
32
|
+
match do |record|
|
33
|
+
!record.find_translation_by_locale(locale).nil?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
RSpec::Matchers.define :have_translated_attribute do |locale, attribute, value|
|
38
|
+
match do |record|
|
39
|
+
record.find_translation_by_locale(locale).send(attribute) == value
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Post do
|
4
|
+
it 'should be valid with both :ru and :en translations' do
|
5
|
+
post = Post.new(:locale => :ru, :name => 'Privet')
|
6
|
+
post.attributes = { :locale => :en, :name => 'Hello' }
|
7
|
+
post.should be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be invalid with only :ru translation' do
|
11
|
+
post = Post.new(:locale => :ru, :name => 'Privet')
|
12
|
+
post.should_not be_valid
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_model_translations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pavel Forkert
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-14 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: database_cleaner
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: ruby-debug
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: sqlite3-ruby
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: shoulda
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: factory_girl
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
107
|
+
- !ruby/object:Gem::Dependency
|
108
|
+
name: rspec
|
109
|
+
prerelease: false
|
110
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 15
|
116
|
+
segments:
|
117
|
+
- 2
|
118
|
+
- 0
|
119
|
+
- 0
|
120
|
+
version: 2.0.0
|
121
|
+
type: :development
|
122
|
+
version_requirements: *id007
|
123
|
+
description: Simple ActiveRecord translations for Rails 3
|
124
|
+
email: fxposter@gmail.com
|
125
|
+
executables: []
|
126
|
+
|
127
|
+
extensions: []
|
128
|
+
|
129
|
+
extra_rdoc_files:
|
130
|
+
- LICENSE
|
131
|
+
- README.rdoc
|
132
|
+
files:
|
133
|
+
- .document
|
134
|
+
- .gitignore
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE
|
138
|
+
- README.rdoc
|
139
|
+
- Rakefile
|
140
|
+
- VERSION
|
141
|
+
- lib/simple_model_translations.rb
|
142
|
+
- lib/simple_model_translations/attributes.rb
|
143
|
+
- lib/simple_model_translations/base.rb
|
144
|
+
- lib/simple_model_translations/class_methods.rb
|
145
|
+
- lib/simple_model_translations/instance_methods.rb
|
146
|
+
- lib/simple_model_translations/validations.rb
|
147
|
+
- simple_model_translations.gemspec
|
148
|
+
- spec/attributes_spec.rb
|
149
|
+
- spec/data/models.rb
|
150
|
+
- spec/data/schema.rb
|
151
|
+
- spec/simple_model_translations_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
- spec/validations_spec.rb
|
154
|
+
has_rdoc: true
|
155
|
+
homepage: http://github.com/fxposter/simple_model_translations
|
156
|
+
licenses: []
|
157
|
+
|
158
|
+
post_install_message:
|
159
|
+
rdoc_options:
|
160
|
+
- --charset=UTF-8
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
none: false
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
hash: 3
|
169
|
+
segments:
|
170
|
+
- 0
|
171
|
+
version: "0"
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
hash: 3
|
178
|
+
segments:
|
179
|
+
- 0
|
180
|
+
version: "0"
|
181
|
+
requirements: []
|
182
|
+
|
183
|
+
rubyforge_project:
|
184
|
+
rubygems_version: 1.3.7
|
185
|
+
signing_key:
|
186
|
+
specification_version: 3
|
187
|
+
summary: Simple ActiveRecord translations for Rails 3
|
188
|
+
test_files:
|
189
|
+
- spec/attributes_spec.rb
|
190
|
+
- spec/data/models.rb
|
191
|
+
- spec/data/schema.rb
|
192
|
+
- spec/simple_model_translations_spec.rb
|
193
|
+
- spec/spec_helper.rb
|
194
|
+
- spec/validations_spec.rb
|