lolita-translation 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,21 +2,21 @@ require 'spec_helper'
2
2
  require 'ar_schema'
3
3
  ARSchema.connect!
4
4
 
5
- describe "Integration with ActiveRecord" do
5
+ describe "Integration with ActiveRecord" do
6
6
 
7
- before(:each) do
8
- ActiveRecord::Base.connection.execute("DELETE FROM categories")
9
- ActiveRecord::Base.connection.execute("DELETE FROM products")
10
- end
11
- context "configuration defination" do
7
+ # before(:each) do
8
+ # ActiveRecord::Base.connection.execute("DELETE FROM categories")
9
+ # ActiveRecord::Base.connection.execute("DELETE FROM products")
10
+ # end
11
+ context "configuration defination" do
12
12
 
13
- before(:each) do
13
+ before do
14
14
  Object.send(:remove_const, :Product) rescue nil
15
15
  klass = Class.new(ActiveRecord::Base)
16
16
  Object.const_set(:Product,klass)
17
17
  end
18
18
 
19
- it "should have class method for translation configuration" do
19
+ it "should have class method for translation configuration" do
20
20
  Product.class_eval do
21
21
  include Lolita::Translation
22
22
  end
@@ -25,17 +25,17 @@ describe "Integration with ActiveRecord" do
25
25
  Product.should.respond_to?(:translate)
26
26
  end
27
27
 
28
- it "should create configuration when class is loaded with field names to translate" do
29
- Product.class_eval do
28
+ it "should create configuration when class is loaded with field names to translate" do
29
+ Product.class_eval do
30
30
  include Lolita::Translation
31
31
  translate :name, :description
32
32
  end
33
33
  Product.translations_configuration.should be_kind_of(Lolita::Translation::Configuration)
34
34
  end
35
35
 
36
- it "should accept block and yield configuration" do
36
+ it "should accept block and yield configuration" do
37
37
  block_called = false
38
- Product.class_eval do
38
+ Product.class_eval do
39
39
  include Lolita::Translation
40
40
  translate :name, :description do |conf|
41
41
  block_called = true
@@ -45,35 +45,34 @@ describe "Integration with ActiveRecord" do
45
45
  end
46
46
  end
47
47
 
48
- context "record" do
48
+ context "record" do
49
49
  let(:category){Category.create(:name => "category_name", :default_locale => "en")}
50
50
 
51
- before(:each) do
51
+ before do
52
52
  I18n.default_locale = :en
53
53
  Object.send(:remove_const, :Category) rescue nil
54
54
  klass = Class.new(ActiveRecord::Base)
55
55
  Object.const_set(:Category,klass)
56
- Category.class_eval do
56
+ Category.class_eval do
57
57
  include Lolita::Translation
58
58
  attr_accessible :name, :default_locale
59
59
  translate :name
60
60
  end
61
61
  end
62
62
 
63
- it "should have default locale" do
64
- category = Category.create(:name => Faker::Name.first_name, :default_locale => "en")
63
+ it "should have default locale" do
65
64
  I18n.default_locale = :lv
66
65
  category.original_locale.to_s.should eq("en")
67
66
  end
68
67
 
69
- it "should have translations" do
68
+ it "should have translations" do
70
69
  category.translations.should be_empty
71
70
  category.update_attributes(:name => "updated",:translations_attributes => [{:name => "translation-lv", :locale => "lv"}])
72
71
  category.errors.should be_empty
73
72
  category.translations.reload.should have(1).item
74
73
  end
75
74
 
76
- it "should return translatable attribute in current locale" do
75
+ it "should return translatable attribute in current locale" do
77
76
  I18n.default_locale = :lv
78
77
  I18n.locale = :lv
79
78
  category.name.should eq("category_name")
@@ -82,22 +81,22 @@ describe "Integration with ActiveRecord" do
82
81
  category.update_attributes(:name => "updated",:translations_attributes => [{:name => "translation-lv", :locale => "lv"}])
83
82
  I18n.locale = :lv
84
83
  category.name.should eq("translation-lv")
85
- I18n.locale = :ru
84
+ I18n.locale = :ru
86
85
  category.name.should eq("updated")
87
86
  end
88
87
 
89
- it "should switch attribute to different locale" do
88
+ it "should switch attribute to different locale" do
90
89
  category.name.should eq("category_name")
91
90
  category.update_attributes(:translations_attributes => [{:name => "translation-lv", :locale => "lv"}])
92
91
  category.name.in(:lv).should eq("translation-lv")
93
92
  end
94
93
  end
95
-
94
+
96
95
  context "saving" do
97
96
  let(:category){Category.create(:name => "category_name", :default_locale => :en)}
98
97
  let(:product){ Product.create(:name => "product_name", :description => "product_description") }
99
98
 
100
- before(:each) do
99
+ before(:each) do
101
100
  I18n.locale = :en
102
101
  I18n.default_locale = :lv
103
102
  Object.send(:remove_const, :Category) rescue nil
@@ -106,19 +105,19 @@ describe "Integration with ActiveRecord" do
106
105
  Object.const_set(:Category,klass)
107
106
  product_klass = Class.new(ActiveRecord::Base)
108
107
  Object.const_set(:Product,product_klass)
109
- Category.class_eval do
108
+ Category.class_eval do
110
109
  include Lolita::Translation
111
110
  attr_accessible :name, :default_locale
112
111
  translate :name
113
112
  end
114
- Product.class_eval do
113
+ Product.class_eval do
115
114
  include Lolita::Translation
116
115
  attr_accessible :name, :default_locale
117
116
  translate :name, :description
118
117
  end
119
118
  end
120
-
121
- it "should create translations for object" do
119
+
120
+ it "should create translations for object" do
122
121
  new_cat = Category.create({
123
122
  :name => "cat_name",
124
123
  :original_locale => :en,
@@ -131,7 +130,7 @@ describe "Integration with ActiveRecord" do
131
130
  new_cat.translations.should have(2).items
132
131
  end
133
132
 
134
- it "should save translation as nested attributes" do
133
+ it "should save translation as nested attributes" do
135
134
  category.translations.should be_empty
136
135
  category.update_attributes(:translations_attributes => [
137
136
  {:name => "translation-lv", :locale => "lv"},
@@ -141,7 +140,7 @@ describe "Integration with ActiveRecord" do
141
140
  category.translations.should have(2).items
142
141
  end
143
142
 
144
- it "should save locale for record if it accepts it" do
143
+ it "should save locale for record if it accepts it" do
145
144
  I18n.locale = :lv
146
145
  category.original_locale.should eq(:en)
147
146
  product.original_locale.should eq(:lv)
@@ -156,7 +155,7 @@ describe "Integration with ActiveRecord" do
156
155
  category.errors.keys.should include(:"translations.locale")
157
156
  end
158
157
 
159
- it "translation record should be associated with original record" do
158
+ it "translation record should be associated with original record" do
160
159
  transl1 = CategoryTranslation.new(:name => "translation-lv", :locale => "lv")
161
160
  transl1.category = category
162
161
  transl1.save
@@ -166,21 +165,21 @@ describe "Integration with ActiveRecord" do
166
165
  transl2.errors.keys.should include(:"category")
167
166
  end
168
167
 
169
- it "should validate that locale is presented" do
168
+ it "should validate that locale is presented" do
170
169
  transl1 = CategoryTranslation.new(:name => "translation-lv")
171
- transl1.category = category
170
+ transl1.category = category
172
171
  transl1.save
173
172
  transl1.errors.keys.should eq([:"locale"])
174
173
  end
175
174
 
176
- it "should validate that locale is unique for each original record" do
175
+ it "should validate that locale is unique for each original record" do
177
176
  transl1 = category.translations.create(:name => "translation-lv", :locale => "lv")
178
177
  transl2 = category.translations.create(:name => "translation-lv", :locale => "lv")
179
178
  transl1.errors.should be_empty
180
179
  transl2.errors.keys.should eq([:"locale"])
181
180
  end
182
181
 
183
- it "should use default locale for records without default locale field" do
182
+ it "should use default locale for records without default locale field" do
184
183
  product.update_attributes(:translations_attributes => [
185
184
  { :name => "product_name-ru", :locale => "ru"}
186
185
  ])
@@ -192,23 +191,23 @@ describe "Integration with ActiveRecord" do
192
191
  end
193
192
  end
194
193
 
195
- context "migrating" do
196
- before(:each) do
194
+ context "migrating" do
195
+ before(:each) do
197
196
  Object.send(:remove_const,:Comment) rescue nil
198
197
  c_class = Class.new(ActiveRecord::Base)
199
198
  Object.const_set(:Comment,c_class)
200
- c_class.class_eval do
199
+ c_class.class_eval do
201
200
  include Lolita::Translation
202
201
  translate :body
203
202
  end
204
203
  ActiveRecord::Base.connection.execute("DROP TABLE comments_translations") rescue nil
205
204
  end
206
205
 
207
- after(:each) do
206
+ after(:each) do
208
207
  Object.send(:remove_const,:Comment) rescue nil
209
208
  end
210
209
 
211
- it "should sync translation table through migrations" do
210
+ it "should sync translation table through migrations" do
212
211
  ActiveRecord::Base.connection.tables.should_not include("comments_translations")
213
212
  Comment.sync_translation_table!
214
213
  ActiveRecord::Base.connection.tables.should include("comments_translations")
@@ -24,23 +24,15 @@ describe Lolita::Translation::Builder::AbstractBuilder do
24
24
  end
25
25
 
26
26
  it "should return class name like <Scoped::ClassName>Translation for class name 'Scoped::ClassName'" do
27
- begin
28
- Object.const_set(:Scoped,Class.new)
29
- set_class_name(some_class,"Scoped::ClassName")
30
- klass.new(some_class).class_name.should eq("Scoped::ClassNameTranslation")
31
- ensure
32
- Object.send(:remove_const, :Scoped)
33
- end
27
+ stub_const('Scoped',Class.new)
28
+ set_class_name(some_class,"Scoped::ClassName")
29
+ klass.new(some_class).class_name.should eq("Scoped::ClassNameTranslation")
34
30
  end
35
31
 
36
32
  it "should create class with scoped name" do
37
- begin
38
- Object.const_set(:Scoped,Class.new)
39
- set_class_name(some_class,"Scoped::OtherClassName")
40
- klass.new(some_class).klass.name.should eq("Scoped::OtherClassNameTranslation")
41
- ensure
42
- Object.send(:remove_const, :Scoped)
43
- end
33
+ stub_const('Scoped',Class.new)
34
+ set_class_name(some_class,"Scoped::OtherClassName")
35
+ klass.new(some_class).klass.name.should eq("Scoped::OtherClassNameTranslation")
44
36
  end
45
37
 
46
38
  it "should show warning when method that should be implemented in concrete builder is not implemented yet" do
@@ -4,27 +4,21 @@ require File.expand_path("lib/lolita-translation/builder/active_record_builder")
4
4
  require File.expand_path("lib/lolita-translation/configuration")
5
5
  ARSchema.connect!
6
6
 
7
- describe Lolita::Translation::Builder::ActiveRecordBuilder do
7
+ describe Lolita::Translation::Builder::ActiveRecordBuilder do
8
8
  let(:klass) { Lolita::Translation::Builder::ActiveRecordBuilder }
9
9
  let(:config){ Lolita::Translation::Configuration.new(Product) }
10
- before(:each) do
11
- Object.send(:remove_const, :Product) if Object.const_defined?(:Product)
10
+ before(:each) do
12
11
  a_klass = Class.new(ActiveRecord::Base)
13
- Object.const_set(:Product, a_klass)
12
+ stub_const('Product', a_klass)
14
13
  end
15
14
 
16
- after(:each) do
17
- Object.send(:remove_const, :Product) rescue nil
18
- end
19
-
20
-
21
- it "should build class with ActiveRecord::Base as superclass" do
15
+ it "should build class with ActiveRecord::Base as superclass" do
22
16
  obj = klass.new(Product)
23
17
  obj.build
24
18
  obj.klass.superclass.should eq(ActiveRecord::Base)
25
19
  end
26
20
 
27
- it "should call class methods on klass" do
21
+ it "should call class methods on klass" do
28
22
  obj = klass.new(Product, config)
29
23
  obj.stub(:association_name).and_return(:product)
30
24
  obj.build
@@ -36,5 +30,5 @@ describe Lolita::Translation::Builder::ActiveRecordBuilder do
36
30
  obj.stub(:translations_association_name).and_return(:translations)
37
31
  obj.build
38
32
  Product.reflections.keys.should include(:translations)
39
- end
33
+ end
40
34
  end
@@ -3,10 +3,10 @@ require File.expand_path("lib/lolita-translation/configuration")
3
3
  require File.expand_path("lib/lolita-translation/translation_class_builder")
4
4
  require File.expand_path("lib/lolita-translation/errors")
5
5
 
6
- describe Lolita::Translation::Configuration do
6
+ describe Lolita::Translation::Configuration do
7
7
  let(:klass){Lolita::Translation::Configuration}
8
- let(:some_class) do
9
- Class.new do
8
+ let(:some_class) do
9
+ Class.new do
10
10
  class << self
11
11
  def table_exists?
12
12
  true
@@ -15,54 +15,50 @@ describe Lolita::Translation::Configuration do
15
15
  end
16
16
  end
17
17
 
18
- before(:each) do
18
+ before(:each) do
19
19
  Lolita::Translation::TranslationClassBuilder.any_instance.stub(:build_class).and_return("builder")
20
20
  Lolita::Translation::TranslationClassBuilder.any_instance.stub(:override_attributes).and_return("attributes")
21
21
  end
22
22
 
23
- it "should have @klass attribute" do
23
+ it "should have @klass attribute" do
24
24
  klass.new(some_class).klass.should eq(some_class)
25
25
  end
26
26
 
27
- it "should have @attributes attribute" do
27
+ it "should have @attributes attribute" do
28
28
  klass.new(some_class, :name, :body).attributes.sort.should eq([:name,:body].sort)
29
29
  end
30
30
 
31
- it "should have @translation_class attribute" do
32
- Object.const_set(:Product,some_class)
31
+ it "should have @translation_class attribute" do
32
+ stub_const('Product',some_class)
33
33
  klass.new(some_class).translation_class.should eq("builder")
34
34
  end
35
35
 
36
- describe "locales" do
37
- before(:each) do
38
- Object.const_set(:Product,some_class)
36
+ describe "locales" do
37
+ before do
38
+ stub_const('Product',some_class)
39
+ stub_const('Lolita::Translation::Locales', Class.new) unless defined?(Lolita::Translation::Locales)
39
40
  end
40
41
 
41
- after(:each) do
42
- Lolita::Translation.send(:remove_const,:Locales) rescue nil
43
- end
44
- it "can be received as option" do
45
- Lolita::Translation.const_set(:Locales, Class.new)
42
+ it "can be received as option" do
46
43
  Lolita::Translation::Locales.should_receive(:new).with([:en,:ru]).and_return([:en,:ru])
47
44
  config = klass.new(some_class, :locales => [:en, :ru])
48
45
  config.locales.should == [:en,:ru]
49
46
  end
50
47
 
51
- it "should fall back to Lolita::Translation.locales when no locales are passed" do
48
+ it "should fall back to Lolita::Translation.locales when no locales are passed" do
52
49
  config = klass.new(some_class)
53
50
  Lolita::Translation.stub(:locales).and_return([:lv,:ru])
54
51
  config.locales.should eq(Lolita::Translation.locales)
55
52
  end
56
53
 
57
- it "can be as anonymous method" do
58
- Lolita::Translation.const_set(:Locales, Class.new)
54
+ it "can be as anonymous method" do
59
55
  Lolita::Translation::Locales.should_receive(:new).and_return([:lv,:ru])
60
56
  config = klass.new(some_class, :locales => Proc.new{ [:lv,:ru] })
61
57
  config.locales.should eq([:lv,:ru])
62
58
  end
63
59
  end
64
60
 
65
- it "should yield block with self when called with block" do
61
+ it "should yield block with self when called with block" do
66
62
  block_called = nil
67
63
  translation_conf = klass.new(some_class) do |conf|
68
64
  block_called = conf
@@ -1,20 +1,20 @@
1
1
  require 'header'
2
2
  require File.expand_path("lib/lolita-translation/locales")
3
3
 
4
- describe Lolita::Translation::Locales do
5
- let(:klass){ Lolita::Translation::Locales }
4
+ describe Lolita::Translation::Locales do
5
+ let(:klass){ ::Lolita::Translation::Locales }
6
6
 
7
- it "should create new with given locales names" do
7
+ it "should create new with given locales names" do
8
8
  obj = klass.new([:lv,:en,:ru])
9
9
  obj.locale_names.should eq([:en,:lv,:ru])
10
10
  end
11
11
 
12
- it "should implement Enumerable and each element should be Locale" do
12
+ it "should implement Enumerable and each element should be Locale" do
13
13
  obj = klass.new([:lv,:en,:ru])
14
14
  obj.first.should be_kind_of(Lolita::Translation::Locale)
15
15
  end
16
16
 
17
- it "should return locales in order where first is locale that belongs to record" do
17
+ it "should return locales in order where first is locale that belongs to record" do
18
18
  obj = klass.new([:lv,:en,:ru])
19
19
  resource = double("resource")
20
20
  transl_rec = double("transl-rec")
@@ -23,7 +23,7 @@ describe Lolita::Translation::Locales do
23
23
  obj.by_resource_locale(resource).map{|r| r.name}.should eq([:ru,:en,:lv])
24
24
  end
25
25
 
26
- it "should return active locale" do
26
+ it "should return active locale" do
27
27
  obj = klass.new([:lv,:en,:ru])
28
28
  I18n.locale = :ru
29
29
  obj.active.name.should eq(:ru)
@@ -32,7 +32,7 @@ describe "Lolita tab extension" do
32
32
 
33
33
  it "should provide tab with #build_translations_nested_form with resource" do
34
34
  c_class = Class.new(ActiveRecord::Base)
35
- Object.const_set(:Product,c_class)
35
+ stub_const('Product',c_class)
36
36
  c_class.class_eval do
37
37
  include Lolita::Configuration
38
38
  include Lolita::Translation
@@ -48,7 +48,7 @@ describe "Lolita tab extension" do
48
48
 
49
49
  it "should add #original_locale field to original tab" do
50
50
  c_class = Class.new(ActiveRecord::Base)
51
- Object.const_set(:Product,c_class)
51
+ stub_const('Product',c_class)
52
52
  c_class.class_eval do
53
53
  include Lolita::Configuration
54
54
  include Lolita::Translation
@@ -1,31 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Lolita::Translation::Migrator do
3
+ describe Lolita::Translation::Migrator do
4
4
  let(:klass) { Lolita::Translation::Migrator }
5
-
6
- before(:each) do
7
- Object.send(:remove_const,:Comment) rescue nil
5
+
6
+ before(:each) do
8
7
  c_class = Class.new(ActiveRecord::Base)
9
- Object.const_set(:Comment,c_class)
10
- c_class.class_eval do
8
+ stub_const('Comment',c_class)
9
+ c_class.class_eval do
11
10
  include Lolita::Translation
12
11
  translate :body
13
12
  end
14
13
  end
15
14
 
16
- after(:each) do
17
- Object.send(:remove_const,:Comment) rescue nil
18
- end
19
-
20
- describe "Instance methods" do
15
+ describe "Instance methods" do
21
16
 
22
- it "should have klass and config attributes" do
17
+ it "should have klass and config attributes" do
23
18
  migrator = klass.new(Comment)
24
19
  migrator.klass.should eq(Comment)
25
20
  migrator.config.should eq(Comment.translations_configuration)
26
21
  end
27
22
 
28
- it "should raise error when #migrate called" do
23
+ it "should raise error when #migrate called" do
29
24
  migrator = klass.new(Comment)
30
25
  expect{
31
26
  migrator.migrate
@@ -33,10 +28,10 @@ describe Lolita::Translation::Migrator do
33
28
  end
34
29
  end
35
30
 
36
- describe "Class methods" do
37
- it "should create concrete migrator for AR" do
31
+ describe "Class methods" do
32
+ it "should create concrete migrator for AR" do
38
33
  klass.create(Comment).should be_kind_of(Lolita::Translation::Migrators::ActiveRecordMigrator)
39
34
  end
40
35
  end
41
36
 
42
- end
37
+ end