akm-selectable_attr_rails 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 0
3
+ :patch: 1
4
4
  :major: 0
@@ -0,0 +1,38 @@
1
+ if defined?(I18n)
2
+ module SelectableAttr
3
+ class Enum
4
+ def self.i18n_export
5
+ result = {}
6
+ instances.each do |instance|
7
+ unless instance.i18n_scope
8
+ # puts "no i18n_scope of #{instance.inspect}"
9
+ next
10
+ end
11
+ paths = instance.i18n_scope.dup
12
+ current = result
13
+ paths.each do |path|
14
+ current = current[path.to_s] ||= {}
15
+ end
16
+ instance.entries.each do |entry|
17
+ current[entry.key.to_s] = entry.name
18
+ end
19
+ end
20
+ result
21
+ end
22
+
23
+ def i18n_scope(*path)
24
+ @i18n_scope = path unless path.empty?
25
+ @i18n_scope
26
+ end
27
+
28
+ class Entry
29
+ def name
30
+ I18n.locale.nil? ? @name :
31
+ @enum.i18n_scope.blank? ? @name :
32
+ I18n.translate(key, :scope => @enum.i18n_scope, :default => @name)
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,254 @@
1
+ # -*- coding: utf-8 -*-
2
+ if defined?(I18n)
3
+ require File.join(File.dirname(__FILE__), 'spec_helper')
4
+
5
+ describe SelectableAttr::Enum do
6
+
7
+ before(:each) do
8
+ I18n.backend = I18n::Backend::Simple.new
9
+ I18n.backend.store_translations 'en', 'selectable_attrs' => {'enum1' => {
10
+ 'entry1' => 'entry one',
11
+ 'entry2' => 'entry two',
12
+ 'entry3' => 'entry three'
13
+ } }
14
+ I18n.backend.store_translations 'ja', 'selectable_attrs' => {'enum1' => {
15
+ 'entry1' => 'エントリ壱',
16
+ 'entry2' => 'エントリ弐',
17
+ 'entry3' => 'エントリ参'
18
+ } }
19
+ end
20
+
21
+ Enum1 = SelectableAttr::Enum.new do
22
+ i18n_scope(:selectable_attrs, :enum1)
23
+ entry 1, :entry1, "エントリ1"
24
+ entry 2, :entry2, "エントリ2"
25
+ entry 3, :entry3, "エントリ3"
26
+ end
27
+
28
+ it 'test_enum1_i18n' do
29
+ I18n.locale = nil
30
+ I18n.locale.should == :en
31
+ Enum1.name_by_key(:entry1).should == "entry one"
32
+ Enum1.name_by_key(:entry2).should == "entry two"
33
+ Enum1.name_by_key(:entry3).should == "entry three"
34
+ Enum1.names.should == ["entry one", "entry two", "entry three"]
35
+
36
+ I18n.locale = 'ja'
37
+ Enum1.name_by_key(:entry1).should == "エントリ壱"
38
+ Enum1.name_by_key(:entry2).should == "エントリ弐"
39
+ Enum1.name_by_key(:entry3).should == "エントリ参"
40
+ Enum1.names.should == ["エントリ壱", "エントリ弐", "エントリ参"]
41
+
42
+ I18n.locale = 'en'
43
+ Enum1.name_by_key(:entry1).should == "entry one"
44
+ Enum1.name_by_key(:entry2).should == "entry two"
45
+ Enum1.name_by_key(:entry3).should == "entry three"
46
+ Enum1.names.should == ["entry one", "entry two", "entry three"]
47
+ end
48
+
49
+ class EnumBase
50
+ include ::SelectableAttr::Base
51
+ end
52
+
53
+ class SelectableAttrMock1 < EnumBase
54
+ selectable_attr :attr1, :default => 2 do
55
+ i18n_scope(:selectable_attrs, :enum1)
56
+ entry 1, :entry1, "エントリ1"
57
+ entry 2, :entry2, "エントリ2"
58
+ entry 3, :entry3, "エントリ3"
59
+ end
60
+ end
61
+
62
+ it 'test_attr1_i18n' do
63
+ I18n.default_locale = 'ja'
64
+ I18n.locale = nil
65
+ I18n.locale.should == 'ja'
66
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "エントリ壱"
67
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "エントリ弐"
68
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "エントリ参"
69
+ SelectableAttrMock1.attr1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
70
+
71
+ I18n.locale = 'ja'
72
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "エントリ壱"
73
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "エントリ弐"
74
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "エントリ参"
75
+ SelectableAttrMock1.attr1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
76
+
77
+ I18n.locale = 'en'
78
+ SelectableAttrMock1.attr1_name_by_key(:entry1).should == "entry one"
79
+ SelectableAttrMock1.attr1_name_by_key(:entry2).should == "entry two"
80
+ SelectableAttrMock1.attr1_name_by_key(:entry3).should == "entry three"
81
+ SelectableAttrMock1.attr1_options.should == [["entry one",1], ["entry two",2], ["entry three",3]]
82
+ end
83
+
84
+ class SelectableAttrMock2 < EnumBase
85
+ selectable_attr :enum1, :default => 2 do
86
+ i18n_scope(:selectable_attrs, :enum1)
87
+ entry 1, :entry1, "エントリ1"
88
+ entry 2, :entry2, "エントリ2"
89
+ entry 3, :entry3, "エントリ3"
90
+ end
91
+ end
92
+
93
+ it 'test_enum1_i18n' do
94
+ I18n.default_locale = 'ja'
95
+ I18n.locale = nil
96
+ I18n.locale.should == 'ja'
97
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "エントリ壱"
98
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "エントリ弐"
99
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "エントリ参"
100
+ SelectableAttrMock2.enum1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
101
+
102
+ I18n.locale = 'ja'
103
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "エントリ壱"
104
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "エントリ弐"
105
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "エントリ参"
106
+ SelectableAttrMock2.enum1_options.should == [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]]
107
+
108
+ I18n.locale = 'en'
109
+ SelectableAttrMock2.enum1_name_by_key(:entry1).should == "entry one"
110
+ SelectableAttrMock2.enum1_name_by_key(:entry2).should == "entry two"
111
+ SelectableAttrMock2.enum1_name_by_key(:entry3).should == "entry three"
112
+ SelectableAttrMock2.enum1_options.should == [["entry one",1], ["entry two",2], ["entry three",3]]
113
+ end
114
+
115
+ # i18n用のlocaleカラムを持つselectable_attrのエントリ名をDB上に保持するためのモデル
116
+ class I18nItemMaster < ActiveRecord::Base
117
+ end
118
+
119
+ # selectable_attrを使った場合その3
120
+ # アクセス時に毎回アクセス時にDBから項目名を取得します。
121
+ # 対象となる項目名はi18n対応している名称です
122
+ class ProductWithI18nDB1 < ActiveRecord::Base
123
+ set_table_name 'products'
124
+ selectable_attr :product_type_cd do
125
+ # update_byメソッドには、エントリのidと名称を返すSELECT文を指定する代わりに、
126
+ # エントリのidと名称の配列の配列を返すブロックを指定することも可能です。
127
+ update_by(:when => :everytime) do
128
+ records = I18nItemMaster.find(:all,
129
+ :conditions => [
130
+ "category_name = 'product_type_cd' and locale = ? ", I18n.locale.to_s],
131
+ :order => "item_no")
132
+ records.map{|r| [r.item_cd, r.name]}
133
+ end
134
+ entry '01', :book, '書籍', :discount => 0.8
135
+ entry '02', :dvd, 'DVD', :discount => 0.2
136
+ entry '03', :cd, 'CD', :discount => 0.5
137
+ entry '09', :other, 'その他', :discount => 1
138
+ end
139
+
140
+ end
141
+
142
+ it "test_update_entry_name_with_i18n" do
143
+ I18n.locale = 'ja'
144
+ # DBに全くデータがなくてもコードで記述してあるエントリは存在します。
145
+ I18nItemMaster.delete_all("category_name = 'product_type_cd'")
146
+ ProductWithI18nDB1.product_type_entries.length.should == 4
147
+ ProductWithI18nDB1.product_type_name_by_key(:book).should == '書籍'
148
+ ProductWithI18nDB1.product_type_name_by_key(:dvd).should == 'DVD'
149
+ ProductWithI18nDB1.product_type_name_by_key(:cd).should == 'CD'
150
+ ProductWithI18nDB1.product_type_name_by_key(:other).should == 'その他'
151
+
152
+ # DBからエントリの名称を動的に変更できます
153
+ item_book = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '01', :name => '本')
154
+ ProductWithI18nDB1.product_type_entries.length.should == 4
155
+ ProductWithI18nDB1.product_type_name_by_key(:book).should == '本'
156
+ ProductWithI18nDB1.product_type_name_by_key(:dvd).should == 'DVD'
157
+ ProductWithI18nDB1.product_type_name_by_key(:cd).should == 'CD'
158
+ ProductWithI18nDB1.product_type_name_by_key(:other).should == 'その他'
159
+ ProductWithI18nDB1.product_type_options.should == [['本', '01'], ['DVD', '02'], ['CD', '03'], ['その他', '09']]
160
+
161
+ # DBからエントリの並び順を動的に変更できます
162
+ item_book.item_no = 4;
163
+ item_book.save!
164
+ item_other = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'その他')
165
+ item_dvd = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02') # nameは指定しなかったらデフォルトが使われます。
166
+ item_cd = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03') # nameは指定しなかったらデフォルトが使われます。
167
+ ProductWithI18nDB1.product_type_options.should == [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01']]
168
+
169
+ # DBからエントリを動的に追加することも可能です。
170
+ item_toys = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'おもちゃ')
171
+ ProductWithI18nDB1.product_type_options.should == [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']]
172
+ ProductWithI18nDB1.product_type_key_by_id('04').should == :entry_04
173
+
174
+ # 英語名を登録
175
+ item_book = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 4, :item_cd => '01', :name => 'Book')
176
+ item_other = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'Others')
177
+ item_dvd = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02', :name => 'DVD')
178
+ item_cd = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03', :name => 'CD')
179
+ item_toys = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'Toy')
180
+
181
+ # 英語名が登録されていてもI18n.localeが変わらなければ、日本語のまま
182
+ ProductWithI18nDB1.product_type_options.should == [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']]
183
+ ProductWithI18nDB1.product_type_key_by_id('04').should == :entry_04
184
+
185
+ # I18n.localeを変更すると取得できるエントリの名称も変わります
186
+ I18n.locale = 'en'
187
+ ProductWithI18nDB1.product_type_options.should == [['Others', '09'], ['DVD', '02'], ['CD', '03'], ['Book', '01'], ['Toy', '04']]
188
+ ProductWithI18nDB1.product_type_key_by_id('04').should == :entry_04
189
+
190
+ I18n.locale = 'ja'
191
+ ProductWithI18nDB1.product_type_options.should == [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']]
192
+ ProductWithI18nDB1.product_type_key_by_id('04').should == :entry_04
193
+
194
+ I18n.locale = 'en'
195
+ ProductWithI18nDB1.product_type_options.should == [['Others', '09'], ['DVD', '02'], ['CD', '03'], ['Book', '01'], ['Toy', '04']]
196
+ ProductWithI18nDB1.product_type_key_by_id('04').should == :entry_04
197
+
198
+ # DBからレコードを削除してもコードで定義したentryは削除されません。
199
+ # 順番はDBからの取得順で並び替えられたものの後になります
200
+ item_dvd.destroy
201
+ ProductWithI18nDB1.product_type_options.should == [['Others', '09'], ['CD', '03'], ['Book', '01'], ['Toy', '04'], ['DVD', '02']]
202
+
203
+ # DB上で追加したレコードを削除すると、エントリも削除されます
204
+ item_toys.destroy
205
+ ProductWithI18nDB1.product_type_options.should == [['Others', '09'], ['CD', '03'], ['Book', '01'], ['DVD', '02']]
206
+
207
+ # 名称を指定していたDBのレコードを削除したら元に戻ります。
208
+ item_book.destroy
209
+ ProductWithI18nDB1.product_type_options.should == [['Others', '09'], ['CD', '03'], ['書籍', '01'], ['DVD', '02']]
210
+
211
+ # エントリに該当するレコードを全部削除したら、元に戻ります。
212
+ I18nItemMaster.delete_all("category_name = 'product_type_cd'")
213
+ ProductWithI18nDB1.product_type_options.should == [['書籍', '01'], ['DVD', '02'], ['CD', '03'], ['その他', '09']]
214
+ end
215
+
216
+ it 'test_i18n_export' do
217
+ I18nItemMaster.delete_all("category_name = 'product_type_cd'")
218
+
219
+ I18n.locale = 'ja'
220
+ actual = SelectableAttr::Enum.i18n_export
221
+ actual.keys.should == ['selectable_attrs']
222
+ actual['selectable_attrs'].keys.include?('enum1').should == true
223
+ actual['selectable_attrs']['enum1'].should ==
224
+ {'entry1'=>"エントリ壱",
225
+ 'entry2'=>"エントリ弐",
226
+ 'entry3'=>"エントリ参"}
227
+
228
+ actual['selectable_attrs']['ProductWithI18nDB1'].should ==
229
+ {'product_type_cd'=>
230
+ {'book'=>"書籍", 'dvd'=>"DVD", 'cd'=>"CD", 'other'=>"その他"}}
231
+
232
+ I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'Others')
233
+ I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02', :name => 'DVD')
234
+ I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03', :name => 'CD')
235
+ I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 4, :item_cd => '01', :name => 'Book')
236
+ I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'Toy')
237
+
238
+ I18n.locale = 'en'
239
+ actual = SelectableAttr::Enum.i18n_export
240
+ actual.keys.should == ['selectable_attrs']
241
+ actual['selectable_attrs'].keys.include?('enum1').should == true
242
+ actual['selectable_attrs']['enum1'].should ==
243
+ {'entry1'=>"entry one",
244
+ 'entry2'=>"entry two",
245
+ 'entry3'=>"entry three"}
246
+ actual['selectable_attrs'].keys.include?('ProductWithI18nDB1').should == true
247
+ actual['selectable_attrs']['ProductWithI18nDB1'].should ==
248
+ {'product_type_cd'=>
249
+ {'book'=>"Book", 'dvd'=>"DVD", 'cd'=>"CD", 'other'=>"Others", 'entry_04'=>"Toy"}}
250
+ end
251
+ end
252
+ else
253
+ $stderr.puts "WARNING! i18n test skipeed because I18n not found"
254
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akm-selectable_attr_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi Akima
@@ -9,10 +9,18 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-27 00:00:00 -08:00
12
+ date: 2009-02-06 00:00:00 -08:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: selectable_attr
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.1
23
+ version:
16
24
  description: selectable_attr_rails makes possible to use selectable_attr in rails application
17
25
  email: akima@gmail.com
18
26
  executables: []
@@ -23,6 +31,7 @@ extra_rdoc_files: []
23
31
 
24
32
  files:
25
33
  - VERSION.yml
34
+ - lib/selectable_attr_i18n.rb
26
35
  - lib/selectable_attr_rails
27
36
  - lib/selectable_attr_rails/db_loadable.rb
28
37
  - lib/selectable_attr_rails/helpers
@@ -38,7 +47,7 @@ files:
38
47
  - spec/fixtures
39
48
  - spec/introduction_spec.rb
40
49
  - spec/schema.rb
41
- - spec/selectable_attr_i18n_test.rb
50
+ - spec/selectable_attr_i18n_spec.rb
42
51
  - spec/spec_helper.rb
43
52
  has_rdoc: true
44
53
  homepage: http://github.com/akm/selectable_attr_rails/
@@ -1,276 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- if defined?(I18n)
3
- require File.join(File.dirname(__FILE__), 'test_helper')
4
-
5
- class SelectableAttrI18nTest < Test::Unit::TestCase
6
-
7
- def setup
8
- I18n.backend = I18n::Backend::Simple.new
9
- I18n.backend.store_translations 'en', :selectable_attrs => {:enum1 => {
10
- :entry1 => 'entry one',
11
- :entry2 => 'entry two',
12
- :entry3 => 'entry three'
13
- } }
14
- I18n.backend.store_translations 'ja', :selectable_attrs => {:enum1 => {
15
- :entry1 => 'エントリ壱',
16
- :entry2 => 'エントリ弐',
17
- :entry3 => 'エントリ参'
18
- } }
19
- end
20
-
21
- Enum1 = SelectableAttr::Enum.new do
22
- i18n_scope(:selectable_attrs, :enum1)
23
- entry 1, :entry1, "エントリ1"
24
- entry 2, :entry2, "エントリ2"
25
- entry 3, :entry3, "エントリ3"
26
- end
27
-
28
- def test_enum1_i18n
29
- I18n.locale = nil
30
- # assert_equal nil, I18n.locale
31
- # assert_equal "エントリ1", Enum1.name_by_key(:entry1)
32
- # assert_equal "エントリ2", Enum1.name_by_key(:entry2)
33
- # assert_equal "エントリ3", Enum1.name_by_key(:entry3)
34
- # assert_equal ["エントリ1", "エントリ2", "エントリ3"], Enum1.names
35
- assert_equal :en, I18n.locale
36
- assert_equal "entry one", Enum1.name_by_key(:entry1)
37
- assert_equal "entry two", Enum1.name_by_key(:entry2)
38
- assert_equal "entry three", Enum1.name_by_key(:entry3)
39
- assert_equal ["entry one", "entry two", "entry three"], Enum1.names
40
-
41
- I18n.locale = 'ja'
42
- assert_equal "エントリ壱", Enum1.name_by_key(:entry1)
43
- assert_equal "エントリ弐", Enum1.name_by_key(:entry2)
44
- assert_equal "エントリ参", Enum1.name_by_key(:entry3)
45
- assert_equal ["エントリ壱", "エントリ弐", "エントリ参"], Enum1.names
46
-
47
- I18n.locale = 'en'
48
- assert_equal "entry one", Enum1.name_by_key(:entry1)
49
- assert_equal "entry two", Enum1.name_by_key(:entry2)
50
- assert_equal "entry three", Enum1.name_by_key(:entry3)
51
- assert_equal ["entry one", "entry two", "entry three"], Enum1.names
52
- end
53
-
54
- class EnumBase
55
- include ::SelectableAttr::Base
56
- end
57
-
58
- class SelectableAttrMock1 < EnumBase
59
- selectable_attr :attr1, :default => 2 do
60
- i18n_scope(:selectable_attrs, :enum1)
61
- entry 1, :entry1, "エントリ1"
62
- entry 2, :entry2, "エントリ2"
63
- entry 3, :entry3, "エントリ3"
64
- end
65
- end
66
-
67
- def test_attr1_i18n
68
- I18n.default_locale = 'ja'
69
- I18n.locale = nil
70
- # assert_equal nil, I18n.locale
71
- # assert_equal "エントリ1", SelectableAttrMock1.attr1_name_by_key(:entry1)
72
- # assert_equal "エントリ2", SelectableAttrMock1.attr1_name_by_key(:entry2)
73
- # assert_equal "エントリ3", SelectableAttrMock1.attr1_name_by_key(:entry3)
74
- assert_equal 'ja', I18n.locale
75
- assert_equal "エントリ壱", SelectableAttrMock1.attr1_name_by_key(:entry1)
76
- assert_equal "エントリ弐", SelectableAttrMock1.attr1_name_by_key(:entry2)
77
- assert_equal "エントリ参", SelectableAttrMock1.attr1_name_by_key(:entry3)
78
- assert_equal [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]], SelectableAttrMock1.attr1_options
79
-
80
- I18n.locale = 'ja'
81
- assert_equal "エントリ壱", SelectableAttrMock1.attr1_name_by_key(:entry1)
82
- assert_equal "エントリ弐", SelectableAttrMock1.attr1_name_by_key(:entry2)
83
- assert_equal "エントリ参", SelectableAttrMock1.attr1_name_by_key(:entry3)
84
- assert_equal [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]], SelectableAttrMock1.attr1_options
85
-
86
- I18n.locale = 'en'
87
- assert_equal "entry one", SelectableAttrMock1.attr1_name_by_key(:entry1)
88
- assert_equal "entry two", SelectableAttrMock1.attr1_name_by_key(:entry2)
89
- assert_equal "entry three", SelectableAttrMock1.attr1_name_by_key(:entry3)
90
- assert_equal [["entry one",1], ["entry two",2], ["entry three",3]], SelectableAttrMock1.attr1_options
91
- end
92
-
93
- class SelectableAttrMock2 < EnumBase
94
- selectable_attr :enum1, :default => 2 do
95
- i18n_scope(:selectable_attrs, :enum1)
96
- entry 1, :entry1, "エントリ1"
97
- entry 2, :entry2, "エントリ2"
98
- entry 3, :entry3, "エントリ3"
99
- end
100
- end
101
-
102
- def test_enum1_i18n
103
- I18n.default_locale = 'ja'
104
- I18n.locale = nil
105
- # assert_equal nil, I18n.locale
106
- # assert_equal "エントリ1", SelectableAttrMock2.enum1_name_by_key(:entry1)
107
- # assert_equal "エントリ2", SelectableAttrMock2.enum1_name_by_key(:entry2)
108
- # assert_equal "エントリ3", SelectableAttrMock2.enum1_name_by_key(:entry3)
109
- assert_equal 'ja', I18n.locale
110
- assert_equal "エントリ壱", SelectableAttrMock2.enum1_name_by_key(:entry1)
111
- assert_equal "エントリ弐", SelectableAttrMock2.enum1_name_by_key(:entry2)
112
- assert_equal "エントリ参", SelectableAttrMock2.enum1_name_by_key(:entry3)
113
- assert_equal [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]], SelectableAttrMock2.enum1_options
114
-
115
- I18n.locale = 'ja'
116
- assert_equal "エントリ壱", SelectableAttrMock2.enum1_name_by_key(:entry1)
117
- assert_equal "エントリ弐", SelectableAttrMock2.enum1_name_by_key(:entry2)
118
- assert_equal "エントリ参", SelectableAttrMock2.enum1_name_by_key(:entry3)
119
- assert_equal [["エントリ壱",1], ["エントリ弐",2], ["エントリ参",3]], SelectableAttrMock2.enum1_options
120
-
121
- I18n.locale = 'en'
122
- assert_equal "entry one", SelectableAttrMock2.enum1_name_by_key(:entry1)
123
- assert_equal "entry two", SelectableAttrMock2.enum1_name_by_key(:entry2)
124
- assert_equal "entry three", SelectableAttrMock2.enum1_name_by_key(:entry3)
125
- assert_equal [["entry one",1], ["entry two",2], ["entry three",3]], SelectableAttrMock2.enum1_options
126
- end
127
-
128
- # i18n用のlocaleカラムを持つselectable_attrのエントリ名をDB上に保持するためのモデル
129
- class I18nItemMaster < ActiveRecord::Base
130
- end
131
-
132
- # selectable_attrを使った場合その3
133
- # アクセス時に毎回アクセス時にDBから項目名を取得します。
134
- # 対象となる項目名はi18n対応している名称です
135
- class ProductWithI18nDB1 < ActiveRecord::Base
136
- set_table_name 'products'
137
- selectable_attr :product_type_cd do
138
- # update_byメソッドには、エントリのidと名称を返すSELECT文を指定する代わりに、
139
- # エントリのidと名称の配列の配列を返すブロックを指定することも可能です。
140
- update_by(:when => :everytime) do
141
- records = I18nItemMaster.find(:all,
142
- :conditions => [
143
- "category_name = 'product_type_cd' and locale = ? ", I18n.locale.to_s],
144
- :order => "item_no")
145
- records.map{|r| [r.item_cd, r.name]}
146
- end
147
- entry '01', :book, '書籍', :discount => 0.8
148
- entry '02', :dvd, 'DVD', :discount => 0.2
149
- entry '03', :cd, 'CD', :discount => 0.5
150
- entry '09', :other, 'その他', :discount => 1
151
- end
152
-
153
- end
154
-
155
- def test_update_entry_name_with_i18n
156
- I18n.locale = 'ja'
157
- # DBに全くデータがなくてもコードで記述してあるエントリは存在します。
158
- I18nItemMaster.delete_all("category_name = 'product_type_cd'")
159
- assert_equal 4, ProductWithI18nDB1.product_type_entries.length
160
- assert_equal '書籍', ProductWithI18nDB1.product_type_name_by_key(:book)
161
- assert_equal 'DVD', ProductWithI18nDB1.product_type_name_by_key(:dvd)
162
- assert_equal 'CD', ProductWithI18nDB1.product_type_name_by_key(:cd)
163
- assert_equal 'その他', ProductWithI18nDB1.product_type_name_by_key(:other)
164
-
165
- # assert_product_discount(ProductWithI18nDB1)
166
-
167
- # DBからエントリの名称を動的に変更できます
168
- item_book = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '01', :name => '本')
169
- assert_equal 4, ProductWithI18nDB1.product_type_entries.length
170
- assert_equal '本', ProductWithI18nDB1.product_type_name_by_key(:book)
171
- assert_equal 'DVD', ProductWithI18nDB1.product_type_name_by_key(:dvd)
172
- assert_equal 'CD', ProductWithI18nDB1.product_type_name_by_key(:cd)
173
- assert_equal 'その他', ProductWithI18nDB1.product_type_name_by_key(:other)
174
- assert_equal [['本', '01'], ['DVD', '02'], ['CD', '03'], ['その他', '09']], ProductWithI18nDB1.product_type_options
175
-
176
- # DBからエントリの並び順を動的に変更できます
177
- item_book.item_no = 4;
178
- item_book.save!
179
- item_other = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'その他')
180
- item_dvd = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02') # nameは指定しなかったらデフォルトが使われます。
181
- item_cd = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03') # nameは指定しなかったらデフォルトが使われます。
182
- assert_equal [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01']], ProductWithI18nDB1.product_type_options
183
-
184
- # DBからエントリを動的に追加することも可能です。
185
- item_toys = I18nItemMaster.create(:locale => 'ja', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'おもちゃ')
186
- assert_equal [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']], ProductWithI18nDB1.product_type_options
187
- assert_equal :entry_04, ProductWithI18nDB1.product_type_key_by_id('04')
188
-
189
- # 英語名を登録
190
- item_book = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 4, :item_cd => '01', :name => 'Book')
191
- item_other = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'Others')
192
- item_dvd = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02', :name => 'DVD')
193
- item_cd = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03', :name => 'CD')
194
- item_toys = I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'Toy')
195
-
196
- # 英語名が登録されていてもI18n.localeが変わらなければ、日本語のまま
197
- assert_equal [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']], ProductWithI18nDB1.product_type_options
198
- assert_equal :entry_04, ProductWithI18nDB1.product_type_key_by_id('04')
199
-
200
- # I18n.localeを変更すると取得できるエントリの名称も変わります
201
- I18n.locale = 'en'
202
- assert_equal [['Others', '09'], ['DVD', '02'], ['CD', '03'], ['Book', '01'], ['Toy', '04']], ProductWithI18nDB1.product_type_options
203
- assert_equal :entry_04, ProductWithI18nDB1.product_type_key_by_id('04')
204
-
205
- I18n.locale = 'ja'
206
- assert_equal [['その他', '09'], ['DVD', '02'], ['CD', '03'], ['本', '01'], ['おもちゃ', '04']], ProductWithI18nDB1.product_type_options
207
- assert_equal :entry_04, ProductWithI18nDB1.product_type_key_by_id('04')
208
-
209
- I18n.locale = 'en'
210
- assert_equal [['Others', '09'], ['DVD', '02'], ['CD', '03'], ['Book', '01'], ['Toy', '04']], ProductWithI18nDB1.product_type_options
211
- assert_equal :entry_04, ProductWithI18nDB1.product_type_key_by_id('04')
212
-
213
- # DBからレコードを削除してもコードで定義したentryは削除されません。
214
- # 順番はDBからの取得順で並び替えられたものの後になります
215
- item_dvd.destroy
216
- assert_equal [['Others', '09'], ['CD', '03'], ['Book', '01'], ['Toy', '04'], ['DVD', '02']], ProductWithI18nDB1.product_type_options
217
-
218
- # DB上で追加したレコードを削除すると、エントリも削除されます
219
- item_toys.destroy
220
- assert_equal [['Others', '09'], ['CD', '03'], ['Book', '01'], ['DVD', '02']], ProductWithI18nDB1.product_type_options
221
-
222
- # 名称を指定していたDBのレコードを削除したら元に戻ります。
223
- item_book.destroy
224
- assert_equal [['Others', '09'], ['CD', '03'], ['書籍', '01'], ['DVD', '02']], ProductWithI18nDB1.product_type_options
225
-
226
- # エントリに該当するレコードを全部削除したら、元に戻ります。
227
- I18nItemMaster.delete_all("category_name = 'product_type_cd'")
228
- assert_equal [['書籍', '01'], ['DVD', '02'], ['CD', '03'], ['その他', '09']], ProductWithI18nDB1.product_type_options
229
- end
230
-
231
- def test_i18n_export
232
- I18nItemMaster.delete_all("category_name = 'product_type_cd'")
233
-
234
- I18n.locale = 'ja'
235
- actual = SelectableAttr::Enum.i18n_export
236
- assert_equal [:selectable_attrs], actual.keys
237
- assert_equal true, actual[:selectable_attrs].keys.include?(:enum1)
238
- assert_equal(
239
- {:entry1=>"エントリ壱",
240
- :entry2=>"エントリ弐",
241
- :entry3=>"エントリ参"},
242
- actual[:selectable_attrs][:enum1])
243
-
244
- assert_equal true, actual[:selectable_attrs].keys.include?(:SelectableAttrI18nTest)
245
- assert_equal(
246
- {:ProductWithI18nDB1=>
247
- {:product_type_cd=>
248
- {:book=>"書籍", :dvd=>"DVD", :cd=>"CD", :other=>"その他"}}},
249
- actual[:selectable_attrs][:SelectableAttrI18nTest])
250
-
251
-
252
- I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 1, :item_cd => '09', :name => 'Others')
253
- I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 2, :item_cd => '02', :name => 'DVD')
254
- I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 3, :item_cd => '03', :name => 'CD')
255
- I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 4, :item_cd => '01', :name => 'Book')
256
- I18nItemMaster.create(:locale => 'en', :category_name => 'product_type_cd', :item_no => 5, :item_cd => '04', :name => 'Toy')
257
-
258
- I18n.locale = 'en'
259
- actual = SelectableAttr::Enum.i18n_export
260
- assert_equal [:selectable_attrs], actual.keys
261
- assert_equal true, actual[:selectable_attrs].keys.include?(:enum1)
262
- assert_equal(
263
- {:entry1=>"entry one",
264
- :entry2=>"entry two",
265
- :entry3=>"entry three"},
266
- actual[:selectable_attrs][:enum1])
267
- assert_equal true, actual[:selectable_attrs].keys.include?(:SelectableAttrI18nTest)
268
- assert_equal(
269
- {:ProductWithI18nDB1=>
270
- {:product_type_cd=>
271
- {:book=>"Book", :dvd=>"DVD", :cd=>"CD", :other=>"Others", :entry_04=>"Toy"}}},
272
- actual[:selectable_attrs][:SelectableAttrI18nTest])
273
-
274
- end
275
- end
276
- end