active_type 0.4.0 → 0.4.1
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 +4 -4
- data/.travis.yml +8 -6
- data/CHANGELOG.md +49 -0
- data/README.md +3 -3
- data/Rakefile +0 -1
- data/gemfiles/Gemfile.3.2 +1 -0
- data/gemfiles/Gemfile.3.2.lock +2 -1
- data/lib/active_type/version.rb +1 -1
- data/lib/active_type/virtual_attributes.rb +18 -0
- data/spec/active_type/object_spec.rb +4 -0
- data/spec/active_type/record_spec.rb +4 -0
- data/spec/active_type/util_spec.rb +12 -12
- data/spec/shared_examples/dirty_tracking.rb +31 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 873fb9515c20f974ad20b3cd2b8f4fd787aa9649
|
4
|
+
data.tar.gz: 9fd4f0f639d4189fcd82778d94a63315733376a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6f51d7a8632c8701c5a1dc100aeadd769c0e646774533b3b4c459139eaa81a6144fca448d242c9da75dbb5968f1e2ef8a8eda034836a8b942ff6eb567bd0d2e
|
7
|
+
data.tar.gz: 1a581ee2b4c8dbab6f9d3be522f7a256b877dff9c1a98e7bef1ef294e4a154d48cbd09acc790425d73aa4844865b8902e90d82c520da6638094da1a58d7ba81e
|
data/.travis.yml
CHANGED
@@ -9,8 +9,8 @@ gemfile:
|
|
9
9
|
- gemfiles/Gemfile.3.2
|
10
10
|
- gemfiles/Gemfile.4.0
|
11
11
|
- gemfiles/Gemfile.4.1
|
12
|
-
- gemfiles/Gemfile.4.2
|
13
|
-
- gemfiles/Gemfile.4.2.pg
|
12
|
+
- gemfiles/Gemfile.4.2.1
|
13
|
+
- gemfiles/Gemfile.4.2.1.pg
|
14
14
|
before_script:
|
15
15
|
- psql -c 'create database active_type_test;' -U postgres
|
16
16
|
script: bundle exec rspec spec
|
@@ -27,17 +27,19 @@ matrix:
|
|
27
27
|
- rvm: "1.8.7"
|
28
28
|
gemfile: gemfiles/Gemfile.4.1
|
29
29
|
- rvm: "1.8.7"
|
30
|
-
gemfile: gemfiles/Gemfile.4.2
|
30
|
+
gemfile: gemfiles/Gemfile.4.2.1
|
31
31
|
- rvm: "1.8.7"
|
32
|
-
gemfile: gemfiles/Gemfile.4.2.pg
|
32
|
+
gemfile: gemfiles/Gemfile.4.2.1.pg
|
33
33
|
- rvm: "1.9.3"
|
34
|
-
gemfile: gemfiles/Gemfile.4.2.pg
|
34
|
+
gemfile: gemfiles/Gemfile.4.2.1.pg
|
35
35
|
- rvm: "2.0.0"
|
36
|
-
gemfile: gemfiles/Gemfile.4.2.pg
|
36
|
+
gemfile: gemfiles/Gemfile.4.2.1.pg
|
37
|
+
|
37
38
|
- rvm: "2.1.2"
|
38
39
|
gemfile: gemfiles/Gemfile.3.2
|
39
40
|
- rvm: "2.1.2"
|
40
41
|
gemfile: gemfiles/Gemfile.4.0
|
42
|
+
|
41
43
|
- rvm: "2.2.0"
|
42
44
|
gemfile: gemfiles/Gemfile.3.2
|
43
45
|
- rvm: "2.2.0"
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# ActiveType Change Log
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
ActiveType is in a pre-1.0 state. This means that its APIs and behavior are subject to breaking changes without deprecation notices. Until 1.0, version numbers will follow a [Semver][]-ish `0.y.z` format, where `y` is incremented when new features or breaking changes are introduced, and `z` is incremented for lesser changes or bug fixes.
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
* Your contribution here!
|
10
|
+
|
11
|
+
## [0.4.0][] (2015-06-12)
|
12
|
+
|
13
|
+
* Add ActiveType.cast to cast ActiveRecord instances and relations to extended models
|
14
|
+
|
15
|
+
## [0.3.5][] (2015-06-11)
|
16
|
+
|
17
|
+
* Make gem crash during loading with ActiveRecord 4.2.0 because [#31](https://github.com/makandra/active_type/issues/31)
|
18
|
+
|
19
|
+
## [0.3.4][] (2015-03-14)
|
20
|
+
|
21
|
+
* Support belongs_to associations for ActiveRecord 4.2.1
|
22
|
+
* Ensure that ActiveType::Object correctly validates boolean attributes (issue [#34](https://github.com/makandra/active_type/issues/34))
|
23
|
+
|
24
|
+
## [0.3.3][] (2015-01-23)
|
25
|
+
|
26
|
+
* Don't crash for database types without casting rules (fixes [#25](https://github.com/makandra/active_type/issues/25))
|
27
|
+
|
28
|
+
## [0.3.2][] (2015-01-22)
|
29
|
+
|
30
|
+
* Making the gem to work with Rails version 4.0.0
|
31
|
+
* Use native database type for type casting in pg
|
32
|
+
|
33
|
+
## [0.3.1][] (2014-11-19)
|
34
|
+
|
35
|
+
* Support nested attributes in extended records (fixes [#17](https://github.com/makandra/active_type/issues/17))
|
36
|
+
|
37
|
+
## [0.3.0][] (2014-09-23)
|
38
|
+
|
39
|
+
* Add support for Rails 4.2beta
|
40
|
+
|
41
|
+
[Semver]: http://semver.org
|
42
|
+
[Unreleased]: https://github.com/makandra/active_type/compare/v0.4.0...HEAD
|
43
|
+
[0.4.0]: https://github.com/makandra/active_type/compare/v0.3.5...v0.4.0
|
44
|
+
[0.3.5]: https://github.com/makandra/active_type/compare/v0.3.4...v0.3.5
|
45
|
+
[0.3.4]: https://github.com/makandra/active_type/compare/v0.3.3...v0.3.4
|
46
|
+
[0.3.3]: https://github.com/makandra/active_type/compare/v0.3.2...v0.3.3
|
47
|
+
[0.3.2]: https://github.com/makandra/active_type/compare/v0.3.1...v0.3.2
|
48
|
+
[0.3.1]: https://github.com/makandra/active_type/compare/v0.3.0...v0.3.1
|
49
|
+
[0.3.0]: https://github.com/makandra/active_type/compare/v0.2.1...v0.3.0
|
data/README.md
CHANGED
@@ -271,8 +271,8 @@ Supported options for `nests_many` / `nests_one` are:
|
|
271
271
|
```
|
272
272
|
|
273
273
|
|
274
|
-
Casting
|
275
|
-
|
274
|
+
Casting records or relations
|
275
|
+
----------------------------
|
276
276
|
|
277
277
|
When working with ActiveType you will often find it useful to cast an ActiveRecord instance to its extended `ActiveType::Record` variant.
|
278
278
|
|
@@ -288,7 +288,7 @@ class SignUp < ActiveType::Record[User]
|
|
288
288
|
end
|
289
289
|
|
290
290
|
user = User.find(1)
|
291
|
-
sign_up = ActiveType.cast(SignUp)
|
291
|
+
sign_up = ActiveType.cast(user, SignUp)
|
292
292
|
sign_up.is_a?(SignUp) # => true
|
293
293
|
```
|
294
294
|
|
data/Rakefile
CHANGED
data/gemfiles/Gemfile.3.2
CHANGED
data/gemfiles/Gemfile.3.2.lock
CHANGED
@@ -21,7 +21,7 @@ GEM
|
|
21
21
|
arel (3.0.3)
|
22
22
|
builder (3.0.4)
|
23
23
|
diff-lcs (1.2.5)
|
24
|
-
i18n (0.
|
24
|
+
i18n (0.6.11)
|
25
25
|
multi_json (1.11.1)
|
26
26
|
rspec (2.14.1)
|
27
27
|
rspec-core (~> 2.14.0)
|
@@ -40,5 +40,6 @@ PLATFORMS
|
|
40
40
|
DEPENDENCIES
|
41
41
|
active_type!
|
42
42
|
activerecord (~> 3.2.0)
|
43
|
+
i18n (= 0.6.11)
|
43
44
|
rspec (< 2.99)
|
44
45
|
sqlite3
|
data/lib/active_type/version.rb
CHANGED
@@ -40,6 +40,8 @@ module ActiveType
|
|
40
40
|
add_virtual_column(name, type, options)
|
41
41
|
build_reader(name)
|
42
42
|
build_writer(name)
|
43
|
+
build_value_was_method(name)
|
44
|
+
build_value_changed_method(name)
|
43
45
|
end
|
44
46
|
|
45
47
|
private
|
@@ -70,6 +72,22 @@ module ActiveType
|
|
70
72
|
BODY
|
71
73
|
end
|
72
74
|
|
75
|
+
def build_value_was_method(name)
|
76
|
+
@module.module_eval <<-BODY, __FILE__, __LINE__ + 1
|
77
|
+
def #{name}_was
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
BODY
|
81
|
+
end
|
82
|
+
|
83
|
+
def build_value_changed_method(name)
|
84
|
+
@module.module_eval <<-BODY, __FILE__, __LINE__ + 1
|
85
|
+
def #{name}_changed?
|
86
|
+
not #{name}.nil?
|
87
|
+
end
|
88
|
+
BODY
|
89
|
+
end
|
90
|
+
|
73
91
|
def validate_attribute_name!(name)
|
74
92
|
unless name.to_s =~ /\A[A-z0-9_]*\z/
|
75
93
|
raise InvalidAttributeNameError.new("'#{name}' is not a valid name for a virtual attribute")
|
@@ -315,6 +315,10 @@ describe ActiveType::Object do
|
|
315
315
|
it_should_behave_like "a class supporting dup for attributes", ActiveType::Object
|
316
316
|
end
|
317
317
|
|
318
|
+
describe 'dirty tracking' do
|
319
|
+
it_should_behave_like 'a class supporting dirty tracking for virtual attributes', ActiveType::Object
|
320
|
+
end
|
321
|
+
|
318
322
|
describe '#belongs_to' do
|
319
323
|
subject { ObjectSpec::ObjectWithBelongsTo.new }
|
320
324
|
|
@@ -199,6 +199,10 @@ describe ActiveType::Record do
|
|
199
199
|
it_should_behave_like "a class supporting dup for attributes", RecordSpec::Record
|
200
200
|
end
|
201
201
|
|
202
|
+
describe 'dirty tracking' do
|
203
|
+
it_should_behave_like 'a class supporting dirty tracking for virtual attributes', RecordSpec::Record
|
204
|
+
end
|
205
|
+
|
202
206
|
describe 'persistence' do
|
203
207
|
|
204
208
|
it 'persists to the database' do
|
@@ -39,8 +39,8 @@ describe ActiveType::Util do
|
|
39
39
|
describe 'for a relation' do
|
40
40
|
|
41
41
|
it 'casts a scope to a scope of another class' do
|
42
|
-
record = UtilSpec::BaseRecord.create!(persisted_string
|
43
|
-
base_scope = UtilSpec::BaseRecord.where(persisted_string
|
42
|
+
record = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
43
|
+
base_scope = UtilSpec::BaseRecord.where(:persisted_string => 'foo')
|
44
44
|
casted_scope = ActiveType::Util.cast(base_scope, UtilSpec::ExtendedRecord)
|
45
45
|
casted_scope.build.should be_a(UtilSpec::ExtendedRecord)
|
46
46
|
found_record = casted_scope.find(record.id)
|
@@ -49,9 +49,9 @@ describe ActiveType::Util do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'preserves existing scope conditions' do
|
52
|
-
match = UtilSpec::BaseRecord.create!(persisted_string
|
53
|
-
no_match = UtilSpec::BaseRecord.create!(persisted_string
|
54
|
-
base_scope = UtilSpec::BaseRecord.where(persisted_string
|
52
|
+
match = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
53
|
+
no_match = UtilSpec::BaseRecord.create!(:persisted_string => 'bar')
|
54
|
+
base_scope = UtilSpec::BaseRecord.where(:persisted_string => 'foo')
|
55
55
|
casted_scope = ActiveType::Util.cast(base_scope, UtilSpec::ExtendedRecord)
|
56
56
|
casted_match = UtilSpec::ExtendedRecord.find(match.id)
|
57
57
|
casted_scope.to_a.should == [casted_match]
|
@@ -62,7 +62,7 @@ describe ActiveType::Util do
|
|
62
62
|
describe 'for a record type' do
|
63
63
|
|
64
64
|
it 'casts a base record to an extended record' do
|
65
|
-
base_record = UtilSpec::BaseRecord.create!(persisted_string
|
65
|
+
base_record = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
66
66
|
extended_record = ActiveType::Util.cast(base_record, UtilSpec::ExtendedRecord)
|
67
67
|
extended_record.should be_a(UtilSpec::ExtendedRecord)
|
68
68
|
extended_record.should be_persisted
|
@@ -72,7 +72,7 @@ describe ActiveType::Util do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'casts an extended record to a base record' do
|
75
|
-
extended_record = UtilSpec::ExtendedRecord.create!(persisted_string
|
75
|
+
extended_record = UtilSpec::ExtendedRecord.create!(:persisted_string => 'foo')
|
76
76
|
base_record = ActiveType::Util.cast(extended_record, UtilSpec::BaseRecord)
|
77
77
|
base_record.should be_a(UtilSpec::BaseRecord)
|
78
78
|
base_record.should be_persisted
|
@@ -82,33 +82,33 @@ describe ActiveType::Util do
|
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'calls after_initialize callbacks of the cast target' do
|
85
|
-
base_record = UtilSpec::BaseRecord.create!(persisted_string
|
85
|
+
base_record = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
86
86
|
extended_record = ActiveType::Util.cast(base_record, UtilSpec::ExtendedRecord)
|
87
87
|
extended_record.virtual_string.should be_present
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'lets after_initialize callbacks access attributes (bug in ActiveRecord#becomes)' do
|
91
|
-
base_record = UtilSpec::BaseRecord.create!(persisted_string
|
91
|
+
base_record = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
92
92
|
extended_record = ActiveType::Util.cast(base_record, UtilSpec::ExtendedRecord)
|
93
93
|
extended_record.virtual_string.should == 'persisted_string is foo'
|
94
94
|
end
|
95
95
|
|
96
96
|
it 'preserves the #type of an STI record that is casted to an ExtendedRecord' do
|
97
|
-
child_record = UtilSpec::Child.create!(persisted_string
|
97
|
+
child_record = UtilSpec::Child.create!(:persisted_string => 'foo')
|
98
98
|
extended_child_record = ActiveType::Util.cast(child_record, UtilSpec::ExtendedChild)
|
99
99
|
extended_child_record.should be_a(UtilSpec::ExtendedChild)
|
100
100
|
extended_child_record.type.should == 'UtilSpec::Child'
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'changes the #type of an STI record when casted to another type in the hierarchy' do
|
104
|
-
child_record = UtilSpec::Child.create!(persisted_string
|
104
|
+
child_record = UtilSpec::Child.create!(:persisted_string => 'foo')
|
105
105
|
child_sibling_record = ActiveType::Util.cast(child_record, UtilSpec::ChildSibling)
|
106
106
|
child_sibling_record.should be_a(UtilSpec::ChildSibling)
|
107
107
|
child_sibling_record.type.should == 'UtilSpec::Child'
|
108
108
|
end
|
109
109
|
|
110
110
|
it 'preserves dirty tracking flags' do
|
111
|
-
base_record = UtilSpec::BaseRecord.create!(persisted_string
|
111
|
+
base_record = UtilSpec::BaseRecord.create!(:persisted_string => 'foo')
|
112
112
|
base_record.changes.should == {}
|
113
113
|
base_record.persisted_string = 'bar'
|
114
114
|
base_record.changes.should == { 'persisted_string' => ['foo', 'bar'] }
|
@@ -0,0 +1,31 @@
|
|
1
|
+
shared_examples_for "a class supporting dirty tracking for virtual attributes" do |klass|
|
2
|
+
|
3
|
+
subject do
|
4
|
+
Class.new(klass) do
|
5
|
+
attribute :virtual_attribute
|
6
|
+
end.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#virtual_attribute_was' do
|
10
|
+
|
11
|
+
it 'always returns nil, since there can be no previously saved value' do
|
12
|
+
subject.virtual_attribute_was.should be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#virtual_attribute_changed?' do
|
18
|
+
|
19
|
+
it 'returns true if the attribute is not nil' do
|
20
|
+
subject.virtual_attribute = 'foo'
|
21
|
+
subject.virtual_attribute_changed?.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns false if the attribute is nil' do
|
25
|
+
subject.virtual_attribute = nil
|
26
|
+
subject.virtual_attribute_changed?.should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
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: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Kraze
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".ruby-version"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- LICENSE
|
66
67
|
- README.md
|
67
68
|
- Rakefile
|
@@ -106,6 +107,7 @@ files:
|
|
106
107
|
- spec/shared_examples/coercible_columns.rb
|
107
108
|
- spec/shared_examples/constructor.rb
|
108
109
|
- spec/shared_examples/defaults.rb
|
110
|
+
- spec/shared_examples/dirty_tracking.rb
|
109
111
|
- spec/shared_examples/dupable.rb
|
110
112
|
- spec/shared_examples/mass_assignment.rb
|
111
113
|
- spec/spec_helper.rb
|
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
136
|
version: '0'
|
135
137
|
requirements: []
|
136
138
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.4.8
|
138
140
|
signing_key:
|
139
141
|
specification_version: 4
|
140
142
|
summary: Make any Ruby object quack like ActiveRecord
|
@@ -154,6 +156,7 @@ test_files:
|
|
154
156
|
- spec/shared_examples/coercible_columns.rb
|
155
157
|
- spec/shared_examples/constructor.rb
|
156
158
|
- spec/shared_examples/defaults.rb
|
159
|
+
- spec/shared_examples/dirty_tracking.rb
|
157
160
|
- spec/shared_examples/dupable.rb
|
158
161
|
- spec/shared_examples/mass_assignment.rb
|
159
162
|
- spec/spec_helper.rb
|