dm_preferences 0.5.6 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +13 -5
  2. data/.gitignore +2 -0
  3. data/.travis.yml +2 -2
  4. data/{CHANGELOG.rdoc → CHANGELOG.md} +26 -15
  5. data/LICENSE +2 -0
  6. data/{README.rdoc → README.md} +144 -96
  7. data/Rakefile +20 -27
  8. data/dm_preferences.gemspec +7 -5
  9. data/lib/generators/{USAGE → preferences/USAGE} +0 -0
  10. data/lib/generators/{preferences_generator.rb → preferences/preferences_generator.rb} +0 -0
  11. data/lib/generators/{templates → preferences/templates}/create_preferences.rb +5 -1
  12. data/lib/preferences.rb +33 -10
  13. data/lib/preferences/engine.rb +2 -1
  14. data/lib/preferences/preference_definition.rb +15 -4
  15. data/lib/preferences/version.rb +1 -1
  16. data/spec/dummy/Rakefile +6 -0
  17. data/spec/dummy/app/models/.keep +0 -0
  18. data/{test/app_root → spec/dummy}/app/models/car.rb +0 -0
  19. data/{test/app_root → spec/dummy}/app/models/employee.rb +0 -0
  20. data/{test/app_root → spec/dummy}/app/models/manager.rb +0 -0
  21. data/{test/app_root → spec/dummy}/app/models/user.rb +0 -0
  22. data/spec/dummy/bin/bundle +3 -0
  23. data/spec/dummy/bin/rails +4 -0
  24. data/spec/dummy/bin/rake +4 -0
  25. data/spec/dummy/bin/setup +29 -0
  26. data/spec/dummy/config.ru +4 -0
  27. data/spec/dummy/config/application.rb +32 -0
  28. data/spec/dummy/config/boot.rb +5 -0
  29. data/spec/dummy/config/database.yml +25 -0
  30. data/spec/dummy/config/environment.rb +5 -0
  31. data/spec/dummy/config/environments/development.rb +41 -0
  32. data/spec/dummy/config/environments/production.rb +79 -0
  33. data/spec/dummy/config/environments/test.rb +42 -0
  34. data/spec/dummy/config/initializers/assets.rb +11 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  37. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/spec/dummy/config/initializers/inflections.rb +16 -0
  39. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  40. data/spec/dummy/config/initializers/session_store.rb +3 -0
  41. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/spec/dummy/config/locales/en.yml +23 -0
  43. data/spec/dummy/config/routes.rb +56 -0
  44. data/spec/dummy/config/secrets.yml +22 -0
  45. data/{test/app_root → spec/dummy}/db/migrate/001_create_users.rb +0 -0
  46. data/{test/app_root → spec/dummy}/db/migrate/002_create_cars.rb +0 -0
  47. data/{test/app_root → spec/dummy}/db/migrate/003_create_employees.rb +0 -0
  48. data/spec/dummy/db/migrate/004_create_preferences.rb +16 -0
  49. data/spec/dummy/db/schema.rb +42 -0
  50. data/spec/functional/preferences_spec.rb +1427 -0
  51. data/spec/spec_helper.rb +20 -0
  52. data/spec/support/assert_queries.rb +36 -0
  53. data/{test → spec/support}/factory.rb +1 -1
  54. data/spec/unit/preference_definition_spec.rb +249 -0
  55. data/{test/unit/preference_test.rb → spec/unit/preference_spec.rb} +114 -122
  56. metadata +127 -53
  57. data/test/app_root/db/migrate/004_migrate_preferences_to_version_1.rb +0 -13
  58. data/test/functional/preferences_test.rb +0 -1387
  59. data/test/test_helper.rb +0 -26
  60. data/test/unit/preference_definition_test.rb +0 -237
@@ -0,0 +1,42 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static file server for tests with Cache-Control for performance.
16
+ config.serve_static_files = true
17
+ config.static_cache_control = 'public, max-age=3600'
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Tell Action Mailer not to deliver emails to the real world.
30
+ # The :test delivery method accumulates sent emails in the
31
+ # ActionMailer::Base.deliveries array.
32
+ config.action_mailer.delivery_method = :test
33
+
34
+ # Randomize the order test cases are executed.
35
+ config.active_support.test_order = :random
36
+
37
+ # Print deprecation notices to the stderr.
38
+ config.active_support.deprecation = :stderr
39
+
40
+ # Raises error for missing translations
41
+ # config.action_view.raise_on_missing_translations = true
42
+ end
@@ -0,0 +1,11 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Version of your assets, change this if you want to expire all your assets.
4
+ Rails.application.config.assets.version = '1.0'
5
+
6
+ # Add additional assets to the asset load path
7
+ # Rails.application.config.assets.paths << Emoji.images_path
8
+
9
+ # Precompile additional assets.
10
+ # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
11
+ # Rails.application.config.assets.precompile += %w( search.js )
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails.application.config.action_dispatch.cookies_serializer = :json
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]
@@ -0,0 +1,16 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format. Inflections
4
+ # are locale specific, and you may define rules for as many different
5
+ # locales as you wish. All of these examples are active by default:
6
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
7
+ # inflect.plural /^(ox)$/i, '\1en'
8
+ # inflect.singular /^(ox)en/i, '\1'
9
+ # inflect.irregular 'person', 'people'
10
+ # inflect.uncountable %w( fish sheep )
11
+ # end
12
+
13
+ # These inflection rules are supported but not enabled by default:
14
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
15
+ # inflect.acronym 'RESTful'
16
+ # end
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails.application.config.session_store :cookie_store, key: '_dummy_session'
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,56 @@
1
+ Rails.application.routes.draw do
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+ # See how all your routes lay out with "rake routes".
4
+
5
+ # You can have the root of your site routed with "root"
6
+ # root 'welcome#index'
7
+
8
+ # Example of regular route:
9
+ # get 'products/:id' => 'catalog#view'
10
+
11
+ # Example of named route that can be invoked with purchase_url(id: product.id)
12
+ # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
13
+
14
+ # Example resource route (maps HTTP verbs to controller actions automatically):
15
+ # resources :products
16
+
17
+ # Example resource route with options:
18
+ # resources :products do
19
+ # member do
20
+ # get 'short'
21
+ # post 'toggle'
22
+ # end
23
+ #
24
+ # collection do
25
+ # get 'sold'
26
+ # end
27
+ # end
28
+
29
+ # Example resource route with sub-resources:
30
+ # resources :products do
31
+ # resources :comments, :sales
32
+ # resource :seller
33
+ # end
34
+
35
+ # Example resource route with more complex sub-resources:
36
+ # resources :products do
37
+ # resources :comments
38
+ # resources :sales do
39
+ # get 'recent', on: :collection
40
+ # end
41
+ # end
42
+
43
+ # Example resource route with concerns:
44
+ # concern :toggleable do
45
+ # post 'toggle'
46
+ # end
47
+ # resources :posts, concerns: :toggleable
48
+ # resources :photos, concerns: :toggleable
49
+
50
+ # Example resource route within a namespace:
51
+ # namespace :admin do
52
+ # # Directs /admin/products/* to Admin::ProductsController
53
+ # # (app/controllers/admin/products_controller.rb)
54
+ # resources :products
55
+ # end
56
+ end
@@ -0,0 +1,22 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure the secrets in this file are kept private
11
+ # if you're sharing your code publicly.
12
+
13
+ development:
14
+ secret_key_base: a6c149db4f26daefcf5faf290797af05897318c80a4b50ffd674a6c3ee4aba634836b9ae1a4f75aa3cd62afe4c6d6e8f788d6a436a2421bb841eeda84391d3e4
15
+
16
+ test:
17
+ secret_key_base: dacc13e99a05debf93f00937c83c0fe3d56ad899a63420bc7c685ac8756074e668187ec972830c89ad1b5a2759288f302062b0801b95a52f69cfb58fdcd7f479
18
+
19
+ # Do not keep production secrets in the repository,
20
+ # instead read values from the environment.
21
+ production:
22
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
@@ -0,0 +1,16 @@
1
+ class CreatePreferences < ActiveRecord::Migration
2
+ def up
3
+ create_table :preferences do |t|
4
+ t.string :name, :null => false
5
+ t.references :owner, :polymorphic => true, :null => false
6
+ t.references :group, :polymorphic => true
7
+ t.string :value
8
+ t.timestamps
9
+ end
10
+ add_index :preferences, [:owner_id, :owner_type, :name, :group_id, :group_type], :unique => true, :name => 'index_preferences_on_owner_and_name_and_preference'
11
+ end
12
+
13
+ def down
14
+ drop_table :preferences
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 4) do
15
+
16
+ create_table "cars", force: :cascade do |t|
17
+ t.string "name", null: false
18
+ end
19
+
20
+ create_table "employees", force: :cascade do |t|
21
+ t.string "name", null: false
22
+ t.string "type"
23
+ end
24
+
25
+ create_table "preferences", force: :cascade do |t|
26
+ t.string "name", null: false
27
+ t.integer "owner_id", null: false
28
+ t.string "owner_type", null: false
29
+ t.integer "group_id"
30
+ t.string "group_type"
31
+ t.string "value"
32
+ t.datetime "created_at"
33
+ t.datetime "updated_at"
34
+ end
35
+
36
+ add_index "preferences", ["owner_id", "owner_type", "name", "group_id", "group_type"], name: "index_preferences_on_owner_and_name_and_preference", unique: true
37
+
38
+ create_table "users", force: :cascade do |t|
39
+ t.string "login", null: false
40
+ end
41
+
42
+ end
@@ -0,0 +1,1427 @@
1
+ require 'spec_helper'
2
+
3
+ describe "ModelPreferenceTest" do
4
+ after do
5
+ User.preference_definitions.clear
6
+ end
7
+
8
+ #------------------------------------------------------------------------------
9
+ describe "ModelWithoutPreferencesTest" do
10
+ it "test_should_not_create_preference_definitions" do
11
+ assert !Car.respond_to?(:preference_definitions)
12
+ end
13
+
14
+ it "test_should_not_create_stored_preferences_association" do
15
+ assert !Car.new.respond_to?(:stored_preferences)
16
+ end
17
+
18
+ it "test_should_not_create_preference_scopes" do
19
+ assert !Car.respond_to?(:with_preferences)
20
+ assert !Car.respond_to?(:without_preferences)
21
+ end
22
+ end
23
+
24
+ #------------------------------------------------------------------------------
25
+ describe "PreferencesAfterFirstBeingDefinedTest" do
26
+ before do
27
+ User.preference :notifications
28
+ @user = new_user
29
+ end
30
+
31
+ it "test_should_create_preference_definitions" do
32
+ assert User.respond_to?(:preference_definitions)
33
+ end
34
+
35
+ it "test_should_create_preference_scopes" do
36
+ assert User.respond_to?(:with_preferences)
37
+ assert User.respond_to?(:without_preferences)
38
+ end
39
+
40
+ it "test_should_create_stored_preferences_associations" do
41
+ assert @user.respond_to?(:stored_preferences)
42
+ end
43
+ end
44
+
45
+ #------------------------------------------------------------------------------
46
+ describe "PreferencesAfterBeingDefinedTest" do
47
+ before do
48
+ @definition = User.preference :notifications
49
+ @user = new_user
50
+ end
51
+
52
+ it "test_should_raise_exception_if_invalid_options_specified" do
53
+ expect {
54
+ User.preference :notifications, :invalid => true
55
+ }.to raise_error(ArgumentError)
56
+ expect {
57
+ User.preference :notifications, :boolean, :invalid => true
58
+ }.to raise_error(ArgumentError)
59
+ end
60
+
61
+ it "test_should_create_definition" do
62
+ expect(@definition.nil?).to eq false
63
+ assert_equal 'notifications', @definition.name
64
+ end
65
+
66
+ it "test_should_create_preferred_query_method" do
67
+ assert @user.respond_to?(:preferred_notifications?)
68
+ end
69
+
70
+ it "test_should_create_prefers_query_method" do
71
+ assert @user.respond_to?(:prefers_notifications?)
72
+ end
73
+
74
+ it "test_should_create_preferred_reader" do
75
+ assert @user.respond_to?(:preferred_notifications)
76
+ end
77
+
78
+ it "test_should_create_prefers_reader" do
79
+ assert @user.respond_to?(:prefers_notifications)
80
+ end
81
+
82
+ it "test_should_create_preferred_writer" do
83
+ assert @user.respond_to?(:preferred_notifications=)
84
+ end
85
+
86
+ it "test_should_create_prefers_writer" do
87
+ assert @user.respond_to?(:prefers_notifications=)
88
+ end
89
+
90
+ it "test_should_create_preferred_changed_query" do
91
+ assert @user.respond_to?(:preferred_notifications_changed?)
92
+ end
93
+
94
+ it "test_should_create_prefers_changed_query" do
95
+ assert @user.respond_to?(:prefers_notifications_changed?)
96
+ end
97
+
98
+ it "test_should_create_preferred_was" do
99
+ assert @user.respond_to?(:preferred_notifications_was)
100
+ end
101
+
102
+ it "test_should_create_prefers_was" do
103
+ assert @user.respond_to?(:prefers_notifications_was)
104
+ end
105
+
106
+ it "test_should_create_preferred_change" do
107
+ assert @user.respond_to?(:preferred_notifications_change)
108
+ end
109
+
110
+ it "test_should_create_prefers_change" do
111
+ assert @user.respond_to?(:prefers_notifications_change)
112
+ end
113
+
114
+ it "test_should_create_preferred_will_change" do
115
+ assert @user.respond_to?(:preferred_notifications_will_change!)
116
+ end
117
+
118
+ it "test_should_create_prefers_will_change" do
119
+ assert @user.respond_to?(:prefers_notifications_will_change!)
120
+ end
121
+
122
+ it "test_should_create_preferred_reset" do
123
+ assert @user.respond_to?(:reset_preferred_notifications!)
124
+ end
125
+
126
+ it "test_should_create_prefers_reset" do
127
+ assert @user.respond_to?(:reset_prefers_notifications!)
128
+ end
129
+
130
+ it "test_should_include_new_definitions_in_preference_definitions" do
131
+ assert_equal User.preference_definitions['notifications'], @definition
132
+ end
133
+ end
134
+
135
+ #------------------------------------------------------------------------------
136
+ describe "PreferencesTypeCasted" do
137
+ before do
138
+ @definition = User.preference :rate, :float, :default => 1.0
139
+ @user = new_user
140
+ end
141
+
142
+ it "test_should_have_float_type" do
143
+ assert_equal :float, @definition.type
144
+ end
145
+
146
+ it "test_should_only_have_default_preferences" do
147
+ assert_equal e = {'rate' => 1.0}, @user.preferences
148
+ end
149
+
150
+ it "test_should_type_cast_changed_values" do
151
+ @user.write_preference(:rate, "1.1")
152
+ assert_equal e = {'rate' => 1.1}, @user.preferences
153
+ end
154
+ end
155
+
156
+ #------------------------------------------------------------------------------
157
+ describe "PreferencesByDefaultTest" do
158
+ before do
159
+ include Factory
160
+
161
+ @definition = User.preference :notifications
162
+ @user = new_user
163
+ end
164
+
165
+ it "test_should_have_boolean_type" do
166
+ assert_equal :boolean, @definition.type
167
+ end
168
+
169
+ it "test_should_not_have_default_value" do
170
+ assert_nil @definition.default_value
171
+ end
172
+
173
+ it "test_should_only_have_default_preferences" do
174
+ # assert_equal e = {'notifications' => nil}, @user.preferences
175
+ assert_equal e = {'notifications' => nil}, @user.preferences
176
+ end
177
+
178
+ it "test_should_not_query_preferences_changed" do
179
+ assert_equal false, @user.preferences_changed?
180
+ end
181
+
182
+ it "test_should_not_query_group_preferences_changed" do
183
+ assert_equal false, @user.preferences_changed?(:chat)
184
+ end
185
+
186
+ it "test_should_not_have_preferences_changed" do
187
+ assert_equal [], @user.preferences_changed
188
+ end
189
+
190
+ it "test_should_not_have_group_preferences_changed" do
191
+ assert_equal [], @user.preferences_changed(:chat)
192
+ end
193
+
194
+ it "test_should_not_have_preference_changes" do
195
+ assert_equal e = {}, @user.preference_changes
196
+ end
197
+
198
+ it "test_should_not_have_group_preference_changes" do
199
+ assert_equal e = {}, @user.preference_changes(:chat)
200
+ end
201
+ end
202
+
203
+ #------------------------------------------------------------------------------
204
+ describe "PreferencesWithCustomTypeTest" do
205
+ before do
206
+ @definition = User.preference :vehicle_id, :integer
207
+ @user = new_user
208
+ end
209
+
210
+ it "test_should_have_integer_type" do
211
+ assert_equal :integer, @definition.type
212
+ end
213
+
214
+ it "test_should_not_have_default_value" do
215
+ assert_nil @definition.default_value
216
+ end
217
+
218
+ it "test_should_only_have_default_preferences" do
219
+ assert_equal e = {'vehicle_id' => nil}, @user.preferences
220
+ end
221
+ end
222
+
223
+ #------------------------------------------------------------------------------
224
+ describe "PreferencesWithCustomDefaultTest" do
225
+ before do
226
+ @definition = User.preference :color, :string, :default => 'red'
227
+ @user = new_user
228
+ end
229
+
230
+ it "test_should_have_boolean_type" do
231
+ assert_equal :string, @definition.type
232
+ end
233
+
234
+ it "test_should_have_default_value" do
235
+ assert_equal 'red', @definition.default_value
236
+ end
237
+
238
+ it "test_should_only_have_default_preferences" do
239
+ assert_equal e = {'color' => 'red'}, @user.preferences
240
+ end
241
+ end
242
+
243
+ #------------------------------------------------------------------------------
244
+ describe "PreferencesWithMultipleDefinitionsTest" do
245
+ before do
246
+ User.preference :notifications, :default => true
247
+ User.preference :color, :string, :default => 'red'
248
+ @user = new_user
249
+ end
250
+
251
+ it "test_should_only_have_default_preferences" do
252
+ assert_equal e = {'notifications' => true, 'color' => 'red'}, @user.preferences
253
+ end
254
+ end
255
+
256
+ #------------------------------------------------------------------------------
257
+ describe "PreferencesAfterBeingCreatedTest" do
258
+ before do
259
+ User.preference :notifications, :default => true
260
+ @user = create_user
261
+ end
262
+
263
+ it "test_should_not_have_any_stored_preferences" do
264
+ assert @user.stored_preferences.empty?
265
+ end
266
+ end
267
+
268
+ #------------------------------------------------------------------------------
269
+ describe "PreferencesReaderTest" do
270
+ before do
271
+ User.preference :notifications, :default => true
272
+ User.preference :rate, :float, :default => 1.0
273
+ @user = create_user
274
+ end
275
+
276
+ it "test_should_raise_exception_if_invalid_preference_read" do
277
+ expect {
278
+ @user.preferred(:invalid)
279
+ }.to raise_error(ArgumentError, 'Unknown preference: invalid')
280
+ end
281
+
282
+ it "test_use_default_value_if_not_stored" do
283
+ assert_equal true, @user.preferred(:notifications)
284
+ end
285
+
286
+ it "test_should_use_group_default_value_if_not_stored" do
287
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
288
+ assert_equal 'English', @user.preferred(:language)
289
+ end
290
+
291
+ it "test_should_use_stored_value_if_stored" do
292
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
293
+ assert_equal false, @user.preferred(:notifications)
294
+ end
295
+
296
+ it "test_should_type_cast_based_on_preference_definition" do
297
+ @user.write_preference(:notifications, 'false')
298
+ assert_equal false, @user.preferred(:notifications)
299
+ @user.write_preference(:rate, "1.2")
300
+ assert_equal 1.2, @user.preferred(:rate)
301
+ end
302
+
303
+ it "test_should_cache_stored_values" do
304
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
305
+ assert_queries(1) { @user.preferred(:notifications) }
306
+ assert_queries(0) { @user.preferred(:notifications) }
307
+ end
308
+
309
+ it "test_should_not_query_if_preferences_already_loaded" do
310
+ @user.preferences
311
+ assert_queries(0) { @user.preferred(:notifications) }
312
+ end
313
+
314
+ it "test_should_use_value_from_preferences_lookup" do
315
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
316
+ @user.preferences
317
+
318
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications) }
319
+ end
320
+
321
+ it "test_should_be_able_to_use_prefers_reader" do
322
+ assert_equal true, @user.prefers_notifications
323
+ end
324
+
325
+ it "test_should_be_able_to_use_preferred_reader" do
326
+ assert_equal true, @user.preferred_notifications
327
+ end
328
+ end
329
+
330
+ #------------------------------------------------------------------------------
331
+ describe "PreferencesGroupReaderTest" do
332
+ before do
333
+ User.preference :notifications, :default => true
334
+ @user = create_user
335
+ end
336
+
337
+ it "test_should_use_default_value_if_not_stored" do
338
+ assert_equal true, @user.preferred(:notifications, :chat)
339
+ end
340
+
341
+ it "test_should_use_group_default_value_if_not_stored" do
342
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
343
+ assert_equal 'Latin', @user.preferred(:language, :chat)
344
+ end
345
+
346
+ it "test_should_use_stored_value_if_stored" do
347
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
348
+ assert_equal false, @user.preferred(:notifications, :chat)
349
+ end
350
+
351
+ it "test_should_cache_stored_values" do
352
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
353
+ assert_queries(1) { @user.preferred(:notifications, :chat) }
354
+ assert_queries(0) { @user.preferred(:notifications, :chat) }
355
+ end
356
+
357
+ it "test_should_not_query_if_preferences_already_loaded" do
358
+ @user.preferences(:chat)
359
+ assert_queries(0) { @user.preferred(:notifications, :chat) }
360
+ end
361
+
362
+ it "test_should_use_value_from_preferences_lookup" do
363
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
364
+ @user.preferences(:chat)
365
+
366
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications, :chat) }
367
+ end
368
+
369
+ it "test_should_be_able_to_use_prefers_reader" do
370
+ assert_equal true, @user.prefers_notifications(:chat)
371
+ end
372
+
373
+ it "test_should_be_able_to_use_preferred_reader" do
374
+ assert_equal true, @user.preferred_notifications(:chat)
375
+ end
376
+ end
377
+
378
+ #------------------------------------------------------------------------------
379
+ describe "PreferencesARGroupReaderTest" do
380
+ before do
381
+ @car = create_car
382
+
383
+ User.preference :notifications, :default => true
384
+ @user = create_user
385
+ end
386
+
387
+ it "test_use_default_value_if_not_stored" do
388
+ assert_equal true, @user.preferred(:notifications, @car)
389
+ end
390
+
391
+ it "test_should_use_stored_value_if_stored" do
392
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
393
+ assert_equal false, @user.preferred(:notifications, @car)
394
+ end
395
+
396
+ it "test_should_use_value_from_preferences_lookup" do
397
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
398
+ @user.preferences(@car)
399
+
400
+ assert_queries(0) { assert_equal false, @user.preferred(:notifications, @car) }
401
+ end
402
+
403
+ it "test_should_be_able_to_use_prefers_reader" do
404
+ assert_equal true, @user.prefers_notifications(@car)
405
+ end
406
+
407
+ it "test_should_be_able_to_use_preferred_reader" do
408
+ assert_equal true, @user.preferred_notifications(@car)
409
+ end
410
+ end
411
+
412
+ #------------------------------------------------------------------------------
413
+ describe "PreferencesQueryTest" do
414
+ before do
415
+ User.preference :language, :string
416
+ @user = create_user
417
+ end
418
+
419
+ it "test_should_raise_exception_if_invalid_preference_queried" do
420
+ expect {
421
+ @user.preferred?(:invalid)
422
+ }.to raise_error(ArgumentError, 'Unknown preference: invalid')
423
+ end
424
+
425
+ it "test_should_be_true_if_present" do
426
+ @user.preferred_language = 'English'
427
+ assert_equal true, @user.preferred?(:language)
428
+ end
429
+
430
+ it "test_should_be_false_if_not_present" do
431
+ assert_equal false, @user.preferred?(:language)
432
+ end
433
+
434
+ it "test_should_use_stored_value_if_stored" do
435
+ create_preference(:owner => @user, :name => 'language', :value => 'English')
436
+ assert_equal true, @user.preferred?(:language)
437
+ end
438
+
439
+ it "test_should_cache_stored_values" do
440
+ create_preference(:owner => @user, :name => 'language', :value => 'English')
441
+ assert_queries(1) { @user.preferred?(:language) }
442
+ assert_queries(0) { @user.preferred?(:language) }
443
+ end
444
+
445
+ it "test_should_be_able_to_use_prefers_reader" do
446
+ assert_equal false, @user.prefers_language?
447
+ end
448
+
449
+ it "test_should_be_able_to_use_preferred_reader" do
450
+ assert_equal false, @user.preferred_language?
451
+ end
452
+ end
453
+
454
+ #------------------------------------------------------------------------------
455
+ describe "PreferencesGroupQueryTest" do
456
+ before do
457
+ User.preference :language, :string
458
+ @user = create_user
459
+ end
460
+
461
+ it "test_should_be_true_if_present" do
462
+ @user.preferred_language = 'English', :chat
463
+ assert_equal true, @user.preferred?(:language, :chat)
464
+ end
465
+
466
+ it "test_should_be_false_if_not_present" do
467
+ assert_equal false, @user.preferred?(:language, :chat)
468
+ end
469
+
470
+ it "test_should_use_stored_value_if_stored" do
471
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'language', :value => 'English')
472
+ assert_equal true, @user.preferred?(:language, :chat)
473
+ end
474
+
475
+ it "test_should_cache_stored_values" do
476
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'language', :value => 'English')
477
+ assert_queries(1) { @user.preferred?(:language, :chat) }
478
+ assert_queries(0) { @user.preferred?(:language, :chat) }
479
+ end
480
+
481
+ it "test_should_be_able_to_use_prefers_reader" do
482
+ assert_equal false, @user.prefers_language?(:chat)
483
+ end
484
+
485
+ it "test_should_be_able_to_use_preferred_reader" do
486
+ assert_equal false, @user.preferred_language?(:chat)
487
+ end
488
+ end
489
+
490
+ #------------------------------------------------------------------------------
491
+ describe "PreferencesARGroupQueryTest" do
492
+ before do
493
+ @car = create_car
494
+
495
+ User.preference :language, :string
496
+ @user = create_user
497
+ end
498
+
499
+ it "test_should_be_true_if_present" do
500
+ @user.preferred_language = 'English', @car
501
+ assert_equal true, @user.preferred?(:language, @car)
502
+ end
503
+
504
+ it "test_should_be_false_if_not_present" do
505
+ assert_equal false, @user.preferred?(:language, @car)
506
+ end
507
+
508
+ it "test_should_use_stored_value_if_stored" do
509
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'language', :value => 'English')
510
+ assert_equal true, @user.preferred?(:language, @car)
511
+ end
512
+
513
+ it "test_should_be_able_to_use_prefers_reader" do
514
+ assert_equal false, @user.prefers_language?(@car)
515
+ end
516
+
517
+ it "test_should_be_able_to_use_preferred_reader" do
518
+ assert_equal false, @user.preferred_language?(@car)
519
+ end
520
+ end
521
+
522
+ #------------------------------------------------------------------------------
523
+ describe "PreferencesWriterTest" do
524
+ before do
525
+ User.preference :notifications, :boolean, :default => true
526
+ User.preference :language, :string, :default => 'English'
527
+ @user = create_user(:login => 'admin')
528
+ end
529
+
530
+ it "test_should_raise_exception_if_invalid_preference_written" do
531
+ expect {
532
+ @user.write_preference(:invalid, true)
533
+ }.to raise_error(ArgumentError, 'Unknown preference: invalid')
534
+ end
535
+
536
+ it "test_should_have_same_value_if_not_changed" do
537
+ @user.write_preference(:notifications, true)
538
+ assert_equal true, @user.preferred(:notifications)
539
+ end
540
+
541
+ it "test_should_use_new_value_if_changed" do
542
+ @user.write_preference(:notifications, false)
543
+ assert_equal false, @user.preferred(:notifications)
544
+ end
545
+
546
+ it "test_should_not_save_record_after_changing_preference" do
547
+ @user.login = 'test'
548
+ @user.write_preference(:notifications, false)
549
+
550
+ assert_equal 'admin', User.find(@user.id).login
551
+ end
552
+
553
+ it "test_should_not_create_stored_preferences_immediately" do
554
+ @user.write_preference(:notifications, false)
555
+ assert @user.stored_preferences.empty?
556
+ end
557
+
558
+ it "test_should_not_create_stored_preference_if_value_not_changed" do
559
+ @user.write_preference(:notifications, true)
560
+ @user.save!
561
+
562
+ assert_equal 0, @user.stored_preferences.count
563
+ end
564
+
565
+ it "test_should_not_create_stored_integer_preference_if_typecast_not_changed" do
566
+ User.preference :age, :integer
567
+
568
+ @user.write_preference(:age, '')
569
+ @user.save!
570
+
571
+ assert_equal 0, @user.stored_preferences.count
572
+ end
573
+
574
+ it "test_should_create_stored_integer_preference_if_typecast_changed" do
575
+ User.preference :age, :integer, :default => 0
576
+
577
+ @user.write_preference(:age, '')
578
+ @user.save!
579
+
580
+ assert_nil @user.preferred(:age)
581
+ assert_equal 1, @user.stored_preferences.count
582
+ end
583
+
584
+ it "test_should_create_stored_preference_if_value_changed" do
585
+ @user.write_preference(:notifications, false)
586
+ @user.save!
587
+
588
+ assert_equal 1, @user.stored_preferences.count
589
+ end
590
+
591
+ it "test_should_overwrite_existing_stored_preference_if_value_changed" do
592
+ preference = create_preference(:owner => @user, :name => 'notifications', :value => true)
593
+
594
+ @user.write_preference(:notifications, false)
595
+ @user.save!
596
+
597
+ preference.reload
598
+ assert_equal false, preference.value
599
+ end
600
+
601
+ it "test_should_not_remove_preference_if_set_to_default" do
602
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
603
+
604
+ @user.write_preference(:notifications, true)
605
+ @user.save!
606
+ @user.reload
607
+
608
+ assert_equal 1, @user.stored_preferences.size
609
+ preference = @user.stored_preferences.first
610
+ assert_equal true, preference.value
611
+ end
612
+
613
+ it "test_should_not_remove_preference_if_set_to_nil" do
614
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
615
+
616
+ @user.write_preference(:notifications, nil)
617
+ @user.save!
618
+ @user.reload
619
+
620
+ assert_equal 1, @user.stored_preferences.size
621
+ preference = @user.stored_preferences.first
622
+ assert_nil preference.value
623
+ end
624
+
625
+ it "test_should_not_query_for_old_value_if_preferences_loaded" do
626
+ @user.preferences
627
+
628
+ assert_queries(0) { @user.write_preference(:notifications, false) }
629
+ end
630
+ end
631
+
632
+ #------------------------------------------------------------------------------
633
+ describe "PreferencesGroupWriterTest" do
634
+ before do
635
+ User.preference :notifications, :boolean, :default => true
636
+ User.preference :language, :string, :default => 'English'
637
+ @user = create_user(:login => 'admin')
638
+ end
639
+
640
+ it "test_should_have_same_value_if_not_changed" do
641
+ @user.write_preference(:notifications, true, :chat)
642
+ assert_equal true, @user.preferred(:notifications, :chat)
643
+ end
644
+
645
+ it "test_should_use_new_value_if_changed" do
646
+ @user.write_preference(:notifications, false, :chat)
647
+ assert_equal false, @user.preferred(:notifications, :chat)
648
+ end
649
+
650
+ it "test_should_not_create_stored_preference_if_value_not_changed" do
651
+ @user.write_preference(:notifications, true, :chat)
652
+ @user.save!
653
+
654
+ assert_equal 0, @user.stored_preferences.count
655
+ end
656
+
657
+ it "test_should_create_stored_preference_if_value_changed" do
658
+ @user.write_preference(:notifications, false, :chat)
659
+ @user.save!
660
+
661
+ assert_equal 1, @user.stored_preferences.count
662
+ end
663
+
664
+ it "test_should_set_group_attributes_on_stored_preferences" do
665
+ @user.write_preference(:notifications, false, :chat)
666
+ @user.save!
667
+
668
+ preference = @user.stored_preferences.first
669
+ assert_equal 'chat', preference.group_type
670
+ assert_nil preference.group_id
671
+ end
672
+
673
+ it "test_should_overwrite_existing_stored_preference_if_value_changed" do
674
+ preference = create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => true)
675
+
676
+ @user.write_preference(:notifications, false, :chat)
677
+ @user.save!
678
+
679
+ preference.reload
680
+ assert_equal false, preference.value
681
+ end
682
+ end
683
+
684
+ #------------------------------------------------------------------------------
685
+ describe "PreferencesARGroupWriterTest" do
686
+ before do
687
+ @car = create_car
688
+
689
+ User.preference :notifications, :boolean, :default => true
690
+ User.preference :language, :string, :default => 'English'
691
+ @user = create_user(:login => 'admin')
692
+ end
693
+
694
+ it "test_should_have_same_value_if_not_changed" do
695
+ @user.write_preference(:notifications, true, @car)
696
+ assert_equal true, @user.preferred(:notifications, @car)
697
+ end
698
+
699
+ it "test_should_use_new_value_if_changed" do
700
+ @user.write_preference(:notifications, false, @car)
701
+ assert_equal false, @user.preferred(:notifications, @car)
702
+ end
703
+
704
+ it "test_should_not_create_stored_preference_if_value_not_changed" do
705
+ @user.write_preference(:notifications, true, @car)
706
+ @user.save!
707
+
708
+ assert_equal 0, @user.stored_preferences.count
709
+ end
710
+
711
+ it "test_should_create_stored_preference_if_value_changed" do
712
+ @user.write_preference(:notifications, false, @car)
713
+ @user.save!
714
+
715
+ assert_equal 1, @user.stored_preferences.count
716
+ end
717
+
718
+ it "test_should_set_group_attributes_on_stored_preferences" do
719
+ @user.write_preference(:notifications, false, @car)
720
+ @user.save!
721
+
722
+ preference = @user.stored_preferences.first
723
+ assert_equal 'Car', preference.group_type
724
+ assert_equal @car.id, preference.group_id
725
+ end
726
+ end
727
+
728
+ #------------------------------------------------------------------------------
729
+ describe "PreferencesAfterChangingPreferenceTest" do
730
+ before do
731
+ User.preference :notifications, :boolean, :default => true
732
+ User.preference :language, :string, :default => 'English'
733
+ @user = create_user
734
+
735
+ @user.write_preference(:notifications, false)
736
+ end
737
+
738
+ it "test_should_query_preferences_changed" do
739
+ assert_equal true, @user.preferences_changed?
740
+ end
741
+
742
+ it "test_should_query_preference_changed" do
743
+ assert_equal true, @user.prefers_notifications_changed?
744
+ end
745
+
746
+ it "test_should_not_query_preferences_changed_for_group" do
747
+ assert_equal false, @user.preferences_changed?(:chat)
748
+ end
749
+
750
+ it "test_should_not_query_preference_changed_for_group" do
751
+ assert_equal false, @user.prefers_notifications_changed?(:chat)
752
+ end
753
+
754
+ it "test_should_have_preferences_changed" do
755
+ assert_equal ['notifications'], @user.preferences_changed
756
+ end
757
+
758
+ it "test_should_not_build_same_preferences_changed_result" do
759
+ expect(@user.preferences_changed).to_not be @user.preferences_changed
760
+ end
761
+
762
+ it "test_should_not_have_preferences_changed_for_group" do
763
+ assert_equal [], @user.preferences_changed(:chat)
764
+ end
765
+
766
+ it "test_should_track_multiple_preferences_changed" do
767
+ @user.write_preference(:language, 'Latin')
768
+ assert_equal ['language', 'notifications'], @user.preferences_changed.sort
769
+ end
770
+
771
+ it "test_should_have_preference_changes" do
772
+ assert_equal e = {'notifications' => [true, false]}, @user.preference_changes
773
+ end
774
+
775
+ it "test_should_not_build_same_preference_changes_result" do
776
+ expect(@user.preference_changes).to_not be @user.preference_changes
777
+ end
778
+
779
+ it "test_should_have_preference_change" do
780
+ assert_equal [true, false], @user.prefers_notifications_change
781
+ end
782
+
783
+ it "test_should_have_preference_was" do
784
+ assert_equal true, @user.prefers_notifications_was
785
+ end
786
+
787
+ it "test_should_not_have_preference_changes_for_group" do
788
+ assert_equal e = {}, @user.preference_changes(:chat)
789
+ end
790
+
791
+ it "test_should_not_have_preference_change_for_group" do
792
+ assert_nil @user.prefers_notifications_change(:chat)
793
+ end
794
+
795
+ it "test_should_have_preference_was_for_group" do
796
+ assert_equal true, @user.prefers_notifications_was(:chat)
797
+ end
798
+
799
+ it "test_should_use_latest_value_for_preference_changes" do
800
+ @user.write_preference(:notifications, nil)
801
+ assert_equal e = {'notifications' => [true, nil]}, @user.preference_changes
802
+ end
803
+
804
+ it "test_should_use_cloned_old_value_for_preference_changes" do
805
+ old_value = @user.preferred(:language)
806
+ @user.write_preference(:language, 'Latin')
807
+
808
+ tracked_old_value = @user.preference_changes['language'][0]
809
+ assert_equal old_value, tracked_old_value
810
+ expect(old_value).to_not be tracked_old_value
811
+ end
812
+
813
+ it "test_should_track_multiple_preference_changes" do
814
+ @user.write_preference(:language, 'Latin')
815
+ assert_equal e = {'notifications' => [true, false], 'language' => ['English', 'Latin']}, @user.preference_changes
816
+ end
817
+ end
818
+
819
+ #------------------------------------------------------------------------------
820
+ describe "PreferencesAfterChangingGroupPreferenceTest" do
821
+ before do
822
+ User.preference :notifications, :boolean, :default => true
823
+ User.preference :language, :string, :default => 'English'
824
+ @user = create_user
825
+
826
+ @user.write_preference(:notifications, false, :chat)
827
+ end
828
+
829
+ it "test_should_not_query_preferences_changed" do
830
+ assert_equal false, @user.preferences_changed?
831
+ end
832
+
833
+ it "test_not_should_query_preference_changed" do
834
+ assert_equal false, @user.prefers_notifications_changed?
835
+ end
836
+
837
+ it "test_should_query_preferences_changed_for_group" do
838
+ assert_equal true, @user.preferences_changed?(:chat)
839
+ end
840
+
841
+ it "test_should_query_preference_changed_for_group" do
842
+ assert_equal true, @user.prefers_notifications_changed?(:chat)
843
+ end
844
+
845
+ it "test_should_have_preferences_changed" do
846
+ assert_equal [], @user.preferences_changed
847
+ end
848
+
849
+ it "test_should_not_have_preferences_changed_for_group" do
850
+ assert_equal ['notifications'], @user.preferences_changed(:chat)
851
+ end
852
+
853
+ it "test_should_have_preference_changes" do
854
+ assert_equal e = {}, @user.preference_changes
855
+ end
856
+
857
+ it "test_should_not_have_preference_change" do
858
+ assert_nil @user.prefers_notifications_change
859
+ end
860
+
861
+ it "test_should_have_preference_was" do
862
+ assert_equal true, @user.prefers_notifications_was
863
+ end
864
+
865
+ it "test_should_not_have_preference_changes_for_group" do
866
+ assert_equal e = {'notifications' => [true, false]}, @user.preference_changes(:chat)
867
+ end
868
+
869
+ it "test_should_have_preference_change_for_group" do
870
+ assert_equal [true, false], @user.prefers_notifications_change(:chat)
871
+ end
872
+
873
+ it "test_should_have_preference_was_for_group" do
874
+ assert_equal true, @user.prefers_notifications_was(:chat)
875
+ end
876
+ end
877
+
878
+ #------------------------------------------------------------------------------
879
+ describe "PreferencesAfterRevertPreferenceChangeTest" do
880
+ before do
881
+ User.preference :notifications, :boolean, :default => true
882
+
883
+ @user = create_user
884
+ @user.write_preference(:notifications, false)
885
+ @user.write_preference(:notifications, true)
886
+ end
887
+
888
+ it "test_should_not_query_preferences_changed" do
889
+ assert_equal false, @user.preferences_changed?
890
+ end
891
+
892
+ it "test_should_not_have_preferences_changed" do
893
+ assert_equal [], @user.preferences_changed
894
+ end
895
+
896
+ it "test_should_not_have_preference_changes" do
897
+ assert_equal e = {}, @user.preference_changes
898
+ end
899
+ end
900
+
901
+ #------------------------------------------------------------------------------
902
+ describe "PreferencesAfterForcingChangeTest" do
903
+ before do
904
+ User.preference :notifications, :boolean, :default => true
905
+
906
+ @user = create_user
907
+ @user.prefers_notifications_will_change!
908
+ @user.save
909
+ end
910
+
911
+ it "test_should_store_preference" do
912
+ assert_equal 1, @user.stored_preferences.count
913
+
914
+ preference = @user.stored_preferences.first
915
+ assert_equal nil, preference.group_type
916
+ assert_equal nil, preference.group_id
917
+ assert_equal true, preference.value
918
+ end
919
+ end
920
+
921
+ #------------------------------------------------------------------------------
922
+ describe "PreferencesAfterForcingChangeForGroupTest" do
923
+ before do
924
+ User.preference :notifications, :boolean, :default => true
925
+ User.preference :language, :string, :default => 'English'
926
+
927
+ @user = create_user
928
+ @user.prefers_notifications_will_change!(:chat)
929
+ @user.save
930
+ end
931
+
932
+ it "test_should_store_preference" do
933
+ assert_equal 1, @user.stored_preferences.count
934
+
935
+ preference = @user.stored_preferences.first
936
+ assert_equal 'chat', preference.group_type
937
+ assert_equal nil, preference.group_id
938
+ assert_equal true, preference.value
939
+ end
940
+
941
+ it "test_should_use_cloned_value_for_tracking_old_value" do
942
+ old_value = @user.preferred(:language)
943
+ @user.preferred_language_will_change!
944
+
945
+ tracked_old_value = @user.preferred_language_was
946
+ assert_equal old_value, tracked_old_value
947
+ expect(old_value).to_not be tracked_old_value
948
+ end
949
+ end
950
+
951
+ #------------------------------------------------------------------------------
952
+ describe "PreferencesAfterResettingPreferenceTest" do
953
+ before do
954
+ User.preference :notifications, :boolean, :default => true
955
+
956
+ @user = create_user
957
+ @user.write_preference(:notifications, false)
958
+ @user.write_preference(:notifications, false, :chat)
959
+ @user.reset_prefers_notifications!
960
+ end
961
+
962
+ it "test_should_revert_to_original_value" do
963
+ assert_equal true, @user.preferred(:notifications)
964
+ end
965
+
966
+ it "test_should_not_reset_groups" do
967
+ assert_equal false, @user.preferred(:notifications, :chat)
968
+ end
969
+ end
970
+
971
+ #------------------------------------------------------------------------------
972
+ describe "PreferencesAfterResettingPreferenceTest" do
973
+ before do
974
+ User.preference :notifications, :boolean, :default => true
975
+
976
+ @user = create_user
977
+ @user.write_preference(:notifications, false)
978
+ @user.write_preference(:notifications, false, :chat)
979
+ @user.reset_prefers_notifications!(:chat)
980
+ end
981
+
982
+ it "test_should_revert_to_original_value" do
983
+ assert_equal true, @user.preferred(:notifications, :chat)
984
+ end
985
+
986
+ it "test_should_not_reset_default_group" do
987
+ assert_equal false, @user.preferred(:notifications)
988
+ end
989
+ end
990
+
991
+ #------------------------------------------------------------------------------
992
+ describe "PreferencesLookupTest" do
993
+ before do
994
+ User.preference :notifications, :boolean, :default => true
995
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
996
+
997
+ @user = create_user
998
+ end
999
+
1000
+ it "test_should_only_have_defaults_if_nothing_customized" do
1001
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1002
+ end
1003
+
1004
+ it "test_should_merge_defaults_with_unsaved_changes" do
1005
+ @user.write_preference(:notifications, false)
1006
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
1007
+ end
1008
+
1009
+ it "test_should_merge_defaults_with_saved_changes" do
1010
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1011
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
1012
+ end
1013
+
1014
+ it "test_should_merge_stored_preferences_with_unsaved_changes" do
1015
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1016
+ @user.write_preference(:language, 'Latin')
1017
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences
1018
+ end
1019
+
1020
+ it "test_should_use_unsaved_changes_over_stored_preferences" do
1021
+ create_preference(:owner => @user, :name => 'notifications', :value => true)
1022
+ @user.write_preference(:notifications, false)
1023
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
1024
+ end
1025
+
1026
+ it "test_should_typecast_unsaved_changes" do
1027
+ @user.write_preference(:notifications, 'true')
1028
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1029
+ end
1030
+
1031
+ it "test_should_cache_results" do
1032
+ assert_queries(1) { @user.preferences }
1033
+ assert_queries(0) { @user.preferences }
1034
+ end
1035
+
1036
+ it "test_should_not_query_if_stored_preferences_eager_loaded" do
1037
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1038
+ user = User.includes(:stored_preferences).where(id: @user.id).first
1039
+ assert_queries(0) do
1040
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, user.preferences
1041
+ end
1042
+ end
1043
+
1044
+ it "test_should_not_generate_same_object_twice" do
1045
+ expect(@user.preferences).to_not be @user.preferences
1046
+ end
1047
+
1048
+ it "test_should_use_preferences_for_prefs" do
1049
+ assert_equal @user.preferences, @user.prefs
1050
+ end
1051
+ end
1052
+
1053
+ #------------------------------------------------------------------------------
1054
+ describe "PreferencesGroupLookupTest" do
1055
+ before do
1056
+ User.preference :notifications, :boolean, :default => true
1057
+ User.preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Latin'}
1058
+
1059
+ @user = create_user
1060
+ end
1061
+
1062
+ it "test_should_only_have_defaults_if_nothing_customized" do
1063
+ assert_equal e = {'notifications' => true, 'language' => 'Latin'}, @user.preferences(:chat)
1064
+ end
1065
+
1066
+ it "test_should_merge_defaults_with_unsaved_changes" do
1067
+ @user.write_preference(:notifications, false, :chat)
1068
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
1069
+ end
1070
+
1071
+ it "test_should_merge_defaults_with_saved_changes" do
1072
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1073
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
1074
+ end
1075
+
1076
+ it "test_should_merge_stored_preferences_with_unsaved_changes" do
1077
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1078
+ @user.write_preference(:language, 'Spanish', :chat)
1079
+ assert_equal e = {'notifications' => false, 'language' => 'Spanish'}, @user.preferences(:chat)
1080
+ end
1081
+
1082
+ it "test_should_typecast_unsaved_changes" do
1083
+ @user.write_preference(:notifications, 'true', :chat)
1084
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1085
+ end
1086
+
1087
+ it "test_should_cache_results" do
1088
+ assert_queries(1) { @user.preferences(:chat) }
1089
+ assert_queries(0) { @user.preferences(:chat) }
1090
+ end
1091
+
1092
+ it "test_should_not_query_if_all_preferences_individually_loaded" do
1093
+ @user.preferred(:notifications, :chat)
1094
+ @user.preferred(:language, :chat)
1095
+
1096
+ assert_queries(0) { @user.preferences(:chat) }
1097
+ end
1098
+
1099
+ it "test_should_not_generate_same_object_twice" do
1100
+ expect(@user.preferences(:chat)).to_not be @user.preferences(:chat)
1101
+ end
1102
+ end
1103
+
1104
+ #------------------------------------------------------------------------------
1105
+ describe "PreferencesARGroupLookupTest" do
1106
+ before do
1107
+ @car = create_car
1108
+
1109
+ User.preference :notifications, :boolean, :default => true
1110
+ User.preference :language, :string, :default => 'English'
1111
+
1112
+ @user = create_user
1113
+ end
1114
+
1115
+ it "test_should_only_have_defaults_if_nothing_customized" do
1116
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences(@car)
1117
+ end
1118
+
1119
+ it "test_should_merge_defaults_with_unsaved_changes" do
1120
+ @user.write_preference(:notifications, false, @car)
1121
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
1122
+ end
1123
+
1124
+ it "test_should_merge_defaults_with_saved_changes" do
1125
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
1126
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
1127
+ end
1128
+
1129
+ it "test_should_merge_stored_preferences_with_unsaved_changes" do
1130
+ create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
1131
+ @user.write_preference(:language, 'Latin', @car)
1132
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(@car)
1133
+ end
1134
+ end
1135
+
1136
+ #------------------------------------------------------------------------------
1137
+ describe "PreferencesNilGroupLookupTest" do
1138
+ before do
1139
+ @car = create_car
1140
+
1141
+ User.preference :notifications, :boolean, :default => true
1142
+ User.preference :language, :string, :default => 'English'
1143
+
1144
+ @user = create_user
1145
+ end
1146
+
1147
+ it "test_should_only_have_defaults_if_nothing_customized" do
1148
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences(nil)
1149
+ end
1150
+
1151
+ it "test_should_merge_defaults_with_unsaved_changes" do
1152
+ @user.write_preference(:notifications, false)
1153
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
1154
+ end
1155
+
1156
+ it "test_should_merge_defaults_with_saved_changes" do
1157
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1158
+ assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
1159
+ end
1160
+
1161
+ it "test_should_merge_stored_preferences_with_unsaved_changes" do
1162
+ create_preference(:owner => @user, :name => 'notifications', :value => false)
1163
+ @user.write_preference(:language, 'Latin')
1164
+ assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(nil)
1165
+ end
1166
+ end
1167
+
1168
+ #------------------------------------------------------------------------------
1169
+ describe "PreferencesLookupWithGroupsTest" do
1170
+ before do
1171
+ User.preference :notifications, :boolean, :default => true
1172
+ User.preference :language, :string, :default => 'English'
1173
+
1174
+ @user = create_user
1175
+ create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
1176
+ end
1177
+
1178
+ it "test_not_include_group_preferences_by_default" do
1179
+ assert_equal e = {'notifications' => true, 'language' => 'English'}, @user.preferences
1180
+ end
1181
+ end
1182
+
1183
+ #------------------------------------------------------------------------------
1184
+ describe "PreferencesAfterBeingReloadedTest" do
1185
+ before do
1186
+ User.preference :notifications, :boolean, :default => true
1187
+
1188
+ @user = create_user
1189
+ @user.write_preference(:notifications, false)
1190
+ @user.reload
1191
+ end
1192
+
1193
+ it "test_should_reset_unsaved_preferences" do
1194
+ assert_equal true, @user.preferred(:notifications)
1195
+ end
1196
+
1197
+ it "test_should_not_save_reset_preferences" do
1198
+ @user.save!
1199
+ assert @user.stored_preferences.empty?
1200
+ end
1201
+
1202
+ it "test_should_reset_preferences" do
1203
+ assert_equal e = {'notifications' => true}, @user.preferences
1204
+ end
1205
+
1206
+ it "test_should_clear_query_cache_for_preferences" do
1207
+ assert_queries(1) { @user.preferences }
1208
+ end
1209
+
1210
+ it "test_should_reset_preferences_changed_query" do
1211
+ assert_equal false, @user.preferences_changed?
1212
+ end
1213
+
1214
+ it "test_should_reset_preferences_changed" do
1215
+ assert_equal [], @user.preferences_changed
1216
+ end
1217
+
1218
+ it "test_should_reset_preference_changes" do
1219
+ assert_equal e = {}, @user.preference_changes
1220
+ end
1221
+ end
1222
+
1223
+ #------------------------------------------------------------------------------
1224
+ describe "PreferencesForGroupAfterBeingReloadedTest" do
1225
+ before do
1226
+ User.preference :notifications, :boolean, :default => true
1227
+
1228
+ @user = create_user
1229
+ @user.write_preference(:notifications, false, :chat)
1230
+ @user.reload
1231
+ end
1232
+
1233
+ it "test_should_reset_unsaved_preferences" do
1234
+ assert_equal true, @user.preferred(:notifications, :chat)
1235
+ end
1236
+
1237
+ it "test_should_reset_preferences" do
1238
+ assert_equal e = {'notifications' => true}, @user.preferences(:chat)
1239
+ end
1240
+
1241
+ it "test_should_clear_query_cache_for_preferences" do
1242
+ assert_queries(1) { @user.preferences(:chat) }
1243
+ end
1244
+
1245
+ it "test_should_reset_preferences_changed_query" do
1246
+ assert_equal false, @user.preferences_changed?(:chat)
1247
+ end
1248
+
1249
+ it "test_should_reset_preferences_changed" do
1250
+ assert_equal [], @user.preferences_changed(:chat)
1251
+ end
1252
+
1253
+ it "test_should_reset_preference_changes" do
1254
+ assert_equal e = {}, @user.preference_changes(:chat)
1255
+ end
1256
+ end
1257
+
1258
+ #------------------------------------------------------------------------------
1259
+ describe "PreferencesWithScopeTest" do
1260
+ before do
1261
+ User.preference :notifications
1262
+ User.preference :language, :string, :default => 'English'
1263
+ User.preference :color, :string, :default => 'red'
1264
+
1265
+ @user = create_user
1266
+ @customized_user = create_user(:login => 'customized',
1267
+ :prefers_notifications => false,
1268
+ :preferred_language => 'Latin'
1269
+ )
1270
+ @customized_user.prefers_notifications = false, :chat
1271
+ @customized_user.preferred_language = 'Latin', :chat
1272
+ @customized_user.save!
1273
+ end
1274
+
1275
+ it "test_should_not_find_if_no_preference_matched" do
1276
+ assert_equal [], User.with_preferences(:language => 'Italian')
1277
+ end
1278
+
1279
+ it "test_should_find_with_null_preference" do
1280
+ assert_equal [@user], User.with_preferences(:notifications => nil)
1281
+ end
1282
+
1283
+ it "test_should_find_with_default_preference" do
1284
+ assert_equal [@user], User.with_preferences(:language => 'English')
1285
+ end
1286
+
1287
+ it "test_should_find_with_multiple_default_preferences" do
1288
+ assert_equal [@user], User.with_preferences(:notifications => nil, :language => 'English')
1289
+ end
1290
+
1291
+ it "test_should_find_with_custom_preference" do
1292
+ assert_equal [@customized_user], User.with_preferences(:language => 'Latin')
1293
+ end
1294
+
1295
+ it "test_should_find_with_multiple_custom_preferences" do
1296
+ assert_equal [@customized_user], User.with_preferences(:notifications => false, :language => 'Latin')
1297
+ end
1298
+
1299
+ it "test_should_find_with_mixed_default_and_custom_preferences" do
1300
+ assert_equal [@customized_user], User.with_preferences(:color => 'red', :language => 'Latin')
1301
+ end
1302
+
1303
+ it "test_should_find_with_default_group_preference" do
1304
+ assert_equal [@user], User.with_preferences(:chat => {:language => 'English'})
1305
+ end
1306
+
1307
+ it "test_should_find_with_customized_default_group_preference" do
1308
+ User.preference :country, :string, :default => 'US', :group_defaults => {:chat => 'UK'}
1309
+ @customized_user.preferred_country = 'US', :chat
1310
+ @customized_user.save!
1311
+
1312
+ assert_equal [@user], User.with_preferences(:chat => {:country => 'UK'})
1313
+ end
1314
+
1315
+ it "test_should_find_with_multiple_default_group_preferences" do
1316
+ assert_equal [@user], User.with_preferences(:chat => {:notifications => nil, :language => 'English'})
1317
+ end
1318
+
1319
+ it "test_should_find_with_custom_group_preference" do
1320
+ assert_equal [@customized_user], User.with_preferences(:chat => {:language => 'Latin'})
1321
+ end
1322
+
1323
+ it "test_should_find_with_multiple_custom_group_preferences" do
1324
+ assert_equal [@customized_user], User.with_preferences(:chat => {:notifications => false, :language => 'Latin'})
1325
+ end
1326
+
1327
+ it "test_should_find_with_mixed_default_and_custom_group_preferences" do
1328
+ assert_equal [@customized_user], User.with_preferences(:chat => {:color => 'red', :language => 'Latin'})
1329
+ end
1330
+
1331
+ it "test_should_find_with_mixed_basic_and_group_preferences" do
1332
+ @customized_user.preferred_language = 'English'
1333
+ @customized_user.save!
1334
+
1335
+ assert_equal [@customized_user], User.with_preferences(:language => 'English', :chat => {:language => 'Latin'})
1336
+ end
1337
+
1338
+ it "test_should_allow_chaining" do
1339
+ assert_equal [@user], User.with_preferences(:language => 'English').with_preferences(:color => 'red')
1340
+ end
1341
+ end
1342
+
1343
+ #------------------------------------------------------------------------------
1344
+ describe "PreferencesWithoutScopeTest" do
1345
+ before do
1346
+ User.preference :notifications
1347
+ User.preference :language, :string, :default => 'English'
1348
+ User.preference :color, :string, :default => 'red'
1349
+
1350
+ @user = create_user
1351
+ @customized_user = create_user(:login => 'customized',
1352
+ :prefers_notifications => false,
1353
+ :preferred_language => 'Latin'
1354
+ )
1355
+ @customized_user.prefers_notifications = false, :chat
1356
+ @customized_user.preferred_language = 'Latin', :chat
1357
+ @customized_user.save!
1358
+ end
1359
+
1360
+ it "test_should_not_find_if_no_preference_matched" do
1361
+ assert_equal [], User.without_preferences(:color => 'red')
1362
+ end
1363
+
1364
+ it "test_should_find_with_null_preference" do
1365
+ assert_equal [@user], User.without_preferences(:notifications => false)
1366
+ end
1367
+
1368
+ it "test_should_find_with_default_preference" do
1369
+ assert_equal [@user], User.without_preferences(:language => 'Latin')
1370
+ end
1371
+
1372
+ it "test_should_find_with_multiple_default_preferences" do
1373
+ assert_equal [@user], User.without_preferences(:language => 'Latin', :notifications => false)
1374
+ end
1375
+
1376
+ it "test_should_find_with_custom_preference" do
1377
+ assert_equal [@customized_user], User.without_preferences(:language => 'English')
1378
+ end
1379
+
1380
+ it "test_should_find_with_multiple_custom_preferences" do
1381
+ assert_equal [@customized_user], User.without_preferences(:language => 'English', :notifications => nil)
1382
+ end
1383
+
1384
+ it "test_should_find_with_mixed_default_and_custom_preferences" do
1385
+ assert_equal [@customized_user], User.without_preferences(:language => 'English', :color => 'blue')
1386
+ end
1387
+
1388
+ it "test_should_find_with_default_group_preference" do
1389
+ assert_equal [@user], User.without_preferences(:chat => {:language => 'Latin'})
1390
+ end
1391
+
1392
+ it "test_should_find_with_customized_default_group_preference" do
1393
+ User.preference :country, :string, :default => 'US', :group_defaults => {:chat => 'UK'}
1394
+ @customized_user.preferred_country = 'US', :chat
1395
+ @customized_user.save!
1396
+
1397
+ assert_equal [@user], User.without_preferences(:chat => {:country => 'US'})
1398
+ end
1399
+
1400
+ it "test_should_find_with_multiple_default_group_preferences" do
1401
+ assert_equal [@user], User.without_preferences(:chat => {:language => 'Latin', :notifications => false})
1402
+ end
1403
+
1404
+ it "test_should_find_with_custom_group_preference" do
1405
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English'})
1406
+ end
1407
+
1408
+ it "test_should_find_with_multiple_custom_group_preferences" do
1409
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English', :notifications => nil})
1410
+ end
1411
+
1412
+ it "test_should_find_with_mixed_default_and_custom_group_preferences" do
1413
+ assert_equal [@customized_user], User.without_preferences(:chat => {:language => 'English', :color => 'blue'})
1414
+ end
1415
+
1416
+ it "test_should_find_with_mixed_basic_and_group_preferences" do
1417
+ @customized_user.preferred_language = 'English'
1418
+ @customized_user.save!
1419
+
1420
+ assert_equal [@customized_user], User.without_preferences(:language => 'Latin', :chat => {:language => 'English'})
1421
+ end
1422
+
1423
+ it "test_should_allow_chaining" do
1424
+ assert_equal [@user], User.without_preferences(:language => 'Latin').without_preferences(:color => 'blue')
1425
+ end
1426
+ end
1427
+ end