flag_shih_tzu 0.3.13 → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f838aaf59bd28697db5750c2102f66f23dfaed0b
|
4
|
+
data.tar.gz: e61cc31873a236b52648fce2815f58b090ffd69d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02d87e4ec423ab7053d0b460bc51a889ba5a96ad343f6939f46b2056c024ed858520349b788a7d2a608a380d01e8eaae2374d945be4e3912eb3d3abe707c5d91
|
7
|
+
data.tar.gz: 0a0b3097c2b6494b838a3b0536c7407117a6d6f4a78cca2cac19b677b03acf6ea6506d130604b2de06fb9b319e52f47a0fe98eec0ab8fff8f16c308e85c1d53f
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
rvm:
|
2
2
|
- 1.9.3
|
3
3
|
- 2.0.0
|
4
|
-
- 2.1.
|
5
|
-
- 2.2.
|
4
|
+
- 2.1.5
|
5
|
+
- 2.2.3
|
6
|
+
- jruby
|
6
7
|
gemfile:
|
7
8
|
- gemfiles/Gemfile.activerecord-2.3.x
|
8
9
|
- gemfiles/Gemfile.activerecord-3.0.x
|
@@ -10,35 +11,42 @@ gemfile:
|
|
10
11
|
- gemfiles/Gemfile.activerecord-3.2.x
|
11
12
|
- gemfiles/Gemfile.activerecord-4.0.x
|
12
13
|
- gemfiles/Gemfile.activerecord-4.1.x
|
14
|
+
- gemfiles/Gemfile.activerecord-4.2.x
|
13
15
|
matrix:
|
14
16
|
exclude:
|
15
|
-
- rvm:
|
17
|
+
- rvm: jruby
|
16
18
|
gemfile: gemfiles/Gemfile.activerecord-2.3.x
|
17
|
-
- rvm:
|
19
|
+
- rvm: jruby
|
18
20
|
gemfile: gemfiles/Gemfile.activerecord-3.0.x
|
19
|
-
- rvm:
|
21
|
+
- rvm: jruby
|
20
22
|
gemfile: gemfiles/Gemfile.activerecord-3.1.x
|
21
|
-
- rvm:
|
23
|
+
- rvm: jruby
|
22
24
|
gemfile: gemfiles/Gemfile.activerecord-3.2.x
|
23
|
-
- rvm:
|
25
|
+
- rvm: jruby
|
24
26
|
gemfile: gemfiles/Gemfile.activerecord-4.0.x
|
25
|
-
- rvm: 2.
|
27
|
+
- rvm: 2.2.3
|
26
28
|
gemfile: gemfiles/Gemfile.activerecord-2.3.x
|
27
|
-
- rvm: 2.
|
29
|
+
- rvm: 2.2.3
|
28
30
|
gemfile: gemfiles/Gemfile.activerecord-3.0.x
|
29
|
-
- rvm: 2.
|
31
|
+
- rvm: 2.2.3
|
30
32
|
gemfile: gemfiles/Gemfile.activerecord-3.1.x
|
31
|
-
- rvm: 2.
|
33
|
+
- rvm: 2.2.3
|
34
|
+
gemfile: gemfiles/Gemfile.activerecord-3.2.x
|
35
|
+
- rvm: 2.2.3
|
36
|
+
gemfile: gemfiles/Gemfile.activerecord-4.0.x
|
37
|
+
- rvm: 2.1.5
|
32
38
|
gemfile: gemfiles/Gemfile.activerecord-2.3.x
|
33
|
-
- rvm: 2.1.
|
39
|
+
- rvm: 2.1.5
|
34
40
|
gemfile: gemfiles/Gemfile.activerecord-3.0.x
|
35
|
-
- rvm: 2.1.
|
41
|
+
- rvm: 2.1.5
|
36
42
|
gemfile: gemfiles/Gemfile.activerecord-3.1.x
|
37
43
|
- rvm: 2.0.0
|
38
44
|
gemfile: gemfiles/Gemfile.activerecord-2.3.x
|
45
|
+
- rvm: 2.0.0
|
46
|
+
gemfile: gemfiles/Gemfile.activerecord-4.2.x
|
47
|
+
- rvm: 1.9.3
|
48
|
+
gemfile: gemfiles/Gemfile.activerecord-4.2.x
|
39
49
|
- rvm: 1.9.3
|
40
50
|
gemfile: gemfiles/Gemfile.activerecord-4.1.x
|
41
|
-
- rvm: 1.9.
|
51
|
+
- rvm: 1.9.3
|
42
52
|
gemfile: gemfiles/Gemfile.activerecord-4.0.x
|
43
|
-
- rvm: 1.9.2
|
44
|
-
gemfile: gemfiles/Gemfile.activerecord-4.1.x
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,16 @@
|
|
1
|
-
|
1
|
+
# HEAD - UNRELEASED
|
2
|
+
|
3
|
+
* Work merged into master branch goes here until it is released.
|
4
|
+
|
5
|
+
# Version 0.3.14 - OCT.08.2015
|
6
|
+
|
7
|
+
* Allow use without ActiveRecord (experimental) by jfcaiceo
|
8
|
+
* Many net-zero code cleanups to follow Ruby Style Guide
|
9
|
+
* Improved local testing script rake test:all
|
10
|
+
* Testing on Travis: added Ruby 1.9.3, 2.1.5, 2.2.3, jruby
|
11
|
+
* Testing on Travis: removed Ruby 2.1.2
|
12
|
+
|
13
|
+
# Version 0.3.13 - MAR.13.2015
|
2
14
|
|
3
15
|
* methods for use with form builders like simple_form by Peter Boling
|
4
16
|
|
@@ -11,7 +23,7 @@ Version 0.3.13 - MAR.13.2015
|
|
11
23
|
* Testing on Travis: added Ruby 2.2.1
|
12
24
|
* Testing on Travis: removed Ruby 1.9.2
|
13
25
|
|
14
|
-
Version 0.3.12 - OCT.01.2014
|
26
|
+
# Version 0.3.12 - OCT.01.2014
|
15
27
|
|
16
28
|
* Improve testing instructions in readme by Peter Boling
|
17
29
|
* fix check_flag_column to return false after warn by Peter Boling
|
@@ -22,41 +34,41 @@ Version 0.3.12 - OCT.01.2014
|
|
22
34
|
* Adds specs for ActiveRecord version 4.1 by Peter Boling
|
23
35
|
* Use Kernel#warn instead of puts by Peter Boling
|
24
36
|
|
25
|
-
Version 0.3.11 - JUL.09.2014
|
37
|
+
# Version 0.3.11 - JUL.09.2014
|
26
38
|
|
27
39
|
* Rename some ambigously-named methods mixed into AR::Base by jdelStrother
|
28
40
|
* Add dynamic ".*_values_for" helpers by atipugin
|
29
41
|
|
30
|
-
Version 0.3.10 - NOV.26.2013
|
42
|
+
# Version 0.3.10 - NOV.26.2013
|
31
43
|
|
32
44
|
* Can run tests without coverage by specifying NOCOVER=true by Peter Boling
|
33
45
|
* Improved test coverage by Peter Boling
|
34
46
|
* Improved documentation by Peter Boling
|
35
47
|
* Readme converted to Markdown by Peter Boling
|
36
48
|
|
37
|
-
Version 0.3.9 - NOV.25.2013
|
49
|
+
# Version 0.3.9 - NOV.25.2013
|
38
50
|
|
39
51
|
* Removed runtime dependency on active record and active support by Peter Boling
|
40
52
|
* Fixed Coveralls Configuration by Peter Boling
|
41
53
|
* Improved Readme by Peter Boling
|
42
54
|
|
43
|
-
Version 0.3.8 - NOV.24.2013
|
55
|
+
# Version 0.3.8 - NOV.24.2013
|
44
56
|
|
45
57
|
* Improved Readme / Documentation by Peter Boling
|
46
58
|
* Added Badges by Peter Boling
|
47
59
|
* Configured Coveralls by Peter Boling
|
48
60
|
* Added Code Climate, Coveralls, Gemnasium, and Version Badges by Peter Boling
|
49
61
|
|
50
|
-
Version 0.3.7 - OCT.25.2013
|
62
|
+
# Version 0.3.7 - OCT.25.2013
|
51
63
|
|
52
64
|
* Change `sql_in_for_flag` to consider values from the range [0, 2 * max - 1] by Blake Thomson
|
53
65
|
|
54
|
-
Version 0.3.6 - AUG.29.2013
|
66
|
+
# Version 0.3.6 - AUG.29.2013
|
55
67
|
|
56
68
|
* Allow use with any gem manager by Peter Boling
|
57
69
|
* No need to alter Ruby's load path by Peter Boling
|
58
70
|
|
59
|
-
Version 0.3.5 - AUG.06.2013
|
71
|
+
# Version 0.3.5 - AUG.06.2013
|
60
72
|
|
61
73
|
* Fix Travis Build & Add Rails 4 by Peter M. Goldstein
|
62
74
|
* Implemented update_flag! by Peter Boling (see https://github.com/pboling/flag_shih_tzu/issues/27)
|
@@ -64,7 +76,7 @@ Version 0.3.5 - AUG.06.2013
|
|
64
76
|
- optionally syncs the instance with new flag value, by default it does not.
|
65
77
|
* Update gemspec by Peter Boling
|
66
78
|
|
67
|
-
Version 0.3.4 - JUN.20.2013
|
79
|
+
# Version 0.3.4 - JUN.20.2013
|
68
80
|
|
69
81
|
* Allow non sequential flag numbers by Thomas Jachmann
|
70
82
|
* Report correct source location for class_evaled methods. by Sebastian Korfmann
|
@@ -80,11 +92,11 @@ Version 0.3.4 - JUN.20.2013
|
|
80
92
|
* convenience methods now have default parameter so `all_flags` works with arity 0. by Peter Boling
|
81
93
|
* Many more tests, including arity tests by Peter Boling
|
82
94
|
|
83
|
-
Version 0.3.3 - JUN.20.2013
|
95
|
+
# Version 0.3.3 - JUN.20.2013
|
84
96
|
|
85
97
|
- Does not exist.
|
86
98
|
|
87
|
-
Version 0.3.2 - NOV.06.2012
|
99
|
+
# Version 0.3.2 - NOV.06.2012
|
88
100
|
|
89
101
|
* Adds skip column check option :check_for_column - from arturaz
|
90
102
|
* Adds a 'smart' set_flag_sql method which will auto determine the correct column for the given flag - from arturaz
|
@@ -93,12 +105,12 @@ Version 0.3.2 - NOV.06.2012
|
|
93
105
|
- Now there is a test ensuring that the generated SQL can be executed by a real DB
|
94
106
|
- This improved sql_set_for_flag underlies the public set_flag_sql method
|
95
107
|
|
96
|
-
Version 0.3.1 - NOV.06.2012
|
108
|
+
# Version 0.3.1 - NOV.06.2012
|
97
109
|
|
98
110
|
* Adds new methods (for a flag column named 'bar', with many individual flags within) - from ddidier
|
99
111
|
- all_bar, selected_bar, select_all_bar, unselect_all_bar, selected_bar=(selected_flags), has_bar?
|
100
112
|
|
101
|
-
Version 0.3.0 - NOV.05.2012 - first version maintained by Peter Boling
|
113
|
+
# Version 0.3.0 - NOV.05.2012 - first version maintained by Peter Boling
|
102
114
|
|
103
115
|
* ClassWithHasFlags.set_#{flag_name}_sql # Returns the sql string for setting a flag for use in customized SQL
|
104
116
|
* ClassWithHasFlags.unset_#{flag_name}_sql # Returns the sql string for unsetting a flag for use in customized SQL
|
@@ -106,12 +118,12 @@ Version 0.3.0 - NOV.05.2012 - first version maintained by Peter Boling
|
|
106
118
|
* has_flags :strict => true # DuplicateFlagColumnException raised when a single DB column is declared as a flag column twice
|
107
119
|
* Less verbosity for expected conditions when the DB connection for the class is unavailable.
|
108
120
|
* Tests for additional features, but does not change any behavior of 0.2.3 / 0.2.4 by default.
|
109
|
-
* Easily migrate from 0.2.3
|
121
|
+
* Easily migrate from 0.2.3 and 0.2.4. Goal is no code changes required. Minor version bump to encourage caution.
|
110
122
|
|
111
|
-
Version 0.2.4 - NOV.05.2012 - released last few changes from XING master
|
123
|
+
# Version 0.2.4 - NOV.05.2012 - released last few changes from XING master
|
112
124
|
|
113
125
|
* Fix deprecation warning for set_table_name
|
114
126
|
* Optional bang methods
|
115
127
|
* Complete Ruby 1.9(\.[^1]) and Rails 3.2.X compatibility
|
116
128
|
|
117
|
-
Version 0.2.3 - last version maintained by XING AG
|
129
|
+
# Version 0.2.3 - last version maintained by XING AG
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -48,9 +48,14 @@ What is a ["Shih Tzu"](http://en.wikipedia.org/wiki/Shih_Tzu)?
|
|
48
48
|
The gem is actively being tested with:
|
49
49
|
|
50
50
|
* MySQL, PostgreSQL and SQLite3 databases
|
51
|
-
* ActiveRecord versions 2.3.x, 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x ;)
|
52
|
-
* Ruby 1.9.
|
53
|
-
*
|
51
|
+
* ActiveRecord versions 2.3.x, 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 4.2.x ;)
|
52
|
+
* Ruby 1.9.3, 2.0.0, 2.1.5, 2.2.3
|
53
|
+
* Travis tests the most important builds. See [.travis.yml](https://github.com/pboling/flag_shih_tzu/blob/master/.travis.yml) for the matrix.
|
54
|
+
* All of the supported builds are run locally by me. See [bin/test.bash](https://github.com/pboling/flag_shih_tzu/blob/master/bin/test.bash)
|
55
|
+
|
56
|
+
**Legacy**
|
57
|
+
|
58
|
+
* Ruby 1.8.7 compatibility is in the [0.2.X branch](https://github.com/pboling/flag_shih_tzu/tree/0.2.X) and no further releases are expected. If you need a patch submit a pull request.
|
54
59
|
|
55
60
|
## Installation
|
56
61
|
|
@@ -58,7 +63,9 @@ The gem is actively being tested with:
|
|
58
63
|
|
59
64
|
In environment.rb:
|
60
65
|
|
61
|
-
|
66
|
+
```ruby
|
67
|
+
config.gem 'flag_shih_tzu'
|
68
|
+
```
|
62
69
|
|
63
70
|
Then:
|
64
71
|
|
@@ -68,7 +75,9 @@ Then:
|
|
68
75
|
|
69
76
|
In Gemfile:
|
70
77
|
|
71
|
-
|
78
|
+
```ruby
|
79
|
+
gem 'flag_shih_tzu'
|
80
|
+
```
|
72
81
|
|
73
82
|
Then:
|
74
83
|
|
@@ -92,23 +101,27 @@ should have a default value of `0`.
|
|
92
101
|
|
93
102
|
I like to document the intent of the `flags` column in the migration when I can...
|
94
103
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
104
|
+
```ruby
|
105
|
+
change_table :spaceships do |t|
|
106
|
+
t.integer :flags, :null => false, :default => 0 # flag_shih_tzu-managed bit field
|
107
|
+
# Effective booleans which will be stored on the flags column:
|
108
|
+
# t.boolean :warpdrive
|
109
|
+
# t.boolean :shields
|
110
|
+
# t.boolean :electrolytes
|
111
|
+
end
|
112
|
+
```
|
102
113
|
|
103
114
|
### Adding to the Model
|
104
115
|
|
105
|
-
|
106
|
-
|
116
|
+
```ruby
|
117
|
+
class Spaceship < ActiveRecord::Base
|
118
|
+
include FlagShihTzu
|
107
119
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
120
|
+
has_flags 1 => :warpdrive,
|
121
|
+
2 => :shields,
|
122
|
+
3 => :electrolytes
|
123
|
+
end
|
124
|
+
```
|
112
125
|
|
113
126
|
`has_flags` takes a hash. The keys must be positive integers and represent
|
114
127
|
the position of the bit being used to enable or disable the flag.
|
@@ -161,16 +174,17 @@ The default column name to store the flags is `flags`, but you can provide a
|
|
161
174
|
custom column name using the `:column` option. This allows you to use
|
162
175
|
different columns for separate flags:
|
163
176
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
has_flags 1 => :spock,
|
170
|
-
2 => :scott,
|
171
|
-
3 => :kirk,
|
172
|
-
:column => 'crew'
|
177
|
+
```ruby
|
178
|
+
has_flags 1 => :warpdrive,
|
179
|
+
2 => :shields,
|
180
|
+
3 => :electrolytes,
|
181
|
+
:column => 'features'
|
173
182
|
|
183
|
+
has_flags 1 => :spock,
|
184
|
+
2 => :scott,
|
185
|
+
3 => :kirk,
|
186
|
+
:column => 'crew'
|
187
|
+
```
|
174
188
|
|
175
189
|
### Generated boolean patterned instance methods
|
176
190
|
|
@@ -239,7 +253,9 @@ Alternatively, if you do want to *save a flag* to the database, while still avoi
|
|
239
253
|
|
240
254
|
Example:
|
241
255
|
|
242
|
-
|
256
|
+
```ruby
|
257
|
+
update_flag!(flag_name, flag_value, update_instance = false)
|
258
|
+
```
|
243
259
|
|
244
260
|
|
245
261
|
### Generated class methods
|
@@ -247,24 +263,30 @@ Example:
|
|
247
263
|
Calling `has_flags` as shown above creates the following class methods
|
248
264
|
on Spaceship:
|
249
265
|
|
250
|
-
|
266
|
+
```ruby
|
267
|
+
Spaceship.flag_columns # [:features, :crew]
|
268
|
+
```
|
251
269
|
|
252
270
|
|
253
271
|
### Generated named scopes
|
254
272
|
|
255
273
|
The following named scopes become available:
|
256
274
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
275
|
+
```ruby
|
276
|
+
Spaceship.warpdrive # :conditions => "(spaceships.flags in (1,3,5,7))"
|
277
|
+
Spaceship.not_warpdrive # :conditions => "(spaceships.flags not in (1,3,5,7))"
|
278
|
+
Spaceship.shields # :conditions => "(spaceships.flags in (2,3,6,7))"
|
279
|
+
Spaceship.not_shields # :conditions => "(spaceships.flags not in (2,3,6,7))"
|
280
|
+
Spaceship.electrolytes # :conditions => "(spaceships.flags in (4,5,6,7))"
|
281
|
+
Spaceship.not_electrolytes # :conditions => "(spaceships.flags not in (4,5,6,7))"
|
282
|
+
```
|
263
283
|
|
264
284
|
If you do not want the named scopes to be defined, set the
|
265
285
|
`:named_scopes` option to false when calling `has_flags`:
|
266
286
|
|
267
|
-
|
287
|
+
```ruby
|
288
|
+
has_flags 1 => :warpdrive, 2 => :shields, 3 => :electrolytes, :named_scopes => false
|
289
|
+
```
|
268
290
|
|
269
291
|
In a Rails 3+ application, FlagShihTzu will use `scope` internally to generate
|
270
292
|
the scopes. The option on `has_flags` is still named `:named_scopes` however.
|
@@ -272,18 +294,20 @@ the scopes. The option on `has_flags` is still named `:named_scopes` however.
|
|
272
294
|
|
273
295
|
### Examples for using the generated methods
|
274
296
|
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
297
|
+
```ruby
|
298
|
+
enterprise = Spaceship.new
|
299
|
+
enterprise.warpdrive = true
|
300
|
+
enterprise.shields = true
|
301
|
+
enterprise.electrolytes = false
|
302
|
+
enterprise.save
|
280
303
|
|
281
|
-
|
282
|
-
|
283
|
-
|
304
|
+
if enterprise.shields?
|
305
|
+
# ...
|
306
|
+
end
|
284
307
|
|
285
|
-
|
286
|
-
|
308
|
+
Spaceship.warpdrive.find(:all)
|
309
|
+
Spaceship.not_electrolytes.count
|
310
|
+
```
|
287
311
|
|
288
312
|
|
289
313
|
### Support for manually building conditions
|
@@ -291,17 +315,20 @@ the scopes. The option on `has_flags` is still named `:named_scopes` however.
|
|
291
315
|
The following class methods may support you when manually building
|
292
316
|
ActiveRecord conditions:
|
293
317
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
318
|
+
```ruby
|
319
|
+
Spaceship.warpdrive_condition # "(spaceships.flags in (1,3,5,7))"
|
320
|
+
Spaceship.not_warpdrive_condition # "(spaceships.flags not in (1,3,5,7))"
|
321
|
+
Spaceship.shields_condition # "(spaceships.flags in (2,3,6,7))"
|
322
|
+
Spaceship.not_shields_condition # "(spaceships.flags not in (2,3,6,7))"
|
323
|
+
Spaceship.electrolytes_condition # "(spaceships.flags in (4,5,6,7))"
|
324
|
+
Spaceship.not_electrolytes_condition # "(spaceships.flags not in (4,5,6,7))"
|
325
|
+
```
|
300
326
|
|
301
327
|
These methods also accept a `:table_alias` option that can be used when
|
302
328
|
generating SQL that references the same table more than once:
|
303
|
-
|
304
|
-
|
329
|
+
```ruby
|
330
|
+
Spaceship.shields_condition(:table_alias => 'evil_spaceships') # "(evil_spaceships.flags in (2,3,6,7))"
|
331
|
+
```
|
305
332
|
|
306
333
|
|
307
334
|
### Choosing a query mode
|
@@ -316,22 +343,26 @@ For MySQL, depending on your MySQL settings, this can even hit the
|
|
316
343
|
In this case, consider changing the flag query mode to `:bit_operator`
|
317
344
|
instead of `:in_list`, like so:
|
318
345
|
|
319
|
-
|
320
|
-
|
321
|
-
|
346
|
+
```ruby
|
347
|
+
has_flags 1 => :warpdrive,
|
348
|
+
2 => :shields,
|
349
|
+
:flag_query_mode => :bit_operator
|
350
|
+
```
|
322
351
|
|
323
352
|
This will modify the generated condition and named_scope methods to use bit
|
324
353
|
operators in the SQL instead of an `IN()` list:
|
325
354
|
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
355
|
+
```ruby
|
356
|
+
Spaceship.warpdrive_condition # "(spaceships.flags & 1 = 1)",
|
357
|
+
Spaceship.not_warpdrive_condition # "(spaceships.flags & 1 = 0)",
|
358
|
+
Spaceship.shields_condition # "(spaceships.flags & 2 = 2)",
|
359
|
+
Spaceship.not_shields_condition # "(spaceships.flags & 2 = 0)",
|
330
360
|
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
361
|
+
Spaceship.warpdrive # :conditions => "(spaceships.flags & 1 = 1)"
|
362
|
+
Spaceship.not_warpdrive # :conditions => "(spaceships.flags & 1 = 0)"
|
363
|
+
Spaceship.shields # :conditions => "(spaceships.flags & 2 = 2)"
|
364
|
+
Spaceship.not_shields # :conditions => "(spaceships.flags & 2 = 0)"
|
365
|
+
```
|
335
366
|
|
336
367
|
The drawback is that due to the [bitwise operation][bitwise_operation] being done on the SQL side,
|
337
368
|
this query can not use an index on the flags column.
|
@@ -341,19 +372,25 @@ this query can not use an index on the flags column.
|
|
341
372
|
If you need to do mass updates without initializing object for each row, you can
|
342
373
|
use `#set_flag_sql` method on your class. Example:
|
343
374
|
|
344
|
-
|
345
|
-
|
375
|
+
```ruby
|
376
|
+
Spaceship.set_flag_sql(:warpdrive, true) # "flags = flags | 1"
|
377
|
+
Spaceship.set_flag_sql(:shields, false) # "flags = flags & ~2"
|
378
|
+
```
|
346
379
|
|
347
380
|
And then use it in:
|
348
381
|
|
349
|
-
|
382
|
+
```ruby
|
383
|
+
Spaceship.update_all Spaceship.set_flag_sql(:shields, false)
|
384
|
+
```
|
350
385
|
|
351
386
|
Beware that using multiple flag manipulation sql statements in the same query
|
352
387
|
probably will not have the desired effect (at least on sqlite3, not tested
|
353
388
|
on other databases), so you *should not* do this:
|
354
389
|
|
355
|
-
|
356
|
-
|
390
|
+
```ruby
|
391
|
+
Spaceship.update_all "#{Spaceship.set_flag_sql(:shields, false)},#{
|
392
|
+
Spaceship.set_flag_sql(:warpdrive, true)}"
|
393
|
+
```
|
357
394
|
|
358
395
|
General rule of thumb: issue only one flag update per update statement.
|
359
396
|
|
@@ -366,33 +403,23 @@ Sometimes this may not be a wanted behaviour (e.g. when loading model without
|
|
366
403
|
database connection established) so you can set `:check_for_column` option to
|
367
404
|
false to avoid it.
|
368
405
|
|
369
|
-
|
370
|
-
|
371
|
-
|
406
|
+
```ruby
|
407
|
+
has_flags 1 => :warpdrive,
|
408
|
+
2 => :shields,
|
409
|
+
:check_for_column => false
|
410
|
+
```
|
372
411
|
|
373
412
|
|
374
413
|
## Running the gem tests
|
375
414
|
|
376
|
-
|
415
|
+
WARNING: You may want to read [bin/test.bash](https://github.com/pboling/flag_shih_tzu/blob/master/bin/test.bash) first.
|
416
|
+
Running the test script will switch rubies, create gemsets, install gems, and get a lil' crazy with the hips.
|
377
417
|
|
378
|
-
|
379
|
-
$ bundle install
|
380
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-2.3.x' bundle update --quiet
|
381
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-3.0.x' bundle update --quiet
|
382
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-3.1.x' bundle update --quiet
|
383
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-3.2.x' bundle update --quiet
|
384
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.0.x' bundle update --quiet
|
385
|
-
$ rvm use 2.1.2
|
386
|
-
$ bundle install
|
387
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-3.2.x' bundle update --quiet
|
388
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.0.x' bundle update --quiet
|
389
|
-
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.1.x' bundle update --quiet
|
390
|
-
|
391
|
-
Then just:
|
418
|
+
Just:
|
392
419
|
|
393
|
-
$
|
420
|
+
$ rake test:all
|
394
421
|
|
395
|
-
This will internally use rvm and bundler to load specific ActiveRecord versions
|
422
|
+
This will internally use rvm and bundler to load specific Rubies and ActiveRecord versions
|
396
423
|
before executing the tests (see `gemfiles/`), e.g.:
|
397
424
|
|
398
425
|
$ NOCOVER=true BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.1.x' bundle exec rake test
|
@@ -404,6 +431,16 @@ specify which config from `test/database.yml` to use, e.g.:
|
|
404
431
|
|
405
432
|
$ NOCOVER=true DB=mysql bundle exec rake
|
406
433
|
|
434
|
+
You will also need to create, and configure access to, the test databases for any adapters you want to test, e.g. mysql:
|
435
|
+
|
436
|
+
mysql> CREATE USER 'foss'@'localhost';
|
437
|
+
Query OK, 0 rows affected (0.00 sec)
|
438
|
+
|
439
|
+
mysql> GRANT ALL PRIVILEGES ON *.* TO 'foss'@'localhost';
|
440
|
+
Query OK, 0 rows affected (0.00 sec)
|
441
|
+
|
442
|
+
mysql> CREATE DATABASE flag_shih_tzu_test;
|
443
|
+
Query OK, 1 row affected (0.00 sec)
|
407
444
|
|
408
445
|
## Authors
|
409
446
|
|
@@ -458,12 +495,13 @@ dependency on this gem using the [Pessimistic Version Constraint](http://docs.ru
|
|
458
495
|
|
459
496
|
For example:
|
460
497
|
|
461
|
-
|
462
|
-
|
498
|
+
```ruby
|
499
|
+
spec.add_dependency 'flag_shih_tzu', '~> 4.0'
|
500
|
+
```
|
463
501
|
|
464
502
|
## 2012 Change of Ownership and 0.3.X Release Notes
|
465
503
|
|
466
|
-
FlagShihTzu was originally a [XING AG]
|
504
|
+
FlagShihTzu was originally a [XING AG](http://www.xing.com/) project. [Peter Boling](http://peterboling.com) was a long time contributor and watcher of the project.
|
467
505
|
In September 2012 XING transferred ownership of the project to Peter Boling. Peter Boling had been maintaining a
|
468
506
|
fork with extended capabilities. These additional features become a part of the 0.3 line. The 0.2 line of the gem will
|
469
507
|
remain true to XING's original. The 0.3 line aims to maintain complete parity and compatibility with XING's original as
|