translatable 0.3.1 → 1.2.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 +15 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +1 -1
- data/Gemfile +6 -8
- data/Gemfile.lock +110 -103
- data/README.rdoc +30 -46
- data/VERSION +1 -1
- data/lib/generators/translatable/model_generator.rb +11 -7
- data/lib/generators/translatable/translation_generator.rb +1 -1
- data/lib/translatable.rb +12 -10
- data/lib/translatable/base.rb +74 -0
- data/lib/translatable/orm/active_record.rb +156 -0
- data/test/cases/{translatable_test.rb → active_record_test.rb} +142 -90
- data/test/cases/base_test.rb +104 -0
- data/test/generators/model_generator_test.rb +27 -15
- data/test/generators/translation_generator_test.rb +5 -5
- data/test/support/active_record.rb +1 -0
- data/test/support/models/messages.rb +13 -14
- data/test/support/models/news.rb +7 -9
- data/test/support/models/posts.rb +10 -10
- data/test/test_helper.rb +1 -17
- data/translatable.gemspec +15 -11
- metadata +41 -27
- data/.document +0 -5
- data/lib/translatable/active_record.rb +0 -254
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
1.2.2
|
@@ -27,18 +27,22 @@ module Translatable
|
|
27
27
|
def generate_translatable_block
|
28
28
|
block = " translatable do"
|
29
29
|
attributes.each do |attr|
|
30
|
-
block << "\n
|
30
|
+
block << "\n field :#{attr.name}, :presence => true#, :uniqueness => true"
|
31
31
|
end
|
32
32
|
block << (options[:translated_model].nil? ?
|
33
|
-
"\n #
|
34
|
-
"\n
|
33
|
+
"\n #class_name 'Translated#{class_name}'" :
|
34
|
+
"\n class_name '#{options[:translated_model]}'")
|
35
35
|
block << (options[:origin].nil? ?
|
36
|
-
"\n #
|
37
|
-
"\n
|
36
|
+
"\n #reflection_name :#{singular_table_name}" :
|
37
|
+
"\n reflection_name :#{options[:origin]}")
|
38
|
+
block << "\n #foreign_key :origin_id"
|
38
39
|
block << (options[:locale].nil? ?
|
39
|
-
"\n #
|
40
|
-
"\n
|
40
|
+
"\n #locale_key :locale" :
|
41
|
+
"\n locale_key :#{options[:locale]}")
|
41
42
|
block << "\n end\n"
|
43
|
+
|
44
|
+
block << "\n #accepts_nested_attributes_for :translations, :current_translation"
|
45
|
+
block << "\n #attr_accessible :translations_attributes, :current_translation_attributes\n"
|
42
46
|
end
|
43
47
|
end
|
44
48
|
end
|
@@ -7,7 +7,7 @@ module Translatable
|
|
7
7
|
|
8
8
|
desc "Creates ActiveRecord for translation"
|
9
9
|
|
10
|
-
class_option :prefix, :type => :string, :default => "
|
10
|
+
class_option :prefix, :type => :string, :default => "translated", :desc => "Specifies the prefix to used tof translation dealer (Default: translatable)"
|
11
11
|
class_option :origin, :type => :string, :default => "origin", :desc => "Specifies the association name to be use for origin (Default: origin)"
|
12
12
|
class_option :locale, :type => :string, :default => "locale", :desc => "Specifies the column to be use for locale (Default: locale)"
|
13
13
|
|
data/lib/translatable.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Translatable
|
2
|
+
end
|
3
|
+
|
4
|
+
require "translatable/base"
|
5
|
+
|
6
|
+
unless Gem::Specification.find_all_by_name("rails").empty?
|
4
7
|
require 'translatable/engine'
|
5
8
|
|
6
9
|
ActiveSupport.on_load(:active_record) do
|
7
|
-
|
10
|
+
require 'translatable/orm/active_record'
|
11
|
+
ActiveRecord::Base.extend Translatable::ActiveRecord
|
8
12
|
end
|
9
13
|
else
|
10
|
-
|
14
|
+
if Gem::Specification.find_by_name("active_record")
|
11
15
|
require 'active_record'
|
12
|
-
require '
|
13
|
-
|
16
|
+
require 'translatable/orm/active_record'
|
17
|
+
|
14
18
|
ActiveRecord::Base.extend Translatable::ActiveRecord
|
15
|
-
rescue LoadError
|
16
|
-
$stderr.puts "Warning: Translatable is not loaded"
|
17
19
|
end
|
18
|
-
end
|
20
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Translatable
|
2
|
+
class Base
|
3
|
+
attr_reader :fields, :translation_model, :origin_key, :origin_reflection_name
|
4
|
+
|
5
|
+
def initialize origin_model
|
6
|
+
@origin_model = origin_model
|
7
|
+
end
|
8
|
+
|
9
|
+
# API
|
10
|
+
def field(*args)
|
11
|
+
(@fields ||= []) << set_mapping(*args)
|
12
|
+
end
|
13
|
+
|
14
|
+
def class_name(model_name)
|
15
|
+
@translation_model = prepare_model model_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def foreign_key(key_name)
|
19
|
+
@origin_key = key_name.to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
def reflection_name(reflection)
|
23
|
+
@origin_reflection_name = reflection.to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
def locale_key(key_name, opts = {})
|
27
|
+
set_mapping key_name, opts
|
28
|
+
@locale_column = key_name.to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
# ACCESS
|
32
|
+
def translation_model
|
33
|
+
@translation_model || (@translation_model = prepare_model)
|
34
|
+
end
|
35
|
+
alias_method :t_model, :translation_model
|
36
|
+
|
37
|
+
def origin_key
|
38
|
+
@origin_key || (@origin_key = :origin_id)
|
39
|
+
end
|
40
|
+
|
41
|
+
def origin_reflection_name
|
42
|
+
@origin_reflection_name || (@origin_reflection_name = :origin)
|
43
|
+
end
|
44
|
+
alias_method :or_name, :origin_reflection_name
|
45
|
+
|
46
|
+
def locale_column
|
47
|
+
@locale_column || (@locale_column = :locale)
|
48
|
+
end
|
49
|
+
|
50
|
+
def mapping
|
51
|
+
unless @mapping_defined
|
52
|
+
(@mapping ||= {})[locale_column] ||= :locale
|
53
|
+
@mapping_defined = true
|
54
|
+
end
|
55
|
+
@mapping || {}
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def set_mapping attribute, options = {}
|
61
|
+
(@mapping ||= {})[attribute.to_sym] = options.delete(:as) || attribute rescue attribute
|
62
|
+
return attribute, options
|
63
|
+
end
|
64
|
+
|
65
|
+
def origin_class_name
|
66
|
+
@origin_model.name
|
67
|
+
end
|
68
|
+
|
69
|
+
def prepare_model(model_name = nil)
|
70
|
+
model_constant = model_name ? model_name.to_s.camelize : "Translated#{origin_class_name}"
|
71
|
+
model_constant.constantize
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module Translatable
|
2
|
+
###
|
3
|
+
# In order to made the model Translatable, an additional fields should
|
4
|
+
# should be added first to it. Here is an example of it might be implemented:
|
5
|
+
#
|
6
|
+
# Examples:
|
7
|
+
#
|
8
|
+
# class Author < ActiveRecord::Base
|
9
|
+
# validates :name, :presence => true
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# class TranslatedNews < ActiveRecord::Base #
|
13
|
+
# attr_accessible :title, :content
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# class News < ActiveRecord::Base
|
17
|
+
#
|
18
|
+
# belongs_to :author
|
19
|
+
#
|
20
|
+
# translatable do
|
21
|
+
# field :title, :presence => true, :uniqueness => true
|
22
|
+
# field :content, :presence => true
|
23
|
+
# class_name "TranslatedNews"
|
24
|
+
# foreign_key :origin_id
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# accepts_nested_attributes_for :translations, :current_translation
|
28
|
+
# attr_accessible :translations_attributes, :current_translation_attributes
|
29
|
+
# attr_accessible :author_id, :author
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# An example of application:
|
33
|
+
#
|
34
|
+
# news = News.create :translations_attributes => [{title: "Resent News", content: "That is where the text goes", locale: "en"}]
|
35
|
+
# news.translations.create title: "Заголовок", content: "Содержание",locale: "ru"
|
36
|
+
#
|
37
|
+
# news.content
|
38
|
+
# # => "That is where the text goes"
|
39
|
+
#
|
40
|
+
# news.set_current_translation :ru
|
41
|
+
# news.content
|
42
|
+
# # => "Сюди идет текст"
|
43
|
+
#
|
44
|
+
# news.set_current_translation :de
|
45
|
+
# news.content
|
46
|
+
# # => nil
|
47
|
+
#
|
48
|
+
# news.set_current_translation
|
49
|
+
# news.content
|
50
|
+
# # => "That is where the text goes"
|
51
|
+
#
|
52
|
+
module ActiveRecord
|
53
|
+
|
54
|
+
def translatable(&block)
|
55
|
+
extend Translatable::ActiveRecord::ClassMethods
|
56
|
+
include Translatable::ActiveRecord::InstanceMethods
|
57
|
+
|
58
|
+
@translatable_base = Translatable::Base.new(self)
|
59
|
+
|
60
|
+
@translatable_base.instance_eval(&block)
|
61
|
+
t_register_origin
|
62
|
+
t_register_translations
|
63
|
+
t_register_locale
|
64
|
+
end
|
65
|
+
|
66
|
+
module ClassMethods
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
###
|
71
|
+
# Handle the routine to define all th required stuff on the original maodel
|
72
|
+
def t_register_origin
|
73
|
+
has_many :translations,
|
74
|
+
:class_name => @translatable_base.translation_model.to_s,
|
75
|
+
:foreign_key => @translatable_base.origin_key,
|
76
|
+
:inverse_of => @translatable_base.or_name,
|
77
|
+
:dependent => :destroy
|
78
|
+
|
79
|
+
class_eval <<-EOS
|
80
|
+
has_one :current_translation, -> { where("#{reflection_locale}" => ::I18n.locale) },
|
81
|
+
:class_name => @translatable_base.translation_model.to_s,
|
82
|
+
:foreign_key => @translatable_base.origin_key
|
83
|
+
EOS
|
84
|
+
end
|
85
|
+
|
86
|
+
def t_register_translations
|
87
|
+
@translatable_base.tap do |t|
|
88
|
+
t.t_model.validates t.locale_column, :presence => true
|
89
|
+
t.t_model.validates t.locale_column, :uniqueness => { :scope => t.origin_key }
|
90
|
+
t.t_model.validates t.locale_column, :format => {:with => /[a-z]{2}/}, :unless => Proc.new {
|
91
|
+
|record| record.public_send(t.locale_column).blank?
|
92
|
+
}
|
93
|
+
|
94
|
+
t.t_model.belongs_to t.or_name, :class_name => self.name, :inverse_of => :translations
|
95
|
+
|
96
|
+
t.fields.each do |f|
|
97
|
+
t.t_model.validates(f.first, f.last) unless f[1].blank?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def t_register_locale
|
103
|
+
@translatable_base.mapping.each_pair do |attr, attr_alias|
|
104
|
+
self.instance_eval do
|
105
|
+
define_method attr_alias do
|
106
|
+
current_translation.try(attr)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def reflection_locale
|
113
|
+
@translatable_base.locale_column
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
module InstanceMethods
|
118
|
+
def other_translations(reload = false)
|
119
|
+
@other_translations = nil if reload
|
120
|
+
@other_translations ||= begin
|
121
|
+
unless association(:current_translation).loaded?
|
122
|
+
translations.to_a.reject {|t|t.send(t_locale_column) == ::I18n.locale.to_s}
|
123
|
+
else
|
124
|
+
translations - [current_translation]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def t(locale)
|
130
|
+
translations.select { |t| t.send(t_locale_column) == locale.to_s }.first
|
131
|
+
end
|
132
|
+
|
133
|
+
def with_locale(locale, &block)
|
134
|
+
begin
|
135
|
+
set_current_translation locale.to_sym
|
136
|
+
result = block.arity > 0 ? block.call(self) : yield
|
137
|
+
ensure
|
138
|
+
set_current_translation
|
139
|
+
end
|
140
|
+
result
|
141
|
+
end
|
142
|
+
|
143
|
+
def t_set_current(locale = ::I18n.locale)
|
144
|
+
translations.load_target unless translations.loaded?
|
145
|
+
association(:current_translation).target = t(locale.to_s)
|
146
|
+
end
|
147
|
+
alias_method :set_current_translation, :t_set_current
|
148
|
+
|
149
|
+
protected
|
150
|
+
|
151
|
+
def t_locale_column
|
152
|
+
self.class.send(:reflection_locale)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
@@ -1,70 +1,98 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
require 'active_record'
|
2
3
|
require 'test_helper'
|
3
4
|
require 'support/models/news'
|
4
5
|
require 'support/models/posts'
|
5
6
|
require 'support/models/messages'
|
6
7
|
|
7
|
-
class
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
class InstanceActiveRecordTest < Test::Unit::TestCase
|
9
|
+
teardown do
|
10
|
+
::I18n.locale = ::I18n.default_locale
|
11
|
+
end
|
11
12
|
|
12
|
-
context "
|
13
|
-
|
14
|
-
|
13
|
+
#context "Instance" do
|
14
|
+
test "should Respond to translatable methods" do
|
15
|
+
news = News.new
|
15
16
|
|
16
|
-
|
17
|
-
assert
|
17
|
+
assert news.respond_to?(:title), "title methods is missing for News instance"
|
18
|
+
assert news.respond_to?(:content), "content methods is missing for News instance"
|
19
|
+
assert news.respond_to?(:locale), "locale methods is missing for News instance"
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
22
|
+
test "should Creates without translation" do
|
23
|
+
news = News.create
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
assert_equal ::TranslatableNews, News.send(:translatable_model_prepared, ::TranslatableNews)
|
25
|
+
assert news.persisted?
|
26
|
+
assert_nil TranslatedNews.last
|
28
27
|
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
test "should Change current translation" do
|
30
|
+
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
31
|
+
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
33
32
|
|
34
|
-
|
35
|
-
assert_equal
|
33
|
+
assert news.persisted?
|
34
|
+
assert_equal TranslatedNews.last, news.current_translation
|
35
|
+
assert_equal "en", news.current_translation.locale
|
36
|
+
|
37
|
+
news.set_current_translation :ru
|
38
|
+
assert_equal TranslatedNews.first, news.current_translation
|
39
|
+
assert_equal "ru", news.current_translation.locale
|
36
40
|
end
|
37
|
-
end
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
news = News.
|
42
|
+
test "should Evaluate under translation" do
|
43
|
+
test_runner = self
|
44
|
+
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
45
|
+
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
42
46
|
|
43
|
-
|
44
|
-
|
47
|
+
assert_equal "en", news.current_translation.locale
|
48
|
+
|
49
|
+
news.with_locale(:ru) do
|
50
|
+
test_runner.assert_equal "ru", news.current_translation.locale
|
51
|
+
end
|
52
|
+
assert_equal "en", news.current_translation.locale
|
53
|
+
|
54
|
+
news.with_locale(:ru) do |n|
|
55
|
+
test_runner.assert_equal "ru", n.current_translation.locale
|
56
|
+
end
|
57
|
+
assert_equal "en", news.current_translation.locale
|
45
58
|
end
|
46
59
|
|
47
|
-
|
48
|
-
news = News.create
|
60
|
+
test "should Shortcut translation" do
|
61
|
+
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
62
|
+
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
49
63
|
|
50
|
-
|
51
|
-
|
64
|
+
assert_equal TranslatedNews.last, news.t(:en)
|
65
|
+
assert_equal "en", news.t(:en).locale
|
66
|
+
|
67
|
+
assert_equal TranslatedNews.first, news.t(:ru)
|
68
|
+
assert_equal "ru", news.t(:ru).locale
|
52
69
|
end
|
53
70
|
|
54
|
-
should
|
71
|
+
test "should Have no other translation" do
|
55
72
|
news = News.create :translations_attributes => [{ :title => "Заголовок", :content => "Содержание", :locale => "ru"}]
|
56
73
|
|
57
74
|
assert news.persisted?
|
58
75
|
|
59
|
-
t_news =
|
76
|
+
t_news = TranslatedNews.last
|
60
77
|
assert_equal [t_news], news.other_translations
|
61
78
|
|
62
79
|
news.set_current_translation :ru
|
63
80
|
|
64
|
-
assert_equal [], news.other_translations
|
81
|
+
assert_equal [], news.other_translations(true)
|
82
|
+
end
|
83
|
+
|
84
|
+
test "should Have other translation" do
|
85
|
+
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
86
|
+
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
87
|
+
|
88
|
+
assert news.persisted?
|
89
|
+
assert_equal [TranslatedNews.first], news.other_translations
|
90
|
+
|
91
|
+
news.set_current_translation :ru
|
92
|
+
assert_equal [TranslatedNews.last], news.other_translations(true)
|
65
93
|
end
|
66
94
|
|
67
|
-
should
|
95
|
+
test "should Provide errors on creation" do
|
68
96
|
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
69
97
|
{:title => "Resent News", :content => "That is where the text goes", :locale => ""}]
|
70
98
|
|
@@ -76,73 +104,91 @@ class TranslatableTest < Test::Unit::TestCase
|
|
76
104
|
assert t.new_record?
|
77
105
|
end
|
78
106
|
end
|
107
|
+
#end
|
108
|
+
end
|
109
|
+
|
110
|
+
class TranslatableInstanceActiveRecordTest < Test::Unit::TestCase
|
111
|
+
teardown do
|
112
|
+
::I18n.locale = ::I18n.default_locale
|
79
113
|
end
|
80
114
|
|
81
|
-
context "Translatable instance" do
|
82
|
-
should
|
83
|
-
news =
|
115
|
+
#context "Translatable instance" do
|
116
|
+
test "should Respond to translatable methods" do
|
117
|
+
news = TranslatedNews.new
|
84
118
|
|
85
|
-
assert news.respond_to?(:title), "Title method is missing for
|
86
|
-
assert news.respond_to?(:content), "Content method is missing for
|
119
|
+
assert news.respond_to?(:title), "Title method is missing for TranslatedNews instance"
|
120
|
+
assert news.respond_to?(:content), "Content method is missing for TranslatedNews instance"
|
87
121
|
end
|
88
122
|
|
89
|
-
should
|
90
|
-
news =
|
123
|
+
test "should Respond to methods related to origin" do
|
124
|
+
news = TranslatedNews.new
|
91
125
|
|
92
|
-
assert news.respond_to?(:locale), "Locale method is missing for
|
93
|
-
assert news.respond_to?(:origin_id), "Origin methods is missing for
|
94
|
-
assert news.respond_to?(:origin), "Origin methods is missing for
|
126
|
+
assert news.respond_to?(:locale), "Locale method is missing for TranslatedNews instance"
|
127
|
+
assert news.respond_to?(:origin_id), "Origin methods is missing for TranslatedNews instance"
|
128
|
+
assert news.respond_to?(:origin), "Origin methods is missing for TranslatedNews instance"
|
95
129
|
end
|
96
|
-
end
|
130
|
+
#end
|
131
|
+
end
|
97
132
|
|
98
|
-
|
99
|
-
|
133
|
+
class CreationActiveRecordTest < Test::Unit::TestCase
|
134
|
+
teardown do
|
135
|
+
::I18n.locale = ::I18n.default_locale
|
136
|
+
end
|
137
|
+
|
138
|
+
#context "Creation with translation" do
|
139
|
+
test "should Assign to origin" do
|
100
140
|
news = News.create
|
101
|
-
t_news =
|
141
|
+
t_news = TranslatedNews.create :title => "Заголовок", :content => "Содержание", :locale => "ru", :origin_id => news.id
|
102
142
|
|
103
143
|
assert t_news.persisted?
|
104
144
|
|
105
|
-
t_news =
|
145
|
+
t_news = TranslatedNews.last
|
106
146
|
assert_equal news.id, t_news.origin_id
|
107
147
|
assert_equal "Заголовок", t_news.title
|
108
148
|
assert_equal "Содержание", t_news.content
|
109
149
|
assert_equal "ru", t_news.locale
|
110
150
|
end
|
111
151
|
|
112
|
-
should
|
152
|
+
test "should Create translation on origin creation" do
|
113
153
|
news = News.create :translations_attributes => [{ :title => "Заголовок", :content => "Содержание", :locale => "ru"}]
|
114
154
|
|
115
155
|
assert news.persisted?
|
116
156
|
|
117
|
-
t_news =
|
157
|
+
t_news = TranslatedNews.last
|
118
158
|
assert_equal news.id, t_news.origin_id.to_i
|
119
159
|
assert_equal "Заголовок", t_news.title
|
120
160
|
assert_equal "Содержание", t_news.content
|
121
161
|
assert_equal "ru", t_news.locale
|
122
162
|
end
|
123
163
|
|
124
|
-
should
|
164
|
+
test "should Create multiple translations" do
|
125
165
|
news = News.create :translations_attributes => [{ :title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
126
166
|
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
127
167
|
|
128
168
|
assert news.persisted?
|
129
169
|
|
130
|
-
t_news =
|
170
|
+
t_news = TranslatedNews.first
|
131
171
|
assert_equal news.id, t_news.origin_id.to_i
|
132
172
|
assert_equal "Заголовок", t_news.title
|
133
173
|
assert_equal "Содержание", t_news.content
|
134
174
|
assert_equal "ru", t_news.locale
|
135
175
|
|
136
|
-
t_news =
|
176
|
+
t_news = TranslatedNews.last
|
137
177
|
assert_equal news.id, t_news.origin_id.to_i
|
138
178
|
assert_equal "Resent News", t_news.title
|
139
179
|
assert_equal "That is where the text goes", t_news.content
|
140
180
|
assert_equal "en", t_news.locale
|
141
181
|
end
|
182
|
+
#end
|
183
|
+
end
|
184
|
+
|
185
|
+
class CurrentTranslationActiveRecordTest < Test::Unit::TestCase
|
186
|
+
teardown do
|
187
|
+
::I18n.locale = ::I18n.default_locale
|
142
188
|
end
|
143
189
|
|
144
|
-
context "Current translation" do
|
145
|
-
should
|
190
|
+
#context "Current translation" do
|
191
|
+
test "should Set default translation" do
|
146
192
|
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
147
193
|
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
148
194
|
|
@@ -152,19 +198,19 @@ class TranslatableTest < Test::Unit::TestCase
|
|
152
198
|
assert_equal "That is where the text goes", news.content
|
153
199
|
end
|
154
200
|
|
155
|
-
should
|
201
|
+
test "should Been set equal to current locale" do
|
156
202
|
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
157
203
|
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
158
204
|
|
159
205
|
assert news.persisted?
|
160
206
|
|
161
|
-
|
207
|
+
news.set_current_translation :ru
|
162
208
|
|
163
209
|
assert_equal "Заголовок", news.title
|
164
210
|
assert_equal "Содержание", news.content
|
165
211
|
end
|
166
212
|
|
167
|
-
should
|
213
|
+
test "should Not been set if unavailable" do
|
168
214
|
news = News.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :locale => "ru"},
|
169
215
|
{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
170
216
|
|
@@ -175,7 +221,7 @@ class TranslatableTest < Test::Unit::TestCase
|
|
175
221
|
assert_nil news.content
|
176
222
|
end
|
177
223
|
|
178
|
-
should
|
224
|
+
test "should Be switched on locale switching" do
|
179
225
|
news = News.create :translations_attributes => [{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
180
226
|
|
181
227
|
assert news.persisted?
|
@@ -193,9 +239,9 @@ class TranslatableTest < Test::Unit::TestCase
|
|
193
239
|
assert_equal "Заголовок", news.title
|
194
240
|
assert_equal "Содержание", news.content
|
195
241
|
end
|
196
|
-
end
|
242
|
+
#end
|
197
243
|
|
198
|
-
should
|
244
|
+
test "should Add translation to existing record" do
|
199
245
|
news = News.create :translations_attributes => [{:title => "Resent News", :content => "That is where the text goes", :locale => "en"}]
|
200
246
|
|
201
247
|
assert news.persisted?
|
@@ -206,41 +252,41 @@ class TranslatableTest < Test::Unit::TestCase
|
|
206
252
|
assert t_news.persisted?
|
207
253
|
end
|
208
254
|
|
209
|
-
should
|
210
|
-
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :
|
211
|
-
{:title => "Resent Post", :content => "That is where the text goes", :
|
255
|
+
test "should Define validations" do
|
256
|
+
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :language => "ru"},
|
257
|
+
{:title => "Resent Post", :content => "That is where the text goes", :language => "en"}]
|
212
258
|
assert post.persisted?, "Message had errors: #{post.errors.inspect}"
|
213
259
|
|
214
|
-
post = Post.create :translations_attributes => [{:content => "Содержание", :
|
215
|
-
{:title => "Resent Post 2", :content => "That is where the text goes", :
|
260
|
+
post = Post.create :translations_attributes => [{:content => "Содержание", :language => "ru"},
|
261
|
+
{:title => "Resent Post 2", :content => "That is where the text goes", :language => "en"}]
|
216
262
|
|
217
263
|
assert post.new_record?, "Message had errors: #{post.errors.full_messages.inspect}"
|
218
264
|
assert_equal ["Translations title can't be blank"], post.errors.full_messages
|
219
265
|
|
220
|
-
post = Post.create :translations_attributes => [{:title => "Заголовок 2", :
|
221
|
-
{:title => "Resent Post 3", :content => "That is where the text goes", :
|
266
|
+
post = Post.create :translations_attributes => [{:title => "Заголовок 2", :language => "ru"},
|
267
|
+
{:title => "Resent Post 3", :content => "That is where the text goes", :language => "en"}]
|
222
268
|
|
223
269
|
assert post.new_record?, "Message had errors: #{post.errors.full_messages.inspect}"
|
224
270
|
assert_equal ["Translations content can't be blank"], post.errors.full_messages
|
225
271
|
|
226
|
-
post = Post.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :
|
227
|
-
{:title => "Resent Post 3", :content => "That is where the text goes", :
|
272
|
+
post = Post.create :translations_attributes => [{:title => "Заголовок", :content => "Содержание", :language => "ru"},
|
273
|
+
{:title => "Resent Post 3", :content => "That is where the text goes", :language => "en"}]
|
228
274
|
|
229
275
|
assert post.new_record?, "Message had errors: #{post.errors.full_messages.inspect}"
|
230
276
|
assert_equal ["Translations title has already been taken"], post.errors.full_messages
|
231
277
|
end
|
232
278
|
|
233
279
|
def test_origin_is_owerwrittent
|
234
|
-
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :
|
235
|
-
{:title => "Resent Post", :content => "That is where the text goes", :
|
280
|
+
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :language => "ru"},
|
281
|
+
{:title => "Resent Post", :content => "That is where the text goes", :language => "en"}]
|
236
282
|
assert post.persisted?, "Message had errors: #{post.errors.inspect}"
|
237
283
|
|
238
|
-
|
284
|
+
assert_equal post.object_id, post.translations.first.post.object_id
|
239
285
|
end
|
240
286
|
|
241
|
-
should
|
242
|
-
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :
|
243
|
-
{:title => "Resent Post", :content => "That is where the text goes", :
|
287
|
+
test "should Accept aliases for fileds" do
|
288
|
+
post = Post.create :translations_attributes => [{:title => "Заголовок",:content => "Содержание", :language => "ru"},
|
289
|
+
{:title => "Resent Post", :content => "That is where the text goes", :language => "en"}]
|
244
290
|
assert post.persisted?, "Message had errors: #{post.errors.inspect}"
|
245
291
|
|
246
292
|
assert_equal "Resent Post", post.translated_title
|
@@ -249,10 +295,16 @@ class TranslatableTest < Test::Unit::TestCase
|
|
249
295
|
|
250
296
|
assert_equal "Заголовок", post.translated_title
|
251
297
|
end
|
298
|
+
end
|
299
|
+
|
300
|
+
class MassAssigmentActiveRecordTest < Test::Unit::TestCase
|
301
|
+
teardown do
|
302
|
+
::I18n.locale = ::I18n.default_locale
|
303
|
+
end
|
252
304
|
|
253
|
-
context "Mass assigment" do
|
254
|
-
should
|
255
|
-
tp =
|
305
|
+
#context "Mass assigment" do
|
306
|
+
test "should Be available to mass assigment by default" do
|
307
|
+
tp = TranslatedNews.new( :title => "Resent News", :content => "That is where the text goes", :locale => "en", :origin_id => 1)
|
256
308
|
|
257
309
|
assert_equal "Resent News", tp.title
|
258
310
|
assert_equal "That is where the text goes", tp.content
|
@@ -260,22 +312,22 @@ class TranslatableTest < Test::Unit::TestCase
|
|
260
312
|
assert_equal 1, tp.origin_id
|
261
313
|
end
|
262
314
|
|
263
|
-
should
|
264
|
-
tm =
|
315
|
+
test "should Protect internal fields on desire" do
|
316
|
+
tm = MessageTranslation.new( :title => "Resent Message", :content => "That is where the text goes", :locale => "en", :message_id => 1)
|
265
317
|
|
266
|
-
assert_equal "Resent
|
318
|
+
assert_equal "Resent Message", tm.title
|
267
319
|
assert_equal "That is where the text goes", tm.content
|
268
320
|
assert_equal nil, tm.locale
|
269
|
-
assert_equal nil, tm.
|
321
|
+
assert_equal nil, tm.origin_id
|
270
322
|
end
|
271
323
|
|
272
|
-
should
|
273
|
-
tm =
|
324
|
+
test "should Allow multiple assigment rules" do
|
325
|
+
tm = MessageTranslation.new( {:title => "Resent Message", :content => "That is where the text goes", :locale => "en", :message_id => 1}, :as => :editor)
|
274
326
|
|
275
|
-
assert_equal "Resent
|
327
|
+
assert_equal "Resent Message", tm.title
|
276
328
|
assert_equal "That is where the text goes", tm.content
|
277
329
|
assert_equal "en", tm.locale
|
278
|
-
assert_equal nil, tm.
|
330
|
+
assert_equal nil, tm.origin_id
|
279
331
|
end
|
280
|
-
end
|
332
|
+
#end
|
281
333
|
end
|