active_data 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.rspec +0 -1
- data/.rvmrc +1 -1
- data/.travis.yml +13 -6
- data/Appraisals +7 -0
- data/Gemfile +1 -5
- data/Guardfile +68 -15
- data/README.md +144 -2
- data/active_data.gemspec +19 -11
- data/gemfiles/rails.4.0.gemfile +14 -0
- data/gemfiles/rails.4.1.gemfile +14 -0
- data/gemfiles/rails.4.2.gemfile +14 -0
- data/gemfiles/rails.5.0.gemfile +14 -0
- data/lib/active_data.rb +120 -3
- data/lib/active_data/active_record/associations.rb +50 -0
- data/lib/active_data/active_record/nested_attributes.rb +24 -0
- data/lib/active_data/config.rb +40 -0
- data/lib/active_data/errors.rb +93 -0
- data/lib/active_data/extensions.rb +33 -0
- data/lib/active_data/model.rb +16 -74
- data/lib/active_data/model/associations.rb +84 -15
- data/lib/active_data/model/associations/base.rb +79 -0
- data/lib/active_data/model/associations/collection/embedded.rb +12 -0
- data/lib/active_data/model/associations/collection/proxy.rb +32 -0
- data/lib/active_data/model/associations/collection/referenced.rb +26 -0
- data/lib/active_data/model/associations/embeds_many.rb +124 -18
- data/lib/active_data/model/associations/embeds_one.rb +90 -15
- data/lib/active_data/model/associations/nested_attributes.rb +180 -0
- data/lib/active_data/model/associations/references_many.rb +96 -0
- data/lib/active_data/model/associations/references_one.rb +83 -0
- data/lib/active_data/model/associations/reflections/base.rb +100 -0
- data/lib/active_data/model/associations/reflections/embeds_many.rb +25 -0
- data/lib/active_data/model/associations/reflections/embeds_one.rb +49 -0
- data/lib/active_data/model/associations/reflections/reference_reflection.rb +45 -0
- data/lib/active_data/model/associations/reflections/references_many.rb +28 -0
- data/lib/active_data/model/associations/reflections/references_one.rb +28 -0
- data/lib/active_data/model/associations/validations.rb +63 -0
- data/lib/active_data/model/attributes.rb +247 -0
- data/lib/active_data/model/attributes/attribute.rb +73 -0
- data/lib/active_data/model/attributes/base.rb +116 -0
- data/lib/active_data/model/attributes/collection.rb +17 -0
- data/lib/active_data/model/attributes/dictionary.rb +26 -0
- data/lib/active_data/model/attributes/localized.rb +42 -0
- data/lib/active_data/model/attributes/reference_many.rb +21 -0
- data/lib/active_data/model/attributes/reference_one.rb +42 -0
- data/lib/active_data/model/attributes/reflections/attribute.rb +55 -0
- data/lib/active_data/model/attributes/reflections/base.rb +62 -0
- data/lib/active_data/model/attributes/reflections/collection.rb +10 -0
- data/lib/active_data/model/attributes/reflections/dictionary.rb +13 -0
- data/lib/active_data/model/attributes/reflections/localized.rb +43 -0
- data/lib/active_data/model/attributes/reflections/reference_many.rb +10 -0
- data/lib/active_data/model/attributes/reflections/reference_one.rb +58 -0
- data/lib/active_data/model/attributes/reflections/represents.rb +55 -0
- data/lib/active_data/model/attributes/represents.rb +64 -0
- data/lib/active_data/model/callbacks.rb +71 -0
- data/lib/active_data/model/conventions.rb +35 -0
- data/lib/active_data/model/dirty.rb +77 -0
- data/lib/active_data/model/lifecycle.rb +307 -0
- data/lib/active_data/model/localization.rb +21 -0
- data/lib/active_data/model/persistence.rb +57 -0
- data/lib/active_data/model/primary.rb +51 -0
- data/lib/active_data/model/scopes.rb +77 -0
- data/lib/active_data/model/validations.rb +27 -0
- data/lib/active_data/model/validations/associated.rb +19 -0
- data/lib/active_data/model/validations/nested.rb +39 -0
- data/lib/active_data/railtie.rb +7 -0
- data/lib/active_data/version.rb +1 -1
- data/spec/lib/active_data/active_record/associations_spec.rb +149 -0
- data/spec/lib/active_data/active_record/nested_attributes_spec.rb +16 -0
- data/spec/lib/active_data/config_spec.rb +44 -0
- data/spec/lib/active_data/model/associations/embeds_many_spec.rb +362 -52
- data/spec/lib/active_data/model/associations/embeds_one_spec.rb +250 -31
- data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +23 -0
- data/spec/lib/active_data/model/associations/references_many_spec.rb +196 -0
- data/spec/lib/active_data/model/associations/references_one_spec.rb +134 -0
- data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +144 -0
- data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +116 -0
- data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +255 -0
- data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +208 -0
- data/spec/lib/active_data/model/associations/validations_spec.rb +153 -0
- data/spec/lib/active_data/model/associations_spec.rb +189 -0
- data/spec/lib/active_data/model/attributes/attribute_spec.rb +144 -0
- data/spec/lib/active_data/model/attributes/base_spec.rb +82 -0
- data/spec/lib/active_data/model/attributes/collection_spec.rb +73 -0
- data/spec/lib/active_data/model/attributes/dictionary_spec.rb +93 -0
- data/spec/lib/active_data/model/attributes/localized_spec.rb +88 -33
- data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +72 -0
- data/spec/lib/active_data/model/attributes/reflections/base_spec.rb +56 -0
- data/spec/lib/active_data/model/attributes/reflections/collection_spec.rb +37 -0
- data/spec/lib/active_data/model/attributes/reflections/dictionary_spec.rb +43 -0
- data/spec/lib/active_data/model/attributes/reflections/localized_spec.rb +37 -0
- data/spec/lib/active_data/model/attributes/reflections/represents_spec.rb +70 -0
- data/spec/lib/active_data/model/attributes/represents_spec.rb +153 -0
- data/spec/lib/active_data/model/attributes_spec.rb +243 -0
- data/spec/lib/active_data/model/callbacks_spec.rb +338 -0
- data/spec/lib/active_data/model/conventions_spec.rb +12 -0
- data/spec/lib/active_data/model/dirty_spec.rb +75 -0
- data/spec/lib/active_data/model/lifecycle_spec.rb +330 -0
- data/spec/lib/active_data/model/nested_attributes.rb +202 -0
- data/spec/lib/active_data/model/persistence_spec.rb +47 -0
- data/spec/lib/active_data/model/primary_spec.rb +84 -0
- data/spec/lib/active_data/model/scopes_spec.rb +88 -0
- data/spec/lib/active_data/model/typecasting_spec.rb +192 -0
- data/spec/lib/active_data/model/validations/associated_spec.rb +94 -0
- data/spec/lib/active_data/model/validations/nested_spec.rb +93 -0
- data/spec/lib/active_data/model/validations_spec.rb +31 -0
- data/spec/lib/active_data/model_spec.rb +1 -32
- data/spec/lib/active_data_spec.rb +12 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/support/model_helpers.rb +10 -0
- metadata +246 -54
- data/gemfiles/Gemfile.rails-3 +0 -14
- data/lib/active_data/attributes/base.rb +0 -69
- data/lib/active_data/attributes/localized.rb +0 -42
- data/lib/active_data/model/associations/association.rb +0 -30
- data/lib/active_data/model/attributable.rb +0 -122
- data/lib/active_data/model/collectionizable.rb +0 -55
- data/lib/active_data/model/collectionizable/proxy.rb +0 -42
- data/lib/active_data/model/extensions.rb +0 -9
- data/lib/active_data/model/extensions/array.rb +0 -24
- data/lib/active_data/model/extensions/big_decimal.rb +0 -17
- data/lib/active_data/model/extensions/boolean.rb +0 -38
- data/lib/active_data/model/extensions/date.rb +0 -17
- data/lib/active_data/model/extensions/date_time.rb +0 -17
- data/lib/active_data/model/extensions/float.rb +0 -17
- data/lib/active_data/model/extensions/hash.rb +0 -22
- data/lib/active_data/model/extensions/integer.rb +0 -17
- data/lib/active_data/model/extensions/localized.rb +0 -22
- data/lib/active_data/model/extensions/object.rb +0 -17
- data/lib/active_data/model/extensions/string.rb +0 -17
- data/lib/active_data/model/extensions/time.rb +0 -17
- data/lib/active_data/model/localizable.rb +0 -31
- data/lib/active_data/model/nested_attributes.rb +0 -58
- data/lib/active_data/model/parameterizable.rb +0 -29
- data/lib/active_data/validations.rb +0 -7
- data/lib/active_data/validations/associated.rb +0 -17
- data/spec/lib/active_data/model/attributable_spec.rb +0 -191
- data/spec/lib/active_data/model/collectionizable_spec.rb +0 -60
- data/spec/lib/active_data/model/nested_attributes_spec.rb +0 -67
- data/spec/lib/active_data/model/type_cast_spec.rb +0 -116
- data/spec/lib/active_data/validations/associated_spec.rb +0 -88
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Attribute do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify { expect(described_class.build(Class.new, Target, :field, String).type).to eq(String) }
|
12
|
+
specify { expect(described_class.build(Class.new, Target, :field) {}.defaultizer).to be_a(Proc) }
|
13
|
+
specify do
|
14
|
+
described_class.build(Class.new, Target, :field)
|
15
|
+
|
16
|
+
expect(Target).to be_method_defined(:field)
|
17
|
+
expect(Target).to be_method_defined(:field=)
|
18
|
+
expect(Target).to be_method_defined(:field?)
|
19
|
+
expect(Target).to be_method_defined(:field_before_type_cast)
|
20
|
+
expect(Target).to be_method_defined(:field_default)
|
21
|
+
expect(Target).to be_method_defined(:field_values)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#generate_methods' do
|
26
|
+
before { stub_class(:target) }
|
27
|
+
|
28
|
+
specify do
|
29
|
+
described_class.generate_methods(:field_alias, Target)
|
30
|
+
|
31
|
+
expect(Target).to be_method_defined(:field_alias)
|
32
|
+
expect(Target).to be_method_defined(:field_alias=)
|
33
|
+
expect(Target).to be_method_defined(:field_alias?)
|
34
|
+
expect(Target).to be_method_defined(:field_alias_before_type_cast)
|
35
|
+
expect(Target).to be_method_defined(:field_alias_default)
|
36
|
+
expect(Target).to be_method_defined(:field_alias_values)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#defaultizer' do
|
41
|
+
specify { expect(reflection.defaultizer).to be_nil }
|
42
|
+
specify { expect(reflection(default: 42).defaultizer).to eq(42) }
|
43
|
+
specify { expect(reflection(default: ->{ }).defaultizer).to be_a Proc }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#typecaster' do
|
47
|
+
before do
|
48
|
+
stub_class(:dummy, String)
|
49
|
+
stub_class(:dummy_dummy, Dummy)
|
50
|
+
end
|
51
|
+
|
52
|
+
specify { expect(reflection(type: Object).typecaster).to eq(ActiveData.typecaster(Object)) }
|
53
|
+
specify { expect(reflection(type: String).typecaster).to eq(ActiveData.typecaster(String)) }
|
54
|
+
specify { expect(reflection(type: Dummy).typecaster).to eq(ActiveData.typecaster(String)) }
|
55
|
+
specify { expect(reflection(type: DummyDummy).typecaster).to eq(ActiveData.typecaster(String)) }
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#enumerizer' do
|
59
|
+
specify { expect(reflection.enumerizer).to be_nil }
|
60
|
+
specify { expect(reflection(enum: 42).enumerizer).to eq(42) }
|
61
|
+
specify { expect(reflection(enum: ->{ }).enumerizer).to be_a Proc }
|
62
|
+
specify { expect(reflection(in: 42).enumerizer).to eq(42) }
|
63
|
+
specify { expect(reflection(in: ->{ }).enumerizer).to be_a Proc }
|
64
|
+
specify { expect(reflection(enum: 42, in: ->{ }).enumerizer).to eq(42) }
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#normalizers' do
|
68
|
+
specify { expect(reflection.normalizers).to eq([]) }
|
69
|
+
specify { expect(reflection(normalizer: ->{}).normalizers).to be_a Array }
|
70
|
+
specify { expect(reflection(normalizer: ->{}).normalizers.first).to be_a Proc }
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Base do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify do
|
12
|
+
described_class.build(Class.new, Target, :field)
|
13
|
+
expect(Target).not_to be_method_defined(:field)
|
14
|
+
end
|
15
|
+
specify { expect(described_class.build(Class.new, Target, :field).name).to eq('field') }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.attribute_class' do
|
19
|
+
before do
|
20
|
+
stub_class('SomeScope::Borogoves', described_class)
|
21
|
+
stub_class('ActiveData::Model::Attributes::Borogoves')
|
22
|
+
end
|
23
|
+
|
24
|
+
specify { expect(described_class.attribute_class).to eq(ActiveData::Model::Attributes::Base) }
|
25
|
+
specify { expect(SomeScope::Borogoves.attribute_class).to eq(ActiveData::Model::Attributes::Borogoves) }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#name' do
|
29
|
+
specify { expect(reflection.name).to eq('field') }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#build_attribute' do
|
33
|
+
before do
|
34
|
+
stub_class('SomeScope::Borogoves', described_class)
|
35
|
+
stub_class('ActiveData::Model::Attributes::Borogoves', ActiveData::Model::Attributes::Base)
|
36
|
+
stub_class(:owner)
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:reflection) { SomeScope::Borogoves.new(:field) }
|
40
|
+
let(:owner) { Owner.new }
|
41
|
+
|
42
|
+
specify { expect(reflection.build_attribute(owner, nil)).to be_a(ActiveData::Model::Attributes::Borogoves) }
|
43
|
+
specify { expect(reflection.build_attribute(owner, nil).name).to eq('field') }
|
44
|
+
specify { expect(reflection.build_attribute(owner, nil).owner).to eq(owner) }
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#type' do
|
48
|
+
before { stub_class(:dummy, String) }
|
49
|
+
|
50
|
+
specify { expect { reflection.type }.to raise_error('Type is not specified for `field`') }
|
51
|
+
specify { expect(reflection(type: String).type).to eq(String) }
|
52
|
+
specify { expect(reflection(type: :string).type).to eq(String) }
|
53
|
+
specify { expect(reflection(type: Dummy).type).to eq(Dummy) }
|
54
|
+
specify { expect { reflection(type: :blabla).type }.to raise_error NameError }
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Collection do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify do
|
12
|
+
described_class.build(Class.new, Target, :field)
|
13
|
+
|
14
|
+
expect(Target).to be_method_defined(:field)
|
15
|
+
expect(Target).to be_method_defined(:field=)
|
16
|
+
expect(Target).to be_method_defined(:field?)
|
17
|
+
expect(Target).to be_method_defined(:field_before_type_cast)
|
18
|
+
expect(Target).to be_method_defined(:field_default)
|
19
|
+
expect(Target).to be_method_defined(:field_values)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#generate_methods' do
|
24
|
+
before { stub_class(:target) }
|
25
|
+
|
26
|
+
specify do
|
27
|
+
described_class.generate_methods(:field_alias, Target)
|
28
|
+
|
29
|
+
expect(Target).to be_method_defined(:field_alias)
|
30
|
+
expect(Target).to be_method_defined(:field_alias=)
|
31
|
+
expect(Target).to be_method_defined(:field_alias?)
|
32
|
+
expect(Target).to be_method_defined(:field_alias_before_type_cast)
|
33
|
+
expect(Target).to be_method_defined(:field_alias_default)
|
34
|
+
expect(Target).to be_method_defined(:field_alias_values)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Dictionary do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify do
|
12
|
+
described_class.build(Class.new, Target, :field)
|
13
|
+
|
14
|
+
expect(Target).to be_method_defined(:field)
|
15
|
+
expect(Target).to be_method_defined(:field=)
|
16
|
+
expect(Target).to be_method_defined(:field?)
|
17
|
+
expect(Target).to be_method_defined(:field_before_type_cast)
|
18
|
+
expect(Target).to be_method_defined(:field_default)
|
19
|
+
expect(Target).to be_method_defined(:field_values)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#generate_methods' do
|
24
|
+
before { stub_class(:target) }
|
25
|
+
|
26
|
+
specify do
|
27
|
+
described_class.generate_methods(:field_alias, Target)
|
28
|
+
|
29
|
+
expect(Target).to be_method_defined(:field_alias)
|
30
|
+
expect(Target).to be_method_defined(:field_alias=)
|
31
|
+
expect(Target).to be_method_defined(:field_alias?)
|
32
|
+
expect(Target).to be_method_defined(:field_alias_before_type_cast)
|
33
|
+
expect(Target).to be_method_defined(:field_alias_default)
|
34
|
+
expect(Target).to be_method_defined(:field_alias_values)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#keys' do
|
39
|
+
specify { expect(reflection.keys).to eq([]) }
|
40
|
+
specify { expect(reflection(keys: ['a', :b]).keys).to eq(%w[a b]) }
|
41
|
+
specify { expect(reflection(keys: :c).keys).to eq(%w[c]) }
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Localized do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify do
|
12
|
+
described_class.build(Class.new, Target, :field)
|
13
|
+
|
14
|
+
expect(Target).to be_method_defined(:field_translations)
|
15
|
+
expect(Target).to be_method_defined(:field_translations=)
|
16
|
+
expect(Target).to be_method_defined(:field)
|
17
|
+
expect(Target).to be_method_defined(:field=)
|
18
|
+
expect(Target).to be_method_defined(:field?)
|
19
|
+
expect(Target).to be_method_defined(:field_before_type_cast)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#generate_methods' do
|
24
|
+
before { stub_class(:target) }
|
25
|
+
|
26
|
+
specify do
|
27
|
+
described_class.generate_methods(:field_alias, Target)
|
28
|
+
|
29
|
+
expect(Target).to be_method_defined(:field_alias_translations)
|
30
|
+
expect(Target).to be_method_defined(:field_alias_translations=)
|
31
|
+
expect(Target).to be_method_defined(:field_alias)
|
32
|
+
expect(Target).to be_method_defined(:field_alias=)
|
33
|
+
expect(Target).to be_method_defined(:field_alias?)
|
34
|
+
expect(Target).to be_method_defined(:field_alias_before_type_cast)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Reflections::Represents do
|
4
|
+
def reflection(options = {})
|
5
|
+
described_class.new(:field, options.reverse_merge(of: :subject))
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
before { stub_class(:target) }
|
10
|
+
|
11
|
+
specify do
|
12
|
+
described_class.build(Class.new, Target, :field, of: :subject)
|
13
|
+
|
14
|
+
expect(Target).to be_method_defined(:field)
|
15
|
+
expect(Target).to be_method_defined(:field=)
|
16
|
+
expect(Target).to be_method_defined(:field?)
|
17
|
+
expect(Target).to be_method_defined(:field_before_type_cast)
|
18
|
+
expect(Target).to be_method_defined(:field_default)
|
19
|
+
expect(Target).to be_method_defined(:field_values)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.generate_methods' do
|
24
|
+
before { stub_class(:target) }
|
25
|
+
|
26
|
+
specify do
|
27
|
+
described_class.generate_methods(:field_alias, Target)
|
28
|
+
|
29
|
+
expect(Target).to be_method_defined(:field_alias)
|
30
|
+
expect(Target).to be_method_defined(:field_alias=)
|
31
|
+
expect(Target).to be_method_defined(:field_alias?)
|
32
|
+
expect(Target).to be_method_defined(:field_alias_before_type_cast)
|
33
|
+
expect(Target).to be_method_defined(:field_alias_default)
|
34
|
+
expect(Target).to be_method_defined(:field_alias_values)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#type' do
|
39
|
+
specify { expect(reflection.type).to eq(Object) }
|
40
|
+
specify { expect(reflection(type: :whatever).type).to eq(Object) }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#reference' do
|
44
|
+
specify { expect { reflection(of: nil) }.to raise_error ArgumentError }
|
45
|
+
specify { expect(reflection(of: :subject).reference).to eq('subject') }
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#column' do
|
49
|
+
specify { expect(reflection.column).to eq('field') }
|
50
|
+
specify { expect(reflection(column: 'hello').column).to eq('hello') }
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#reader' do
|
54
|
+
specify { expect(reflection.reader).to eq('field') }
|
55
|
+
specify { expect(reflection(column: 'hello').reader).to eq('hello') }
|
56
|
+
specify { expect(reflection(reader: 'world').reader).to eq('world') }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#reader_before_type_cast' do
|
60
|
+
specify { expect(reflection.reader_before_type_cast).to eq('field_before_type_cast') }
|
61
|
+
specify { expect(reflection(column: 'hello').reader_before_type_cast).to eq('hello_before_type_cast') }
|
62
|
+
specify { expect(reflection(reader: 'world').reader_before_type_cast).to eq('world_before_type_cast') }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#writer' do
|
66
|
+
specify { expect(reflection.writer).to eq('field=') }
|
67
|
+
specify { expect(reflection(column: 'hello').writer).to eq('hello=') }
|
68
|
+
specify { expect(reflection(writer: 'world').writer).to eq('world=') }
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Attributes::Represents do
|
5
|
+
before { stub_model(:dummy) }
|
6
|
+
|
7
|
+
def attribute(*args)
|
8
|
+
options = args.extract_options!
|
9
|
+
reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Represents, :full_name, options.reverse_merge(of: :subject))
|
10
|
+
Dummy.new.attribute(:full_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
stub_model :subject do
|
15
|
+
attribute :full_name, String
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#write' do
|
20
|
+
subject { Subject.new }
|
21
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 42, subject: subject) }
|
22
|
+
let(:field) { attribute }
|
23
|
+
|
24
|
+
specify { expect { field.write('hello') }.to change { subject.full_name }.to('hello') }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#read' do
|
28
|
+
subject { Subject.new(full_name: :hello) }
|
29
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 42, subject: subject) }
|
30
|
+
let(:field) { attribute(normalizer: ->(v){ v && v.is_a?(String) ? v.strip : v }, default: :world, enum: ['hello', '42', 'world', :world]) }
|
31
|
+
|
32
|
+
specify { expect(field.read).to eq('hello') }
|
33
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq(:world) }
|
34
|
+
specify { expect(field.tap { |r| r.write(:world) }.read).to eq('world') }
|
35
|
+
specify { expect(field.tap { |r| r.write('hello') }.read).to eq('hello') }
|
36
|
+
specify { expect(field.tap { |r| r.write(' hello ') }.read).to eq(nil) }
|
37
|
+
specify { expect(field.tap { |r| r.write(42) }.read).to eq('42') }
|
38
|
+
specify { expect(field.tap { |r| r.write(43) }.read).to eq(nil) }
|
39
|
+
specify { expect(field.tap { |r| r.write('') }.read).to eq(nil) }
|
40
|
+
|
41
|
+
specify { expect { subject.full_name = 42 }.to change { field.read }.to('42') }
|
42
|
+
|
43
|
+
context ':readonly' do
|
44
|
+
specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read).to eq('hello') }
|
45
|
+
end
|
46
|
+
|
47
|
+
context do
|
48
|
+
subject { Subject.new }
|
49
|
+
let(:field) { attribute(default: -> { Time.now.to_f }) }
|
50
|
+
specify { expect { sleep(0.01) }.not_to change { field.read } }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#read_before_type_cast' do
|
55
|
+
subject { Subject.new(full_name: :hello) }
|
56
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 42, subject: subject) }
|
57
|
+
let(:field) { attribute(normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
|
58
|
+
|
59
|
+
specify { expect(field.read_before_type_cast).to eq(:hello) }
|
60
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
|
61
|
+
specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
|
62
|
+
specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq('hello') }
|
63
|
+
specify { expect(field.tap { |r| r.write(42) }.read_before_type_cast).to eq(42) }
|
64
|
+
specify { expect(field.tap { |r| r.write(43) }.read_before_type_cast).to eq(43) }
|
65
|
+
specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
|
66
|
+
|
67
|
+
specify { expect { subject.full_name = 42 }.to change { field.read_before_type_cast }.to(42) }
|
68
|
+
|
69
|
+
context ':readonly' do
|
70
|
+
specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read_before_type_cast).to eq(:hello) }
|
71
|
+
end
|
72
|
+
|
73
|
+
context do
|
74
|
+
subject { Subject.new }
|
75
|
+
let(:field) { attribute(default: -> { Time.now.to_f }) }
|
76
|
+
specify { expect { sleep(0.01) }.not_to change { field.read_before_type_cast } }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'integration' do
|
81
|
+
before do
|
82
|
+
stub_model(:author) do
|
83
|
+
attribute :rate, Integer
|
84
|
+
end
|
85
|
+
|
86
|
+
stub_model(:post) do
|
87
|
+
attribute :author, Object
|
88
|
+
alias_attribute :a, :author
|
89
|
+
represents :rate, of: :a
|
90
|
+
alias_attribute :r, :rate
|
91
|
+
end
|
92
|
+
end
|
93
|
+
let(:author) { Author.new(rate: '42') }
|
94
|
+
|
95
|
+
specify { expect(Post.reflect_on_attribute(:rate).reference).to eq('author') }
|
96
|
+
|
97
|
+
specify { expect(Post.new(author: author).rate).to eq(42) }
|
98
|
+
specify { expect(Post.new(author: author).rate_before_type_cast).to eq('42') }
|
99
|
+
specify { expect(Post.new(rate: '33', author: author).rate).to eq(33) }
|
100
|
+
specify { expect(Post.new(rate: '33', author: author).author.rate).to eq(33) }
|
101
|
+
specify { expect(Post.new(r: '33', author: author).rate).to eq(33) }
|
102
|
+
specify { expect(Post.new(r: '33', author: author).author.rate).to eq(33) }
|
103
|
+
specify { expect(Post.new(author: author).rate?).to eq(true) }
|
104
|
+
specify { expect(Post.new(rate: nil, author: author).rate?).to eq(false) }
|
105
|
+
|
106
|
+
specify { expect(Post.new.rate).to be_nil }
|
107
|
+
specify { expect(Post.new.rate_before_type_cast).to be_nil }
|
108
|
+
specify { expect { Post.new(rate: '33') }.to raise_error(NoMethodError) }
|
109
|
+
|
110
|
+
context do
|
111
|
+
before do
|
112
|
+
stub_class(:author, ActiveRecord::Base)
|
113
|
+
|
114
|
+
stub_model(:post) do
|
115
|
+
include ActiveData::Model::Associations
|
116
|
+
|
117
|
+
references_one :author
|
118
|
+
alias_association :a, :author
|
119
|
+
represents :name, of: :a
|
120
|
+
end
|
121
|
+
end
|
122
|
+
let!(:author) { Author.create!(name: 42) }
|
123
|
+
|
124
|
+
specify { expect(Post.reflect_on_attribute(:name).reference).to eq('author') }
|
125
|
+
|
126
|
+
specify { expect(Post.new(name: '33', author: author).name).to eq('33') }
|
127
|
+
specify { expect(Post.new(name: '33', author: author).author.name).to eq('33') }
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'multiple attributes in a single represents definition' do
|
131
|
+
before do
|
132
|
+
stub_model(:author) do
|
133
|
+
attribute :first_name, String
|
134
|
+
attribute :last_name, String
|
135
|
+
end
|
136
|
+
|
137
|
+
stub_model(:post) do
|
138
|
+
attribute :author, Object
|
139
|
+
represents :first_name, :last_name, of: :author
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
let(:author) { Author.new(first_name: 'John', last_name: 'Doe') }
|
144
|
+
let(:post) { Post.new }
|
145
|
+
|
146
|
+
specify do
|
147
|
+
expect { post.update(author: author) }
|
148
|
+
.to change { post.first_name }.to('John')
|
149
|
+
.and change { post.last_name }.to('Doe')
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|