assignable_values 0.16.1 → 0.16.2
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.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +7 -9
- data/CHANGELOG.md +6 -1
- data/Gemfile +1 -1
- data/Gemfile.2.3.lock +1 -1
- data/Gemfile.3.2.lock +1 -1
- data/Gemfile.4.2.lock +1 -1
- data/Gemfile.5.0.lock +1 -1
- data/Gemfile.5.1.lock +2 -2
- data/Gemfile.5.1.pg.lock +1 -1
- data/Gemfile.6.0.pg +1 -1
- data/Gemfile.6.0.pg.lock +10 -10
- data/Gemfile.lock +1 -1
- data/README.md +8 -5
- data/lib/assignable_values/active_record/restriction/base.rb +29 -9
- data/lib/assignable_values/version.rb +1 -1
- data/spec/assignable_values/active_record_spec.rb +43 -2
- metadata +3 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b9fa8864bca68f5d9a2c7199bd08a2c3f97bb46cc3e40b0b7860c62d9bb563bb
|
|
4
|
+
data.tar.gz: fe00de9f990305f33905cbd8b0dcaf9fc318521b4378c344aa33bc94a536b63b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 815643f34c5d95f2ed47ae5ebdec2dd31c7da83ae1ded589efebf6409789bf30f002315d94468a5e8c14f2f64412fbae025f8a0dec7c057a66826417282a0ca9
|
|
7
|
+
data.tar.gz: 500c291826c51db9bfad4d3f222052a1b0e3c2cd59260a7db2f9ee58368d5574734cf58db92c42ec6aa34f1c5a5657d57919e8e1df2c97b05da4b4e69bb9d384
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.3.
|
|
1
|
+
2.3.8
|
data/.travis.yml
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
language: ruby
|
|
2
2
|
|
|
3
|
+
dist: trusty
|
|
4
|
+
|
|
3
5
|
rvm:
|
|
4
6
|
- 1.8.7
|
|
5
7
|
- 2.1.8
|
|
6
|
-
- 2.3.
|
|
8
|
+
- 2.3.8
|
|
7
9
|
- 2.4.2
|
|
8
10
|
- 2.5.3
|
|
9
11
|
|
|
@@ -21,13 +23,13 @@ matrix:
|
|
|
21
23
|
- gemfile: Gemfile.2.3
|
|
22
24
|
rvm: 2.1.8
|
|
23
25
|
- gemfile: Gemfile.2.3
|
|
24
|
-
rvm: 2.3.
|
|
26
|
+
rvm: 2.3.8
|
|
25
27
|
- gemfile: Gemfile.2.3
|
|
26
28
|
rvm: 2.4.2
|
|
27
29
|
- gemfile: Gemfile.2.3
|
|
28
30
|
rvm: 2.5.3
|
|
29
31
|
- gemfile: Gemfile.3.2
|
|
30
|
-
rvm: 2.3.
|
|
32
|
+
rvm: 2.3.8
|
|
31
33
|
- gemfile: Gemfile.3.2
|
|
32
34
|
rvm: 2.4.2
|
|
33
35
|
- gemfile: Gemfile.3.2
|
|
@@ -55,7 +57,7 @@ matrix:
|
|
|
55
57
|
- gemfile: Gemfile.6.0.pg
|
|
56
58
|
rvm: 2.1.8
|
|
57
59
|
- gemfile: Gemfile.6.0.pg
|
|
58
|
-
rvm: 2.3.
|
|
60
|
+
rvm: 2.3.8
|
|
59
61
|
- gemfile: Gemfile.6.0.pg
|
|
60
62
|
rvm: 2.4.2
|
|
61
63
|
|
|
@@ -66,17 +68,13 @@ sudo: false
|
|
|
66
68
|
|
|
67
69
|
cache: bundler
|
|
68
70
|
|
|
69
|
-
notifications:
|
|
70
|
-
email:
|
|
71
|
-
- fail@makandra.de
|
|
72
|
-
|
|
73
71
|
before_script:
|
|
74
72
|
- psql -c 'create database assignable_values_test;' -U postgres
|
|
75
73
|
- mysql -e 'create database IF NOT EXISTS assignable_values_test;'
|
|
76
74
|
|
|
77
75
|
install:
|
|
78
76
|
# Old Travis CI bundler explodes when lockfile version doesn't match recently bumped version
|
|
79
|
-
- gem install bundler --version='=1.
|
|
77
|
+
- gem install bundler --version='=1.17.3'
|
|
80
78
|
# This is the default Travis CI install step
|
|
81
79
|
- bundle install --jobs=3 --retry=3 --deployment --path=${BUNDLE_PATH:-vendor/bundle}
|
|
82
80
|
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
|
|
|
8
8
|
|
|
9
9
|
### Compatible changes
|
|
10
10
|
|
|
11
|
+
## 0.16.2 - 2020-10-06
|
|
12
|
+
|
|
13
|
+
### Compatible changes
|
|
14
|
+
|
|
15
|
+
- when given a scope, do not load all records to memory during validation
|
|
11
16
|
|
|
12
17
|
## 0.16.1 - 2019-05-14
|
|
13
18
|
|
|
@@ -28,7 +33,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
|
|
|
28
33
|
|
|
29
34
|
### Compatible changes
|
|
30
35
|
|
|
31
|
-
- Add `#humanized_values` for the `
|
|
36
|
+
- Add `#humanized_values` for the `multiple: true` case.
|
|
32
37
|
|
|
33
38
|
|
|
34
39
|
## 0.15.0 - 2018-10-26
|
data/Gemfile
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Gemfile.4.2
|
|
1
|
+
./Gemfile.4.2
|
data/Gemfile.2.3.lock
CHANGED
data/Gemfile.3.2.lock
CHANGED
data/Gemfile.4.2.lock
CHANGED
data/Gemfile.5.0.lock
CHANGED
data/Gemfile.5.1.lock
CHANGED
data/Gemfile.5.1.pg.lock
CHANGED
data/Gemfile.6.0.pg
CHANGED
data/Gemfile.6.0.pg.lock
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
assignable_values (0.16.
|
|
4
|
+
assignable_values (0.16.2)
|
|
5
5
|
activerecord (>= 2.3)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
activemodel (6.0.0
|
|
11
|
-
activesupport (= 6.0.0
|
|
12
|
-
activerecord (6.0.0
|
|
13
|
-
activemodel (= 6.0.0
|
|
14
|
-
activesupport (= 6.0.0
|
|
15
|
-
activesupport (6.0.0
|
|
10
|
+
activemodel (6.0.0)
|
|
11
|
+
activesupport (= 6.0.0)
|
|
12
|
+
activerecord (6.0.0)
|
|
13
|
+
activemodel (= 6.0.0)
|
|
14
|
+
activesupport (= 6.0.0)
|
|
15
|
+
activesupport (6.0.0)
|
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
17
17
|
i18n (>= 0.7, < 2)
|
|
18
18
|
minitest (~> 5.1)
|
|
19
19
|
tzinfo (~> 1.1)
|
|
20
|
-
zeitwerk (~> 1
|
|
20
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
|
21
21
|
concurrent-ruby (1.1.5)
|
|
22
22
|
database_cleaner (1.7.0)
|
|
23
23
|
diff-lcs (1.3)
|
|
@@ -48,13 +48,13 @@ GEM
|
|
|
48
48
|
thread_safe (0.3.6)
|
|
49
49
|
tzinfo (1.2.5)
|
|
50
50
|
thread_safe (~> 0.1)
|
|
51
|
-
zeitwerk (1.
|
|
51
|
+
zeitwerk (2.1.9)
|
|
52
52
|
|
|
53
53
|
PLATFORMS
|
|
54
54
|
ruby
|
|
55
55
|
|
|
56
56
|
DEPENDENCIES
|
|
57
|
-
activerecord (~> 6.0.
|
|
57
|
+
activerecord (~> 6.0.0)
|
|
58
58
|
assignable_values!
|
|
59
59
|
database_cleaner
|
|
60
60
|
gemika
|
data/Gemfile.lock
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Gemfile.4.2.lock
|
|
1
|
+
./Gemfile.4.2.lock
|
data/README.md
CHANGED
|
@@ -91,7 +91,7 @@ The default is applied to new records:
|
|
|
91
91
|
Defaults can be procs:
|
|
92
92
|
|
|
93
93
|
class Song < ActiveRecord::Base
|
|
94
|
-
assignable_values_for :
|
|
94
|
+
assignable_values_for :year, default: proc { Date.today.year } do
|
|
95
95
|
1980 .. 2011
|
|
96
96
|
end
|
|
97
97
|
end
|
|
@@ -205,7 +205,7 @@ You can restrict `belongs_to` associations in the same manner as scalar attribut
|
|
|
205
205
|
|
|
206
206
|
end
|
|
207
207
|
|
|
208
|
-
Listing and validating
|
|
208
|
+
Listing and validating also works the same:
|
|
209
209
|
|
|
210
210
|
chicane = Artist.create!(name: 'Chicane', signed: true)
|
|
211
211
|
lt2 = Artist.create!(name: 'LT2', signed: false)
|
|
@@ -349,9 +349,12 @@ Development
|
|
|
349
349
|
|
|
350
350
|
There are tests in `spec`. We only accept PRs with tests. To run tests:
|
|
351
351
|
|
|
352
|
-
- Install Ruby 2.
|
|
353
|
-
- Create a local test database `assignable_values_test` in both MySQL and PostgreSQL
|
|
354
|
-
|
|
352
|
+
- Install Ruby 2.3.8
|
|
353
|
+
- Create a local test database `assignable_values_test` in both MySQL and PostgreSQL (see
|
|
354
|
+
`.travis.yml` for commands to do so)
|
|
355
|
+
- Copy `spec/support/database.sample.yml` to `spec/support/database.yml` and enter your local
|
|
356
|
+
credentials for the test databases (postgres entry is not required if you are using a socket
|
|
357
|
+
connection)
|
|
355
358
|
- Install development dependencies using `bundle install`
|
|
356
359
|
- Run tests using `bundle exec rake current_rspec`
|
|
357
360
|
|
|
@@ -48,25 +48,31 @@ module AssignableValues
|
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def assignable_values(record, options = {})
|
|
51
|
-
|
|
51
|
+
additional_assignable_values = []
|
|
52
52
|
current_values = assignable_values_from_record_or_delegate(record)
|
|
53
53
|
|
|
54
54
|
if options.fetch(:include_old_value, true) && has_previously_saved_value?(record)
|
|
55
55
|
old_value = previously_saved_value(record)
|
|
56
56
|
if @options[:multiple]
|
|
57
57
|
if old_value.is_a?(Array)
|
|
58
|
-
|
|
58
|
+
additional_assignable_values |= old_value
|
|
59
59
|
end
|
|
60
60
|
elsif !old_value.blank? && !current_values.include?(old_value)
|
|
61
|
-
|
|
61
|
+
additional_assignable_values << old_value
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
assignable_values += current_values
|
|
66
65
|
if options[:decorate]
|
|
67
|
-
|
|
66
|
+
current_values = decorate_values(current_values)
|
|
67
|
+
additional_assignable_values = decorate_values(additional_assignable_values)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
if additional_assignable_values.present?
|
|
71
|
+
# will not keep current_values scoped
|
|
72
|
+
additional_assignable_values + current_values
|
|
73
|
+
else
|
|
74
|
+
current_values
|
|
68
75
|
end
|
|
69
|
-
assignable_values
|
|
70
76
|
end
|
|
71
77
|
|
|
72
78
|
private
|
|
@@ -94,7 +100,21 @@ module AssignableValues
|
|
|
94
100
|
def assignable_single_value?(record, value)
|
|
95
101
|
(has_previously_saved_value?(record) && value == previously_saved_value(record)) ||
|
|
96
102
|
(value.blank? && allow_blank?(record)) ||
|
|
97
|
-
|
|
103
|
+
included_in_assignable_values?(record, value)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def included_in_assignable_values?(record, value)
|
|
107
|
+
values_or_scope = assignable_values(record, :include_old_value => false)
|
|
108
|
+
|
|
109
|
+
if is_scope?(values_or_scope)
|
|
110
|
+
values_or_scope.exists?(value.id)
|
|
111
|
+
else
|
|
112
|
+
values_or_scope.include?(value)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def is_scope?(object)
|
|
117
|
+
object.respond_to?(:scoped) || object.respond_to?(:all)
|
|
98
118
|
end
|
|
99
119
|
|
|
100
120
|
def assignable_multi_value?(record, value)
|
|
@@ -217,9 +237,9 @@ module AssignableValues
|
|
|
217
237
|
|
|
218
238
|
def assignable_values_from_record_or_delegate(record)
|
|
219
239
|
if delegate?
|
|
220
|
-
assignable_values_from_delegate(record)
|
|
240
|
+
assignable_values_from_delegate(record)
|
|
221
241
|
else
|
|
222
|
-
record.instance_exec(&@values)
|
|
242
|
+
record.instance_exec(&@values)
|
|
223
243
|
end
|
|
224
244
|
end
|
|
225
245
|
|
|
@@ -448,7 +448,7 @@ describe AssignableValues::ActiveRecord do
|
|
|
448
448
|
record.should be_valid
|
|
449
449
|
end
|
|
450
450
|
|
|
451
|
-
it '
|
|
451
|
+
it 'should not allow nil for an association (the "previously saved value") if the record is new' do
|
|
452
452
|
allowed_association = Artist.create!
|
|
453
453
|
klass = Song.disposable_copy
|
|
454
454
|
record = klass.new(:artist => nil)
|
|
@@ -510,6 +510,47 @@ describe AssignableValues::ActiveRecord do
|
|
|
510
510
|
record.valid?
|
|
511
511
|
end
|
|
512
512
|
|
|
513
|
+
it 'should not load all records to memory when assignable values are scoped' do
|
|
514
|
+
unless ::ActiveRecord::VERSION::MAJOR < 3 # somehow rails 2 still initializes Objects during the scope.exists?-call
|
|
515
|
+
initialized_artists_count = 0
|
|
516
|
+
MyArtist = Artist.disposable_copy
|
|
517
|
+
|
|
518
|
+
MyArtist.class_eval do
|
|
519
|
+
after_initialize :increase_initialized_count
|
|
520
|
+
|
|
521
|
+
define_method :increase_initialized_count do
|
|
522
|
+
initialized_artists_count += 1
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
klass = Song.disposable_copy
|
|
527
|
+
|
|
528
|
+
klass.class_eval do
|
|
529
|
+
assignable_values_for :artist do
|
|
530
|
+
if ::ActiveRecord::VERSION::MAJOR < 4
|
|
531
|
+
MyArtist.scoped
|
|
532
|
+
else
|
|
533
|
+
MyArtist.all
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
artist = MyArtist.create!
|
|
539
|
+
initialized_artists_count.should == 1
|
|
540
|
+
|
|
541
|
+
song = klass.new(:artist => artist)
|
|
542
|
+
|
|
543
|
+
song.valid?
|
|
544
|
+
initialized_artists_count.should == 1
|
|
545
|
+
|
|
546
|
+
song.assignable_artists
|
|
547
|
+
initialized_artists_count.should == 1
|
|
548
|
+
|
|
549
|
+
song.assignable_artists.to_a
|
|
550
|
+
initialized_artists_count.should == 2
|
|
551
|
+
end
|
|
552
|
+
end
|
|
553
|
+
|
|
513
554
|
end
|
|
514
555
|
|
|
515
556
|
context 'when delegating using the :through option' do
|
|
@@ -730,7 +771,7 @@ describe AssignableValues::ActiveRecord do
|
|
|
730
771
|
klass.new.assignable_genres.should == %w[pop rock]
|
|
731
772
|
end
|
|
732
773
|
|
|
733
|
-
it 'should
|
|
774
|
+
it 'should work with ranges' do
|
|
734
775
|
klass = Song.disposable_copy do
|
|
735
776
|
assignable_values_for :year do
|
|
736
777
|
1999..2001
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: assignable_values
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.16.
|
|
4
|
+
version: 0.16.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Henning Koch
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2020-10-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -91,8 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
91
91
|
- !ruby/object:Gem::Version
|
|
92
92
|
version: '0'
|
|
93
93
|
requirements: []
|
|
94
|
-
|
|
95
|
-
rubygems_version: 2.5.1
|
|
94
|
+
rubygems_version: 3.0.3
|
|
96
95
|
signing_key:
|
|
97
96
|
specification_version: 4
|
|
98
97
|
summary: Restrict the values assignable to ActiveRecord attributes or associations
|