assignable_values 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -130,6 +130,10 @@ If you would like to change this behavior and allow blank values to be valid, us
130
130
  end
131
131
  end
132
132
 
133
+ The `:allow_blank` option can be a symbol, in which case a method of that name will be called on the record.
134
+
135
+ The `:allow_blank` option can also be a lambda, in which case the lambda will be called in the context of the record.
136
+
133
137
 
134
138
  ### Values are only validated when they change
135
139
 
@@ -18,7 +18,7 @@ module AssignableValues
18
18
 
19
19
  def validate_record(record)
20
20
  value = current_value(record)
21
- unless allow_blank? && value.blank?
21
+ unless allow_blank?(record) && value.blank?
22
22
  begin
23
23
  unless assignable_value?(record, value)
24
24
  record.errors.add(error_property, not_included_error_message)
@@ -115,8 +115,8 @@ module AssignableValues
115
115
  @options.has_key?(:secondary_default)
116
116
  end
117
117
 
118
- def allow_blank?
119
- @options[:allow_blank]
118
+ def allow_blank?(record)
119
+ evaluate_option(record, @options[:allow_blank])
120
120
  end
121
121
 
122
122
  def delegate_definition
@@ -187,10 +187,15 @@ module AssignableValues
187
187
  end
188
188
 
189
189
  def delegate(record)
190
- case delegate_definition
191
- when Symbol then record.send(delegate_definition)
192
- when Proc then record.instance_eval(&delegate_definition)
193
- else raise "Illegal delegate definition: #{delegate_definition.inspect}"
190
+ evaluate_option(record, delegate_definition)
191
+ end
192
+
193
+ def evaluate_option(record, option)
194
+ case option
195
+ when NilClass, TrueClass, FalseClass then option
196
+ when Symbol then record.send(option)
197
+ when Proc then record.instance_eval(&option)
198
+ else raise "Illegal option type: #{option.inspect}"
194
199
  end
195
200
  end
196
201
 
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.5.3'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.2)
4
+ assignable_values (0.5.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.2)
4
+ assignable_values (0.5.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.5.2)
4
+ assignable_values (0.5.3)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -69,7 +69,7 @@ describe AssignableValues::ActiveRecord do
69
69
 
70
70
  end
71
71
 
72
- context 'if the :allow_blank option is set' do
72
+ context 'if the :allow_blank option is set to true' do
73
73
 
74
74
  before :each do
75
75
  @klass = Song.disposable_copy do
@@ -83,12 +83,55 @@ describe AssignableValues::ActiveRecord do
83
83
  @klass.new(:genre => nil).should be_valid
84
84
  end
85
85
 
86
- it 'should allow an empty string as value if the :allow_blank option is set' do
86
+ it 'should allow an empty string as value' do
87
87
  @klass.new(:genre => '').should be_valid
88
88
  end
89
89
 
90
90
  end
91
91
 
92
+ context 'if the :allow_blank option is set to a symbol that refers to an instance method' do
93
+
94
+ before :each do
95
+ @klass = Song.disposable_copy do
96
+
97
+ attr_accessor :genre_may_be_blank
98
+
99
+ assignable_values_for :genre, :allow_blank => :genre_may_be_blank do
100
+ %w[pop rock]
101
+ end
102
+
103
+ end
104
+ end
105
+
106
+ it 'should call that method to determine if a blank value is allowed' do
107
+ @klass.new(:genre => '', :genre_may_be_blank => true).should be_valid
108
+ @klass.new(:genre => '', :genre_may_be_blank => false).should_not be_valid
109
+ end
110
+
111
+ end
112
+
113
+ context 'if the :allow_blank option is set to a lambda ' do
114
+
115
+
116
+ before :each do
117
+ @klass = Song.disposable_copy do
118
+
119
+ attr_accessor :genre_may_be_blank
120
+
121
+ assignable_values_for :genre, :allow_blank => lambda { genre_may_be_blank } do
122
+ %w[pop rock]
123
+ end
124
+
125
+ end
126
+ end
127
+
128
+ it 'should evaluate that lambda in the record context to determine if a blank value is allowed' do
129
+ @klass.new(:genre => '', :genre_may_be_blank => true).should be_valid
130
+ @klass.new(:genre => '', :genre_may_be_blank => false).should_not be_valid
131
+ end
132
+
133
+ end
134
+
92
135
  end
93
136
 
94
137
  context 'when validating belongs_to associations' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 3
10
- version: 0.5.3
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-19 00:00:00 +01:00
18
+ date: 2013-01-03 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency