optimeez_preferences 0.4.2

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