forked from norman/friendly_id
/
simple_i18n_test.rb
118 lines (106 loc) · 3.67 KB
/
simple_i18n_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require "helper"
class SimpleI18nTest < MiniTest::Unit::TestCase
include FriendlyId::Test
class Journalist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :simple_i18n
end
test "friendly_id should return the current locale's slug" do
journalist = Journalist.new(:name => "John Doe")
journalist.slug_es = "juan-fulano"
journalist.valid?
I18n.with_locale(I18n.default_locale) do
assert_equal "john-doe", journalist.friendly_id
end
I18n.with_locale(:es) do
assert_equal "juan-fulano", journalist.friendly_id
end
end
test "should create record with slug in column for the current locale" do
I18n.with_locale(I18n.default_locale) do
journalist = Journalist.new(:name => "John Doe")
journalist.valid?
assert_equal "john-doe", journalist.slug_en
assert_nil journalist.slug_es
end
I18n.with_locale(:es) do
journalist = Journalist.new(:name => "John Doe")
journalist.valid?
assert_equal "john-doe", journalist.slug_es
assert_nil journalist.slug_en
end
end
test "to_param should return the numeric id when there's no slug for the current locale" do
transaction do
journalist = Journalist.new(:name => "Juan Fulano")
I18n.with_locale(:es) do
journalist.save!
assert_equal "juan-fulano", journalist.to_param
end
assert_equal journalist.id.to_s, journalist.to_param
end
end
test "should set friendly id for locale" do
transaction do
journalist = Journalist.create!(:name => "John Smith")
journalist.set_friendly_id("Juan Fulano", :es)
journalist.save!
assert_equal "juan-fulano", journalist.slug_es
I18n.with_locale(:es) do
assert_equal "juan-fulano", journalist.to_param
end
end
end
test "should sequence localized slugs" do
transaction do
journalist = Journalist.create!(:name => "John Smith")
I18n.with_locale(:es) do
Journalist.create!(:name => "Juan Fulano")
end
journalist.set_friendly_id("Juan Fulano", :es)
journalist.save!
assert_equal "john-smith", journalist.to_param
I18n.with_locale(:es) do
assert_equal "juan-fulano--2", journalist.to_param
end
end
end
class RegressionTest < MiniTest::Unit::TestCase
include FriendlyId::Test
test "should not overwrite slugs on update_attributes" do
transaction do
journalist = Journalist.create!(:name => "John Smith")
journalist.set_friendly_id("Juan Fulano", :es)
journalist.save!
assert_equal "john-smith", journalist.to_param
journalist.update_attributes :name => "Johnny Smith"
assert_equal "johnny-smith", journalist.to_param
I18n.with_locale(:es) do
assert_equal "juan-fulano", journalist.to_param
end
end
end
end
class ConfigurationTest < MiniTest::Unit::TestCase
test "should add locale to slug column for a non-default locale" do
I18n.with_locale :es do
assert_equal "slug_es", Journalist.friendly_id_config.slug_column
end
end
test "should add locale to non-default slug column and non-default locale" do
model_class = Class.new(ActiveRecord::Base) do
self.abstract_class = true
extend FriendlyId
friendly_id :name, :use => :simple_i18n, :slug_column => :foo
end
I18n.with_locale :es do
assert_equal "foo_es", model_class.friendly_id_config.slug_column
end
end
test "should add locale to slug column for default locale" do
I18n.with_locale(I18n.default_locale) do
assert_equal "slug_en", Journalist.friendly_id_config.slug_column
end
end
end
end