bitmask_attributes 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +47 -26
- data/Gemfile.lock +29 -18
- data/README.rdoc +1 -1
- data/lib/bitmask_attributes.rb +23 -6
- data/lib/bitmask_attributes/definition.rb +53 -23
- data/lib/bitmask_attributes/version.rb +1 -1
- data/test/bitmask_attributes/definition_test.rb +34 -0
- data/test/bitmask_attributes_test.rb +40 -2
- data/test/support/models.rb +18 -2
- metadata +4 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,48 @@
|
|
1
|
+
=== Version 0.4.0 - 2012-12-14 19:37:49 +0000
|
2
|
+
|
3
|
+
Adrian Gomez (1):
|
4
|
+
* Fixing issue #22
|
5
|
+
|
6
|
+
Brad Dunbar (1):
|
7
|
+
* Add default option.
|
8
|
+
|
9
|
+
Dominik Bamberger (5):
|
10
|
+
* Introduce raw bitmask setter.
|
11
|
+
* Ensure unsupported values for raw bitmasks raise an exception
|
12
|
+
* First reset instance variable then write attribute.
|
13
|
+
* Introduce inverse convenience class method
|
14
|
+
* Allow only valid entries when setting raw bitmask values directly
|
15
|
+
|
16
|
+
François DELFORT (2):
|
17
|
+
* Handle single table inheritance
|
18
|
+
* Fix for handling STI table
|
19
|
+
|
20
|
+
Joel Moss (7):
|
21
|
+
* Merge pull request #17 from teeparham/patch-1
|
22
|
+
* Merge pull request #18 from tight/master
|
23
|
+
* Merge pull request #29 from adimitrov/include-table-name
|
24
|
+
* Merge pull request #33 from teeparham/or_is_null
|
25
|
+
* Merge pull request #30 from pathable/default
|
26
|
+
* Bundle update
|
27
|
+
* Merge branch 'master' of github.com:joelmoss/bitmask_attributes
|
28
|
+
|
29
|
+
Steve Hoeksema (2):
|
30
|
+
* Strip trailing whitespace
|
31
|
+
* Add table name to one scope that missed out
|
32
|
+
|
33
|
+
Tee Parham (2):
|
34
|
+
* update sample controller code
|
35
|
+
* add table name scope to or_is_null_condition
|
36
|
+
|
37
|
+
alexanderd (1):
|
38
|
+
* Include table name in scope definitions
|
39
|
+
|
40
|
+
steve emmons (3):
|
41
|
+
* Merge pull request #26 from b4mboo/inverse_convenience
|
42
|
+
* Merge pull request #25 from b4mboo/raw_bitmask_setter
|
43
|
+
* Merge pull request #23 from Moove-it/master
|
44
|
+
|
45
|
+
|
1
46
|
=== Version 0.3.0 - 2012-04-30 21:36:18 +0100
|
2
47
|
|
3
48
|
Aaron Hamid (1):
|
@@ -9,42 +54,17 @@ Andre Duffeck (1):
|
|
9
54
|
Ivan Buiko (1):
|
10
55
|
* Adding retrieval by exact value
|
11
56
|
|
12
|
-
Joel Moss (9):
|
13
|
-
* Merge pull request #6 from incandescent/fix-scope-proc
|
14
|
-
* Merge pull request #4 from SUSE/fix_values_for
|
15
|
-
* Bundle update
|
16
|
-
* Adding retrieval by exact value
|
17
|
-
* Merge branch 'retrieval-by-exact-value'
|
18
|
-
* Resolved merge conflict
|
19
|
-
* Merge pull request #9 from numerex/master
|
20
|
-
* Merge pull request #13 from spemmons/master
|
21
|
-
* Merge pull request #16 from spemmons/master
|
22
|
-
|
23
57
|
Sebastian Borrazas (1):
|
24
58
|
* Not raising exception when the bitmask column is not found, since its adding it on a migration
|
25
59
|
|
26
60
|
spemmons (22):
|
27
61
|
* detect whether or not nulls are possible with an attribute and remove OR conditions checking if it is null or not
|
28
|
-
* ignore RubyMine info
|
29
|
-
* merge changes with joel moss for scopes
|
30
62
|
* check for model not being ready when creating scopes
|
31
63
|
* support nulls with an explicit attribute, not by inspecting the model due to preloading issues
|
32
|
-
* add documentation for the :null option
|
33
64
|
* support multiple values in the "without_..." scope that excludes match for of any of the bits; previously 2nd and following values were ignored
|
34
|
-
* update documentation to reflect support for multiple arguments to "without_..."
|
35
|
-
* update documentation to reflect support for multiple arguments to "without_..."
|
36
|
-
* update documentation to reflect support for multiple arguments to "without_..."
|
37
|
-
* added tests to verify the validity of zykadelic's complaint that "with_" scopes were improperly including "OR IS NOT NULL" and fixed the code by removing the condition in this case
|
38
65
|
* DRY-up scope expressions, standardizing on "no_" and "bitmask_for_...(*values)" instead of repeating more complex calculations; also found another example of zykadelic's complaint with "with_any_" that is fixed and has tests
|
39
66
|
* add travis configuration file
|
40
|
-
*
|
41
|
-
* Merge branch 'master' of github.com:spemmons/bitmask_attributes
|
42
|
-
* remove out-dated helpers that, although no longer used, were being loaded by Travis-CI and failing
|
43
|
-
* clarify travis config to fix failing CI build that works locally
|
44
|
-
* apparently 1.9.2 doesn't work... will figure out why later
|
45
|
-
* add :zero option and tests
|
46
|
-
* check for a string match instead of a symbol to allow values arriving from web forms and services to match properly
|
47
|
-
* added documentation for the :zero attribute
|
67
|
+
* add :zero_value option and tests
|
48
68
|
* rename :zero to :zero_value for clarity
|
49
69
|
|
50
70
|
steve emmons (3):
|
@@ -65,3 +85,4 @@ Joel Moss (3):
|
|
65
85
|
|
66
86
|
|
67
87
|
|
88
|
+
|
data/Gemfile.lock
CHANGED
@@ -1,40 +1,51 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bitmask_attributes (0.
|
4
|
+
bitmask_attributes (0.3.0)
|
5
5
|
activerecord (~> 3.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (3.2.
|
11
|
-
activesupport (= 3.2.
|
10
|
+
activemodel (3.2.9)
|
11
|
+
activesupport (= 3.2.9)
|
12
12
|
builder (~> 3.0.0)
|
13
|
-
activerecord (3.2.
|
14
|
-
activemodel (= 3.2.
|
15
|
-
activesupport (= 3.2.
|
13
|
+
activerecord (3.2.9)
|
14
|
+
activemodel (= 3.2.9)
|
15
|
+
activesupport (= 3.2.9)
|
16
16
|
arel (~> 3.0.2)
|
17
17
|
tzinfo (~> 0.3.29)
|
18
|
-
activesupport (3.2.
|
18
|
+
activesupport (3.2.9)
|
19
19
|
i18n (~> 0.6)
|
20
20
|
multi_json (~> 1.0)
|
21
|
-
ansi (1.4.
|
21
|
+
ansi (1.4.3)
|
22
22
|
arel (3.0.2)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
bourne (1.1.2)
|
24
|
+
mocha (= 0.10.5)
|
25
|
+
builder (3.0.4)
|
26
|
+
i18n (0.6.1)
|
27
|
+
json (1.7.5)
|
28
|
+
metaclass (0.0.1)
|
29
|
+
mocha (0.10.5)
|
30
|
+
metaclass (~> 0.0.1)
|
31
|
+
multi_json (1.5.0)
|
32
|
+
rake (10.0.3)
|
28
33
|
rdoc (3.12)
|
29
34
|
json (~> 1.4)
|
30
|
-
sdoc (0.3.
|
35
|
+
sdoc (0.3.20)
|
31
36
|
json (>= 1.1.3)
|
32
37
|
rdoc (~> 3.10)
|
33
|
-
shoulda (
|
34
|
-
|
35
|
-
|
38
|
+
shoulda (3.3.2)
|
39
|
+
shoulda-context (~> 1.0.1)
|
40
|
+
shoulda-matchers (~> 1.4.1)
|
41
|
+
shoulda-context (1.0.1)
|
42
|
+
shoulda-matchers (1.4.2)
|
43
|
+
activesupport (>= 3.0.0)
|
44
|
+
bourne (~> 1.1.2)
|
45
|
+
sqlite3 (1.3.6)
|
46
|
+
turn (0.9.6)
|
36
47
|
ansi
|
37
|
-
tzinfo (0.3.
|
48
|
+
tzinfo (0.3.35)
|
38
49
|
|
39
50
|
PLATFORMS
|
40
51
|
ruby
|
data/README.rdoc
CHANGED
@@ -137,7 +137,7 @@ will not change the attribute. For example:
|
|
137
137
|
In controller...
|
138
138
|
def update
|
139
139
|
@some_model = SomeModel.find(params[:id])
|
140
|
-
params[:some_attribute] ||= []
|
140
|
+
params[:some_model][:some_attribute] ||= []
|
141
141
|
@some_model.update_attributes(params)
|
142
142
|
end
|
143
143
|
|
data/lib/bitmask_attributes.rb
CHANGED
@@ -9,18 +9,35 @@ module BitmaskAttributes
|
|
9
9
|
unless options[:as] && options[:as].kind_of?(Array)
|
10
10
|
raise ArgumentError, "Must provide an Array :as option"
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
|
+
if default = options[:default]
|
14
|
+
after_initialize do
|
15
|
+
send("#{attribute}=", default) unless send("#{attribute}?")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
bitmask_definitions[attribute] = Definition.new(attribute, options[:as].to_a,options[:null].nil? || options[:null], options[:zero_value], &extension)
|
13
20
|
bitmask_definitions[attribute].install_on(self)
|
14
21
|
end
|
15
|
-
|
22
|
+
|
16
23
|
def bitmask_definitions
|
17
|
-
|
24
|
+
base_class.base_class_bitmask_definitions
|
18
25
|
end
|
19
|
-
|
26
|
+
|
20
27
|
def bitmasks
|
28
|
+
base_class.base_class_bitmasks
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def base_class_bitmask_definitions
|
34
|
+
@bitmask_definitions ||= {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def base_class_bitmasks
|
21
38
|
@bitmasks ||= {}
|
22
39
|
end
|
23
|
-
end
|
40
|
+
end
|
24
41
|
end
|
25
42
|
|
26
|
-
ActiveRecord::Base.send :include, BitmaskAttributes
|
43
|
+
ActiveRecord::Base.send :include, BitmaskAttributes
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module BitmaskAttributes
|
2
2
|
class Definition
|
3
3
|
attr_reader :attribute, :values, :allow_null, :zero_value, :extension
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(attribute, values=[],allow_null = true,zero_value = nil, &extension)
|
6
6
|
@attribute = attribute
|
7
7
|
@values = values
|
@@ -9,7 +9,7 @@ module BitmaskAttributes
|
|
9
9
|
@allow_null = allow_null
|
10
10
|
@zero_value = zero_value
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def install_on(model)
|
14
14
|
validate_for model
|
15
15
|
generate_bitmasks_on model
|
@@ -29,10 +29,19 @@ module BitmaskAttributes
|
|
29
29
|
return if defined?(Rails) && Rails.configuration.cache_classes || !model.table_exists?
|
30
30
|
|
31
31
|
unless model.columns.detect { |col| col.name == attribute.to_s }
|
32
|
-
|
32
|
+
missing_attribute(attribute, model)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def missing_attribute(attribute, model)
|
37
|
+
message = "WARNING: `#{attribute}' is not an attribute of `#{model.class.name}'. But, it's ok if it happens during migrations and your \"bitmasked\" attribute is still not created."
|
38
|
+
if defined?(Rails)
|
39
|
+
Rails.logger.warn message
|
40
|
+
else
|
41
|
+
STDERR.puts message
|
33
42
|
end
|
34
43
|
end
|
35
|
-
|
44
|
+
|
36
45
|
def generate_bitmasks_on(model)
|
37
46
|
model.bitmasks[attribute] = HashWithIndifferentAccess.new.tap do |mapping|
|
38
47
|
values.each_with_index do |value, index|
|
@@ -40,12 +49,12 @@ module BitmaskAttributes
|
|
40
49
|
end
|
41
50
|
end
|
42
51
|
end
|
43
|
-
|
52
|
+
|
44
53
|
def override(model)
|
45
54
|
override_getter_on(model)
|
46
55
|
override_setter_on(model)
|
47
56
|
end
|
48
|
-
|
57
|
+
|
49
58
|
def override_getter_on(model)
|
50
59
|
model.class_eval %(
|
51
60
|
def #{attribute}
|
@@ -53,16 +62,26 @@ module BitmaskAttributes
|
|
53
62
|
end
|
54
63
|
)
|
55
64
|
end
|
56
|
-
|
65
|
+
|
57
66
|
def override_setter_on(model)
|
58
67
|
model.class_eval %(
|
59
68
|
def #{attribute}=(raw_value)
|
60
69
|
values = raw_value.kind_of?(Array) ? raw_value : [raw_value]
|
61
70
|
self.#{attribute}.replace(values.reject{|value| #{eval_string_for_zero('value')}})
|
62
71
|
end
|
72
|
+
def #{attribute}_bitmask=(entry)
|
73
|
+
unless entry.is_a? Fixnum
|
74
|
+
raise ArgumentError, "Expected a Fixnum, but got: \#{entry.inspect}"
|
75
|
+
end
|
76
|
+
unless entry.between?(0, 2 ** (self.class.bitmasks[:#{attribute}].size - 1))
|
77
|
+
raise ArgumentError, "Unsupported value for #{attribute}: \#{entry.inspect}"
|
78
|
+
end
|
79
|
+
@#{attribute} = nil
|
80
|
+
self.send(:write_attribute, :#{attribute}, entry)
|
81
|
+
end
|
63
82
|
)
|
64
83
|
end
|
65
|
-
|
84
|
+
|
66
85
|
# Returns the defined values as an Array.
|
67
86
|
def create_attribute_methods_on(model)
|
68
87
|
model.class_eval %(
|
@@ -71,7 +90,7 @@ module BitmaskAttributes
|
|
71
90
|
end # end
|
72
91
|
)
|
73
92
|
end
|
74
|
-
|
93
|
+
|
75
94
|
def create_convenience_class_method_on(model)
|
76
95
|
model.class_eval %(
|
77
96
|
def self.bitmask_for_#{attribute}(*values)
|
@@ -84,13 +103,24 @@ module BitmaskAttributes
|
|
84
103
|
bitmask | bit
|
85
104
|
end
|
86
105
|
end
|
106
|
+
|
107
|
+
def self.#{attribute}_for_bitmask(entry)
|
108
|
+
unless entry.is_a? Fixnum
|
109
|
+
raise ArgumentError, "Expected a Fixnum, but got: \#{entry.inspect}"
|
110
|
+
end
|
111
|
+
self.bitmasks[:#{attribute}].inject([]) do |values, (value, bitmask)|
|
112
|
+
values.tap do
|
113
|
+
values << value.to_sym if (entry & bitmask > 0)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
87
117
|
)
|
88
118
|
end
|
89
119
|
|
90
120
|
def create_convenience_instance_methods_on(model)
|
91
121
|
values.each do |value|
|
92
122
|
model.class_eval %(
|
93
|
-
def #{attribute}_for_#{value}?
|
123
|
+
def #{attribute}_for_#{value}?
|
94
124
|
self.#{attribute}?(:#{value})
|
95
125
|
end
|
96
126
|
)
|
@@ -107,29 +137,29 @@ module BitmaskAttributes
|
|
107
137
|
end
|
108
138
|
)
|
109
139
|
end
|
110
|
-
|
140
|
+
|
111
141
|
def create_scopes_on(model)
|
112
|
-
or_is_null_condition = " OR #{attribute} IS NULL" if allow_null
|
142
|
+
or_is_null_condition = " OR #{model.table_name}.#{attribute} IS NULL" if allow_null
|
113
143
|
|
114
144
|
model.class_eval %(
|
115
145
|
scope :with_#{attribute},
|
116
146
|
proc { |*values|
|
117
147
|
if values.blank?
|
118
|
-
where('#{attribute} > 0')
|
148
|
+
where('#{model.table_name}.#{attribute} > 0')
|
119
149
|
else
|
120
150
|
sets = values.map do |value|
|
121
151
|
mask = ::#{model}.bitmask_for_#{attribute}(value)
|
122
|
-
"#{attribute} & \#{mask} <> 0"
|
152
|
+
"#{model.table_name}.#{attribute} & \#{mask} <> 0"
|
123
153
|
end
|
124
154
|
where(sets.join(' AND '))
|
125
155
|
end
|
126
156
|
}
|
127
|
-
scope :without_#{attribute},
|
157
|
+
scope :without_#{attribute},
|
128
158
|
proc { |*values|
|
129
159
|
if values.blank?
|
130
160
|
no_#{attribute}
|
131
161
|
else
|
132
|
-
where("#{attribute} & ? = 0#{or_is_null_condition}", ::#{model}.bitmask_for_#{attribute}(*values))
|
162
|
+
where("#{model.table_name}.#{attribute} & ? = 0#{or_is_null_condition}", ::#{model}.bitmask_for_#{attribute}(*values))
|
133
163
|
end
|
134
164
|
}
|
135
165
|
|
@@ -138,27 +168,27 @@ module BitmaskAttributes
|
|
138
168
|
if values.blank?
|
139
169
|
no_#{attribute}
|
140
170
|
else
|
141
|
-
where("#{attribute} = ?", ::#{model}.bitmask_for_#{attribute}(*values))
|
171
|
+
where("#{model.table_name}.#{attribute} = ?", ::#{model}.bitmask_for_#{attribute}(*values))
|
142
172
|
end
|
143
173
|
}
|
144
|
-
|
145
|
-
scope :no_#{attribute}, proc { where("#{attribute} = 0#{or_is_null_condition}") }
|
174
|
+
|
175
|
+
scope :no_#{attribute}, proc { where("#{model.table_name}.#{attribute} = 0#{or_is_null_condition}") }
|
146
176
|
|
147
177
|
scope :with_any_#{attribute},
|
148
178
|
proc { |*values|
|
149
179
|
if values.blank?
|
150
|
-
where('#{attribute} > 0')
|
180
|
+
where('#{model.table_name}.#{attribute} > 0')
|
151
181
|
else
|
152
|
-
where("#{attribute} & ? <> 0", ::#{model}.bitmask_for_#{attribute}(*values))
|
182
|
+
where("#{model.table_name}.#{attribute} & ? <> 0", ::#{model}.bitmask_for_#{attribute}(*values))
|
153
183
|
end
|
154
184
|
}
|
155
185
|
)
|
156
186
|
values.each do |value|
|
157
187
|
model.class_eval %(
|
158
188
|
scope :#{attribute}_for_#{value},
|
159
|
-
proc { where('#{attribute} & ? <> 0', ::#{model}.bitmask_for_#{attribute}(:#{value})) }
|
189
|
+
proc { where('#{model.table_name}.#{attribute} & ? <> 0', ::#{model}.bitmask_for_#{attribute}(:#{value})) }
|
160
190
|
)
|
161
|
-
end
|
191
|
+
end
|
162
192
|
end
|
163
193
|
|
164
194
|
def eval_string_for_zero(value_string)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BitmaskAttributesTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context 'validate_for' do
|
6
|
+
context 'when missing column' do
|
7
|
+
setup do
|
8
|
+
@column = Class.new do
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
def initialize(name); @name = name end
|
12
|
+
end
|
13
|
+
|
14
|
+
@model = Class.new do
|
15
|
+
attr_reader :columns
|
16
|
+
|
17
|
+
def initialize(columns); @columns = columns end
|
18
|
+
|
19
|
+
def table_exists?; true end
|
20
|
+
|
21
|
+
def self.name; 'Model' end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'not fail' do
|
26
|
+
definition = BitmaskAttributes::Definition.new(:missing_column)
|
27
|
+
|
28
|
+
some_model = @model.new([@column.new(:name)])
|
29
|
+
definition.send(:validate_for, some_model)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -53,6 +53,14 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
|
|
53
53
|
assert_stored campaign, :phone, :email
|
54
54
|
end
|
55
55
|
|
56
|
+
should "can assign raw bitmask values" do
|
57
|
+
campaign = @campaign_class.new
|
58
|
+
campaign.medium_bitmask = 3
|
59
|
+
assert_stored campaign, :web, :print
|
60
|
+
campaign.medium_bitmask = 0
|
61
|
+
assert_empty campaign.medium
|
62
|
+
end
|
63
|
+
|
56
64
|
should "can save bitmask to db and retrieve values transparently" do
|
57
65
|
campaign = @campaign_class.new(:medium => [:web, :print])
|
58
66
|
assert_stored campaign, :web, :print
|
@@ -78,6 +86,18 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
|
|
78
86
|
assert_unsupported { campaign.medium = [:so_will_this] }
|
79
87
|
end
|
80
88
|
|
89
|
+
should "can only use Fixnum values for raw bitmask values" do
|
90
|
+
campaign = @campaign_class.new(:medium => :web)
|
91
|
+
assert_unsupported { campaign.medium_bitmask = :this_will_fail }
|
92
|
+
end
|
93
|
+
|
94
|
+
should "cannot use unsupported values for raw bitmask values" do
|
95
|
+
campaign = @campaign_class.new(:medium => :web)
|
96
|
+
number_of_attributes = @campaign_class.bitmasks[:medium].size
|
97
|
+
assert_unsupported { campaign.medium_bitmask = (2 ** number_of_attributes) }
|
98
|
+
assert_unsupported { campaign.medium_bitmask = -1 }
|
99
|
+
end
|
100
|
+
|
81
101
|
should "can determine bitmasks using convenience method" do
|
82
102
|
assert @campaign_class.bitmask_for_medium(:web, :print)
|
83
103
|
assert_equal(
|
@@ -90,6 +110,15 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
|
|
90
110
|
assert_unsupported { @campaign_class.bitmask_for_medium(:web, :and_this_isnt_valid) }
|
91
111
|
end
|
92
112
|
|
113
|
+
should "can determine bitmask entries using inverse convenience method" do
|
114
|
+
assert @campaign_class.medium_for_bitmask(3)
|
115
|
+
assert_equal([:web, :print], @campaign_class.medium_for_bitmask(3))
|
116
|
+
end
|
117
|
+
|
118
|
+
should "assert use of non Fixnum value in inverse convenience method will result in exception" do
|
119
|
+
assert_unsupported { @campaign_class.medium_for_bitmask(:this_isnt_valid) }
|
120
|
+
end
|
121
|
+
|
93
122
|
should "hash of values is with indifferent access" do
|
94
123
|
string_bit = nil
|
95
124
|
assert_nothing_raised do
|
@@ -174,7 +203,7 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
|
|
174
203
|
assert_equal [@campaign2, @campaign7], @company.campaigns.without_medium(:web, :print)
|
175
204
|
assert_equal [@campaign2, @campaign3, @campaign4], @company.campaigns.without_medium(:print, :phone)
|
176
205
|
end
|
177
|
-
|
206
|
+
|
178
207
|
should "support retrieval by exact value" do
|
179
208
|
assert_equal [@campaign4], @company.campaigns.with_exact_medium(:web)
|
180
209
|
assert_equal [@campaign1], @company.campaigns.with_exact_medium(:web, :print)
|
@@ -270,6 +299,15 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
|
|
270
299
|
end
|
271
300
|
end
|
272
301
|
|
302
|
+
should "accept a default value option" do
|
303
|
+
assert_equal DefaultValue.new.default_sym, [:y]
|
304
|
+
assert_equal DefaultValue.new.default_array, [:y, :z]
|
305
|
+
assert_equal DefaultValue.new(:default_sym => :x).default_sym, [:x]
|
306
|
+
assert_equal DefaultValue.new(:default_array => [:x]).default_array, [:x]
|
307
|
+
end
|
308
|
+
|
273
309
|
context_with_classes 'Campaign with null attributes',CampaignWithNull,CompanyWithNull
|
274
310
|
context_with_classes 'Campaign without null attributes',CampaignWithoutNull,CompanyWithoutNull
|
275
|
-
|
311
|
+
context_with_classes 'SubCampaign with null attributes',SubCampaignWithNull,CompanyWithNull
|
312
|
+
context_with_classes 'SubCampaign without null attributes',SubCampaignWithoutNull,CompanyWithoutNull
|
313
|
+
end
|
data/test/support/models.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
ActiveRecord::Schema.define do
|
1
|
+
ActiveRecord::Schema.define do
|
2
2
|
create_table :campaign_with_nulls do |t|
|
3
3
|
t.integer :company_id
|
4
4
|
t.integer :medium, :allow_zero, :misc, :Legacy
|
5
|
+
t.string :type # STI
|
5
6
|
end
|
6
7
|
create_table :company_with_nulls do |t|
|
7
8
|
t.string :name
|
@@ -9,10 +10,14 @@ ActiveRecord::Schema.define do
|
|
9
10
|
create_table :campaign_without_nulls do |t|
|
10
11
|
t.integer :company_id
|
11
12
|
t.integer :medium, :allow_zero, :misc, :Legacy, :null => false, :default => 0
|
13
|
+
t.string :type # STI
|
12
14
|
end
|
13
15
|
create_table :company_without_nulls do |t|
|
14
16
|
t.string :name
|
15
17
|
end
|
18
|
+
create_table :default_values do |t|
|
19
|
+
t.integer :default_sym, :default_array
|
20
|
+
end
|
16
21
|
end
|
17
22
|
|
18
23
|
# Pseudo models for testing purposes
|
@@ -33,6 +38,9 @@ class CampaignWithNull < ActiveRecord::Base
|
|
33
38
|
bitmask :Legacy, :as => [:upper, :case]
|
34
39
|
end
|
35
40
|
|
41
|
+
class SubCampaignWithNull < CampaignWithNull
|
42
|
+
end
|
43
|
+
|
36
44
|
class CompanyWithoutNull < ActiveRecord::Base
|
37
45
|
has_many :campaigns,:class_name => 'CampaignWithoutNull',:foreign_key => 'company_id'
|
38
46
|
end
|
@@ -47,4 +55,12 @@ class CampaignWithoutNull < ActiveRecord::Base
|
|
47
55
|
end
|
48
56
|
end
|
49
57
|
bitmask :Legacy, :as => [:upper, :case], :null => false
|
50
|
-
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class SubCampaignWithoutNull < CampaignWithNull
|
61
|
+
end
|
62
|
+
|
63
|
+
class DefaultValue < ActiveRecord::Base
|
64
|
+
bitmask :default_sym, :as => [:x, :y, :z], :default => :y
|
65
|
+
bitmask :default_array, :as => [:x, :y, :z], :default => [:y, :z]
|
66
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitmask_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/bitmask_attributes/definition.rb
|
81
81
|
- lib/bitmask_attributes/value_proxy.rb
|
82
82
|
- lib/bitmask_attributes/version.rb
|
83
|
+
- test/bitmask_attributes/definition_test.rb
|
83
84
|
- test/bitmask_attributes_test.rb
|
84
85
|
- test/support/models.rb
|
85
86
|
- test/test_helper.rb
|
@@ -108,6 +109,7 @@ signing_key:
|
|
108
109
|
specification_version: 3
|
109
110
|
summary: Simple bitmask attribute support for ActiveRecord
|
110
111
|
test_files:
|
112
|
+
- test/bitmask_attributes/definition_test.rb
|
111
113
|
- test/bitmask_attributes_test.rb
|
112
114
|
- test/support/models.rb
|
113
115
|
- test/test_helper.rb
|