mcommons-values_for 0.0.7 → 0.0.8

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.
@@ -26,6 +26,9 @@ module EnumFor
26
26
 
27
27
  prefix = opts.has_key?(:prefix) ? opts[:prefix] : attribute_s
28
28
 
29
+ # We don't accept the case where an empty string is a valid value, but we should provide a useful error message
30
+ raise ArgumentError, "Can't use values_for with an empty string" if opts[:has].any?{|v| v.respond_to?(:empty?) && v.empty? }
31
+
29
32
  # Valid values are most likely Symbols anyway, but coerce them to be safe.
30
33
  valid_symbols = opts[:has].map{|v| v.to_sym }
31
34
 
@@ -46,7 +49,6 @@ module EnumFor
46
49
  read_attribute(attribute) == val_s # read_attribute(:foo) == 'foo'
47
50
  end # end
48
51
  end
49
-
50
52
  end
51
53
 
52
54
  # Accepts assignment both from String and Symbol form of valid values.
@@ -54,15 +56,17 @@ module EnumFor
54
56
  merge(:in => valid_symbols | valid_symbols.map{|s| s.to_s } )
55
57
 
56
58
  # Custom reader method presents attribute value in Symbol form.
57
- define_method(attribute_s) do # def foo
58
- self[attribute].to_sym unless self[attribute].nil? # self[:foo].to_sym unless self[:foo].nil?
59
- end # end
59
+ define_method(attribute_s) do # def foo
60
+ unless self[attribute].nil? || self[attribute].empty? # unless self[:foo].nil? || self[:foo].empty?
61
+ self[attribute].to_sym # self[:foo].to_sym unless self[:foo].nil?
62
+ end # end
63
+ end # end
60
64
 
61
65
  # Custom setter method casting all attribute input to String, allows
62
66
  # assignment from Symbol form.
63
- define_method(attribute_s + '=') do |other| # def foo=(other)
64
- self[attribute] = other.to_s # self[foo] = other
65
- end # end
67
+ define_method(attribute_s + '=') do |other| # def foo=(other)
68
+ self[attribute] = other.nil? ? nil : other.to_s # self[foo] = other unless other.nil?
69
+ end # end
66
70
 
67
71
  # Make collection of all valid attribute Symbols available to user
68
72
  # from plural name of attribute as class method.
@@ -130,6 +130,17 @@ describe EnumFor do
130
130
  end
131
131
  end
132
132
 
133
+ describe "when a user tries to set an empty String as a valid value" do
134
+ it "should raise an IllegalArgumentError" do
135
+ lambda {
136
+ class Food2 < ActiveRecord::Base
137
+ set_table_name "tacos"
138
+ values_for :state, :has => ['stuff', '']
139
+ end
140
+ }.should raise_error(ArgumentError)
141
+ end
142
+ end
143
+
133
144
  describe "behavior with existing methods of same name" do
134
145
  class Balloon < ActiveRecord::Base
135
146
  set_table_name "tacos"
@@ -160,6 +171,32 @@ describe EnumFor do
160
171
  end
161
172
  end
162
173
 
174
+ describe "getting and setting nil values" do
175
+ class Food2 < ActiveRecord::Base
176
+ set_table_name "tacos"
177
+ values_for :state, :has => ['stuff', 'morestuff'], :prefix => nil, :allow_nil => true, :message => "is great!"
178
+ end
179
+
180
+ before do
181
+ @f = Food2.new
182
+ @f.state = nil
183
+ end
184
+
185
+ it "should return a nil value without error" do
186
+ lambda {
187
+ @f.state
188
+ }.should_not raise_error
189
+ end
190
+
191
+ it "should return nil when nil is set" do
192
+ @f.state.should == nil
193
+ end
194
+
195
+ it "should store nil when nil is set" do
196
+ @f.state_before_type_cast.should == nil
197
+ end
198
+ end
199
+
163
200
  describe "named scope creation" do
164
201
  describe "when not added" do
165
202
  it "should not create named scopes" do
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{values_for}
3
- s.version = "0.0.7"
3
+ s.version = "0.0.8"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Mal McKay and Justin Leitgeb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcommons-values_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mal McKay and Justin Leitgeb