has-bit-field 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm gemset use has-bit-field
data/README.md CHANGED
@@ -43,6 +43,13 @@ One of the great advantages of this approach is that it is easy to add additiona
43
43
  end
44
44
 
45
45
  The new flag will be evaluated as false for existing rows on the database table until their values are explicitly set. Be careful with the peanuts!
46
+
47
+ Another gotcha to be aware of is when combining a bit field with Active Record's `validates_acceptance_of`. When you call `validates_acceptance_of`, if there is no database column, Active Record will define an `attr_accessor` for that boolean field. If you have already defined the bit field, this will clobber those methods. Also, you need to set the value it's looking for to `true` instead of the default of `"1"`. So here's an example of how to use it:
48
+
49
+ class Person < ActiveRecord::Base
50
+ validates_acceptance_of :read_books, :message => "You must agree to read", :accept => true
51
+ has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
52
+ end
46
53
 
47
54
  Copyright
48
55
  ---------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has-bit-field}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Paul Barry"]
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.files = [
19
19
  ".document",
20
20
  ".gitignore",
21
+ ".rvmrc",
21
22
  "Gemfile",
22
23
  "LICENSE",
23
24
  "README.md",
@@ -13,23 +13,40 @@ end
13
13
 
14
14
  class Person < ActiveRecord::Base
15
15
  extend HasBitField
16
- #attr_accessor :bit_field
16
+
17
17
  has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
18
18
  end
19
19
 
20
+
21
+ ActiveRecord::Base.connection.create_table(:skills) do |t|
22
+ t.integer :outdoor_bit_field, :default => 0
23
+ t.integer :indoor_bit_field, :default => 0
24
+ end
25
+
26
+ class Skill < ActiveRecord::Base
27
+ extend HasBitField
28
+
29
+ validates_acceptance_of :chops_trees, :message => "You have to be a lumberjack", :accept => true
30
+ validates_acceptance_of :plays_piano, :message => "You must be a pianist", :accept => true
31
+
32
+ has_bit_field :outdoor_bit_field, :chops_trees, :builds_fences, :cuts_hedges
33
+ has_bit_field :indoor_bit_field, :plays_piano, :mops_floors, :makes_soup
34
+ end
35
+
20
36
  class HasBitFieldTest < Test::Unit::TestCase
37
+
21
38
  def test_bit_field
22
39
  p = Person.new
23
40
  [:likes_ice_cream, :plays_golf, :watches_tv, :reads_books].each do |f|
24
41
  assert p.respond_to?("#{f}?"), "Expected #{p.inspect} to respond to #{f}?"
25
42
  assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
26
43
  end
27
-
44
+
28
45
  assert_equal Person.likes_ice_cream_bit, (1 << 0)
29
46
  assert_equal Person.plays_golf_bit, (1 << 1)
30
47
  assert_equal Person.watches_tv_bit, (1 << 2)
31
48
  assert_equal Person.reads_books_bit, (1 << 3)
32
-
49
+
33
50
  p.likes_ice_cream = true
34
51
  assert p.likes_ice_cream?
35
52
  assert !p.plays_golf?
@@ -78,12 +95,12 @@ class HasBitFieldTest < Test::Unit::TestCase
78
95
  a.plays_golf = true
79
96
  a.reads_books = true
80
97
  assert a.save
81
-
98
+
82
99
  b = Person.new
83
100
  b.likes_ice_cream = true
84
101
  b.watches_tv = true
85
102
  assert b.save
86
-
103
+
87
104
  c = Person.create!
88
105
 
89
106
  assert_equal [b], Person.likes_ice_cream.all(:order => "id")
@@ -91,13 +108,14 @@ class HasBitFieldTest < Test::Unit::TestCase
91
108
 
92
109
  assert_equal [a], Person.plays_golf.all(:order => "id")
93
110
  assert_equal [b,c], Person.not_plays_golf.all(:order => "id")
94
-
111
+
95
112
  assert_equal [b], Person.watches_tv.all(:order => "id")
96
113
  assert_equal [a,c], Person.not_watches_tv.all(:order => "id")
97
-
114
+
98
115
  assert_equal [a], Person.reads_books.all(:order => "id")
99
116
  assert_equal [b,c], Person.not_reads_books.all(:order => "id")
100
117
  end
118
+
101
119
  def test_dirty_attributes
102
120
  Person.delete_all
103
121
  a = Person.new
@@ -113,4 +131,53 @@ class HasBitFieldTest < Test::Unit::TestCase
113
131
  assert_equal true, a.reads_books_was
114
132
  assert !a.reads_books_changed?
115
133
  end
134
+
135
+ def test_ar_validations_no_default
136
+ s = Skill.new(:plays_piano => true, :builds_fences => true, :cuts_hedges => false)
137
+
138
+ assert_equal false, s.chops_trees?
139
+ assert_equal false, s.chops_trees
140
+ assert s.builds_fences?
141
+ assert !s.cuts_hedges?
142
+ assert !s.cuts_hedges
143
+
144
+ assert !s.valid?
145
+ assert s.errors.on(:chops_trees)
146
+
147
+ s.chops_trees = false
148
+ assert !s.chops_trees?
149
+ assert !s.valid?
150
+ assert s.errors.on(:chops_trees)
151
+
152
+ s.chops_trees = true
153
+ assert s.chops_trees?
154
+ assert s.valid?
155
+ assert !s.errors.on(:chops_trees)
156
+ assert s.save
157
+ end
158
+
159
+ def test_ar_validations_with_default
160
+ s = Skill.new(:chops_trees => true, :mops_floors => true, :makes_soup => false)
161
+
162
+ assert_equal false, s.plays_piano?
163
+ assert_equal false, s.plays_piano
164
+ assert s.mops_floors?
165
+ assert !s.makes_soup?
166
+ assert !s.makes_soup
167
+
168
+ assert !s.valid?
169
+ assert s.errors.on(:plays_piano)
170
+
171
+ s.plays_piano = false
172
+ assert !s.plays_piano?
173
+ assert !s.valid?
174
+ assert s.errors.on(:plays_piano)
175
+
176
+ s.plays_piano = true
177
+ assert s.plays_piano?
178
+ assert s.valid?
179
+ assert !s.errors.on(:plays_piano)
180
+ assert s.save
181
+ end
182
+
116
183
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has-bit-field
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Barry
@@ -31,6 +31,7 @@ extra_rdoc_files:
31
31
  files:
32
32
  - .document
33
33
  - .gitignore
34
+ - .rvmrc
34
35
  - Gemfile
35
36
  - LICENSE
36
37
  - README.md