active_type 1.9.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +27 -27
- data/.ruby-version +1 -1
- data/CHANGELOG.md +17 -0
- data/Gemfile.4.2.mysql2.lock +2 -2
- data/Gemfile.4.2.pg.lock +2 -2
- data/Gemfile.4.2.sqlite3.lock +2 -2
- data/Gemfile.5.2.mysql2.lock +1 -1
- data/Gemfile.5.2.pg.lock +2 -2
- data/Gemfile.5.2.sqlite3.lock +1 -1
- data/Gemfile.6.0.sqlite3.lock +1 -1
- data/Gemfile.6.1.pg.lock +2 -1
- data/Gemfile.6.1.sqlite3.lock +2 -1
- data/Gemfile.7.0.pg +9 -0
- data/Gemfile.7.0.pg.lock +58 -0
- data/{Gemfile.3.2.sqlite3 → Gemfile.7.0.sqlite3} +1 -2
- data/Gemfile.7.0.sqlite3.lock +58 -0
- data/README.md +7 -5
- data/active_type.gemspec +2 -2
- data/lib/active_type/mutation_after_cast_error.rb +6 -0
- data/lib/active_type/not_castable_error.rb +6 -0
- data/lib/active_type/record_extension/inheritance.rb +1 -1
- data/lib/active_type/util/unmutable_attributes.rb +32 -0
- data/lib/active_type/util.rb +32 -5
- data/lib/active_type/version.rb +1 -1
- metadata +13 -10
- data/Gemfile.3.2.mysql2 +0 -10
- data/Gemfile.3.2.mysql2.lock +0 -63
- data/Gemfile.3.2.sqlite3.lock +0 -57
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ed97bd5057f4193d1ced6df666bc9250c4d57cbf830db65112c7686311ed2e5
|
4
|
+
data.tar.gz: 9b38760b9e205770507223ef9c11aae1813abb078b48e6a9e5bb5210a6fa464b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79d3b07256931a655d9a761485ffeb5a630442a83e13b12d44b519f4f485d5b05485c457273de50e9381ca8beddc9b1d6368c901b5ba121fc7761910b9f3c81a
|
7
|
+
data.tar.gz: 2c36129837464338002cb14c90442654ec7dc3b647ee4c9f01ace6b5611018600edd37927ce9882051336c54a54169cd1e4292e7c7e819e3e914170ea1720de4
|
data/.github/workflows/test.yml
CHANGED
@@ -2,9 +2,9 @@ name: Tests
|
|
2
2
|
|
3
3
|
on:
|
4
4
|
push:
|
5
|
-
branches: [
|
5
|
+
branches: [master]
|
6
6
|
pull_request:
|
7
|
-
branches: [
|
7
|
+
branches: [master]
|
8
8
|
|
9
9
|
jobs:
|
10
10
|
test_sqlite:
|
@@ -14,12 +14,8 @@ jobs:
|
|
14
14
|
fail-fast: false
|
15
15
|
matrix:
|
16
16
|
include:
|
17
|
-
- ruby: "2.
|
18
|
-
gemfile: Gemfile.3.2.sqlite3
|
19
|
-
- ruby: "2.3.8"
|
17
|
+
- ruby: "2.5.5"
|
20
18
|
gemfile: Gemfile.4.2.sqlite3
|
21
|
-
- ruby: "2.3.8"
|
22
|
-
gemfile: Gemfile.5.2.sqlite3
|
23
19
|
- ruby: "2.5.5"
|
24
20
|
gemfile: Gemfile.5.2.sqlite3
|
25
21
|
- ruby: "2.5.5"
|
@@ -32,16 +28,22 @@ jobs:
|
|
32
28
|
gemfile: Gemfile.6.0.sqlite3
|
33
29
|
- ruby: "2.6.3"
|
34
30
|
gemfile: Gemfile.6.1.sqlite3
|
35
|
-
- ruby: "2.
|
31
|
+
- ruby: "2.6.3"
|
32
|
+
gemfile: Gemfile.7.0.sqlite3
|
33
|
+
- ruby: "2.7.4"
|
36
34
|
gemfile: Gemfile.5.2.sqlite3
|
37
|
-
- ruby: "2.7.
|
35
|
+
- ruby: "2.7.4"
|
38
36
|
gemfile: Gemfile.6.0.sqlite3
|
39
|
-
- ruby: "2.7.
|
37
|
+
- ruby: "2.7.4"
|
40
38
|
gemfile: Gemfile.6.1.sqlite3
|
39
|
+
- ruby: "2.7.4"
|
40
|
+
gemfile: Gemfile.7.0.sqlite3
|
41
41
|
- ruby: "3.0.1"
|
42
42
|
gemfile: Gemfile.6.0.sqlite3
|
43
43
|
- ruby: "3.0.1"
|
44
44
|
gemfile: Gemfile.6.1.sqlite3
|
45
|
+
- ruby: "3.0.1"
|
46
|
+
gemfile: Gemfile.7.0.sqlite3
|
45
47
|
|
46
48
|
env:
|
47
49
|
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
@@ -54,12 +56,11 @@ jobs:
|
|
54
56
|
ruby-version: ${{ matrix.ruby }}
|
55
57
|
- name: Bundle
|
56
58
|
run: |
|
57
|
-
gem install bundler:2.
|
59
|
+
gem install bundler:2.2.27
|
58
60
|
bundle install --no-deployment
|
59
61
|
- name: Run tests
|
60
62
|
run: bundle exec rake spec
|
61
63
|
|
62
|
-
|
63
64
|
test_mysql:
|
64
65
|
runs-on: ubuntu-20.04
|
65
66
|
|
@@ -69,7 +70,7 @@ jobs:
|
|
69
70
|
env:
|
70
71
|
MYSQL_ROOT_PASSWORD: password
|
71
72
|
ports:
|
72
|
-
|
73
|
+
- 3306:3306
|
73
74
|
options: >-
|
74
75
|
--health-cmd="mysqladmin ping"
|
75
76
|
--health-interval=10s
|
@@ -80,15 +81,11 @@ jobs:
|
|
80
81
|
fail-fast: false
|
81
82
|
matrix:
|
82
83
|
include:
|
83
|
-
- ruby: "2.
|
84
|
-
gemfile: Gemfile.3.2.mysql2
|
85
|
-
- ruby: "2.3.8"
|
84
|
+
- ruby: "2.5.5"
|
86
85
|
gemfile: Gemfile.4.2.mysql2
|
87
|
-
- ruby: "2.3.8"
|
88
|
-
gemfile: Gemfile.5.2.mysql2
|
89
86
|
- ruby: "2.6.3"
|
90
87
|
gemfile: Gemfile.5.2.mysql2
|
91
|
-
- ruby: "2.7.
|
88
|
+
- ruby: "2.7.4"
|
92
89
|
gemfile: Gemfile.5.2.mysql2
|
93
90
|
|
94
91
|
env:
|
@@ -103,10 +100,10 @@ jobs:
|
|
103
100
|
- name: Install database client
|
104
101
|
run: |
|
105
102
|
sudo apt-get update
|
106
|
-
sudo apt-get install -y
|
103
|
+
sudo apt-get install -y mariadb-client libmariadbclient-dev
|
107
104
|
- name: Bundle
|
108
105
|
run: |
|
109
|
-
gem install bundler:2.
|
106
|
+
gem install bundler:2.2.27
|
110
107
|
bundle install --no-deployment
|
111
108
|
- name: Setup databases
|
112
109
|
run: |
|
@@ -114,7 +111,6 @@ jobs:
|
|
114
111
|
- name: Run tests
|
115
112
|
run: bundle exec rake spec
|
116
113
|
|
117
|
-
|
118
114
|
test_pg:
|
119
115
|
runs-on: ubuntu-20.04
|
120
116
|
|
@@ -135,9 +131,9 @@ jobs:
|
|
135
131
|
fail-fast: false
|
136
132
|
matrix:
|
137
133
|
include:
|
138
|
-
- ruby: "2.
|
134
|
+
- ruby: "2.5.5"
|
139
135
|
gemfile: Gemfile.4.2.pg
|
140
|
-
- ruby: "2.
|
136
|
+
- ruby: "2.5.5"
|
141
137
|
gemfile: Gemfile.5.2.pg
|
142
138
|
- ruby: "2.5.5"
|
143
139
|
gemfile: Gemfile.6.1.pg
|
@@ -145,12 +141,16 @@ jobs:
|
|
145
141
|
gemfile: Gemfile.5.2.pg
|
146
142
|
- ruby: "2.6.3"
|
147
143
|
gemfile: Gemfile.6.1.pg
|
148
|
-
- ruby: "2.7.
|
144
|
+
- ruby: "2.7.4"
|
149
145
|
gemfile: Gemfile.5.2.pg
|
150
|
-
- ruby: "2.7.
|
146
|
+
- ruby: "2.7.4"
|
151
147
|
gemfile: Gemfile.6.1.pg
|
148
|
+
- ruby: "2.7.4"
|
149
|
+
gemfile: Gemfile.7.0.pg
|
152
150
|
- ruby: "3.0.1"
|
153
151
|
gemfile: Gemfile.6.1.pg
|
152
|
+
- ruby: "3.0.1"
|
153
|
+
gemfile: Gemfile.7.0.pg
|
154
154
|
|
155
155
|
env:
|
156
156
|
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
@@ -166,7 +166,7 @@ jobs:
|
|
166
166
|
sudo apt-get install -y postgresql-client
|
167
167
|
- name: Bundle
|
168
168
|
run: |
|
169
|
-
gem install bundler:2.
|
169
|
+
gem install bundler:2.2.27
|
170
170
|
bundle install --no-deployment
|
171
171
|
- name: Setup databases
|
172
172
|
run: |
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.4
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
|
5
5
|
## Unreleased changes
|
6
6
|
|
7
|
+
## 2.1.0 (2021-12-22)
|
8
|
+
|
9
|
+
* Fixed: ActiveType now works for Rails 7.
|
10
|
+
|
11
|
+
## 2.0.0 (2021-11-24)
|
12
|
+
|
13
|
+
* Added: Casting is prevented when the base record has changes in its already loaded associations, because those would be lost. Option `force: true` can be used to override this and still do the cast (this is not recommended).
|
14
|
+
* Added: After casting, the base record will not be usable any more. The base record and the newly created casted record share state which is unexpected. Option `force: true` can be used to override this, so the base record is still usable (this is not recommended).
|
15
|
+
|
16
|
+
## 1.10.1 (2021-10-19)
|
17
|
+
|
18
|
+
* Removed: When casting an unsaved record, the new record will no longer have the same associations as the base record. This was introduced with version 1.8.0 and lead to issues (#147 and #148). Therefore the change was rolled back.
|
19
|
+
|
20
|
+
## 1.10.0 (2021-08-11)
|
21
|
+
|
22
|
+
* Removed: Tests for Ruby 2.4 and ActiveRecord 3.2.
|
23
|
+
|
7
24
|
## 1.9.1 (2021-06-29)
|
8
25
|
|
9
26
|
* Fixed: Retain `#mutations_from_database` when using ActiveType.cast (Rails 5.2+). Thanks to @unrooty-infinum.
|
data/Gemfile.4.2.mysql2.lock
CHANGED
data/Gemfile.4.2.pg.lock
CHANGED
data/Gemfile.4.2.sqlite3.lock
CHANGED
data/Gemfile.5.2.mysql2.lock
CHANGED
data/Gemfile.5.2.pg.lock
CHANGED
data/Gemfile.5.2.sqlite3.lock
CHANGED
data/Gemfile.6.0.sqlite3.lock
CHANGED
data/Gemfile.6.1.pg.lock
CHANGED
data/Gemfile.6.1.sqlite3.lock
CHANGED
data/Gemfile.7.0.pg
ADDED
data/Gemfile.7.0.pg.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_type (2.0)
|
5
|
+
activerecord (>= 3.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.1.4.4)
|
11
|
+
activesupport (= 6.1.4.4)
|
12
|
+
activerecord (6.1.4.4)
|
13
|
+
activemodel (= 6.1.4.4)
|
14
|
+
activesupport (= 6.1.4.4)
|
15
|
+
activesupport (6.1.4.4)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
concurrent-ruby (1.1.9)
|
22
|
+
diff-lcs (1.4.4)
|
23
|
+
gemika (0.6.1)
|
24
|
+
i18n (1.8.11)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.15.0)
|
27
|
+
pg (1.2.3)
|
28
|
+
rake (13.0.6)
|
29
|
+
rspec (3.10.0)
|
30
|
+
rspec-core (~> 3.10.0)
|
31
|
+
rspec-expectations (~> 3.10.0)
|
32
|
+
rspec-mocks (~> 3.10.0)
|
33
|
+
rspec-core (3.10.1)
|
34
|
+
rspec-support (~> 3.10.0)
|
35
|
+
rspec-expectations (3.10.1)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.10.0)
|
38
|
+
rspec-mocks (3.10.2)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.10.0)
|
41
|
+
rspec-support (3.10.3)
|
42
|
+
tzinfo (2.0.4)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
zeitwerk (2.5.1)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
active_type!
|
51
|
+
activerecord (~> 6.1.0)
|
52
|
+
gemika
|
53
|
+
pg
|
54
|
+
rake
|
55
|
+
rspec (~> 3.4)
|
56
|
+
|
57
|
+
BUNDLED WITH
|
58
|
+
2.1.4
|
@@ -1,10 +1,9 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem 'activerecord', '~>
|
3
|
+
gem 'activerecord', '~>6.1.0'
|
4
4
|
gem 'rspec', '~>3.4'
|
5
5
|
gem 'sqlite3'
|
6
6
|
gem 'rake'
|
7
7
|
gem 'gemika'
|
8
8
|
|
9
9
|
gem 'active_type', :path => '.'
|
10
|
-
gem 'i18n', '=0.6.11' # 0.7 no longer builds for Ruby 1.8.7
|
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_type (2.0)
|
5
|
+
activerecord (>= 3.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.1.4.4)
|
11
|
+
activesupport (= 6.1.4.4)
|
12
|
+
activerecord (6.1.4.4)
|
13
|
+
activemodel (= 6.1.4.4)
|
14
|
+
activesupport (= 6.1.4.4)
|
15
|
+
activesupport (6.1.4.4)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
concurrent-ruby (1.1.9)
|
22
|
+
diff-lcs (1.4.4)
|
23
|
+
gemika (0.6.1)
|
24
|
+
i18n (1.8.11)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.15.0)
|
27
|
+
rake (13.0.6)
|
28
|
+
rspec (3.10.0)
|
29
|
+
rspec-core (~> 3.10.0)
|
30
|
+
rspec-expectations (~> 3.10.0)
|
31
|
+
rspec-mocks (~> 3.10.0)
|
32
|
+
rspec-core (3.10.1)
|
33
|
+
rspec-support (~> 3.10.0)
|
34
|
+
rspec-expectations (3.10.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.10.0)
|
37
|
+
rspec-mocks (3.10.2)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.10.0)
|
40
|
+
rspec-support (3.10.3)
|
41
|
+
sqlite3 (1.4.2)
|
42
|
+
tzinfo (2.0.4)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
zeitwerk (2.5.1)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
active_type!
|
51
|
+
activerecord (~> 6.1.0)
|
52
|
+
gemika
|
53
|
+
rake
|
54
|
+
rspec (~> 3.4)
|
55
|
+
sqlite3
|
56
|
+
|
57
|
+
BUNDLED WITH
|
58
|
+
2.1.4
|
data/README.md
CHANGED
@@ -53,7 +53,7 @@ end
|
|
53
53
|
|
54
54
|
### A note on Rails 5+
|
55
55
|
|
56
|
-
Rails 5 comes with its own implementation of `.attribute`. This implementation is functionally very
|
56
|
+
Rails 5+ comes with its own implementation of `.attribute`. This implementation is functionally very
|
57
57
|
similar, but not identical to ActiveType's.
|
58
58
|
|
59
59
|
We have decided to continue to use our own implementation. This means that if you use ActiveType, `ActiveRecord::Base.attribute` will be overriden.
|
@@ -364,8 +364,10 @@ sign_up.is_a?(SignUp) # => true
|
|
364
364
|
This is basically like [`ActiveRecord#becomes`](http://apidock.com/rails/v4.2.1/ActiveRecord/Persistence/becomes), but with less bugs and more consistent behavior.
|
365
365
|
|
366
366
|
**Note that `cast` is destructive.** The originally casted record (`user`) and the returned record (`sign_up`)
|
367
|
-
share internal state (such as attributes). To avoid unexpected behavior,
|
368
|
-
|
367
|
+
share internal state (such as attributes). To avoid unexpected behavior, the original record will raise an error when trying to change or persist it. Also, casting of a record that has changes in its loaded associations is prevented, because those changes would be lost.
|
368
|
+
If you know what you are doing and absolutely want that, you may use the option `force: true` to allow this potentially problematic behaviour, e.g. `sign_up = ActiveType.cast(user, SignUp, force: true)`
|
369
|
+
|
370
|
+
|
369
371
|
|
370
372
|
You can also cast an entire relation (scope) to a relation of an `ActiveType::Record`:
|
371
373
|
|
@@ -405,14 +407,14 @@ Now, if you load `credentials`, you will automatically receive records of type `
|
|
405
407
|
Supported Rails versions
|
406
408
|
------------------------
|
407
409
|
|
408
|
-
ActiveType is tested against ActiveRecord
|
410
|
+
ActiveType is tested against ActiveRecord 4.2, 5.1, 5.2, 6.0 and 6.1.
|
409
411
|
|
410
412
|
Later versions might work, earlier will not.
|
411
413
|
|
412
414
|
Supported Ruby versions
|
413
415
|
------------------------
|
414
416
|
|
415
|
-
ActiveType is tested against 2.
|
417
|
+
ActiveType is tested against 2.5, 2.6, 2.7 and 3.0.
|
416
418
|
|
417
419
|
|
418
420
|
Installation
|
data/active_type.gemspec
CHANGED
@@ -16,10 +16,10 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
|
-
s.add_development_dependency "bundler", "
|
19
|
+
s.add_development_dependency "bundler", ">= 1.5"
|
20
20
|
s.add_development_dependency "rake"
|
21
21
|
|
22
22
|
s.add_runtime_dependency('activerecord', '>= 3.2')
|
23
23
|
|
24
|
-
s.required_ruby_version = '>=
|
24
|
+
s.required_ruby_version = '>= 2.5.0'
|
25
25
|
end
|
@@ -120,7 +120,7 @@ module ActiveType
|
|
120
120
|
type_name = base_class.type_for_attribute(inheritance_column).cast(type_name)
|
121
121
|
subclass = begin
|
122
122
|
if store_full_sti_class
|
123
|
-
|
123
|
+
type_name.constantize
|
124
124
|
else
|
125
125
|
compute_type(type_name)
|
126
126
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_type/mutation_after_cast_error'
|
2
|
+
|
3
|
+
module ActiveType
|
4
|
+
module Util
|
5
|
+
|
6
|
+
# This object is used as a substitute for a record's @attributes.
|
7
|
+
# Reading from the original @attributes is still allowed, to enable
|
8
|
+
# `#inspect` and similar functions.
|
9
|
+
# But the @attributes can no longer be mutated and will raise instead.
|
10
|
+
class UnmutableAttributes
|
11
|
+
|
12
|
+
attr_reader :original_attributes
|
13
|
+
|
14
|
+
def initialize(attributes)
|
15
|
+
@original_attributes = attributes
|
16
|
+
end
|
17
|
+
|
18
|
+
def fetch_value(key)
|
19
|
+
original_attributes.fetch_value(key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def key?(key)
|
23
|
+
original_attributes.key?(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(*args)
|
27
|
+
raise MutationAfterCastError, 'Changing a record that has been used to create an ActiveType::Record could have unexpected side effects!'
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/active_type/util.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
+
require 'active_type/not_castable_error'
|
2
|
+
require 'active_type/util/unmutable_attributes'
|
3
|
+
|
1
4
|
module ActiveType
|
2
5
|
module Util
|
3
6
|
|
4
|
-
def cast(object, klass)
|
7
|
+
def cast(object, klass, force: false)
|
5
8
|
if object.is_a?(ActiveRecord::Relation)
|
6
9
|
cast_relation(object, klass)
|
7
10
|
elsif object.is_a?(ActiveRecord::Base)
|
8
|
-
cast_record(object, klass)
|
11
|
+
cast_record(object, klass, force: force)
|
9
12
|
else
|
10
13
|
raise ArgumentError, "Don't know how to cast #{object.inspect}"
|
11
14
|
end
|
@@ -17,7 +20,11 @@ module ActiveType
|
|
17
20
|
|
18
21
|
private
|
19
22
|
|
20
|
-
def cast_record(record, klass)
|
23
|
+
def cast_record(record, klass, force: false)
|
24
|
+
if associations_touched?(record) && !force
|
25
|
+
raise NotCastableError, 'Record has changes in its loaded associations!'
|
26
|
+
end
|
27
|
+
|
21
28
|
# record.becomes(klass).dup
|
22
29
|
klass.new do |casted|
|
23
30
|
using_single_table_inheritance = using_single_table_inheritance?(klass, casted)
|
@@ -37,8 +44,6 @@ module ActiveType
|
|
37
44
|
# Rails 5.2+
|
38
45
|
casted.instance_variable_set(:@mutations_from_database, record.instance_variable_get(:@mutations_from_database))
|
39
46
|
|
40
|
-
casted.instance_variable_set(:@association_cache, record.instance_variable_get(:@association_cache))
|
41
|
-
|
42
47
|
# Rails 3.2, 4.2
|
43
48
|
errors = record.errors
|
44
49
|
if errors.kind_of? ActiveModel::Errors
|
@@ -50,6 +55,11 @@ module ActiveType
|
|
50
55
|
casted.instance_variable_set(:@errors, errors)
|
51
56
|
|
52
57
|
casted[klass.inheritance_column] = klass.sti_name if using_single_table_inheritance
|
58
|
+
|
59
|
+
if !force
|
60
|
+
make_record_unusable(record)
|
61
|
+
end
|
62
|
+
casted
|
53
63
|
end
|
54
64
|
end
|
55
65
|
|
@@ -63,6 +73,23 @@ module ActiveType
|
|
63
73
|
scoped(klass).merge(scoped(relation))
|
64
74
|
end
|
65
75
|
|
76
|
+
def associations_touched?(record)
|
77
|
+
return false unless record.instance_variable_get(:@association_cache)
|
78
|
+
|
79
|
+
!!record.instance_variable_get(:@association_cache)[:associated_records]&.target&.any? do |target|
|
80
|
+
target.changed?
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def make_record_unusable(record)
|
85
|
+
# Changing and saving the base record may lead to unexpected behaviour,
|
86
|
+
# since the casted record may have different changes in its autosave
|
87
|
+
# associations and will be saved to the same record in the database as
|
88
|
+
# the casted record. Therefore we prevent that.
|
89
|
+
original_attributes = record.instance_variable_get(:@attributes)
|
90
|
+
record.instance_variable_set(:@attributes, UnmutableAttributes.new(original_attributes) )
|
91
|
+
end
|
92
|
+
|
66
93
|
extend self
|
67
94
|
|
68
95
|
end
|
data/lib/active_type/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Kraze
|
@@ -9,20 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-12-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '1.5'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '1.5'
|
28
28
|
- !ruby/object:Gem::Dependency
|
@@ -65,10 +65,6 @@ files:
|
|
65
65
|
- ".ruby-version"
|
66
66
|
- CHANGELOG.md
|
67
67
|
- Gemfile
|
68
|
-
- Gemfile.3.2.mysql2
|
69
|
-
- Gemfile.3.2.mysql2.lock
|
70
|
-
- Gemfile.3.2.sqlite3
|
71
|
-
- Gemfile.3.2.sqlite3.lock
|
72
68
|
- Gemfile.4.2.mysql2
|
73
69
|
- Gemfile.4.2.mysql2.lock
|
74
70
|
- Gemfile.4.2.pg
|
@@ -87,6 +83,10 @@ files:
|
|
87
83
|
- Gemfile.6.1.pg.lock
|
88
84
|
- Gemfile.6.1.sqlite3
|
89
85
|
- Gemfile.6.1.sqlite3.lock
|
86
|
+
- Gemfile.7.0.pg
|
87
|
+
- Gemfile.7.0.pg.lock
|
88
|
+
- Gemfile.7.0.sqlite3
|
89
|
+
- Gemfile.7.0.sqlite3.lock
|
90
90
|
- Gemfile.lock
|
91
91
|
- LICENSE
|
92
92
|
- README.md
|
@@ -94,18 +94,21 @@ files:
|
|
94
94
|
- active_type.gemspec
|
95
95
|
- lib/active_type.rb
|
96
96
|
- lib/active_type/change_association.rb
|
97
|
+
- lib/active_type/mutation_after_cast_error.rb
|
97
98
|
- lib/active_type/nested_attributes.rb
|
98
99
|
- lib/active_type/nested_attributes/association.rb
|
99
100
|
- lib/active_type/nested_attributes/builder.rb
|
100
101
|
- lib/active_type/nested_attributes/nests_many_association.rb
|
101
102
|
- lib/active_type/nested_attributes/nests_one_association.rb
|
102
103
|
- lib/active_type/no_table.rb
|
104
|
+
- lib/active_type/not_castable_error.rb
|
103
105
|
- lib/active_type/object.rb
|
104
106
|
- lib/active_type/record.rb
|
105
107
|
- lib/active_type/record_extension.rb
|
106
108
|
- lib/active_type/record_extension/inheritance.rb
|
107
109
|
- lib/active_type/type_caster.rb
|
108
110
|
- lib/active_type/util.rb
|
111
|
+
- lib/active_type/util/unmutable_attributes.rb
|
109
112
|
- lib/active_type/version.rb
|
110
113
|
- lib/active_type/virtual_attributes.rb
|
111
114
|
homepage: https://github.com/makandra/active_type
|
@@ -120,14 +123,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
123
|
requirements:
|
121
124
|
- - ">="
|
122
125
|
- !ruby/object:Gem::Version
|
123
|
-
version:
|
126
|
+
version: 2.5.0
|
124
127
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
128
|
requirements:
|
126
129
|
- - ">="
|
127
130
|
- !ruby/object:Gem::Version
|
128
131
|
version: '0'
|
129
132
|
requirements: []
|
130
|
-
rubygems_version: 3.
|
133
|
+
rubygems_version: 3.2.6
|
131
134
|
signing_key:
|
132
135
|
specification_version: 4
|
133
136
|
summary: Make any Ruby object quack like ActiveRecord
|
data/Gemfile.3.2.mysql2
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'activerecord', '~> 3.2.22.5'
|
4
|
-
gem 'rspec', '~> 3.4'
|
5
|
-
gem 'mysql2', git: 'https://github.com/makandra/mysql2', branch: '0.3.x-lts'
|
6
|
-
gem 'rake'
|
7
|
-
gem 'gemika'
|
8
|
-
|
9
|
-
gem 'active_type', :path => '.'
|
10
|
-
gem 'i18n', '=0.6.11' # 0.7 no longer builds for Ruby 1.8.7
|
data/Gemfile.3.2.mysql2.lock
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
GIT
|
2
|
-
remote: https://github.com/makandra/mysql2
|
3
|
-
revision: c920d41e43c4722d4c065d2ea9d21494c560bd85
|
4
|
-
branch: 0.3.x-lts
|
5
|
-
specs:
|
6
|
-
mysql2 (0.3.21)
|
7
|
-
|
8
|
-
PATH
|
9
|
-
remote: .
|
10
|
-
specs:
|
11
|
-
active_type (1.6.0)
|
12
|
-
activerecord (>= 3.2)
|
13
|
-
|
14
|
-
GEM
|
15
|
-
remote: https://rubygems.org/
|
16
|
-
specs:
|
17
|
-
activemodel (3.2.22.5)
|
18
|
-
activesupport (= 3.2.22.5)
|
19
|
-
builder (~> 3.0.0)
|
20
|
-
activerecord (3.2.22.5)
|
21
|
-
activemodel (= 3.2.22.5)
|
22
|
-
activesupport (= 3.2.22.5)
|
23
|
-
arel (~> 3.0.2)
|
24
|
-
tzinfo (~> 0.3.29)
|
25
|
-
activesupport (3.2.22.5)
|
26
|
-
i18n (~> 0.6, >= 0.6.4)
|
27
|
-
multi_json (~> 1.0)
|
28
|
-
arel (3.0.3)
|
29
|
-
builder (3.0.4)
|
30
|
-
diff-lcs (1.2.5)
|
31
|
-
gemika (0.4.1)
|
32
|
-
i18n (0.6.11)
|
33
|
-
multi_json (1.13.1)
|
34
|
-
rake (10.4.2)
|
35
|
-
rspec (3.4.0)
|
36
|
-
rspec-core (~> 3.4.0)
|
37
|
-
rspec-expectations (~> 3.4.0)
|
38
|
-
rspec-mocks (~> 3.4.0)
|
39
|
-
rspec-core (3.4.1)
|
40
|
-
rspec-support (~> 3.4.0)
|
41
|
-
rspec-expectations (3.4.0)
|
42
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
-
rspec-support (~> 3.4.0)
|
44
|
-
rspec-mocks (3.4.1)
|
45
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.4.0)
|
47
|
-
rspec-support (3.4.1)
|
48
|
-
tzinfo (0.3.55)
|
49
|
-
|
50
|
-
PLATFORMS
|
51
|
-
ruby
|
52
|
-
|
53
|
-
DEPENDENCIES
|
54
|
-
active_type!
|
55
|
-
activerecord (~> 3.2.22.5)
|
56
|
-
gemika
|
57
|
-
i18n (= 0.6.11)
|
58
|
-
mysql2!
|
59
|
-
rake
|
60
|
-
rspec (~> 3.4)
|
61
|
-
|
62
|
-
BUNDLED WITH
|
63
|
-
2.1.4
|
data/Gemfile.3.2.sqlite3.lock
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
active_type (1.9.1)
|
5
|
-
activerecord (>= 3.2)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
activemodel (3.2.22.5)
|
11
|
-
activesupport (= 3.2.22.5)
|
12
|
-
builder (~> 3.0.0)
|
13
|
-
activerecord (3.2.22.5)
|
14
|
-
activemodel (= 3.2.22.5)
|
15
|
-
activesupport (= 3.2.22.5)
|
16
|
-
arel (~> 3.0.2)
|
17
|
-
tzinfo (~> 0.3.29)
|
18
|
-
activesupport (3.2.22.5)
|
19
|
-
i18n (~> 0.6, >= 0.6.4)
|
20
|
-
multi_json (~> 1.0)
|
21
|
-
arel (3.0.3)
|
22
|
-
builder (3.0.4)
|
23
|
-
diff-lcs (1.2.5)
|
24
|
-
gemika (0.4.1)
|
25
|
-
i18n (0.6.11)
|
26
|
-
multi_json (1.13.1)
|
27
|
-
rake (10.4.2)
|
28
|
-
rspec (3.4.0)
|
29
|
-
rspec-core (~> 3.4.0)
|
30
|
-
rspec-expectations (~> 3.4.0)
|
31
|
-
rspec-mocks (~> 3.4.0)
|
32
|
-
rspec-core (3.4.1)
|
33
|
-
rspec-support (~> 3.4.0)
|
34
|
-
rspec-expectations (3.4.0)
|
35
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
-
rspec-support (~> 3.4.0)
|
37
|
-
rspec-mocks (3.4.1)
|
38
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
-
rspec-support (~> 3.4.0)
|
40
|
-
rspec-support (3.4.1)
|
41
|
-
sqlite3 (1.3.11)
|
42
|
-
tzinfo (0.3.55)
|
43
|
-
|
44
|
-
PLATFORMS
|
45
|
-
ruby
|
46
|
-
|
47
|
-
DEPENDENCIES
|
48
|
-
active_type!
|
49
|
-
activerecord (~> 3.2.22.5)
|
50
|
-
gemika
|
51
|
-
i18n (= 0.6.11)
|
52
|
-
rake
|
53
|
-
rspec (~> 3.4)
|
54
|
-
sqlite3
|
55
|
-
|
56
|
-
BUNDLED WITH
|
57
|
-
2.1.4
|