assignable_values 0.5.3 → 0.6.0
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.
- data/README.md +4 -0
- data/lib/assignable_values/active_record/restriction/base.rb +12 -7
- data/lib/assignable_values/version.rb +1 -1
- data/spec/rails-2.3/Gemfile.lock +1 -1
- data/spec/rails-3.0/Gemfile.lock +1 -1
- data/spec/rails-3.2/Gemfile.lock +1 -1
- data/spec/shared/assignable_values/active_record_spec.rb +45 -2
- metadata +5 -5
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
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
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
|
|
data/spec/rails-2.3/Gemfile.lock
CHANGED
data/spec/rails-3.0/Gemfile.lock
CHANGED
data/spec/rails-3.2/Gemfile.lock
CHANGED
@@ -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
|
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:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
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:
|
18
|
+
date: 2013-01-03 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|