assignable_values 0.11.1 → 0.11.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/assignable_values/active_record/restriction/base.rb +7 -3
- data/lib/assignable_values/active_record/restriction/belongs_to_association.rb +11 -1
- data/lib/assignable_values/active_record/restriction/scalar_attribute.rb +11 -4
- data/lib/assignable_values/version.rb +1 -1
- data/spec/rails-2.3/.bundle/config +2 -0
- data/spec/rails-2.3/Gemfile.lock +1 -1
- data/spec/rails-3.0/.bundle/config +2 -0
- data/spec/rails-3.0/Gemfile.lock +1 -1
- data/spec/rails-3.2/.bundle/config +2 -0
- data/spec/rails-3.2/Gemfile.lock +1 -1
- data/spec/rails-4.1/.bundle/config +2 -0
- data/spec/rails-4.1/Gemfile.lock +1 -1
- data/spec/shared/assignable_values/active_record_spec.rb +60 -2
- metadata +114 -84
- checksums.yaml +0 -15
@@ -50,9 +50,9 @@ module AssignableValues
|
|
50
50
|
parsed_values = parsed_assignable_values(record)
|
51
51
|
current_values = parsed_values.delete(:values)
|
52
52
|
|
53
|
-
if options.fetch(:include_old_value, true)
|
53
|
+
if options.fetch(:include_old_value, true) && has_previously_saved_value?(record)
|
54
54
|
old_value = previously_saved_value(record)
|
55
|
-
|
55
|
+
unless current_values.include?(old_value)
|
56
56
|
assignable_values << old_value
|
57
57
|
end
|
58
58
|
end
|
@@ -103,8 +103,12 @@ module AssignableValues
|
|
103
103
|
record.send(property)
|
104
104
|
end
|
105
105
|
|
106
|
+
def has_previously_saved_value?(record)
|
107
|
+
raise NotImplementedError
|
108
|
+
end
|
109
|
+
|
106
110
|
def previously_saved_value(record)
|
107
|
-
|
111
|
+
raise NotImplementedError
|
108
112
|
end
|
109
113
|
|
110
114
|
def decorate_values(values)
|
@@ -26,8 +26,12 @@ module AssignableValues
|
|
26
26
|
record.send(association_id_method)
|
27
27
|
end
|
28
28
|
|
29
|
+
def has_previously_saved_value?(record)
|
30
|
+
!record.new_record? && record.respond_to?(value_was_method)
|
31
|
+
end
|
32
|
+
|
29
33
|
def previously_saved_value(record)
|
30
|
-
if old_id = record.send(
|
34
|
+
if old_id = record.send(value_was_method).presence
|
31
35
|
if old_id == association_id(record)
|
32
36
|
current_value(record) # no need to query the database if nothing changed
|
33
37
|
else
|
@@ -42,6 +46,12 @@ module AssignableValues
|
|
42
46
|
value
|
43
47
|
end
|
44
48
|
|
49
|
+
private
|
50
|
+
|
51
|
+
def value_was_method
|
52
|
+
"#{association_id_method}_was"
|
53
|
+
end
|
54
|
+
|
45
55
|
end
|
46
56
|
end
|
47
57
|
end
|
@@ -88,11 +88,18 @@ module AssignableValues
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
def has_previously_saved_value?(record)
|
92
|
+
!record.new_record? && record.respond_to?(value_was_method)
|
93
|
+
end
|
94
|
+
|
91
95
|
def previously_saved_value(record)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
+
record.send(value_was_method)
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def value_was_method
|
102
|
+
"#{property}_was"
|
96
103
|
end
|
97
104
|
|
98
105
|
end
|
data/spec/rails-2.3/Gemfile.lock
CHANGED
data/spec/rails-3.0/Gemfile.lock
CHANGED
data/spec/rails-3.2/Gemfile.lock
CHANGED
data/spec/rails-4.1/Gemfile.lock
CHANGED
@@ -75,6 +75,18 @@ describe AssignableValues::ActiveRecord do
|
|
75
75
|
record.reload.should be_valid
|
76
76
|
end
|
77
77
|
|
78
|
+
it 'should allow a previously saved, blank value even if that value is no longer allowed' do
|
79
|
+
record = @klass.create!(:genre => 'pop')
|
80
|
+
@klass.update_all(:genre => '') # update without validations for the sake of this test
|
81
|
+
record.reload.should be_valid
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should not allow nil (the "previous value") if the record was never saved' do
|
85
|
+
record = @klass.new(:genre => nil)
|
86
|
+
# Show that nil is not assignable, even though `record.genre_was` is nil.
|
87
|
+
record.should_not be_valid
|
88
|
+
end
|
89
|
+
|
78
90
|
it 'should generate a method returning the humanized value' do
|
79
91
|
song = @klass.new(:genre => 'pop')
|
80
92
|
song.humanized_genre.should == 'Pop music'
|
@@ -235,7 +247,7 @@ describe AssignableValues::ActiveRecord do
|
|
235
247
|
record.should be_valid
|
236
248
|
end
|
237
249
|
|
238
|
-
it 'should allow a previously saved association even if that association is no longer allowed' do
|
250
|
+
it 'should allow a previously saved association, even if that association is no longer allowed' do
|
239
251
|
allowed_association = Artist.create!
|
240
252
|
disallowed_association = Artist.create!
|
241
253
|
klass = Song.disposable_copy
|
@@ -248,6 +260,30 @@ describe AssignableValues::ActiveRecord do
|
|
248
260
|
record.should be_valid
|
249
261
|
end
|
250
262
|
|
263
|
+
it 'should allow nil for an association if the record was saved before with a nil association' do
|
264
|
+
allowed_association = Artist.create!
|
265
|
+
klass = Song.disposable_copy
|
266
|
+
record = klass.create!(:artist => nil)
|
267
|
+
klass.class_eval do
|
268
|
+
assignable_values_for :artist do
|
269
|
+
[allowed_association]
|
270
|
+
end
|
271
|
+
end
|
272
|
+
record.should be_valid
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'sould not allow nil for an association (the "previously saved value") if the record is new' do
|
276
|
+
allowed_association = Artist.create!
|
277
|
+
klass = Song.disposable_copy
|
278
|
+
record = klass.new(:artist => nil)
|
279
|
+
klass.class_eval do
|
280
|
+
assignable_values_for :artist do
|
281
|
+
[allowed_association]
|
282
|
+
end
|
283
|
+
end
|
284
|
+
record.should_not be_valid
|
285
|
+
end
|
286
|
+
|
251
287
|
it "should not load a previously saved association if the association's foreign key hasn't changed" do
|
252
288
|
association = Artist.create!
|
253
289
|
klass = Song.disposable_copy do
|
@@ -516,7 +552,29 @@ describe AssignableValues::ActiveRecord do
|
|
516
552
|
record.assignable_genres.should == %w[pop rock]
|
517
553
|
end
|
518
554
|
|
519
|
-
it 'should
|
555
|
+
it 'should prepend a previously saved blank value to the top of the list, even if is no longer allowed' do
|
556
|
+
klass = Song.disposable_copy do
|
557
|
+
assignable_values_for :genre do
|
558
|
+
%w[pop rock]
|
559
|
+
end
|
560
|
+
end
|
561
|
+
record = klass.create!(:genre => 'pop')
|
562
|
+
klass.update_all(:genre => '') # update without validation for the sake of this test
|
563
|
+
record.reload.assignable_genres.should == ['', 'pop', 'rock']
|
564
|
+
end
|
565
|
+
|
566
|
+
it 'should not prepend nil to the top of the list if the record was never saved' do
|
567
|
+
klass = Song.disposable_copy do
|
568
|
+
assignable_values_for :genre do
|
569
|
+
%w[pop rock]
|
570
|
+
end
|
571
|
+
end
|
572
|
+
record = klass.new(:genre => 'pop')
|
573
|
+
record.genre_was.should be_nil
|
574
|
+
record.assignable_genres.should == ['pop', 'rock']
|
575
|
+
end
|
576
|
+
|
577
|
+
it 'should allow omitting a previously saved value with :include_old_value => false option' do
|
520
578
|
klass = Song.disposable_copy do
|
521
579
|
assignable_values_for :genre do
|
522
580
|
%w[pop rock]
|
metadata
CHANGED
@@ -1,92 +1,105 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: assignable_values
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 55
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 11
|
9
|
+
- 2
|
10
|
+
version: 0.11.2
|
5
11
|
platform: ruby
|
6
|
-
authors:
|
12
|
+
authors:
|
7
13
|
- Henning Koch
|
8
14
|
autorequire:
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
17
|
+
|
18
|
+
date: 2015-09-24 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
14
22
|
name: activerecord
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ! '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
23
|
prerelease: false
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
28
36
|
name: rails
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '3.1'
|
34
|
-
type: :development
|
35
37
|
prerelease: false
|
36
|
-
|
37
|
-
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
38
41
|
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '2.8'
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 5
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 1
|
47
|
+
version: "3.1"
|
48
48
|
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
49
52
|
prerelease: false
|
50
|
-
|
51
|
-
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
52
56
|
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '2.8'
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 19
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 8
|
62
|
+
version: "2.8"
|
62
63
|
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rspec-rails
|
63
67
|
prerelease: false
|
64
|
-
|
65
|
-
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
66
71
|
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
- - ! '>='
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 19
|
74
|
+
segments:
|
75
|
+
- 2
|
76
|
+
- 8
|
77
|
+
version: "2.8"
|
76
78
|
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sqlite3
|
77
82
|
prerelease: false
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
type: :development
|
93
|
+
version_requirements: *id005
|
94
|
+
description: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.
|
85
95
|
email: henning.koch@makandra.de
|
86
96
|
executables: []
|
97
|
+
|
87
98
|
extensions: []
|
99
|
+
|
88
100
|
extra_rdoc_files: []
|
89
|
-
|
101
|
+
|
102
|
+
files:
|
90
103
|
- .gitignore
|
91
104
|
- .rspec
|
92
105
|
- .ruby-version
|
@@ -104,6 +117,7 @@ files:
|
|
104
117
|
- lib/assignable_values/humanizable_string.rb
|
105
118
|
- lib/assignable_values/humanized_value.rb
|
106
119
|
- lib/assignable_values/version.rb
|
120
|
+
- spec/rails-2.3/.bundle/config
|
107
121
|
- spec/rails-2.3/Gemfile
|
108
122
|
- spec/rails-2.3/Gemfile.lock
|
109
123
|
- spec/rails-2.3/Rakefile
|
@@ -119,6 +133,7 @@ files:
|
|
119
133
|
- spec/rails-2.3/rcov.opts
|
120
134
|
- spec/rails-2.3/spec.opts
|
121
135
|
- spec/rails-2.3/spec/spec_helper.rb
|
136
|
+
- spec/rails-3.0/.bundle/config
|
122
137
|
- spec/rails-3.0/.rspec
|
123
138
|
- spec/rails-3.0/Gemfile
|
124
139
|
- spec/rails-3.0/Gemfile.lock
|
@@ -140,6 +155,7 @@ files:
|
|
140
155
|
- spec/rails-3.0/app_root/script/rails
|
141
156
|
- spec/rails-3.0/rcov.opts
|
142
157
|
- spec/rails-3.0/spec/spec_helper.rb
|
158
|
+
- spec/rails-3.2/.bundle/config
|
143
159
|
- spec/rails-3.2/.rspec
|
144
160
|
- spec/rails-3.2/Gemfile
|
145
161
|
- spec/rails-3.2/Gemfile.lock
|
@@ -161,6 +177,7 @@ files:
|
|
161
177
|
- spec/rails-3.2/app_root/script/rails
|
162
178
|
- spec/rails-3.2/rcov.opts
|
163
179
|
- spec/rails-3.2/spec/spec_helper.rb
|
180
|
+
- spec/rails-4.1/.bundle/config
|
164
181
|
- spec/rails-4.1/.rspec
|
165
182
|
- spec/rails-4.1/.ruby-version
|
166
183
|
- spec/rails-4.1/Gemfile
|
@@ -201,32 +218,42 @@ files:
|
|
201
218
|
- spec/shared/app_root/db/migrate/003_create_recordings.rb
|
202
219
|
- spec/shared/assignable_values/active_record_spec.rb
|
203
220
|
- spec/shared/assignable_values/humanized_value_spec.rb
|
221
|
+
has_rdoc: true
|
204
222
|
homepage: https://github.com/makandra/assignable_values
|
205
|
-
licenses:
|
223
|
+
licenses:
|
206
224
|
- MIT
|
207
|
-
metadata: {}
|
208
225
|
post_install_message:
|
209
226
|
rdoc_options: []
|
210
|
-
|
227
|
+
|
228
|
+
require_paths:
|
211
229
|
- lib
|
212
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
230
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
231
|
+
none: false
|
232
|
+
requirements:
|
233
|
+
- - ">="
|
234
|
+
- !ruby/object:Gem::Version
|
235
|
+
hash: 3
|
236
|
+
segments:
|
237
|
+
- 0
|
238
|
+
version: "0"
|
239
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
240
|
+
none: false
|
241
|
+
requirements:
|
242
|
+
- - ">="
|
243
|
+
- !ruby/object:Gem::Version
|
244
|
+
hash: 3
|
245
|
+
segments:
|
246
|
+
- 0
|
247
|
+
version: "0"
|
222
248
|
requirements: []
|
249
|
+
|
223
250
|
rubyforge_project:
|
224
|
-
rubygems_version:
|
251
|
+
rubygems_version: 1.3.9.5
|
225
252
|
signing_key:
|
226
|
-
specification_version:
|
227
|
-
summary: Restrict the values assignable to ActiveRecord attributes or associations.
|
228
|
-
|
229
|
-
|
253
|
+
specification_version: 3
|
254
|
+
summary: Restrict the values assignable to ActiveRecord attributes or associations. Or enums on steroids.
|
255
|
+
test_files:
|
256
|
+
- spec/rails-2.3/.bundle/config
|
230
257
|
- spec/rails-2.3/Gemfile
|
231
258
|
- spec/rails-2.3/Gemfile.lock
|
232
259
|
- spec/rails-2.3/Rakefile
|
@@ -242,6 +269,7 @@ test_files:
|
|
242
269
|
- spec/rails-2.3/rcov.opts
|
243
270
|
- spec/rails-2.3/spec.opts
|
244
271
|
- spec/rails-2.3/spec/spec_helper.rb
|
272
|
+
- spec/rails-3.0/.bundle/config
|
245
273
|
- spec/rails-3.0/.rspec
|
246
274
|
- spec/rails-3.0/Gemfile
|
247
275
|
- spec/rails-3.0/Gemfile.lock
|
@@ -263,6 +291,7 @@ test_files:
|
|
263
291
|
- spec/rails-3.0/app_root/script/rails
|
264
292
|
- spec/rails-3.0/rcov.opts
|
265
293
|
- spec/rails-3.0/spec/spec_helper.rb
|
294
|
+
- spec/rails-3.2/.bundle/config
|
266
295
|
- spec/rails-3.2/.rspec
|
267
296
|
- spec/rails-3.2/Gemfile
|
268
297
|
- spec/rails-3.2/Gemfile.lock
|
@@ -284,6 +313,7 @@ test_files:
|
|
284
313
|
- spec/rails-3.2/app_root/script/rails
|
285
314
|
- spec/rails-3.2/rcov.opts
|
286
315
|
- spec/rails-3.2/spec/spec_helper.rb
|
316
|
+
- spec/rails-4.1/.bundle/config
|
287
317
|
- spec/rails-4.1/.rspec
|
288
318
|
- spec/rails-4.1/.ruby-version
|
289
319
|
- spec/rails-4.1/Gemfile
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
NDE4NmE4ZGNlYWVkMDFjYWVkYzYyNTlhNTQ5NGQ3NGFhOTZkYzg5ZA==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MGU2OGRjNjliYTM2ZjU0ODY5MzljY2ZiMWYwY2ViN2FjMDViM2QyNQ==
|
7
|
-
SHA512:
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NWJkZjEyYzhmNGZiZWJiYjZmMTAwZTJhYmY0NjU4ZjVlZjFhMTBkYTQ0NDc5
|
10
|
-
ZTZlNTU3Y2Y3NWNjZmU3YWE4YjQ2MDBhNGQ4Nzg1NmYyN2ZmYzhkN2UxMGEy
|
11
|
-
MTUwYmYzM2I5YTcwYmYxMzUxZDk3NjU2MjczZjlkZTRmMTBkYmY=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MGFmZDgzMmUzZjZiMzdkOTlmNTg4ZjdiN2QxNTY5ODk0ZGQ4NzFhMDA4ZDZh
|
14
|
-
MzEyODllMjFjMDgzZTc5NDMzYzg3M2U0NzkyMTQ4ZTk4OGUzZjM5NzNmYTMz
|
15
|
-
MDkxYjI2NDUwN2M1Njg1ZjQyYzRhOTRmYmYyOGZhYTEyMmQ5MDY=
|