dm_preferences 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ module Preferences
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,56 @@
1
+ module Preferences
2
+ # Represents the definition of a preference for a particular model
3
+ class PreferenceDefinition
4
+ # The data type for the content stored in this preference type
5
+ attr_reader :type
6
+
7
+ def initialize(name, *args) #:nodoc:
8
+ options = args.extract_options!
9
+ options.assert_valid_keys(:default, :group_defaults)
10
+
11
+ @type = args.first ? args.first.to_sym : :boolean
12
+
13
+ # Create a column that will be responsible for typecasting
14
+ @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], @type == :any ? nil : @type.to_s)
15
+
16
+ @group_defaults = (options[:group_defaults] || {}).inject({}) do |defaults, (group, default)|
17
+ defaults[group.is_a?(Symbol) ? group.to_s : group] = type_cast(default)
18
+ defaults
19
+ end
20
+ end
21
+
22
+ # The name of the preference
23
+ def name
24
+ @column.name
25
+ end
26
+
27
+ # The default value to use for the preference in case none have been
28
+ # previously defined
29
+ def default_value(group = nil)
30
+ @group_defaults.include?(group) ? @group_defaults[group] : @column.default
31
+ end
32
+
33
+ # Determines whether column backing this preference stores numberic values
34
+ def number?
35
+ @column.number?
36
+ end
37
+
38
+ # Typecasts the value based on the type of preference that was defined.
39
+ # This uses ActiveRecord's typecast functionality so the same rules for
40
+ # typecasting a model's columns apply here.
41
+ def type_cast(value)
42
+ @type == :any ? value : @column.type_cast(value)
43
+ end
44
+
45
+ # Typecasts the value to true/false depending on the type of preference
46
+ def query(value)
47
+ if !(value = type_cast(value))
48
+ false
49
+ elsif number?
50
+ !value.zero?
51
+ else
52
+ !value.blank?
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Preferences
2
+ VERSION = '0.5.6'
3
+ end
@@ -0,0 +1,2 @@
1
+ class Car < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Employee < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Manager < Employee
2
+ preference :health_insurance
3
+ end
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+ # preference :hot_salsa
3
+ # preference :dark_chocolate, :default => true
4
+ # preference :color, :string
5
+ # preference :car, :integer
6
+ # preference :language, :string, :default => 'English'
7
+ # preference :data, :any
8
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :login, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateCars < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :cars do |t|
4
+ t.string :name, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :cars
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreateEmployees < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :employees do |t|
4
+ t.string :name, :null => false
5
+ t.string :type
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :employees
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class MigratePreferencesToVersion1 < ActiveRecord::Migration
2
+ def self.up
3
+ ActiveRecord::Migrator.new(:up, "#{Rails.root}/../../generators/preferences/templates", 0).migrations.each do |migration|
4
+ migration.migrate(:up)
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ ActiveRecord::Migrator.new(:down, "#{Rails.root}/../../generators/preferences/templates", 0).migrations.each do |migration|
10
+ migration.migrate(:down)
11
+ end
12
+ end
13
+ end
data/test/factory.rb ADDED
@@ -0,0 +1,65 @@
1
+ module Factory
2
+ # Build actions for the model
3
+ def self.build(model, &block)
4
+ name = model.to_s.underscore
5
+
6
+ define_method("#{name}_attributes", block)
7
+ define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
8
+ define_method("new_#{name}") {|*args| new_record(model, *args)}
9
+ define_method("create_#{name}") {|*args| create_record(model, *args)}
10
+ end
11
+
12
+ # Get valid attributes for the model
13
+ def valid_attributes_for(model, attributes = {})
14
+ name = model.to_s.underscore
15
+ send("#{name}_attributes", attributes)
16
+ attributes.stringify_keys!
17
+ attributes
18
+ end
19
+
20
+ # Build an unsaved record
21
+ def new_record(model, *args)
22
+ attributes = valid_attributes_for(model, *args)
23
+ record = model.new(attributes)
24
+ attributes.each {|attr, value| record.send("#{attr}=", value) if model.accessible_attributes && !model.accessible_attributes.include?(attr) || model.protected_attributes && model.protected_attributes.include?(attr)}
25
+ record
26
+ end
27
+
28
+ # Build and save/reload a record
29
+ def create_record(model, *args)
30
+ record = new_record(model, *args)
31
+ record.save!
32
+ record.reload
33
+ record
34
+ end
35
+
36
+ build Car do |attributes|
37
+ attributes.reverse_merge!(
38
+ :name => 'Porsche'
39
+ )
40
+ end
41
+
42
+ build Employee do |attributes|
43
+ attributes.reverse_merge!(
44
+ :name => 'John Smith'
45
+ )
46
+ end
47
+
48
+ build Manager do |attributes|
49
+ valid_employee_attributes(attributes)
50
+ end
51
+
52
+ build Preference do |attributes|
53
+ attributes[:owner] = create_user unless attributes.include?(:owner)
54
+ attributes.reverse_merge!(
55
+ :name => 'notifications',
56
+ :value => false
57
+ )
58
+ end
59
+
60
+ build User do |attributes|
61
+ attributes.reverse_merge!(
62
+ :login => 'admin'
63
+ )
64
+ end
65
+ end
@@ -0,0 +1,1387 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class ModelPreferenceTest < ActiveRecord::TestCase
4
+ def default_test
5
+ end
6
+
7
+ def teardown
8
+ User.preference_definitions.clear
9
+ end
10
+ end
11
+
12
+ class ModelWithoutPreferencesTest < ActiveRecord::TestCase
13
+ def test_should_not_create_preference_definitions
14
+ assert !Car.respond_to?(:preference_definitions)
15
+ end
16
+
17
+ def test_should_not_create_stored_preferences_association
18
+ assert !Car.new.respond_to?(:stored_preferences)
19
+ end
20
+
21
+ def test_should_not_create_preference_scopes
22
+ assert !Car.respond_to?(:with_preferences)
23
+ assert !Car.respond_to?(:without_preferences)
24
+ end
25
+ end
26
+
27
+ class PreferencesAfterFirstBeingDefinedTest < ModelPreferenceTest
28
+ def setup
29
+ User.preference :notifications
30
+ @user = new_user
31
+ end
32
+
33
+ def test_should_create_preference_definitions
34
+ assert User.respond_to?(:preference_definitions)
35
+ end
36
+
37
+ def test_should_create_preference_scopes
38
+ assert User.respond_to?(:with_preferences)
39
+ assert User.respond_to?(:without_preferences)
40
+ end
41
+
42
+ def test_should_create_stored_preferences_associations
43
+ assert @user.respond_to?(:stored_preferences)
44
+ end
45
+ end
46
+
47
+ class PreferencesAfterBeingDefinedTest < ModelPreferenceTest
48
+ def setup
49
+ @definition = User.preference :notifications
50
+ @user = new_user
51
+ end
52
+
53
+ def test_should_raise_exception_if_invalid_options_specified
54
+ assert_raise(ArgumentError) {User.preference :notifications, :invalid => true}
55
+ assert_raise(ArgumentError) {User.preference :notifications, :boolean, :invalid => true}
56
+ end
57
+
58
+ def test_should_create_definition
59
+ assert_not_nil @definition
60
+ assert_equal 'notifications', @definition.name
61
+ end
62
+
63
+ def test_should_create_preferred_query_method
64
+ assert @user.respond_to?(:preferred_notifications?)
65
+ end
66
+
67
+ def test_should_create_prefers_query_method
68
+ assert @user.respond_to?(:prefers_notifications?)
69
+ end
70
+
71
+ def test_should_create_preferred_reader
72
+ assert @user.respond_to?(:preferred_notifications)
73
+ end
74
+
75
+ def test_should_create_prefers_reader
76
+ assert @user.respond_to?(:prefers_notifications)
77
+ end
78
+
79
+ def test_should_create_preferred_writer
80
+ assert @user.respond_to?(:preferred_notifications=)
81
+ end
82
+
83
+ def test_should_create_prefers_writer
84
+ assert @user.respond_to?(:prefers_notifications=)
85
+ end
86
+
87
+ def test_should_create_preferred_changed_query
88
+ assert @user.respond_to?(:preferred_notifications_changed?)
89
+ end
90
+
91
+ def test_should_create_prefers_changed_query
92
+ assert @user.respond_to?(:prefers_notifications_changed?)
93
+ end
94
+
95
+ def test_should_create_preferred_was
96
+ assert @user.respond_to?(:preferred_notifications_was)
97
+ end
98
+
99
+ def test_should_create_prefers_was
100
+ assert @user.respond_to?(:prefers_notifications_was)
101
+ end
102
+
103
+ def test_should_create_preferred_change
104
+ assert @user.respond_to?(:preferred_notifications_change)
105
+ end
106
+
107
+ def test_should_create_prefers_change
108
+ assert @user.respond_to?(:prefers_notifications_change)
109
+ end
110
+
111
+ def test_should_create_preferred_will_change
112
+ assert @user.respond_to?(:preferred_notifications_will_change!)
113
+ end
114
+
115
+ def test_should_create_prefers_will_change
116
+ assert @user.respond_to?(:prefers_notifications_will_change!)
117
+ end
118
+
119
+ def test_should_create_preferred_reset
120
+ assert @user.respond_to?(:reset_preferred_notifications!)
121
+ end
122
+
123
+ def test_should_create_prefers_reset
124
+ assert @user.respond_to?(:reset_prefers_notifications!)
125
+ end
126
+
127
+ def test_should_include_new_definitions_in_preference_definitions
128
+ assert_equal e = {'notifications' => @definition}, User.preference_definitions
129
+ end
130
+ end
131
+
132
+ class PreferencesTypeCasted < ModelPreferenceTest
133
+ def setup
134
+ @definition = User.preference :rate, :float, :default => 1.0
135
+ @user = new_user
136
+ end
137
+
138
+ def test_should_have_float_type
139
+ assert_equal :float, @definition.type
140
+ end
141
+
142
+ def test_should_only_have_default_preferences
143
+ assert_equal e = {'rate' => 1.0}, @user.preferences
144
+ end
145
+
146
+ def test_should_type_cast_changed_values
147
+ @user.write_preference(:rate, "1.1")
148
+ assert_equal e = {'rate' => 1.1}, @user.preferences
149
+ end
150
+ end
151
+
152
+ class PreferencesByDefaultTest < ModelPreferenceTest
153
+ def setup
154
+ @definition = User.preference :notifications
155
+ @user = new_user
156
+ end
157
+
158
+ def test_should_have_boolean_type
159
+ assert_equal :boolean, @definition.type
160
+ end
161
+
162
+ def test_should_not_have_default_value
163
+ assert_nil @definition.default_value
164
+ end
165
+
166
+ def test_should_only_have_default_preferences
167
+ assert_equal e = {'notifications' => nil}, @user.preferences
168
+ end
169
+
170
+ def test_should_not_query_preferences_changed
171
+ assert_equal false, @user.preferences_changed?
172
+ end
173
+
174
+ def test_should_not_query_group_preferences_changed
175
+ assert_equal false, @user.preferences_changed?(:chat)
176
+ end
177
+
178
+ def test_should_not_have_preferences_changed
179
+ assert_equal [], @user.preferences_changed
180
+ end
181
+
182
+ def test_should_not_have_group_preferences_changed
183
+ assert_equal [], @user.preferences_changed(:chat)
184
+ end
185
+
186
+ def test_should_not_have_preference_changes
187
+ assert_equal e = {}, @user.preference_changes
188
+ end
189
+
190
+ def test_should_not_have_group_preference_changes
191
+ assert_equal e = {}, @user.preference_changes(:chat)
192
+ end
193
+ end
194
+
195
+ class PreferencesWithCustomTypeTest < ModelPreferenceTest
196
+ def setup
197
+ @definition = User.preference :vehicle_id, :integer
198
+ @user = new_user
199
+ end
200
+
201
+ def test_should_have_integer_type
202
+ assert_equal :integer, @definition.type
203
+ end
204
+
205
+ def test_should_not_have_default_value
206
+ assert_nil @definition.default_value
207
+ end
208
+
209
+ def test_should_only_have_default_preferences
210
+ assert_equal e = {'vehicle_id' => nil}, @user.preferences
211
+ end
212
+ end
213
+
214
+ class PreferencesWithCustomDefaultTest < ModelPreferenceTest
215
+ def setup
216
+ @definition = User.preference :color, :string, :default => 'red'
217
+ @user = new_user
218
+ end
219
+
220
+ def test_should_have_boolean_type
221
+ assert_equal :string, @definition.type
222
+ end
223
+
224
+ def test_should_have_default_value
225
+ assert_equal 'red', @definition.default_value
226
+ end
227
+
228
+ def test_should_only_have_default_preferences
229
+ assert_equal e = {'color' => 'red'}, @user.preferences
230
+ end
231
+ end
232
+
233
+ class PreferencesWithMultipleDefinitionsTest < ModelPreferenceTest
234
+ def setup
235
+ User.preference :notifications, :default => true
236
+ User.preference :color, :string, :default => 'red'
237
+ @user = new_user
238
+ end
239
+
240
+ def test_should_only_have_default_preferences
241
+ assert_equal e = {'notifications' => true, 'color' => 'red'}, @user.preferences
242
+ end
243
+ end
244
+
245
+ class PreferencesAfterBeingCreatedTest < ModelPreferenceTest
246
+ def setup
247
+ User.preference :notifications, :default => true
248
+ @user = create_user
249
+ end
250
+
251
+ def test_should_not_have_any_stored_preferences
252
+ assert @user.stored_preferences.empty?
253
+ end
254
+ end
255
+
256
+ class PreferencesReaderTest < ModelPreferenceTest
257
+ def setup
258
+ User.preference :notifications, :default => true
259
+ User.preference :rate, :float, :default => 1.0
260
+ @user = create_user
261
+ end
262
+
263
+ def test_should_raise_exception_if_invalid_preference_read
264
+ exception = assert_raise(ArgumentError) { @user.preferred(:invalid) }
265
+ assert_equal 'Unknown preference: invalid', exception.message
266
+ end
267
+
268
+ def test_use_default_value_if_not_stored
269
+ assert_equal true, @user.preferred(:notifications)
270
+ end
271
+
272
+ def test_should_use_group_default_value_if_not_stored
273
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
274
+ assert_equal 'English', @user.preferred(:language)
275
+ end
276
+
277
+ def test_should_use_stored_value_if_stored
278
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
279
+ assert_equal false, @user.preferred(:notifications)
280
+ end
281
+
282
+ def test_should_type_cast_based_on_preference_definition
283
+ @user.write_preference(:notifications, 'false')
284
+ assert_equal false, @user.preferred(:notifications)
285
+ @user.write_preference(:rate, "1.2")
286
+ assert_equal 1.2, @user.preferred(:rate)
287
+ end
288
+
289
+ def test_should_cache_stored_values
290
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
291
+ assert_queries(1) { @user.preferred(:notifications) }
292
+ assert_queries(0) { @user.preferred(:notifications) }
293
+ end
294
+
295
+ def test_should_not_query_if_preferences_already_loaded
296
+ @user.preferences
297
+ assert_queries(0) { @user.preferred(:notifications) }
298
+ end
299
+
300
+ def test_should_use_value_from_preferences_lookup
301
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
302
+ @user.preferences
303
+
304
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications) }
305
+ end
306
+
307
+ def test_should_be_able_to_use_prefers_reader
308
+ assert_equal true, @user.prefers_notifications
309
+ end
310
+
311
+ def test_should_be_able_to_use_preferred_reader
312
+ assert_equal true, @user.preferred_notifications
313
+ end
314
+ end
315
+
316
+ class PreferencesGroupReaderTest < ModelPreferenceTest
317
+ def setup
318
+ User.preference :notifications, :default => true
319
+ @user = create_user
320
+ end
321
+
322
+ def test_should_use_default_value_if_not_stored
323
+ assert_equal true, @user.preferred(:notifications, :chat)
324
+ end
325
+
326
+ def test_should_use_group_default_value_if_not_stored
327
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
328
+ assert_equal 'Latin', @user.preferred(:language, :chat)
329
+ end
330
+
331
+ def test_should_use_stored_value_if_stored
332
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
333
+ assert_equal false, @user.preferred(:notifications, :chat)
334
+ end
335
+
336
+ def test_should_cache_stored_values
337
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
338
+ assert_queries(1) { @user.preferred(:notifications, :chat) }
339
+ assert_queries(0) { @user.preferred(:notifications, :chat) }
340
+ end
341
+
342
+ def test_should_not_query_if_preferences_already_loaded
343
+ @user.preferences(:chat)
344
+ assert_queries(0) { @user.preferred(:notifications, :chat) }
345
+ end
346
+
347
+ def test_should_use_value_from_preferences_lookup
348
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
349
+ @user.preferences(:chat)
350
+
351
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications, :chat) }
352
+ end
353
+
354
+ def test_should_be_able_to_use_prefers_reader
355
+ assert_equal true, @user.prefers_notifications(:chat)
356
+ end
357
+
358
+ def test_should_be_able_to_use_preferred_reader
359
+ assert_equal true, @user.preferred_notifications(:chat)
360
+ end
361
+ end
362
+
363
+ class PreferencesARGroupReaderTest < ModelPreferenceTest
364
+ def setup
365
+ @car = create_car
366
+
367
+ User.preference :notifications, :default => true
368
+ @user = create_user
369
+ end
370
+
371
+ def test_use_default_value_if_not_stored
372
+ assert_equal true, @user.preferred(:notifications, @car)
373
+ end
374
+
375
+ def test_should_use_stored_value_if_stored
376
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
377
+ assert_equal false, @user.preferred(:notifications, @car)
378
+ end
379
+
380
+ def test_should_use_value_from_preferences_lookup
381
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
382
+ @user.preferences(@car)
383
+
384
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications, @car) }
385
+ end
386
+
387
+ def test_should_be_able_to_use_prefers_reader
388
+ assert_equal true, @user.prefers_notifications(@car)
389
+ end
390
+
391
+ def test_should_be_able_to_use_preferred_reader
392
+ assert_equal true, @user.preferred_notifications(@car)
393
+ end
394
+ end
395
+
396
+ class PreferencesQueryTest < ModelPreferenceTest
397
+ def setup
398
+ User.preference :language, :string
399
+ @user = create_user
400
+ end
401
+
402
+ def test_should_raise_exception_if_invalid_preference_queried
403
+ exception = assert_raise(ArgumentError) { @user.preferred?(:invalid) }
404
+ assert_equal 'Unknown preference: invalid', exception.message
405
+ end
406
+
407
+ def test_should_be_true_if_present
408
+ @user.preferred_language = 'English'
409
+ assert_equal true, @user.preferred?(:language)
410
+ end
411
+
412
+ def test_should_be_false_if_not_present
413
+ assert_equal false, @user.preferred?(:language)
414
+ end
415
+
416
+ def test_should_use_stored_value_if_stored
417
+ create_preference(:owner => @user, :name => 'language', :value => 'English')
418
+ assert_equal true, @user.preferred?(:language)
419
+ end
420
+
421
+ def test_should_cache_stored_values
422
+ create_preference(:owner => @user, :name => 'language', :value => 'English')
423
+ assert_queries(1) { @user.preferred?(:language) }
424
+ assert_queries(0) { @user.preferred?(:language) }
425
+ end
426
+
427
+ def test_should_be_able_to_use_prefers_reader
428
+ assert_equal false, @user.prefers_language?
429
+ end
430
+
431
+ def test_should_be_able_to_use_preferred_reader
432
+ assert_equal false, @user.preferred_language?
433
+ end
434
+ end
435
+
436
+ class PreferencesGroupQueryTest < ModelPreferenceTest
437
+ def setup
438
+ User.preference :language, :string
439
+ @user = create_user
440
+ end
441
+
442
+ def test_should_be_true_if_present
443
+ @user.preferred_language = 'English', :chat
444
+ assert_equal true, @user.preferred?(:language, :chat)
445
+ end
446
+
447
+ def test_should_be_false_if_not_present
448
+ assert_equal false, @user.preferred?(:language, :chat)
449
+ end
450
+
451
+ def test_should_use_stored_value_if_stored
452
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'language', :value => 'English')
453
+ assert_equal true, @user.preferred?(:language, :chat)
454
+ end
455
+
456
+ def test_should_cache_stored_values
457
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'language', :value => 'English')
458
+ assert_queries(1) { @user.preferred?(:language, :chat) }
459
+ assert_queries(0) { @user.preferred?(:language, :chat) }
460
+ end
461
+
462
+ def test_should_be_able_to_use_prefers_reader
463
+ assert_equal false, @user.prefers_language?(:chat)
464
+ end
465
+
466
+ def test_should_be_able_to_use_preferred_reader
467
+ assert_equal false, @user.preferred_language?(:chat)
468
+ end
469
+ end
470
+
471
+ class PreferencesARGroupQueryTest < ModelPreferenceTest
472
+ def setup
473
+ @car = create_car
474
+
475
+ User.preference :language, :string
476
+ @user = create_user
477
+ end
478
+
479
+ def test_should_be_true_if_present
480
+ @user.preferred_language = 'English', @car
481
+ assert_equal true, @user.preferred?(:language, @car)
482
+ end
483
+
484
+ def test_should_be_false_if_not_present
485
+ assert_equal false, @user.preferred?(:language, @car)
486
+ end
487
+
488
+ def test_should_use_stored_value_if_stored
489
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'language', :value => 'English')
490
+ assert_equal true, @user.preferred?(:language, @car)
491
+ end
492
+
493
+ def test_should_be_able_to_use_prefers_reader
494
+ assert_equal false, @user.prefers_language?(@car)
495
+ end
496
+
497
+ def test_should_be_able_to_use_preferred_reader
498
+ assert_equal false, @user.preferred_language?(@car)
499
+ end
500
+ end
501
+
502
+ class PreferencesWriterTest < ModelPreferenceTest
503
+ def setup
504
+ User.preference :notifications, :boolean, :default => true
505
+ User.preference :language, :string, :default => 'English'
506
+ @user = create_user(:login => 'admin')
507
+ end
508
+
509
+ def test_should_raise_exception_if_invalid_preference_written
510
+ exception = assert_raise(ArgumentError) { @user.write_preference(:invalid, true) }
511
+ assert_equal 'Unknown preference: invalid', exception.message
512
+ end
513
+
514
+ def test_should_have_same_value_if_not_changed
515
+ @user.write_preference(:notifications, true)
516
+ assert_equal true, @user.preferred(:notifications)
517
+ end
518
+
519
+ def test_should_use_new_value_if_changed
520
+ @user.write_preference(:notifications, false)
521
+ assert_equal false, @user.preferred(:notifications)
522
+ end
523
+
524
+ def test_should_not_save_record_after_changing_preference
525
+ @user.login = 'test'
526
+ @user.write_preference(:notifications, false)
527
+
528
+ assert_equal 'admin', User.find(@user.id).login
529
+ end
530
+
531
+ def test_should_not_create_stored_preferences_immediately
532
+ @user.write_preference(:notifications, false)
533
+ assert @user.stored_preferences.empty?
534
+ end
535
+
536
+ def test_should_not_create_stored_preference_if_value_not_changed
537
+ @user.write_preference(:notifications, true)
538
+ @user.save!
539
+
540
+ assert_equal 0, @user.stored_preferences.count
541
+ end
542
+
543
+ def test_should_not_create_stored_integer_preference_if_typecast_not_changed
544
+ User.preference :age, :integer
545
+
546
+ @user.write_preference(:age, '')
547
+ @user.save!
548
+
549
+ assert_equal 0, @user.stored_preferences.count
550
+ end
551
+
552
+ def test_should_create_stored_integer_preference_if_typecast_changed
553
+ User.preference :age, :integer, :default => 0
554
+
555
+ @user.write_preference(:age, '')
556
+ @user.save!
557
+
558
+ assert_nil @user.preferred(:age)
559
+ assert_equal 1, @user.stored_preferences.count
560
+ end
561
+
562
+ def test_should_create_stored_preference_if_value_changed
563
+ @user.write_preference(:notifications, false)
564
+ @user.save!
565
+
566
+ assert_equal 1, @user.stored_preferences.count
567
+ end
568
+
569
+ def test_should_overwrite_existing_stored_preference_if_value_changed
570
+ preference = create_preference(:owner => @user, :name => 'notifications', :value => true)
571
+
572
+ @user.write_preference(:notifications, false)
573
+ @user.save!
574
+
575
+ preference.reload
576
+ assert_equal false, preference.value
577
+ end
578
+
579
+ def test_should_not_remove_preference_if_set_to_default
580
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
581
+
582
+ @user.write_preference(:notifications, true)
583
+ @user.save!
584
+ @user.reload
585
+
586
+ assert_equal 1, @user.stored_preferences.size
587
+ preference = @user.stored_preferences.first
588
+ assert_equal true, preference.value
589
+ end
590
+
591
+ def test_should_not_remove_preference_if_set_to_nil
592
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
593
+
594
+ @user.write_preference(:notifications, nil)
595
+ @user.save!
596
+ @user.reload
597
+
598
+ assert_equal 1, @user.stored_preferences.size
599
+ preference = @user.stored_preferences.first
600
+ assert_nil preference.value
601
+ end
602
+
603
+ def test_should_not_query_for_old_value_if_preferences_loaded
604
+ @user.preferences
605
+
606
+ assert_queries(0) { @user.write_preference(:notifications, false) }
607
+ end
608
+ end
609
+
610
+ class PreferencesGroupWriterTest < ModelPreferenceTest
611
+ def setup
612
+ User.preference :notifications, :boolean, :default => true
613
+ User.preference :language, :string, :default => 'English'
614
+ @user = create_user(:login => 'admin')
615
+ end
616
+
617
+ def test_should_have_same_value_if_not_changed
618
+ @user.write_preference(:notifications, true, :chat)
619
+ assert_equal true, @user.preferred(:notifications, :chat)
620
+ end
621
+
622
+ def test_should_use_new_value_if_changed
623
+ @user.write_preference(:notifications, false, :chat)
624
+ assert_equal false, @user.preferred(:notifications, :chat)
625
+ end
626
+
627
+ def test_should_not_create_stored_preference_if_value_not_changed
628
+ @user.write_preference(:notifications, true, :chat)
629
+ @user.save!
630
+
631
+ assert_equal 0, @user.stored_preferences.count
632
+ end
633
+
634
+ def test_should_create_stored_preference_if_value_changed
635
+ @user.write_preference(:notifications, false, :chat)
636
+ @user.save!
637
+
638
+ assert_equal 1, @user.stored_preferences.count
639
+ end
640
+
641
+ def test_should_set_group_attributes_on_stored_preferences
642
+ @user.write_preference(:notifications, false, :chat)
643
+ @user.save!
644
+
645
+ preference = @user.stored_preferences.first
646
+ assert_equal 'chat', preference.group_type
647
+ assert_nil preference.group_id
648
+ end
649
+
650
+ def test_should_overwrite_existing_stored_preference_if_value_changed
651
+ preference = create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => true)
652
+
653
+ @user.write_preference(:notifications, false, :chat)
654
+ @user.save!
655
+
656
+ preference.reload
657
+ assert_equal false, preference.value
658
+ end
659
+ end
660
+
661
+ class PreferencesARGroupWriterTest < ModelPreferenceTest
662
+ def setup
663
+ @car = create_car
664
+
665
+ User.preference :notifications, :boolean, :default => true
666
+ User.preference :language, :string, :default => 'English'
667
+ @user = create_user(:login => 'admin')
668
+ end
669
+
670
+ def test_should_have_same_value_if_not_changed
671
+ @user.write_preference(:notifications, true, @car)
672
+ assert_equal true, @user.preferred(:notifications, @car)
673
+ end
674
+
675
+ def test_should_use_new_value_if_changed
676
+ @user.write_preference(:notifications, false, @car)
677
+ assert_equal false, @user.preferred(:notifications, @car)
678
+ end
679
+
680
+ def test_should_not_create_stored_preference_if_value_not_changed
681
+ @user.write_preference(:notifications, true, @car)
682
+ @user.save!
683
+
684
+ assert_equal 0, @user.stored_preferences.count
685
+ end
686
+
687
+ def test_should_create_stored_preference_if_value_changed
688
+ @user.write_preference(:notifications, false, @car)
689
+ @user.save!
690
+
691
+ assert_equal 1, @user.stored_preferences.count
692
+ end
693
+
694
+ def test_should_set_group_attributes_on_stored_preferences
695
+ @user.write_preference(:notifications, false, @car)
696
+ @user.save!
697
+
698
+ preference = @user.stored_preferences.first
699
+ assert_equal 'Car', preference.group_type
700
+ assert_equal @car.id, preference.group_id
701
+ end
702
+ end
703
+
704
+ class PreferencesAfterChangingPreferenceTest < ModelPreferenceTest
705
+ def setup
706
+ User.preference :notifications, :boolean, :default => true
707
+ User.preference :language, :string, :default => 'English'
708
+ @user = create_user
709
+
710
+ @user.write_preference(:notifications, false)
711
+ end
712
+
713
+ def test_should_query_preferences_changed
714
+ assert_equal true, @user.preferences_changed?
715
+ end
716
+
717
+ def test_should_query_preference_changed
718
+ assert_equal true, @user.prefers_notifications_changed?
719
+ end
720
+
721
+ def test_should_not_query_preferences_changed_for_group
722
+ assert_equal false, @user.preferences_changed?(:chat)
723
+ end
724
+
725
+ def test_should_not_query_preference_changed_for_group
726
+ assert_equal false, @user.prefers_notifications_changed?(:chat)
727
+ end
728
+
729
+ def test_should_have_preferences_changed
730
+ assert_equal ['notifications'], @user.preferences_changed
731
+ end
732
+
733
+ def test_should_not_build_same_preferences_changed_result
734
+ assert_not_same @user.preferences_changed, @user.preferences_changed
735
+ end
736
+
737
+ def test_should_not_have_preferences_changed_for_group
738
+ assert_equal [], @user.preferences_changed(:chat)
739
+ end
740
+
741
+ def test_should_track_multiple_preferences_changed
742
+ @user.write_preference(:language, 'Latin')
743
+ assert_equal ['language', 'notifications'], @user.preferences_changed.sort
744
+ end
745
+
746
+ def test_should_have_preference_changes
747
+ assert_equal e = {'notifications' => [true, false]}, @user.preference_changes
748
+ end
749
+
750
+ def test_should_not_build_same_preference_changes_result
751
+ assert_not_same @user.preference_changes, @user.preference_changes
752
+ end
753
+
754
+ def test_should_have_preference_change
755
+ assert_equal [true, false], @user.prefers_notifications_change
756
+ end
757
+
758
+ def test_should_have_preference_was
759
+ assert_equal true, @user.prefers_notifications_was
760
+ end
761
+
762
+ def test_should_not_have_preference_changes_for_group
763
+ assert_equal e = {}, @user.preference_changes(:chat)
764
+ end
765
+
766
+ def test_should_not_have_preference_change_for_group
767
+ assert_nil @user.prefers_notifications_change(:chat)
768
+ end
769
+
770
+ def test_should_have_preference_was_for_group
771
+ assert_equal true, @user.prefers_notifications_was(:chat)
772
+ end
773
+
774
+ def test_should_use_latest_value_for_preference_changes
775
+ @user.write_preference(:notifications, nil)
776
+ assert_equal e = {'notifications' => [true, nil]}, @user.preference_changes
777
+ end
778
+
779
+ def test_should_use_cloned_old_value_for_preference_changes
780
+ old_value = @user.preferred(:language)
781
+ @user.write_preference(:language, 'Latin')
782
+
783
+ tracked_old_value = @user.preference_changes['language'][0]
784
+ assert_equal old_value, tracked_old_value
785
+ assert_not_same old_value, tracked_old_value
786
+ end
787
+
788
+ def test_should_track_multiple_preference_changes
789
+ @user.write_preference(:language, 'Latin')
790
+ assert_equal e = {'notifications' => [true, false], 'language' => ['English', 'Latin']}, @user.preference_changes
791
+ end
792
+ end
793
+
794
+ class PreferencesAfterChangingGroupPreferenceTest < ModelPreferenceTest
795
+ def setup
796
+ User.preference :notifications, :boolean, :default => true
797
+ User.preference :language, :string, :default => 'English'
798
+ @user = create_user
799
+
800
+ @user.write_preference(:notifications, false, :chat)
801
+ end
802
+
803
+ def test_should_not_query_preferences_changed
804
+ assert_equal false, @user.preferences_changed?
805
+ end
806
+
807
+ def test_not_should_query_preference_changed
808
+ assert_equal false, @user.prefers_notifications_changed?
809
+ end
810
+
811
+ def test_should_query_preferences_changed_for_group
812
+ assert_equal true, @user.preferences_changed?(:chat)
813
+ end
814
+
815
+ def test_should_query_preference_changed_for_group
816
+ assert_equal true, @user.prefers_notifications_changed?(:chat)
817
+ end
818
+
819
+ def test_should_have_preferences_changed
820
+ assert_equal [], @user.preferences_changed
821
+ end
822
+
823
+ def test_should_not_have_preferences_changed_for_group
824
+ assert_equal ['notifications'], @user.preferences_changed(:chat)
825
+ end
826
+
827
+ def test_should_have_preference_changes
828
+ assert_equal e = {}, @user.preference_changes
829
+ end
830
+
831
+ def test_should_not_have_preference_change
832
+ assert_nil @user.prefers_notifications_change
833
+ end
834
+
835
+ def test_should_have_preference_was
836
+ assert_equal true, @user.prefers_notifications_was
837
+ end
838
+
839
+ def test_should_not_have_preference_changes_for_group
840
+ assert_equal e = {'notifications' => [true, false]}, @user.preference_changes(:chat)
841
+ end
842
+
843
+ def test_should_have_preference_change_for_group
844
+ assert_equal [true, false], @user.prefers_notifications_change(:chat)
845
+ end
846
+
847
+ def test_should_have_preference_was_for_group
848
+ assert_equal true, @user.prefers_notifications_was(:chat)
849
+ end
850
+ end
851
+
852
+ class PreferencesAfterRevertPreferenceChangeTest < ModelPreferenceTest
853
+ def setup
854
+ User.preference :notifications, :boolean, :default => true
855
+
856
+ @user = create_user
857
+ @user.write_preference(:notifications, false)
858
+ @user.write_preference(:notifications, true)
859
+ end
860
+
861
+ def test_should_not_query_preferences_changed
862
+ assert_equal false, @user.preferences_changed?
863
+ end
864
+
865
+ def test_should_not_have_preferences_changed
866
+ assert_equal [], @user.preferences_changed
867
+ end
868
+
869
+ def test_should_not_have_preference_changes
870
+ assert_equal e = {}, @user.preference_changes
871
+ end
872
+ end
873
+
874
+ class PreferencesAfterForcingChangeTest < ModelPreferenceTest
875
+ def setup
876
+ User.preference :notifications, :boolean, :default => true
877
+
878
+ @user = create_user
879
+ @user.prefers_notifications_will_change!
880
+ @user.save
881
+ end
882
+
883
+ def test_should_store_preference
884
+ assert_equal 1, @user.stored_preferences.count
885
+
886
+ preference = @user.stored_preferences.first
887
+ assert_equal nil, preference.group_type
888
+ assert_equal nil, preference.group_id
889
+ assert_equal true, preference.value
890
+ end
891
+ end
892
+
893
+ class PreferencesAfterForcingChangeForGroupTest < ModelPreferenceTest
894
+ def setup
895
+ User.preference :notifications, :boolean, :default => true
896
+ User.preference :language, :string, :default => 'English'
897
+
898
+ @user = create_user
899
+ @user.prefers_notifications_will_change!(:chat)
900
+ @user.save
901
+ end
902
+
903
+ def test_should_store_preference
904
+ assert_equal 1, @user.stored_preferences.count
905
+
906
+ preference = @user.stored_preferences.first
907
+ assert_equal 'chat', preference.group_type
908
+ assert_equal nil, preference.group_id
909
+ assert_equal true, preference.value
910
+ end
911
+
912
+ def test_should_use_cloned_value_for_tracking_old_value
913
+ old_value = @user.preferred(:language)
914
+ @user.preferred_language_will_change!
915
+
916
+ tracked_old_value = @user.preferred_language_was
917
+ assert_equal old_value, tracked_old_value
918
+ assert_not_same old_value, tracked_old_value
919
+ end
920
+ end
921
+
922
+ class PreferencesAfterResettingPreferenceTest < ModelPreferenceTest
923
+ def setup
924
+ User.preference :notifications, :boolean, :default => true
925
+
926
+ @user = create_user
927
+ @user.write_preference(:notifications, false)
928
+ @user.write_preference(:notifications, false, :chat)
929
+ @user.reset_prefers_notifications!
930
+ end
931
+
932
+ def test_should_revert_to_original_value
933
+ assert_equal true, @user.preferred(:notifications)
934
+ end
935
+
936
+ def test_should_not_reset_groups
937
+ assert_equal true, @user.preferred(:notifications, :chat)
938
+ end
939
+ end
940
+
941
+ class PreferencesAfterResettingPreferenceTest < ModelPreferenceTest
942
+ def setup
943
+ User.preference :notifications, :boolean, :default => true
944
+
945
+ @user = create_user
946
+ @user.write_preference(:notifications, false)
947
+ @user.write_preference(:notifications, false, :chat)
948
+ @user.reset_prefers_notifications!(:chat)
949
+ end
950
+
951
+ def test_should_revert_to_original_value
952
+ assert_equal true, @user.preferred(:notifications, :chat)
953
+ end
954
+
955
+ def test_should_not_reset_default_group
956
+ assert_equal false, @user.preferred(:notifications)
957
+ end
958
+ end
959
+
960
+ class PreferencesLookupTest < ModelPreferenceTest
961
+ def setup
962
+ User.preference :notifications, :boolean, :default => true
963
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
964
+
965
+ @user = create_user
966
+ end
967
+
968
+ def test_should_only_have_defaults_if_nothing_customized
969
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
970
+ end
971
+
972
+ def test_should_merge_defaults_with_unsaved_changes
973
+ @user.write_preference(:notifications, false)
974
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
975
+ end
976
+
977
+ def test_should_merge_defaults_with_saved_changes
978
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
979
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
980
+ end
981
+
982
+ def test_should_merge_stored_preferences_with_unsaved_changes
983
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
984
+ @user.write_preference(:language, 'Latin')
985
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences
986
+ end
987
+
988
+ def test_should_use_unsaved_changes_over_stored_preferences
989
+ create_preference(:owner => @user, :name => 'notifications', :value => true)
990
+ @user.write_preference(:notifications, false)
991
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
992
+ end
993
+
994
+ def test_should_typecast_unsaved_changes
995
+ @user.write_preference(:notifications, 'true')
996
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
997
+ end
998
+
999
+ def test_should_cache_results
1000
+ assert_queries(1) { @user.preferences }
1001
+ assert_queries(0) { @user.preferences }
1002
+ end
1003
+
1004
+ def test_should_not_query_if_stored_preferences_eager_loaded
1005
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1006
+ user = User.find(@user.id, :include => :stored_preferences)
1007
+
1008
+ assert_queries(0) do
1009
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, user.preferences
1010
+ end
1011
+ end
1012
+
1013
+ def test_should_not_generate_same_object_twice
1014
+ assert_not_same @user.preferences, @user.preferences
1015
+ end
1016
+
1017
+ def test_should_use_preferences_for_prefs
1018
+ assert_equal @user.preferences, @user.prefs
1019
+ end
1020
+ end
1021
+
1022
+ class PreferencesGroupLookupTest < ModelPreferenceTest
1023
+ def setup
1024
+ User.preference :notifications, :boolean, :default => true
1025
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
1026
+
1027
+ @user = create_user
1028
+ end
1029
+
1030
+ def test_should_only_have_defaults_if_nothing_customized
1031
+ assert_equal e = {'notifications' => true, 'language' => 'Latin'}, @user.preferences(:chat)
1032
+ end
1033
+
1034
+ def test_should_merge_defaults_with_unsaved_changes
1035
+ @user.write_preference(:notifications, false, :chat)
1036
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
1037
+ end
1038
+
1039
+ def test_should_merge_defaults_with_saved_changes
1040
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1041
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
1042
+ end
1043
+
1044
+ def test_should_merge_stored_preferences_with_unsaved_changes
1045
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1046
+ @user.write_preference(:language, 'Spanish', :chat)
1047
+ assert_equal e = {'notifications' => false, 'language' => 'Spanish'}, @user.preferences(:chat)
1048
+ end
1049
+
1050
+ def test_should_typecast_unsaved_changes
1051
+ @user.write_preference(:notifications, 'true', :chat)
1052
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1053
+ end
1054
+
1055
+ def test_should_cache_results
1056
+ assert_queries(1) { @user.preferences(:chat) }
1057
+ assert_queries(0) { @user.preferences(:chat) }
1058
+ end
1059
+
1060
+ def test_should_not_query_if_all_preferences_individually_loaded
1061
+ @user.preferred(:notifications, :chat)
1062
+ @user.preferred(:language, :chat)
1063
+
1064
+ assert_queries(0) { @user.preferences(:chat) }
1065
+ end
1066
+
1067
+ def test_should_not_generate_same_object_twice
1068
+ assert_not_same @user.preferences(:chat), @user.preferences(:chat)
1069
+ end
1070
+ end
1071
+
1072
+ class PreferencesARGroupLookupTest < ModelPreferenceTest
1073
+ def setup
1074
+ @car = create_car
1075
+
1076
+ User.preference :notifications, :boolean, :default => true
1077
+ User.preference :language, :string, :default => 'English'
1078
+
1079
+ @user = create_user
1080
+ end
1081
+
1082
+ def test_should_only_have_defaults_if_nothing_customized
1083
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences(@car)
1084
+ end
1085
+
1086
+ def test_should_merge_defaults_with_unsaved_changes
1087
+ @user.write_preference(:notifications, false, @car)
1088
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
1089
+ end
1090
+
1091
+ def test_should_merge_defaults_with_saved_changes
1092
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
1093
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
1094
+ end
1095
+
1096
+ def test_should_merge_stored_preferences_with_unsaved_changes
1097
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
1098
+ @user.write_preference(:language, 'Latin', @car)
1099
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(@car)
1100
+ end
1101
+ end
1102
+
1103
+ class PreferencesNilGroupLookupTest < ModelPreferenceTest
1104
+ def setup
1105
+ @car = create_car
1106
+
1107
+ User.preference :notifications, :boolean, :default => true
1108
+ User.preference :language, :string, :default => 'English'
1109
+
1110
+ @user = create_user
1111
+ end
1112
+
1113
+ def test_should_only_have_defaults_if_nothing_customized
1114
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences(nil)
1115
+ end
1116
+
1117
+ def test_should_merge_defaults_with_unsaved_changes
1118
+ @user.write_preference(:notifications, false)
1119
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
1120
+ end
1121
+
1122
+ def test_should_merge_defaults_with_saved_changes
1123
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1124
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
1125
+ end
1126
+
1127
+ def test_should_merge_stored_preferences_with_unsaved_changes
1128
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1129
+ @user.write_preference(:language, 'Latin')
1130
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(nil)
1131
+ end
1132
+ end
1133
+
1134
+ class PreferencesLookupWithGroupsTest < ModelPreferenceTest
1135
+ def setup
1136
+ User.preference :notifications, :boolean, :default => true
1137
+ User.preference :language, :string, :default => 'English'
1138
+
1139
+ @user = create_user
1140
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1141
+ end
1142
+
1143
+ def test_not_include_group_preferences_by_default
1144
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1145
+ end
1146
+ end
1147
+
1148
+ class PreferencesAfterBeingReloadedTest < ModelPreferenceTest
1149
+ def setup
1150
+ User.preference :notifications, :boolean, :default => true
1151
+
1152
+ @user = create_user
1153
+ @user.write_preference(:notifications, false)
1154
+ @user.reload
1155
+ end
1156
+
1157
+ def test_should_reset_unsaved_preferences
1158
+ assert_equal true, @user.preferred(:notifications)
1159
+ end
1160
+
1161
+ def test_should_not_save_reset_preferences
1162
+ @user.save!
1163
+ assert @user.stored_preferences.empty?
1164
+ end
1165
+
1166
+ def test_should_reset_preferences
1167
+ assert_equal e = {'notifications' => true}, @user.preferences
1168
+ end
1169
+
1170
+ def test_should_clear_query_cache_for_preferences
1171
+ assert_queries(1) { @user.preferences }
1172
+ end
1173
+
1174
+ def test_should_reset_preferences_changed_query
1175
+ assert_equal false, @user.preferences_changed?
1176
+ end
1177
+
1178
+ def test_should_reset_preferences_changed
1179
+ assert_equal [], @user.preferences_changed
1180
+ end
1181
+
1182
+ def test_should_reset_preference_changes
1183
+ assert_equal e = {}, @user.preference_changes
1184
+ end
1185
+ end
1186
+
1187
+ class PreferencesForGroupAfterBeingReloadedTest < ModelPreferenceTest
1188
+ def setup
1189
+ User.preference :notifications, :boolean, :default => true
1190
+
1191
+ @user = create_user
1192
+ @user.write_preference(:notifications, false, :chat)
1193
+ @user.reload
1194
+ end
1195
+
1196
+ def test_should_reset_unsaved_preferences
1197
+ assert_equal true, @user.preferred(:notifications, :chat)
1198
+ end
1199
+
1200
+ def test_should_reset_preferences
1201
+ assert_equal e = {'notifications' => true}, @user.preferences(:chat)
1202
+ end
1203
+
1204
+ def test_should_clear_query_cache_for_preferences
1205
+ assert_queries(1) { @user.preferences(:chat) }
1206
+ end
1207
+
1208
+ def test_should_reset_preferences_changed_query
1209
+ assert_equal false, @user.preferences_changed?(:chat)
1210
+ end
1211
+
1212
+ def test_should_reset_preferences_changed
1213
+ assert_equal [], @user.preferences_changed(:chat)
1214
+ end
1215
+
1216
+ def test_should_reset_preference_changes
1217
+ assert_equal e = {}, @user.preference_changes(:chat)
1218
+ end
1219
+ end
1220
+
1221
+ class PreferencesWithScopeTest < ModelPreferenceTest
1222
+ def setup
1223
+ User.preference :notifications
1224
+ User.preference :language, :string, :default => 'English'
1225
+ User.preference :color, :string, :default => 'red'
1226
+
1227
+ @user = create_user
1228
+ @customized_user = create_user(:login => 'customized',
1229
+ :prefers_notifications => false,
1230
+ :preferred_language => 'Latin'
1231
+ )
1232
+ @customized_user.prefers_notifications = false, :chat
1233
+ @customized_user.preferred_language = 'Latin', :chat
1234
+ @customized_user.save!
1235
+ end
1236
+
1237
+ def test_should_not_find_if_no_preference_matched
1238
+ assert_equal [], User.with_preferences(:language => 'Italian')
1239
+ end
1240
+
1241
+ def test_should_find_with_null_preference
1242
+ assert_equal [@user], User.with_preferences(:notifications => nil)
1243
+ end
1244
+
1245
+ def test_should_find_with_default_preference
1246
+ assert_equal [@user], User.with_preferences(:language => 'English')
1247
+ end
1248
+
1249
+ def test_should_find_with_multiple_default_preferences
1250
+ assert_equal [@user], User.with_preferences(:notifications => nil, :language => 'English')
1251
+ end
1252
+
1253
+ def test_should_find_with_custom_preference
1254
+ assert_equal [@customized_user], User.with_preferences(:language => 'Latin')
1255
+ end
1256
+
1257
+ def test_should_find_with_multiple_custom_preferences
1258
+ assert_equal [@customized_user], User.with_preferences(:notifications => false, :language => 'Latin')
1259
+ end
1260
+
1261
+ def test_should_find_with_mixed_default_and_custom_preferences
1262
+ assert_equal [@customized_user], User.with_preferences(:color => 'red', :language => 'Latin')
1263
+ end
1264
+
1265
+ def test_should_find_with_default_group_preference
1266
+ assert_equal [@user], User.with_preferences(:chat => {:language => 'English'})
1267
+ end
1268
+
1269
+ def test_should_find_with_customized_default_group_preference
1270
+ User.preference :country, :string, :default => 'US', :group_defaults => {:chat => 'UK'}
1271
+ @customized_user.preferred_country = 'US', :chat
1272
+ @customized_user.save!
1273
+
1274
+ assert_equal [@user], User.with_preferences(:chat => {:country => 'UK'})
1275
+ end
1276
+
1277
+ def test_should_find_with_multiple_default_group_preferences
1278
+ assert_equal [@user], User.with_preferences(:chat => {:notifications => nil, :language => 'English'})
1279
+ end
1280
+
1281
+ def test_should_find_with_custom_group_preference
1282
+ assert_equal [@customized_user], User.with_preferences(:chat => {:language => 'Latin'})
1283
+ end
1284
+
1285
+ def test_should_find_with_multiple_custom_group_preferences
1286
+ assert_equal [@customized_user], User.with_preferences(:chat => {:notifications => false, :language => 'Latin'})
1287
+ end
1288
+
1289
+ def test_should_find_with_mixed_default_and_custom_group_preferences
1290
+ assert_equal [@customized_user], User.with_preferences(:chat => {:color => 'red', :language => 'Latin'})
1291
+ end
1292
+
1293
+ def test_should_find_with_mixed_basic_and_group_preferences
1294
+ @customized_user.preferred_language = 'English'
1295
+ @customized_user.save!
1296
+
1297
+ assert_equal [@customized_user], User.with_preferences(:language => 'English', :chat => {:language => 'Latin'})
1298
+ end
1299
+
1300
+ def test_should_allow_chaining
1301
+ assert_equal [@user], User.with_preferences(:language => 'English').with_preferences(:color => 'red')
1302
+ end
1303
+ end
1304
+
1305
+ class PreferencesWithoutScopeTest < ModelPreferenceTest
1306
+ def setup
1307
+ User.preference :notifications
1308
+ User.preference :language, :string, :default => 'English'
1309
+ User.preference :color, :string, :default => 'red'
1310
+
1311
+ @user = create_user
1312
+ @customized_user = create_user(:login => 'customized',
1313
+ :prefers_notifications => false,
1314
+ :preferred_language => 'Latin'
1315
+ )
1316
+ @customized_user.prefers_notifications = false, :chat
1317
+ @customized_user.preferred_language = 'Latin', :chat
1318
+ @customized_user.save!
1319
+ end
1320
+
1321
+ def test_should_not_find_if_no_preference_matched
1322
+ assert_equal [], User.without_preferences(:color => 'red')
1323
+ end
1324
+
1325
+ def test_should_find_with_null_preference
1326
+ assert_equal [@user], User.without_preferences(:notifications => false)
1327
+ end
1328
+
1329
+ def test_should_find_with_default_preference
1330
+ assert_equal [@user], User.without_preferences(:language => 'Latin')
1331
+ end
1332
+
1333
+ def test_should_find_with_multiple_default_preferences
1334
+ assert_equal [@user], User.without_preferences(:language => 'Latin', :notifications => false)
1335
+ end
1336
+
1337
+ def test_should_find_with_custom_preference
1338
+ assert_equal [@customized_user], User.without_preferences(:language => 'English')
1339
+ end
1340
+
1341
+ def test_should_find_with_multiple_custom_preferences
1342
+ assert_equal [@customized_user], User.without_preferences(:language => 'English', :notifications => nil)
1343
+ end
1344
+
1345
+ def test_should_find_with_mixed_default_and_custom_preferences
1346
+ assert_equal [@customized_user], User.without_preferences(:language => 'English', :color => 'blue')
1347
+ end
1348
+
1349
+ def test_should_find_with_default_group_preference
1350
+ assert_equal [@user], User.without_preferences(:chat => {:language => 'Latin'})
1351
+ end
1352
+
1353
+ def test_should_find_with_customized_default_group_preference
1354
+ User.preference :country, :string, :default => 'US', :group_defaults => {:chat => 'UK'}
1355
+ @customized_user.preferred_country = 'US', :chat
1356
+ @customized_user.save!
1357
+
1358
+ assert_equal [@user], User.without_preferences(:chat => {:country => 'US'})
1359
+ end
1360
+
1361
+ def test_should_find_with_multiple_default_group_preferences
1362
+ assert_equal [@user], User.without_preferences(:chat => {:language => 'Latin', :notifications => false})
1363
+ end
1364
+
1365
+ def test_should_find_with_custom_group_preference
1366
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English'})
1367
+ end
1368
+
1369
+ def test_should_find_with_multiple_custom_group_preferences
1370
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English', :notifications => nil})
1371
+ end
1372
+
1373
+ def test_should_find_with_mixed_default_and_custom_group_preferences
1374
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English', :color => 'blue'})
1375
+ end
1376
+
1377
+ def test_should_find_with_mixed_basic_and_group_preferences
1378
+ @customized_user.preferred_language = 'English'
1379
+ @customized_user.save!
1380
+
1381
+ assert_equal [@customized_user], User.without_preferences(:language => 'Latin', :chat => {:language => 'English'})
1382
+ end
1383
+
1384
+ def test_should_allow_chaining
1385
+ assert_equal [@user], User.without_preferences(:language => 'Latin').without_preferences(:color => 'blue')
1386
+ end
1387
+ end