optimeez_preferences 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +82 -0
- data/LICENSE +20 -0
- data/README.rdoc +216 -0
- data/Rakefile +75 -0
- data/app/models/preference.rb +65 -0
- data/init.rb +1 -0
- data/lib/generators/preferences/USAGE +5 -0
- data/lib/generators/preferences/preferences_generator.rb +17 -0
- data/lib/generators/preferences/templates/001_create_preferences.rb +16 -0
- data/lib/preferences.rb +619 -0
- data/lib/preferences/engine.rb +8 -0
- data/lib/preferences/preference_definition.rb +56 -0
- data/test/app_root/app/models/car.rb +2 -0
- data/test/app_root/app/models/employee.rb +2 -0
- data/test/app_root/app/models/manager.rb +3 -0
- data/test/app_root/app/models/user.rb +8 -0
- data/test/app_root/db/migrate/001_create_users.rb +11 -0
- data/test/app_root/db/migrate/002_create_cars.rb +11 -0
- data/test/app_root/db/migrate/003_create_employees.rb +12 -0
- data/test/app_root/db/migrate/004_migrate_preferences_to_version_1.rb +13 -0
- data/test/factory.rb +65 -0
- data/test/functional/preferences_test.rb +1364 -0
- data/test/test_helper.rb +26 -0
- data/test/unit/preference_definition_test.rb +237 -0
- data/test/unit/preference_test.rb +236 -0
- metadata +83 -0
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Load the plugin testing framework
|
2
|
+
$:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
|
3
|
+
require 'rubygems'
|
4
|
+
require 'plugin_test_helper'
|
5
|
+
|
6
|
+
# Run the migrations
|
7
|
+
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
|
8
|
+
|
9
|
+
# Mixin the factory helper
|
10
|
+
require File.expand_path("#{File.dirname(__FILE__)}/factory")
|
11
|
+
Test::Unit::TestCase.class_eval do
|
12
|
+
include Factory
|
13
|
+
end
|
14
|
+
|
15
|
+
# Add query counter
|
16
|
+
ActiveRecord::Base.connection.class.class_eval do
|
17
|
+
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
|
18
|
+
|
19
|
+
def execute_with_query_record(sql, name = nil, &block)
|
20
|
+
$queries_executed ||= []
|
21
|
+
$queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
|
22
|
+
execute_without_query_record(sql, name, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
alias_method_chain :execute, :query_record
|
26
|
+
end
|
@@ -0,0 +1,237 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class PreferenceDefinitionByDefaultTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_have_a_name
|
9
|
+
assert_equal 'notifications', @definition.name
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_not_have_a_default_value
|
13
|
+
assert_nil @definition.default_value
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_have_a_type
|
17
|
+
assert_equal :boolean, @definition.type
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_type_cast_values_as_booleans
|
21
|
+
assert_equal nil, @definition.type_cast(nil)
|
22
|
+
assert_equal true, @definition.type_cast(true)
|
23
|
+
assert_equal false, @definition.type_cast(false)
|
24
|
+
assert_equal false, @definition.type_cast(0)
|
25
|
+
assert_equal true, @definition.type_cast(1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class PreferenceDefinitionTest < ActiveSupport::TestCase
|
30
|
+
def test_should_raise_exception_if_invalid_option_specified
|
31
|
+
assert_raise(ArgumentError) {Preferences::PreferenceDefinition.new(:notifications, :invalid => true)}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class PreferenceDefinitionWithDefaultValueTest < ActiveSupport::TestCase
|
36
|
+
def setup
|
37
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, :boolean, :default => 1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_type_cast_default_values
|
41
|
+
assert_equal true, @definition.default_value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class PreferenceDefinitionWithGroupDefaultsTest < ActiveSupport::TestCase
|
46
|
+
def setup
|
47
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, :boolean, :default => 1, :group_defaults => {:chat => 0})
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_use_default_for_default_group
|
51
|
+
assert_equal true, @definition.default_value
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_use_default_for_unknown_group
|
55
|
+
assert_equal true, @definition.default_value('email')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_use_group_default_for_known_group
|
59
|
+
assert_equal false, @definition.default_value('chat')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class PreferenceDefinitionWithStringifiedTypeTest < ActiveSupport::TestCase
|
64
|
+
def setup
|
65
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, 'any')
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_symbolize_type
|
69
|
+
assert_equal :any, @definition.type
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class PreferenceDefinitionWithAnyTypeTest < ActiveSupport::TestCase
|
74
|
+
def setup
|
75
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, :any)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_use_custom_type
|
79
|
+
assert_equal :any, @definition.type
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_not_be_number
|
83
|
+
assert !@definition.number?
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_should_not_type_cast
|
87
|
+
assert_equal nil, @definition.type_cast(nil)
|
88
|
+
assert_equal 0, @definition.type_cast(0)
|
89
|
+
assert_equal 1, @definition.type_cast(1)
|
90
|
+
assert_equal false, @definition.type_cast(false)
|
91
|
+
assert_equal true, @definition.type_cast(true)
|
92
|
+
assert_equal '', @definition.type_cast('')
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_should_query_false_if_value_is_nil
|
96
|
+
assert_equal false, @definition.query(nil)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_query_true_if_value_is_zero
|
100
|
+
assert_equal true, @definition.query(0)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_query_true_if_value_is_not_zero
|
104
|
+
assert_equal true, @definition.query(1)
|
105
|
+
assert_equal true, @definition.query(-1)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_should_query_false_if_value_is_blank
|
109
|
+
assert_equal false, @definition.query('')
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_should_query_true_if_value_is_not_blank
|
113
|
+
assert_equal true, @definition.query('hello')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class PreferenceDefinitionWithBooleanTypeTest < ActiveSupport::TestCase
|
118
|
+
def setup
|
119
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_should_not_be_number
|
123
|
+
assert !@definition.number?
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_should_not_type_cast_if_value_is_nil
|
127
|
+
assert_equal nil, @definition.type_cast(nil)
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_should_type_cast_to_false_if_value_is_zero
|
131
|
+
assert_equal false, @definition.type_cast(0)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_should_type_cast_to_true_if_value_is_not_zero
|
135
|
+
assert_equal true, @definition.type_cast(1)
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_should_type_cast_to_true_if_value_is_true_string
|
139
|
+
assert_equal true, @definition.type_cast('true')
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_should_type_cast_to_nil_if_value_is_not_true_string
|
143
|
+
assert_nil @definition.type_cast('')
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_should_query_false_if_value_is_nil
|
147
|
+
assert_equal false, @definition.query(nil)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_should_query_true_if_value_is_one
|
151
|
+
assert_equal true, @definition.query(1)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_should_query_false_if_value_not_one
|
155
|
+
assert_equal false, @definition.query(0)
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_should_query_true_if_value_is_true_string
|
159
|
+
assert_equal true, @definition.query('true')
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_should_query_false_if_value_is_not_true_string
|
163
|
+
assert_equal false, @definition.query('')
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
class PreferenceDefinitionWithNumericTypeTest < ActiveSupport::TestCase
|
168
|
+
def setup
|
169
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, :integer)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_should_be_number
|
173
|
+
assert @definition.number?
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_should_type_cast_true_to_integer
|
177
|
+
assert_equal 1, @definition.type_cast(true)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_should_type_cast_false_to_integer
|
181
|
+
assert_equal 0, @definition.type_cast(false)
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_should_type_cast_string_to_integer
|
185
|
+
assert_equal 0, @definition.type_cast('hello')
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_should_query_false_if_value_is_nil
|
189
|
+
assert_equal false, @definition.query(nil)
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_should_query_true_if_value_is_one
|
193
|
+
assert_equal true, @definition.query(1)
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_should_query_false_if_value_is_zero
|
197
|
+
assert_equal false, @definition.query(0)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
class PreferenceDefinitionWithStringTypeTest < ActiveSupport::TestCase
|
202
|
+
def setup
|
203
|
+
@definition = Preferences::PreferenceDefinition.new(:notifications, :string)
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_should_not_be_number
|
207
|
+
assert !@definition.number?
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_should_type_cast_integers_to_strings
|
211
|
+
assert_equal '1', @definition.type_cast('1')
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_should_not_type_cast_booleans
|
215
|
+
assert_equal true, @definition.type_cast(true)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_should_query_false_if_value_is_nil
|
219
|
+
assert_equal false, @definition.query(nil)
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_should_query_true_if_value_is_one
|
223
|
+
assert_equal true, @definition.query(1)
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_should_query_true_if_value_is_zero
|
227
|
+
assert_equal true, @definition.query(0)
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_should_query_false_if_value_is_blank
|
231
|
+
assert_equal false, @definition.query('')
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_should_query_true_if_value_is_not_blank
|
235
|
+
assert_equal true, @definition.query('hello')
|
236
|
+
end
|
237
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class PreferenceByDefaultTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
@preference = Preference.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_not_have_a_name
|
9
|
+
assert @preference.name.blank?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_not_have_an_owner
|
13
|
+
assert_nil @preference.owner_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_not_have_an_owner_type
|
17
|
+
assert @preference.owner_type.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_not_have_a_group_association
|
21
|
+
assert_nil @preference.group_id
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_not_have_a_group_type
|
25
|
+
assert @preference.group_type.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_not_have_a_value
|
29
|
+
assert @preference.value.blank?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_not_have_a_definition
|
33
|
+
assert_nil @preference.definition
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class PreferenceTest < ActiveSupport::TestCase
|
38
|
+
def test_should_be_valid_with_a_set_of_valid_attributes
|
39
|
+
preference = new_preference
|
40
|
+
assert preference.valid?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_require_a_name
|
44
|
+
preference = new_preference(:name => nil)
|
45
|
+
assert !preference.valid?
|
46
|
+
assert preference.errors.invalid?(:name)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_require_an_owner_id
|
50
|
+
preference = new_preference(:owner => nil)
|
51
|
+
assert !preference.valid?
|
52
|
+
assert preference.errors.invalid?(:owner_id)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_require_an_owner_type
|
56
|
+
preference = new_preference(:owner => nil)
|
57
|
+
assert !preference.valid?
|
58
|
+
assert preference.errors.invalid?(:owner_type)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_not_require_a_group_id
|
62
|
+
preference = new_preference(:group => nil)
|
63
|
+
assert preference.valid?
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_should_not_require_a_group_id_if_type_specified
|
67
|
+
preference = new_preference(:group => nil)
|
68
|
+
preference.group_type = 'Car'
|
69
|
+
assert preference.valid?
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_not_require_a_group_type
|
73
|
+
preference = new_preference(:group => nil)
|
74
|
+
assert preference.valid?
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_should_require_a_group_type_if_id_specified
|
78
|
+
preference = new_preference(:group => nil)
|
79
|
+
preference.group_id = 1
|
80
|
+
assert !preference.valid?
|
81
|
+
assert preference.errors.invalid?(:group_type)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_should_protect_attributes_from_mass_assignment
|
85
|
+
preference = Preference.new(
|
86
|
+
:id => 1,
|
87
|
+
:name => 'notifications',
|
88
|
+
:value => '123',
|
89
|
+
:owner_id => 1,
|
90
|
+
:owner_type => 'User',
|
91
|
+
:group_id => 1,
|
92
|
+
:group_type => 'Car'
|
93
|
+
)
|
94
|
+
|
95
|
+
assert_nil preference.id
|
96
|
+
assert_equal 'notifications', preference.name
|
97
|
+
assert_equal '123', preference.value
|
98
|
+
assert_equal 1, preference.owner_id
|
99
|
+
assert_equal 'User', preference.owner_type
|
100
|
+
assert_equal 1, preference.group_id
|
101
|
+
assert_equal 'Car', preference.group_type
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class PreferenceAsAClassTest < ActiveSupport::TestCase
|
106
|
+
def test_should_be_able_to_split_nil_groups
|
107
|
+
group_id, group_type = Preference.split_group(nil)
|
108
|
+
assert_nil group_id
|
109
|
+
assert_nil group_type
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_should_be_able_to_split_non_active_record_groups
|
113
|
+
group_id, group_type = Preference.split_group('car')
|
114
|
+
assert_nil group_id
|
115
|
+
assert_equal 'car', group_type
|
116
|
+
|
117
|
+
group_id, group_type = Preference.split_group(:car)
|
118
|
+
assert_nil group_id
|
119
|
+
assert_equal 'car', group_type
|
120
|
+
|
121
|
+
group_id, group_type = Preference.split_group(10)
|
122
|
+
assert_nil group_id
|
123
|
+
assert_equal 10, group_type
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_should_be_able_to_split_active_record_groups
|
127
|
+
car = create_car
|
128
|
+
|
129
|
+
group_id, group_type = Preference.split_group(car)
|
130
|
+
assert_equal 1, group_id
|
131
|
+
assert_equal 'Car', group_type
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
class PreferenceAfterBeingCreatedTest < ActiveSupport::TestCase
|
136
|
+
def setup
|
137
|
+
User.preference :notifications, :boolean
|
138
|
+
|
139
|
+
@preference = create_preference(:name => 'notifications')
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_should_have_an_owner
|
143
|
+
assert_not_nil @preference.owner
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_should_have_a_definition
|
147
|
+
assert_not_nil @preference.definition
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_should_have_a_value
|
151
|
+
assert_not_nil @preference.value
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_should_not_have_a_group_association
|
155
|
+
assert_nil @preference.group
|
156
|
+
end
|
157
|
+
|
158
|
+
def teardown
|
159
|
+
User.preference_definitions.delete('notifications')
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
class PreferenceWithBasicGroupTest < ActiveSupport::TestCase
|
164
|
+
def setup
|
165
|
+
@preference = create_preference(:group_type => 'car')
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_should_have_a_group_association
|
169
|
+
assert_equal 'car', @preference.group
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
class PreferenceWithActiveRecordGroupTest < ActiveSupport::TestCase
|
174
|
+
def setup
|
175
|
+
@car = create_car
|
176
|
+
@preference = create_preference(:group => @car)
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_should_have_a_group_association
|
180
|
+
assert_equal @car, @preference.group
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class PreferenceWithBooleanTypeTest < ActiveSupport::TestCase
|
185
|
+
def setup
|
186
|
+
User.preference :notifications, :boolean
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_should_type_cast_nil_values
|
190
|
+
preference = new_preference(:name => 'notifications', :value => nil)
|
191
|
+
assert_nil preference.value
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_should_type_cast_numeric_values
|
195
|
+
preference = new_preference(:name => 'notifications', :value => 0)
|
196
|
+
assert_equal false, preference.value
|
197
|
+
|
198
|
+
preference.value = 1
|
199
|
+
assert_equal true, preference.value
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_should_type_cast_boolean_values
|
203
|
+
preference = new_preference(:name => 'notifications', :value => false)
|
204
|
+
assert_equal false, preference.value
|
205
|
+
|
206
|
+
preference.value = true
|
207
|
+
assert_equal true, preference.value
|
208
|
+
end
|
209
|
+
|
210
|
+
def teardown
|
211
|
+
User.preference_definitions.delete('notifications')
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
class PreferenceWithSTIOwnerTest < ActiveSupport::TestCase
|
216
|
+
def setup
|
217
|
+
@manager = create_manager
|
218
|
+
@preference = create_preference(:owner => @manager, :name => 'health_insurance', :value => true)
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_should_have_an_owner
|
222
|
+
assert_equal @manager, @preference.owner
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_should_have_an_owner_type
|
226
|
+
assert_equal 'Employee', @preference.owner_type
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_should_have_a_definition
|
230
|
+
assert_not_nil @preference.definition
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_should_have_a_value
|
234
|
+
assert_equal true, @preference.value
|
235
|
+
end
|
236
|
+
end
|