flag_shih_tzu 0.3.13 → 0.3.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +24 -16
- data/CHANGELOG.md +29 -17
- data/Gemfile +3 -0
- data/README.md +131 -93
- data/REEK +62 -66
- data/Rakefile +1 -1
- data/bin/test.bash +58 -8
- data/flag_shih_tzu.gemspec +4 -4
- data/gemfiles/Gemfile.activerecord-2.3.x +14 -3
- data/gemfiles/Gemfile.activerecord-3.0.x +13 -3
- data/gemfiles/Gemfile.activerecord-3.1.x +13 -3
- data/gemfiles/Gemfile.activerecord-3.2.x +13 -3
- data/gemfiles/Gemfile.activerecord-4.0.x +8 -2
- data/gemfiles/Gemfile.activerecord-4.1.x +8 -3
- data/gemfiles/Gemfile.activerecord-4.2.x +11 -0
- data/lib/flag_shih_tzu.rb +303 -205
- data/lib/flag_shih_tzu/validators.rb +5 -2
- data/lib/flag_shih_tzu/version.rb +1 -1
- data/test/database.yml +1 -1
- data/test/flag_shih_tzu_test.rb +368 -193
- data/test/test_helper.rb +12 -6
- metadata +17 -17
@@ -2,7 +2,7 @@
|
|
2
2
|
unless defined?(::ActiveRecord)
|
3
3
|
begin
|
4
4
|
# If by some miracle it hasn't been loaded yet, try to load it.
|
5
|
-
require
|
5
|
+
require "active_record"
|
6
6
|
rescue LoadError
|
7
7
|
# If it fails to load, then assume the user is try to use flag_shih_tzu with some other database adapter
|
8
8
|
warn "FlagShihTzu probably won't work unless you have some version of Active Record loaded. Versions >= 2.3 are supported."
|
@@ -12,13 +12,15 @@ end
|
|
12
12
|
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 3
|
13
13
|
|
14
14
|
module ActiveModel
|
15
|
+
# Open ActiveModel::Validations to define some additional ones
|
15
16
|
module Validations
|
16
17
|
|
18
|
+
# A simple EachValidator that will check for the presence of the flags specified
|
17
19
|
class PresenceOfFlagsValidator < EachValidator
|
18
20
|
def validate_each(record, attribute, value)
|
19
21
|
value = record.send(:read_attribute_for_validation, attribute)
|
20
22
|
check_flag(record, attribute)
|
21
|
-
record.errors.add(attribute, :blank, options) if value.blank?
|
23
|
+
record.errors.add(attribute, :blank, options) if value.blank? || value == 0
|
22
24
|
end
|
23
25
|
|
24
26
|
private
|
@@ -30,6 +32,7 @@ if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 3
|
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
35
|
+
# Use these validators in your model
|
33
36
|
module HelperMethods
|
34
37
|
# Validates that the specified attributes are flags and are not blank.
|
35
38
|
# Happens by default on save. Example:
|
data/test/database.yml
CHANGED
data/test/flag_shih_tzu_test.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) +
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper.rb")
|
2
2
|
|
3
3
|
class Spaceship < ActiveRecord::Base
|
4
|
-
self.table_name =
|
4
|
+
self.table_name = "spaceships"
|
5
5
|
include FlagShihTzu
|
6
6
|
|
7
7
|
has_flags 1 => :warpdrive,
|
@@ -10,73 +10,91 @@ class Spaceship < ActiveRecord::Base
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class SpaceshipWithoutNamedScopes < ActiveRecord::Base
|
13
|
-
self.table_name =
|
13
|
+
self.table_name = "spaceships"
|
14
14
|
include FlagShihTzu
|
15
15
|
|
16
|
-
has_flags(1 => :warpdrive,
|
16
|
+
has_flags(1 => :warpdrive,
|
17
|
+
named_scopes: false)
|
17
18
|
end
|
18
19
|
|
19
20
|
class SpaceshipWithoutNamedScopesOldStyle < ActiveRecord::Base
|
20
|
-
self.table_name =
|
21
|
+
self.table_name = "spaceships"
|
21
22
|
include FlagShihTzu
|
22
23
|
|
23
|
-
has_flags({1 => :warpdrive},
|
24
|
+
has_flags({ 1 => :warpdrive },
|
25
|
+
named_scopes: false)
|
24
26
|
end
|
25
27
|
|
26
28
|
class SpaceshipWithCustomFlagsColumn < ActiveRecord::Base
|
27
|
-
self.table_name =
|
29
|
+
self.table_name = "spaceships_with_custom_flags_column"
|
28
30
|
include FlagShihTzu
|
29
31
|
|
30
|
-
has_flags(1 => :warpdrive,
|
32
|
+
has_flags(1 => :warpdrive,
|
33
|
+
2 => :hyperspace,
|
34
|
+
column: "bits")
|
31
35
|
end
|
32
36
|
|
33
37
|
class SpaceshipWithColumnNameAsSymbol < ActiveRecord::Base
|
34
|
-
self.table_name =
|
38
|
+
self.table_name = "spaceships_with_custom_flags_column"
|
35
39
|
include FlagShihTzu
|
36
40
|
|
37
|
-
has_flags(1 => :warpdrive,
|
41
|
+
has_flags(1 => :warpdrive,
|
42
|
+
2 => :hyperspace,
|
43
|
+
column: :bits)
|
38
44
|
end
|
39
45
|
|
40
46
|
class SpaceshipWith2CustomFlagsColumn < ActiveRecord::Base
|
41
|
-
self.table_name =
|
47
|
+
self.table_name = "spaceships_with_2_custom_flags_column"
|
42
48
|
include FlagShihTzu
|
43
49
|
|
44
|
-
has_flags(
|
45
|
-
|
50
|
+
has_flags(
|
51
|
+
{ 1 => :warpdrive, 2 => :hyperspace },
|
52
|
+
column: "bits")
|
53
|
+
has_flags(
|
54
|
+
{ 1 => :jeanlucpicard, 2 => :dajanatroj },
|
55
|
+
column: "commanders")
|
46
56
|
end
|
47
57
|
|
48
58
|
class SpaceshipWith3CustomFlagsColumn < ActiveRecord::Base
|
49
|
-
self.table_name =
|
59
|
+
self.table_name = "spaceships_with_3_custom_flags_column"
|
50
60
|
include FlagShihTzu
|
51
61
|
|
52
|
-
has_flags({ 1 => :warpdrive,
|
53
|
-
|
54
|
-
|
62
|
+
has_flags({ 1 => :warpdrive,
|
63
|
+
2 => :hyperspace },
|
64
|
+
column: "engines")
|
65
|
+
has_flags({ 1 => :photon,
|
66
|
+
2 => :laser,
|
67
|
+
3 => :ion_cannon,
|
68
|
+
4 => :particle_beam },
|
69
|
+
column: "weapons")
|
70
|
+
has_flags({ 1 => :power,
|
71
|
+
2 => :anti_ax_routine },
|
72
|
+
column: "hal3000")
|
55
73
|
end
|
56
74
|
|
57
75
|
class SpaceshipWithInListQueryMode < ActiveRecord::Base
|
58
|
-
self.table_name =
|
76
|
+
self.table_name = "spaceships"
|
59
77
|
include FlagShihTzu
|
60
78
|
|
61
|
-
has_flags(1 => :warpdrive, 2 => :shields, :
|
79
|
+
has_flags(1 => :warpdrive, 2 => :shields, flag_query_mode: :in_list)
|
62
80
|
end
|
63
81
|
|
64
82
|
class SpaceshipWithBitOperatorQueryMode < ActiveRecord::Base
|
65
|
-
self.table_name =
|
83
|
+
self.table_name = "spaceships"
|
66
84
|
include FlagShihTzu
|
67
85
|
|
68
|
-
has_flags(1 => :warpdrive, 2 => :shields, :
|
86
|
+
has_flags(1 => :warpdrive, 2 => :shields, flag_query_mode: :bit_operator)
|
69
87
|
end
|
70
88
|
|
71
89
|
class SpaceshipWithBangMethods < ActiveRecord::Base
|
72
|
-
self.table_name =
|
90
|
+
self.table_name = "spaceships"
|
73
91
|
include FlagShihTzu
|
74
92
|
|
75
|
-
has_flags(1 => :warpdrive, 2 => :shields, :
|
93
|
+
has_flags(1 => :warpdrive, 2 => :shields, bang_methods: true)
|
76
94
|
end
|
77
95
|
|
78
96
|
class SpaceshipWithMissingFlags < ActiveRecord::Base
|
79
|
-
self.table_name =
|
97
|
+
self.table_name = "spaceships"
|
80
98
|
include FlagShihTzu
|
81
99
|
|
82
100
|
has_flags 1 => :warpdrive,
|
@@ -88,44 +106,62 @@ end
|
|
88
106
|
|
89
107
|
if (ActiveRecord::VERSION::MAJOR >= 3)
|
90
108
|
class SpaceshipWithSymbolAndStringFlagColumns < ActiveRecord::Base
|
91
|
-
self.table_name =
|
109
|
+
self.table_name = "spaceships_with_symbol_and_string_flag_columns"
|
92
110
|
include FlagShihTzu
|
93
111
|
|
94
|
-
has_flags({ 1 => :warpdrive,
|
95
|
-
|
96
|
-
|
112
|
+
has_flags({ 1 => :warpdrive,
|
113
|
+
2 => :hyperspace },
|
114
|
+
column: :peace,
|
115
|
+
check_for_column: true)
|
116
|
+
has_flags({ 1 => :photon,
|
117
|
+
2 => :laser,
|
118
|
+
3 => :ion_cannon,
|
119
|
+
4 => :particle_beam },
|
120
|
+
column: :love,
|
121
|
+
check_for_column: true)
|
122
|
+
has_flags({ 1 => :power,
|
123
|
+
2 => :anti_ax_routine },
|
124
|
+
column: "happiness",
|
125
|
+
check_for_column: true)
|
97
126
|
validates_presence_of_flags :peace, :love
|
98
127
|
end
|
99
128
|
|
100
129
|
class SpaceshipWithValidationsAndCustomFlagsColumn < ActiveRecord::Base
|
101
|
-
self.table_name =
|
130
|
+
self.table_name = "spaceships_with_custom_flags_column"
|
102
131
|
include FlagShihTzu
|
103
132
|
|
104
|
-
has_flags(1 => :warpdrive, 2 => :hyperspace, :column =>
|
133
|
+
has_flags(1 => :warpdrive, 2 => :hyperspace, :column => "bits")
|
105
134
|
validates_presence_of_flags :bits
|
106
135
|
end
|
107
136
|
|
108
137
|
class SpaceshipWithValidationsAnd3CustomFlagsColumn < ActiveRecord::Base
|
109
|
-
self.table_name =
|
138
|
+
self.table_name = "spaceships_with_3_custom_flags_column"
|
110
139
|
include FlagShihTzu
|
111
140
|
|
112
|
-
has_flags(
|
113
|
-
|
114
|
-
|
141
|
+
has_flags(
|
142
|
+
{ 1 => :warpdrive, 2 => :hyperspace },
|
143
|
+
column: "engines")
|
144
|
+
has_flags(
|
145
|
+
{ 1 => :photon, 2 => :laser, 3 => :ion_cannon, 4 => :particle_beam },
|
146
|
+
column: "weapons")
|
147
|
+
has_flags(
|
148
|
+
{ 1 => :power, 2 => :anti_ax_routine },
|
149
|
+
column: "hal3000")
|
115
150
|
|
116
151
|
validates_presence_of_flags :engines, :weapons
|
117
152
|
end
|
118
153
|
|
119
154
|
class SpaceshipWithValidationsOnNonFlagsColumn < ActiveRecord::Base
|
120
|
-
self.table_name =
|
155
|
+
self.table_name = "spaceships_with_custom_flags_column"
|
121
156
|
include FlagShihTzu
|
122
157
|
|
123
|
-
has_flags(1 => :warpdrive, 2 => :hyperspace, :
|
158
|
+
has_flags(1 => :warpdrive, 2 => :hyperspace, column: "bits")
|
124
159
|
validates_presence_of_flags :id
|
125
160
|
end
|
126
161
|
end
|
127
162
|
|
128
|
-
# table planets is missing intentionally to see if
|
163
|
+
# table planets is missing intentionally to see if
|
164
|
+
# flag_shih_tzu handles missing tables gracefully
|
129
165
|
class Planet < ActiveRecord::Base
|
130
166
|
end
|
131
167
|
|
@@ -139,13 +175,13 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
139
175
|
assert_raises ArgumentError do
|
140
176
|
eval(<<-EOF
|
141
177
|
class SpaceshipWithInvalidFlagKey < ActiveRecord::Base
|
142
|
-
self.table_name =
|
178
|
+
self.table_name = "spaceships"
|
143
179
|
include FlagShihTzu
|
144
180
|
|
145
181
|
has_flags({ -1 => :error })
|
146
182
|
end
|
147
|
-
|
148
|
-
|
183
|
+
EOF
|
184
|
+
)
|
149
185
|
end
|
150
186
|
end
|
151
187
|
|
@@ -153,14 +189,14 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
153
189
|
assert_raises ArgumentError do
|
154
190
|
eval(<<-EOF
|
155
191
|
class SpaceshipWithAlreadyUsedFlag < ActiveRecord::Base
|
156
|
-
self.table_name =
|
192
|
+
self.table_name = "spaceships_with_2_custom_flags_column"
|
157
193
|
include FlagShihTzu
|
158
194
|
|
159
|
-
has_flags({ 1 => :jeanluckpicard }, :
|
160
|
-
has_flags({ 1 => :jeanluckpicard }, :
|
195
|
+
has_flags({ 1 => :jeanluckpicard }, column: "bits")
|
196
|
+
has_flags({ 1 => :jeanluckpicard }, column: "commanders")
|
161
197
|
end
|
162
|
-
|
163
|
-
|
198
|
+
EOF
|
199
|
+
)
|
164
200
|
end
|
165
201
|
end
|
166
202
|
|
@@ -168,15 +204,15 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
168
204
|
assert_raises ArgumentError do
|
169
205
|
eval(<<-EOF
|
170
206
|
class SpaceshipWithAlreadyUsedMethod < ActiveRecord::Base
|
171
|
-
self.table_name =
|
207
|
+
self.table_name = "spaceships_with_2_custom_flags_column"
|
172
208
|
include FlagShihTzu
|
173
209
|
|
174
210
|
def jeanluckpicard; end
|
175
211
|
|
176
|
-
has_flags({ 1 => :jeanluckpicard }, :
|
212
|
+
has_flags({ 1 => :jeanluckpicard }, column: "bits")
|
177
213
|
end
|
178
|
-
|
179
|
-
|
214
|
+
EOF
|
215
|
+
)
|
180
216
|
end
|
181
217
|
end
|
182
218
|
|
@@ -184,14 +220,22 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
184
220
|
assert_raises FlagShihTzu::DuplicateFlagColumnException do
|
185
221
|
eval(<<-EOF
|
186
222
|
class SpaceshipWithAlreadyUsedMethodByFlagshitzuStrict < ActiveRecord::Base
|
187
|
-
self.table_name =
|
223
|
+
self.table_name = "spaceships_with_2_custom_flags_column"
|
188
224
|
include FlagShihTzu
|
189
225
|
|
190
|
-
has_flags(
|
191
|
-
|
192
|
-
|
193
|
-
|
226
|
+
has_flags(
|
227
|
+
{ 1 => :jeanluckpicard },
|
228
|
+
column: "bits",
|
229
|
+
strict: true
|
194
230
|
)
|
231
|
+
has_flags(
|
232
|
+
{ 1 => :jeanluckpicard },
|
233
|
+
column: "bits",
|
234
|
+
strict: true
|
235
|
+
)
|
236
|
+
end
|
237
|
+
EOF
|
238
|
+
)
|
195
239
|
end
|
196
240
|
end
|
197
241
|
|
@@ -199,14 +243,14 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
199
243
|
assert_nothing_raised ArgumentError do
|
200
244
|
eval(<<-EOF
|
201
245
|
class SpaceshipWithAlreadyUsedMethodByFlagshitzu < ActiveRecord::Base
|
202
|
-
self.table_name =
|
246
|
+
self.table_name = "spaceships_with_2_custom_flags_column"
|
203
247
|
include FlagShihTzu
|
204
248
|
|
205
|
-
has_flags({ 1 => :jeanluckpicard }, :
|
206
|
-
has_flags({ 1 => :jeanluckpicard }, :
|
249
|
+
has_flags({ 1 => :jeanluckpicard }, column: "bits")
|
250
|
+
has_flags({ 1 => :jeanluckpicard }, column: "bits")
|
207
251
|
end
|
208
|
-
|
209
|
-
|
252
|
+
EOF
|
253
|
+
)
|
210
254
|
end
|
211
255
|
end
|
212
256
|
|
@@ -214,92 +258,138 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
214
258
|
assert_raises ArgumentError do
|
215
259
|
eval(<<-EOF
|
216
260
|
class SpaceshipWithInvalidFlagName < ActiveRecord::Base
|
217
|
-
self.table_name =
|
261
|
+
self.table_name = "spaceships"
|
218
262
|
include FlagShihTzu
|
219
263
|
|
220
|
-
has_flags({ 1 =>
|
264
|
+
has_flags({ 1 => "error" })
|
221
265
|
end
|
222
|
-
|
223
|
-
|
266
|
+
EOF
|
267
|
+
)
|
224
268
|
end
|
225
269
|
end
|
226
270
|
|
227
271
|
def test_should_define_a_sql_condition_method_for_flag_enabled
|
228
|
-
assert_equal "(spaceships.flags in (1,3,5,7))",
|
229
|
-
|
230
|
-
assert_equal "(spaceships.flags in (
|
272
|
+
assert_equal "(spaceships.flags in (1,3,5,7))",
|
273
|
+
Spaceship.warpdrive_condition
|
274
|
+
assert_equal "(spaceships.flags in (2,3,6,7))",
|
275
|
+
Spaceship.shields_condition
|
276
|
+
assert_equal "(spaceships.flags in (4,5,6,7))",
|
277
|
+
Spaceship.electrolytes_condition
|
231
278
|
end
|
232
279
|
|
233
280
|
def test_should_define_a_sql_condition_method_for_flag_enabled_with_missing_flags
|
234
|
-
assert_equal "(spaceships.flags in (1,3,5,7))",
|
235
|
-
|
281
|
+
assert_equal "(spaceships.flags in (1,3,5,7))",
|
282
|
+
SpaceshipWithMissingFlags.warpdrive_condition
|
283
|
+
assert_equal "(spaceships.flags in (4,5,6,7))",
|
284
|
+
SpaceshipWithMissingFlags.electrolytes_condition
|
236
285
|
end
|
237
286
|
|
238
287
|
def test_should_accept_a_table_alias_option_for_sql_condition_method
|
239
|
-
assert_equal "(old_spaceships.flags in (1,3,5,7))",
|
288
|
+
assert_equal "(old_spaceships.flags in (1,3,5,7))",
|
289
|
+
Spaceship.warpdrive_condition(table_alias: "old_spaceships")
|
240
290
|
end
|
241
291
|
|
242
292
|
def test_should_define_a_sql_condition_method_for_flag_enabled_with_2_colmns
|
243
|
-
assert_equal "(spaceships_with_2_custom_flags_column.bits in (1,3))",
|
244
|
-
|
245
|
-
assert_equal "(spaceships_with_2_custom_flags_column.
|
246
|
-
|
293
|
+
assert_equal "(spaceships_with_2_custom_flags_column.bits in (1,3))",
|
294
|
+
SpaceshipWith2CustomFlagsColumn.warpdrive_condition
|
295
|
+
assert_equal "(spaceships_with_2_custom_flags_column.bits in (2,3))",
|
296
|
+
SpaceshipWith2CustomFlagsColumn.hyperspace_condition
|
297
|
+
assert_equal "(spaceships_with_2_custom_flags_column.commanders in (1,3))",
|
298
|
+
SpaceshipWith2CustomFlagsColumn.jeanlucpicard_condition
|
299
|
+
assert_equal "(spaceships_with_2_custom_flags_column.commanders in (2,3))",
|
300
|
+
SpaceshipWith2CustomFlagsColumn.dajanatroj_condition
|
247
301
|
end
|
248
302
|
|
249
303
|
def test_should_define_a_sql_condition_method_for_flag_not_enabled
|
250
|
-
assert_equal "(spaceships.flags not in (1,3,5,7))",
|
251
|
-
|
252
|
-
assert_equal "(spaceships.flags not in (
|
304
|
+
assert_equal "(spaceships.flags not in (1,3,5,7))",
|
305
|
+
Spaceship.not_warpdrive_condition
|
306
|
+
assert_equal "(spaceships.flags not in (2,3,6,7))",
|
307
|
+
Spaceship.not_shields_condition
|
308
|
+
assert_equal "(spaceships.flags not in (4,5,6,7))",
|
309
|
+
Spaceship.not_electrolytes_condition
|
253
310
|
end
|
254
311
|
|
255
312
|
def test_should_define_a_sql_condition_method_for_flag_not_enabled_with_missing_flags
|
256
|
-
assert_equal "(spaceships.flags not in (1,3,5,7))",
|
257
|
-
|
313
|
+
assert_equal "(spaceships.flags not in (1,3,5,7))",
|
314
|
+
SpaceshipWithMissingFlags.not_warpdrive_condition
|
315
|
+
assert_equal "(spaceships.flags not in (4,5,6,7))",
|
316
|
+
SpaceshipWithMissingFlags.not_electrolytes_condition
|
258
317
|
end
|
259
318
|
|
260
319
|
def test_sql_condition_for_flag_with_custom_table_name_and_default_query_mode
|
261
|
-
assert_equal "(custom_spaceships.flags in (1,3,5,7))",
|
320
|
+
assert_equal "(custom_spaceships.flags in (1,3,5,7))",
|
321
|
+
Spaceship.send(:sql_condition_for_flag,
|
322
|
+
:warpdrive,
|
323
|
+
"flags",
|
324
|
+
true,
|
325
|
+
"custom_spaceships")
|
262
326
|
end
|
263
327
|
def test_sql_condition_for_flag_with_in_list_query_mode
|
264
|
-
assert_equal "(spaceships.flags in (1,3))",
|
328
|
+
assert_equal "(spaceships.flags in (1,3))",
|
329
|
+
SpaceshipWithInListQueryMode.send(:sql_condition_for_flag,
|
330
|
+
:warpdrive,
|
331
|
+
"flags",
|
332
|
+
true,
|
333
|
+
"spaceships")
|
265
334
|
end
|
266
335
|
def test_sql_condition_for_flag_with_bit_operator_query_mode
|
267
|
-
assert_equal "(spaceships.flags & 1 = 1)",
|
336
|
+
assert_equal "(spaceships.flags & 1 = 1)",
|
337
|
+
SpaceshipWithBitOperatorQueryMode.send(:sql_condition_for_flag,
|
338
|
+
:warpdrive,
|
339
|
+
"flags",
|
340
|
+
true,
|
341
|
+
"spaceships")
|
268
342
|
end
|
269
343
|
def test_sql_in_for_flag
|
270
|
-
assert_equal [1,3,5,7],
|
344
|
+
assert_equal [1, 3, 5, 7],
|
345
|
+
Spaceship.send(:sql_in_for_flag, :warpdrive, "flags")
|
271
346
|
end
|
272
347
|
def test_sql_set_for_flag
|
273
|
-
assert_equal "flags = flags | 1",
|
348
|
+
assert_equal "flags = flags | 1",
|
349
|
+
Spaceship.send(:sql_set_for_flag, :warpdrive, "flags")
|
274
350
|
end
|
275
351
|
|
276
352
|
def test_should_define_a_sql_condition_method_for_flag_enabled_with_2_colmns_not_enabled
|
277
|
-
assert_equal "(spaceships_with_2_custom_flags_column.bits not in (1,3))",
|
278
|
-
|
279
|
-
assert_equal "(spaceships_with_2_custom_flags_column.
|
280
|
-
|
353
|
+
assert_equal "(spaceships_with_2_custom_flags_column.bits not in (1,3))",
|
354
|
+
SpaceshipWith2CustomFlagsColumn.not_warpdrive_condition
|
355
|
+
assert_equal "(spaceships_with_2_custom_flags_column.bits not in (2,3))",
|
356
|
+
SpaceshipWith2CustomFlagsColumn.not_hyperspace_condition
|
357
|
+
assert_equal "(spaceships_with_2_custom_flags_column.commanders not in (1,3))",
|
358
|
+
SpaceshipWith2CustomFlagsColumn.not_jeanlucpicard_condition
|
359
|
+
assert_equal "(spaceships_with_2_custom_flags_column.commanders not in (2,3))",
|
360
|
+
SpaceshipWith2CustomFlagsColumn.not_dajanatroj_condition
|
281
361
|
end
|
282
362
|
|
283
363
|
def test_should_define_a_sql_condition_method_for_flag_enabled_using_bit_operators
|
284
|
-
assert_equal "(spaceships.flags & 1 = 1)",
|
285
|
-
|
364
|
+
assert_equal "(spaceships.flags & 1 = 1)",
|
365
|
+
SpaceshipWithBitOperatorQueryMode.warpdrive_condition
|
366
|
+
assert_equal "(spaceships.flags & 2 = 2)",
|
367
|
+
SpaceshipWithBitOperatorQueryMode.shields_condition
|
286
368
|
end
|
287
369
|
|
288
370
|
def test_should_define_a_sql_condition_method_for_flag_not_enabled_using_bit_operators
|
289
|
-
assert_equal "(spaceships.flags & 1 = 0)",
|
290
|
-
|
371
|
+
assert_equal "(spaceships.flags & 1 = 0)",
|
372
|
+
SpaceshipWithBitOperatorQueryMode.not_warpdrive_condition
|
373
|
+
assert_equal "(spaceships.flags & 2 = 0)",
|
374
|
+
SpaceshipWithBitOperatorQueryMode.not_shields_condition
|
291
375
|
end
|
292
376
|
|
293
377
|
def test_should_define_a_named_scope_for_flag_enabled
|
294
|
-
assert_where_value "(spaceships.flags in (1,3,5,7))",
|
295
|
-
|
296
|
-
assert_where_value "(spaceships.flags in (
|
378
|
+
assert_where_value "(spaceships.flags in (1,3,5,7))",
|
379
|
+
Spaceship.warpdrive
|
380
|
+
assert_where_value "(spaceships.flags in (2,3,6,7))",
|
381
|
+
Spaceship.shields
|
382
|
+
assert_where_value "(spaceships.flags in (4,5,6,7))",
|
383
|
+
Spaceship.electrolytes
|
297
384
|
end
|
298
385
|
|
299
386
|
def test_should_define_a_named_scope_for_flag_not_enabled
|
300
|
-
assert_where_value "(spaceships.flags not in (1,3,5,7))",
|
301
|
-
|
302
|
-
assert_where_value "(spaceships.flags not in (
|
387
|
+
assert_where_value "(spaceships.flags not in (1,3,5,7))",
|
388
|
+
Spaceship.not_warpdrive
|
389
|
+
assert_where_value "(spaceships.flags not in (2,3,6,7))",
|
390
|
+
Spaceship.not_shields
|
391
|
+
assert_where_value "(spaceships.flags not in (4,5,6,7))",
|
392
|
+
Spaceship.not_electrolytes
|
303
393
|
end
|
304
394
|
|
305
395
|
def test_should_define_a_dynamic_column_value_helpers_for_flags
|
@@ -308,27 +398,39 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
308
398
|
end
|
309
399
|
|
310
400
|
def test_should_define_a_named_scope_for_flag_enabled_with_2_columns
|
311
|
-
assert_where_value "(spaceships_with_2_custom_flags_column.bits in (1,3))",
|
312
|
-
|
313
|
-
assert_where_value "(spaceships_with_2_custom_flags_column.
|
314
|
-
|
401
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.bits in (1,3))",
|
402
|
+
SpaceshipWith2CustomFlagsColumn.warpdrive
|
403
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.bits in (2,3))",
|
404
|
+
SpaceshipWith2CustomFlagsColumn.hyperspace
|
405
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.commanders in (1,3))",
|
406
|
+
SpaceshipWith2CustomFlagsColumn.jeanlucpicard
|
407
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.commanders in (2,3))",
|
408
|
+
SpaceshipWith2CustomFlagsColumn.dajanatroj
|
315
409
|
end
|
316
410
|
|
317
411
|
def test_should_define_a_named_scope_for_flag_not_enabled_with_2_columns
|
318
|
-
assert_where_value "(spaceships_with_2_custom_flags_column.bits not in (1,3))",
|
319
|
-
|
320
|
-
assert_where_value "(spaceships_with_2_custom_flags_column.
|
321
|
-
|
412
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.bits not in (1,3))",
|
413
|
+
SpaceshipWith2CustomFlagsColumn.not_warpdrive
|
414
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.bits not in (2,3))",
|
415
|
+
SpaceshipWith2CustomFlagsColumn.not_hyperspace
|
416
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.commanders not in (1,3))",
|
417
|
+
SpaceshipWith2CustomFlagsColumn.not_jeanlucpicard
|
418
|
+
assert_where_value "(spaceships_with_2_custom_flags_column.commanders not in (2,3))",
|
419
|
+
SpaceshipWith2CustomFlagsColumn.not_dajanatroj
|
322
420
|
end
|
323
421
|
|
324
422
|
def test_should_define_a_named_scope_for_flag_enabled_using_bit_operators
|
325
|
-
assert_where_value "(spaceships.flags & 1 = 1)",
|
326
|
-
|
423
|
+
assert_where_value "(spaceships.flags & 1 = 1)",
|
424
|
+
SpaceshipWithBitOperatorQueryMode.warpdrive
|
425
|
+
assert_where_value "(spaceships.flags & 2 = 2)",
|
426
|
+
SpaceshipWithBitOperatorQueryMode.shields
|
327
427
|
end
|
328
428
|
|
329
429
|
def test_should_define_a_named_scope_for_flag_not_enabled_using_bit_operators
|
330
|
-
assert_where_value "(spaceships.flags & 1 = 0)",
|
331
|
-
|
430
|
+
assert_where_value "(spaceships.flags & 1 = 0)",
|
431
|
+
SpaceshipWithBitOperatorQueryMode.not_warpdrive
|
432
|
+
assert_where_value "(spaceships.flags & 2 = 0)",
|
433
|
+
SpaceshipWithBitOperatorQueryMode.not_shields
|
332
434
|
end
|
333
435
|
|
334
436
|
def test_should_work_with_raw_sql
|
@@ -342,10 +444,14 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
342
444
|
assert_equal true, spaceship.electrolytes
|
343
445
|
|
344
446
|
if (ActiveRecord::VERSION::MAJOR <= 3)
|
345
|
-
Spaceship.update_all
|
346
|
-
|
447
|
+
Spaceship.update_all(
|
448
|
+
Spaceship.set_flag_sql(:warpdrive, true),
|
449
|
+
["id=?", spaceship.id]
|
450
|
+
)
|
347
451
|
else
|
348
|
-
Spaceship.where("id=?", spaceship.id).update_all
|
452
|
+
Spaceship.where("id=?", spaceship.id).update_all(
|
453
|
+
Spaceship.set_flag_sql(:warpdrive, true)
|
454
|
+
)
|
349
455
|
end
|
350
456
|
|
351
457
|
spaceship.reload
|
@@ -365,10 +471,14 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
365
471
|
assert_equal true, spaceship.electrolytes
|
366
472
|
|
367
473
|
if (ActiveRecord::VERSION::MAJOR <= 3)
|
368
|
-
Spaceship.update_all
|
369
|
-
|
474
|
+
Spaceship.update_all(
|
475
|
+
Spaceship.set_flag_sql(:shields, false),
|
476
|
+
["id=?", spaceship.id]
|
477
|
+
)
|
370
478
|
else
|
371
|
-
Spaceship.where("id=?", spaceship.id).update_all
|
479
|
+
Spaceship.where("id=?", spaceship.id).update_all(
|
480
|
+
Spaceship.set_flag_sql(:shields, false)
|
481
|
+
)
|
372
482
|
end
|
373
483
|
|
374
484
|
spaceship.reload
|
@@ -401,9 +511,19 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
401
511
|
end
|
402
512
|
|
403
513
|
def test_should_return_the_correct_condition_with_chained_flags
|
404
|
-
assert_equal "(spaceships.flags in (3,7))",
|
405
|
-
|
406
|
-
|
514
|
+
assert_equal "(spaceships.flags in (3,7))",
|
515
|
+
Spaceship.chained_flags_condition("flags",
|
516
|
+
:warpdrive,
|
517
|
+
:shields)
|
518
|
+
assert_equal "(spaceships.flags in (7))",
|
519
|
+
Spaceship.chained_flags_condition("flags",
|
520
|
+
:warpdrive,
|
521
|
+
:shields,
|
522
|
+
:electrolytes)
|
523
|
+
assert_equal "(spaceships.flags in (2,6))",
|
524
|
+
Spaceship.chained_flags_condition("flags",
|
525
|
+
:not_warpdrive,
|
526
|
+
:shields)
|
407
527
|
end
|
408
528
|
|
409
529
|
def test_should_return_the_correct_number_of_items_with_chained_flags_with
|
@@ -423,12 +543,26 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
423
543
|
spaceship_4 = Spaceship.new
|
424
544
|
spaceship_4.save!
|
425
545
|
spaceship_4.reload
|
426
|
-
assert_equal 2, Spaceship.chained_flags_with("flags",
|
427
|
-
|
428
|
-
assert_equal 1, Spaceship.chained_flags_with("flags",
|
429
|
-
|
430
|
-
|
431
|
-
assert_equal 1, Spaceship.chained_flags_with("flags",
|
546
|
+
assert_equal 2, Spaceship.chained_flags_with("flags",
|
547
|
+
:warpdrive).count
|
548
|
+
assert_equal 1, Spaceship.chained_flags_with("flags",
|
549
|
+
:warpdrive,
|
550
|
+
:shields).count
|
551
|
+
assert_equal 1, Spaceship.chained_flags_with("flags",
|
552
|
+
:warpdrive,
|
553
|
+
:not_shields).count
|
554
|
+
assert_equal 0, Spaceship.chained_flags_with("flags",
|
555
|
+
:not_warpdrive,
|
556
|
+
:shields,
|
557
|
+
:electrolytes).count
|
558
|
+
assert_equal 1, Spaceship.chained_flags_with("flags",
|
559
|
+
:not_warpdrive,
|
560
|
+
:shields,
|
561
|
+
:not_electrolytes).count
|
562
|
+
assert_equal 1, Spaceship.chained_flags_with("flags",
|
563
|
+
:not_warpdrive,
|
564
|
+
:not_shields,
|
565
|
+
:not_electrolytes).count
|
432
566
|
end
|
433
567
|
|
434
568
|
def test_should_not_define_named_scopes_if_not_wanted
|
@@ -442,15 +576,23 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
442
576
|
spaceship.enable_flag(:hyperspace)
|
443
577
|
spaceship.save!
|
444
578
|
spaceship.reload
|
445
|
-
assert_equal 3, spaceship.flags(
|
446
|
-
assert_equal "(spaceships_with_custom_flags_column.bits in (1,3))",
|
447
|
-
|
448
|
-
assert_equal "(spaceships_with_custom_flags_column.bits in (
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
579
|
+
assert_equal 3, spaceship.flags("bits")
|
580
|
+
assert_equal "(spaceships_with_custom_flags_column.bits in (1,3))",
|
581
|
+
SpaceshipWithCustomFlagsColumn.warpdrive_condition
|
582
|
+
assert_equal "(spaceships_with_custom_flags_column.bits not in (1,3))",
|
583
|
+
SpaceshipWithCustomFlagsColumn.not_warpdrive_condition
|
584
|
+
assert_equal "(spaceships_with_custom_flags_column.bits in (2,3))",
|
585
|
+
SpaceshipWithCustomFlagsColumn.hyperspace_condition
|
586
|
+
assert_equal "(spaceships_with_custom_flags_column.bits not in (2,3))",
|
587
|
+
SpaceshipWithCustomFlagsColumn.not_hyperspace_condition
|
588
|
+
assert_where_value %[(spaceships_with_custom_flags_column.bits in (1,3))],
|
589
|
+
SpaceshipWithCustomFlagsColumn.warpdrive
|
590
|
+
assert_where_value %[(spaceships_with_custom_flags_column.bits not in (1,3))],
|
591
|
+
SpaceshipWithCustomFlagsColumn.not_warpdrive
|
592
|
+
assert_where_value %[(spaceships_with_custom_flags_column.bits in (2,3))],
|
593
|
+
SpaceshipWithCustomFlagsColumn.hyperspace
|
594
|
+
assert_where_value %[(spaceships_with_custom_flags_column.bits not in (2,3))],
|
595
|
+
SpaceshipWithCustomFlagsColumn.not_hyperspace
|
454
596
|
end
|
455
597
|
|
456
598
|
def test_should_work_with_a_custom_flags_column_name_as_symbol
|
@@ -458,7 +600,7 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
458
600
|
spaceship.enable_flag(:warpdrive)
|
459
601
|
spaceship.save!
|
460
602
|
spaceship.reload
|
461
|
-
assert_equal 1, spaceship.flags(
|
603
|
+
assert_equal 1, spaceship.flags("bits")
|
462
604
|
end
|
463
605
|
|
464
606
|
def test_should_not_error_out_when_table_is_not_present
|
@@ -474,10 +616,12 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
474
616
|
assert_nothing_raised(ActiveRecord::StatementInvalid) do
|
475
617
|
Planet.class_eval do
|
476
618
|
# Now it has a table that exists, but the column does not.
|
477
|
-
self.table_name =
|
619
|
+
self.table_name = "spaceships"
|
478
620
|
include FlagShihTzu
|
479
621
|
|
480
|
-
has_flags({ 1 => :warpdrive, 2 => :hyperspace },
|
622
|
+
has_flags({ 1 => :warpdrive, 2 => :hyperspace },
|
623
|
+
column: :i_do_not_exist,
|
624
|
+
check_for_column: true)
|
481
625
|
end
|
482
626
|
end
|
483
627
|
end
|
@@ -485,8 +629,12 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
|
485
629
|
private
|
486
630
|
|
487
631
|
def assert_where_value(expected, scope)
|
488
|
-
|
489
|
-
|
632
|
+
actual = if ActiveRecord::VERSION::MAJOR == 2
|
633
|
+
scope.proxy_options[:conditions]
|
634
|
+
else
|
635
|
+
scope.where_values.first
|
636
|
+
end
|
637
|
+
assert_equal expected, actual
|
490
638
|
end
|
491
639
|
|
492
640
|
end
|
@@ -553,8 +701,8 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
553
701
|
@big_spaceship.save!
|
554
702
|
@big_spaceship.reload
|
555
703
|
|
556
|
-
assert_equal 1, @big_spaceship.flags(
|
557
|
-
assert_equal 2, @big_spaceship.flags(
|
704
|
+
assert_equal 1, @big_spaceship.flags("bits")
|
705
|
+
assert_equal 2, @big_spaceship.flags("commanders")
|
558
706
|
|
559
707
|
assert @big_spaceship.flag_enabled?(:warpdrive)
|
560
708
|
assert !@big_spaceship.flag_enabled?(:hyperspace)
|
@@ -586,25 +734,29 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
586
734
|
# --------------------------------------------------
|
587
735
|
|
588
736
|
def test_should_define_an_all_flags_reader_method_with_arity_1
|
589
|
-
assert_array_similarity [:electrolytes, :warpdrive, :shields],
|
737
|
+
assert_array_similarity [:electrolytes, :warpdrive, :shields],
|
738
|
+
@spaceship.all_flags("flags")
|
590
739
|
end
|
591
740
|
|
592
741
|
def test_should_define_an_all_flags_reader_method_with_arity_0
|
593
|
-
assert_array_similarity [:electrolytes, :warpdrive, :shields],
|
742
|
+
assert_array_similarity [:electrolytes, :warpdrive, :shields],
|
743
|
+
@spaceship.all_flags
|
594
744
|
end
|
595
745
|
|
596
746
|
def test_should_define_a_selected_flags_reader_method_with_arity_1
|
597
|
-
assert_array_similarity [], @spaceship.selected_flags(
|
747
|
+
assert_array_similarity [], @spaceship.selected_flags("flags")
|
598
748
|
|
599
749
|
@spaceship.warpdrive = true
|
600
|
-
assert_array_similarity [:warpdrive],
|
750
|
+
assert_array_similarity [:warpdrive],
|
751
|
+
@spaceship.selected_flags("flags")
|
601
752
|
|
602
753
|
@spaceship.electrolytes = true
|
603
|
-
assert_array_similarity [:electrolytes, :warpdrive],
|
754
|
+
assert_array_similarity [:electrolytes, :warpdrive],
|
755
|
+
@spaceship.selected_flags("flags")
|
604
756
|
|
605
757
|
@spaceship.warpdrive = false
|
606
758
|
@spaceship.electrolytes = false
|
607
|
-
assert_array_similarity [], @spaceship.selected_flags(
|
759
|
+
assert_array_similarity [], @spaceship.selected_flags("flags")
|
608
760
|
end
|
609
761
|
|
610
762
|
def test_should_define_a_selected_flags_reader_method_with_arity_0
|
@@ -614,7 +766,8 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
614
766
|
assert_array_similarity [:warpdrive], @spaceship.selected_flags
|
615
767
|
|
616
768
|
@spaceship.electrolytes = true
|
617
|
-
assert_array_similarity [:electrolytes, :warpdrive],
|
769
|
+
assert_array_similarity [:electrolytes, :warpdrive],
|
770
|
+
@spaceship.selected_flags
|
618
771
|
|
619
772
|
@spaceship.warpdrive = false
|
620
773
|
@spaceship.electrolytes = false
|
@@ -622,7 +775,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
622
775
|
end
|
623
776
|
|
624
777
|
def test_should_define_a_select_all_flags_method_with_arity_1
|
625
|
-
@spaceship.select_all_flags(
|
778
|
+
@spaceship.select_all_flags("flags")
|
626
779
|
assert @spaceship.warpdrive
|
627
780
|
assert @spaceship.shields
|
628
781
|
assert @spaceship.electrolytes
|
@@ -640,7 +793,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
640
793
|
@spaceship.shields = true
|
641
794
|
@spaceship.electrolytes = true
|
642
795
|
|
643
|
-
@spaceship.unselect_all_flags(
|
796
|
+
@spaceship.unselect_all_flags("flags")
|
644
797
|
|
645
798
|
assert !@spaceship.warpdrive
|
646
799
|
assert !@spaceship.shields
|
@@ -660,19 +813,19 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
660
813
|
end
|
661
814
|
|
662
815
|
def test_should_define_an_has_flag_method_with_arity_1
|
663
|
-
assert !@spaceship.has_flag?(
|
816
|
+
assert !@spaceship.has_flag?("flags")
|
664
817
|
|
665
818
|
@spaceship.warpdrive = true
|
666
|
-
assert @spaceship.has_flag?(
|
819
|
+
assert @spaceship.has_flag?("flags")
|
667
820
|
|
668
821
|
@spaceship.shields = true
|
669
|
-
assert @spaceship.has_flag?(
|
822
|
+
assert @spaceship.has_flag?("flags")
|
670
823
|
|
671
824
|
@spaceship.electrolytes = true
|
672
|
-
assert @spaceship.has_flag?(
|
825
|
+
assert @spaceship.has_flag?("flags")
|
673
826
|
|
674
|
-
@spaceship.unselect_all_flags(
|
675
|
-
assert !@spaceship.has_flag?(
|
827
|
+
@spaceship.unselect_all_flags("flags")
|
828
|
+
assert !@spaceship.has_flag?("flags")
|
676
829
|
end
|
677
830
|
|
678
831
|
def test_should_define_an_has_flag_method_with_arity_0
|
@@ -694,17 +847,20 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
694
847
|
# --------------------------------------------------
|
695
848
|
|
696
849
|
def test_should_define_a_customized_all_flags_reader_method
|
697
|
-
assert_array_similarity [:hyperspace, :warpdrive],
|
850
|
+
assert_array_similarity [:hyperspace, :warpdrive],
|
851
|
+
@small_spaceship.all_bits
|
698
852
|
end
|
699
853
|
|
700
854
|
def test_should_define_a_customized_selected_flags_reader_method
|
701
855
|
assert_array_similarity [], @small_spaceship.selected_bits
|
702
856
|
|
703
857
|
@small_spaceship.warpdrive = true
|
704
|
-
assert_array_similarity [:warpdrive],
|
858
|
+
assert_array_similarity [:warpdrive],
|
859
|
+
@small_spaceship.selected_bits
|
705
860
|
|
706
861
|
@small_spaceship.hyperspace = true
|
707
|
-
assert_array_similarity [:hyperspace, :warpdrive],
|
862
|
+
assert_array_similarity [:hyperspace, :warpdrive],
|
863
|
+
@small_spaceship.selected_bits
|
708
864
|
|
709
865
|
@small_spaceship.warpdrive = false
|
710
866
|
@small_spaceship.hyperspace = false
|
@@ -761,25 +917,33 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
761
917
|
# --------------------------------------------------
|
762
918
|
|
763
919
|
def test_should_define_a_customized_all_flags_reader_method_with_2_columns
|
764
|
-
assert_array_similarity [:hyperspace, :warpdrive],
|
765
|
-
|
920
|
+
assert_array_similarity [:hyperspace, :warpdrive],
|
921
|
+
@big_spaceship.all_bits
|
922
|
+
assert_array_similarity [:dajanatroj, :jeanlucpicard],
|
923
|
+
@big_spaceship.all_commanders
|
766
924
|
end
|
767
925
|
|
768
926
|
def test_should_define_a_customized_selected_flags_reader_method_with_2_columns
|
769
|
-
assert_array_similarity [],
|
770
|
-
|
927
|
+
assert_array_similarity [],
|
928
|
+
@big_spaceship.selected_bits
|
929
|
+
assert_array_similarity [],
|
930
|
+
@big_spaceship.selected_commanders
|
771
931
|
|
772
932
|
@big_spaceship.warpdrive = true
|
773
933
|
@big_spaceship.jeanlucpicard = true
|
774
|
-
assert_array_similarity [:warpdrive],
|
775
|
-
|
934
|
+
assert_array_similarity [:warpdrive],
|
935
|
+
@big_spaceship.selected_bits
|
936
|
+
assert_array_similarity [:jeanlucpicard],
|
937
|
+
@big_spaceship.selected_commanders
|
776
938
|
|
777
939
|
@big_spaceship.hyperspace = true
|
778
940
|
@big_spaceship.hyperspace = true
|
779
941
|
@big_spaceship.jeanlucpicard = true
|
780
942
|
@big_spaceship.dajanatroj = true
|
781
|
-
assert_array_similarity [:hyperspace, :warpdrive],
|
782
|
-
|
943
|
+
assert_array_similarity [:hyperspace, :warpdrive],
|
944
|
+
@big_spaceship.selected_bits
|
945
|
+
assert_array_similarity [:dajanatroj, :jeanlucpicard],
|
946
|
+
@big_spaceship.selected_commanders
|
783
947
|
|
784
948
|
@big_spaceship.warpdrive = false
|
785
949
|
@big_spaceship.hyperspace = false
|
@@ -903,12 +1067,12 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
903
1067
|
end
|
904
1068
|
|
905
1069
|
def test_should_respect_true_values_like_active_record
|
906
|
-
[true, 1,
|
1070
|
+
[true, 1, "1", "t", "T", "true", "TRUE"].each do |true_value|
|
907
1071
|
@spaceship.warpdrive = true_value
|
908
1072
|
assert @spaceship.warpdrive
|
909
1073
|
end
|
910
1074
|
|
911
|
-
[false, 0,
|
1075
|
+
[false, 0, "0", "f", "F", "false", "FALSE"].each do |false_value|
|
912
1076
|
@spaceship.warpdrive = false_value
|
913
1077
|
assert !@spaceship.warpdrive
|
914
1078
|
end
|
@@ -918,7 +1082,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
918
1082
|
assert_nothing_raised do
|
919
1083
|
eval(<<-EOF
|
920
1084
|
class SpaceshipWithoutFlagsColumn1 < ActiveRecord::Base
|
921
|
-
self.table_name =
|
1085
|
+
self.table_name = "spaceships_without_flags_column"
|
922
1086
|
include FlagShihTzu
|
923
1087
|
|
924
1088
|
has_flags 1 => :warpdrive,
|
@@ -936,7 +1100,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
936
1100
|
assert_raises FlagShihTzu::IncorrectFlagColumnException do
|
937
1101
|
eval(<<-EOF
|
938
1102
|
class SpaceshipWithNonIntegerColumn1 < ActiveRecord::Base
|
939
|
-
self.table_name =
|
1103
|
+
self.table_name ="spaceships_with_non_integer_column"
|
940
1104
|
include FlagShihTzu
|
941
1105
|
|
942
1106
|
has_flags 1 => :warpdrive,
|
@@ -954,18 +1118,18 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
954
1118
|
assert_nothing_raised do
|
955
1119
|
eval(<<-EOF
|
956
1120
|
class SpaceshipWithoutFlagsColumn2 < ActiveRecord::Base
|
957
|
-
self.table_name =
|
1121
|
+
self.table_name = "spaceships_without_flags_column"
|
958
1122
|
include FlagShihTzu
|
959
1123
|
|
960
1124
|
has_flags 1 => :warpdrive,
|
961
1125
|
2 => :shields,
|
962
1126
|
3 => :electrolytes,
|
963
|
-
:
|
1127
|
+
check_for_column: true
|
964
1128
|
end
|
965
1129
|
EOF
|
966
1130
|
)
|
967
1131
|
end
|
968
|
-
assert !SpaceshipWithoutFlagsColumn2.send(:check_flag_column,
|
1132
|
+
assert !SpaceshipWithoutFlagsColumn2.send(:check_flag_column, "flags")
|
969
1133
|
assert !SpaceshipWithoutFlagsColumn2.method_defined?(:warpdrive)
|
970
1134
|
end
|
971
1135
|
|
@@ -973,13 +1137,13 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
973
1137
|
assert_raises FlagShihTzu::IncorrectFlagColumnException do
|
974
1138
|
eval(<<-EOF
|
975
1139
|
class SpaceshipWithNonIntegerColumn2 < ActiveRecord::Base
|
976
|
-
self.table_name =
|
1140
|
+
self.table_name ="spaceships_with_non_integer_column"
|
977
1141
|
include FlagShihTzu
|
978
1142
|
|
979
1143
|
has_flags 1 => :warpdrive,
|
980
1144
|
2 => :shields,
|
981
1145
|
3 => :electrolytes,
|
982
|
-
:
|
1146
|
+
check_for_column: true
|
983
1147
|
end
|
984
1148
|
EOF
|
985
1149
|
)
|
@@ -992,13 +1156,13 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
992
1156
|
assert_nothing_raised do
|
993
1157
|
eval(<<-EOF
|
994
1158
|
class SpaceshipWithoutFlagsColumn3 < ActiveRecord::Base
|
995
|
-
self.table_name =
|
1159
|
+
self.table_name = "spaceships_without_flags_column"
|
996
1160
|
include FlagShihTzu
|
997
1161
|
|
998
1162
|
has_flags 1 => :warpdrive,
|
999
1163
|
2 => :shields,
|
1000
1164
|
3 => :electrolytes,
|
1001
|
-
:
|
1165
|
+
check_for_column: false
|
1002
1166
|
end
|
1003
1167
|
EOF
|
1004
1168
|
)
|
@@ -1011,13 +1175,13 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
1011
1175
|
assert_nothing_raised do
|
1012
1176
|
eval(<<-EOF
|
1013
1177
|
class SpaceshipWithNonIntegerColumn3 < ActiveRecord::Base
|
1014
|
-
self.table_name =
|
1178
|
+
self.table_name ="spaceships_with_non_integer_column"
|
1015
1179
|
include FlagShihTzu
|
1016
1180
|
|
1017
1181
|
has_flags 1 => :warpdrive,
|
1018
1182
|
2 => :shields,
|
1019
1183
|
3 => :electrolytes,
|
1020
|
-
:
|
1184
|
+
check_for_column: false
|
1021
1185
|
end
|
1022
1186
|
EOF
|
1023
1187
|
)
|
@@ -1027,7 +1191,8 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
1027
1191
|
end
|
1028
1192
|
|
1029
1193
|
def test_column_guessing_for_default_column_2
|
1030
|
-
assert_equal
|
1194
|
+
assert_equal "flags",
|
1195
|
+
@spaceship.class.determine_flag_colmn_for(:warpdrive)
|
1031
1196
|
end
|
1032
1197
|
|
1033
1198
|
def test_column_guessing_for_default_column_1
|
@@ -1037,8 +1202,10 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
1037
1202
|
end
|
1038
1203
|
|
1039
1204
|
def test_column_guessing_for_2_columns
|
1040
|
-
assert_equal
|
1041
|
-
|
1205
|
+
assert_equal "commanders",
|
1206
|
+
@big_spaceship.class.determine_flag_colmn_for(:jeanlucpicard)
|
1207
|
+
assert_equal "bits",
|
1208
|
+
@big_spaceship.class.determine_flag_colmn_for(:warpdrive)
|
1042
1209
|
end
|
1043
1210
|
|
1044
1211
|
def test_update_flag_without_updating_instance!
|
@@ -1053,7 +1220,8 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
1053
1220
|
assert_equal true, my_spaceship.update_flag!(:jeanlucpicard, false)
|
1054
1221
|
assert_equal true, my_spaceship.update_flag!(:warpdrive, true)
|
1055
1222
|
|
1056
|
-
# Not updating the instance here,
|
1223
|
+
# Not updating the instance here,
|
1224
|
+
# so it won't reflect the result of the SQL update until after reloaded
|
1057
1225
|
assert_equal true, my_spaceship.jeanlucpicard
|
1058
1226
|
assert_equal false, my_spaceship.warpdrive
|
1059
1227
|
|
@@ -1075,7 +1243,8 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
1075
1243
|
assert_equal true, my_spaceship.update_flag!(:jeanlucpicard, false, true)
|
1076
1244
|
assert_equal true, my_spaceship.update_flag!(:warpdrive, true, true)
|
1077
1245
|
|
1078
|
-
# Updating the instance here,
|
1246
|
+
# Updating the instance here,
|
1247
|
+
# so it will reflect the result of the SQL update before and after reload
|
1079
1248
|
assert_equal false, my_spaceship.jeanlucpicard
|
1080
1249
|
assert_equal true, my_spaceship.warpdrive
|
1081
1250
|
|
@@ -1230,12 +1399,12 @@ class FlagShihTzuDerivedClassTest < Test::Unit::TestCase
|
|
1230
1399
|
end
|
1231
1400
|
|
1232
1401
|
def test_should_respect_true_values_like_active_record
|
1233
|
-
[true, 1,
|
1402
|
+
[true, 1, "1", "t", "T", "true", "TRUE"].each do |true_value|
|
1234
1403
|
@spaceship.warpdrive = true_value
|
1235
1404
|
assert @spaceship.warpdrive
|
1236
1405
|
end
|
1237
1406
|
|
1238
|
-
[false, 0,
|
1407
|
+
[false, 0, "0", "f", "F", "false", "FALSE"].each do |false_value|
|
1239
1408
|
@spaceship.warpdrive = false_value
|
1240
1409
|
assert !@spaceship.warpdrive
|
1241
1410
|
end
|
@@ -1250,8 +1419,10 @@ class FlagShihTzuDerivedClassTest < Test::Unit::TestCase
|
|
1250
1419
|
end
|
1251
1420
|
|
1252
1421
|
def test_should_return_a_sql_set_method_for_flag
|
1253
|
-
assert_equal "flags = flags | 1",
|
1254
|
-
|
1422
|
+
assert_equal "flags = flags | 1",
|
1423
|
+
Spaceship.send(:sql_set_for_flag, :warpdrive, "flags", true)
|
1424
|
+
assert_equal "flags = flags & ~1",
|
1425
|
+
Spaceship.send(:sql_set_for_flag, :warpdrive, "flags", false)
|
1255
1426
|
end
|
1256
1427
|
|
1257
1428
|
end
|
@@ -1259,12 +1430,16 @@ end
|
|
1259
1430
|
class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
|
1260
1431
|
|
1261
1432
|
def test_should_track_columns_used_by_FlagShihTzu
|
1262
|
-
assert_equal Spaceship.flag_columns, [
|
1263
|
-
assert_equal SpaceshipWith2CustomFlagsColumn.flag_columns,
|
1264
|
-
|
1433
|
+
assert_equal Spaceship.flag_columns, ["flags"]
|
1434
|
+
assert_equal SpaceshipWith2CustomFlagsColumn.flag_columns,
|
1435
|
+
["bits", "commanders"]
|
1436
|
+
assert_equal SpaceshipWith3CustomFlagsColumn.flag_columns,
|
1437
|
+
["engines", "weapons", "hal3000"]
|
1265
1438
|
if (ActiveRecord::VERSION::MAJOR >= 3)
|
1266
|
-
assert_equal SpaceshipWithValidationsAnd3CustomFlagsColumn.flag_columns,
|
1267
|
-
|
1439
|
+
assert_equal SpaceshipWithValidationsAnd3CustomFlagsColumn.flag_columns,
|
1440
|
+
["engines", "weapons", "hal3000"]
|
1441
|
+
assert_equal SpaceshipWithSymbolAndStringFlagColumns.flag_columns,
|
1442
|
+
["peace", "love", "happiness"]
|
1268
1443
|
end
|
1269
1444
|
end
|
1270
1445
|
|