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,144 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveData::Model::Attributes::Attribute do
|
4
|
+
before { stub_model(:dummy) }
|
5
|
+
|
6
|
+
def attribute(*args)
|
7
|
+
options = args.extract_options!
|
8
|
+
reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Attribute, :field, {type: Object}.merge(options))
|
9
|
+
Dummy.new.attribute(:field)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#read' do
|
13
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
|
14
|
+
|
15
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq('world') }
|
16
|
+
specify { expect(field.tap { |r| r.write(:world) }.read).to eq('world') }
|
17
|
+
specify { expect(field.tap { |r| r.write('hello') }.read).to eq('hello') }
|
18
|
+
specify { expect(field.tap { |r| r.write(' hello ') }.read).to eq(nil) }
|
19
|
+
specify { expect(field.tap { |r| r.write(42) }.read).to eq('42') }
|
20
|
+
specify { expect(field.tap { |r| r.write(43) }.read).to eq(nil) }
|
21
|
+
specify { expect(field.tap { |r| r.write('') }.read).to eq(nil) }
|
22
|
+
|
23
|
+
context ':readonly' do
|
24
|
+
specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read).to eq(:world) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#read_before_type_cast' do
|
29
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
|
30
|
+
|
31
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
|
32
|
+
specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
|
33
|
+
specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq('hello') }
|
34
|
+
specify { expect(field.tap { |r| r.write(42) }.read_before_type_cast).to eq(42) }
|
35
|
+
specify { expect(field.tap { |r| r.write(43) }.read_before_type_cast).to eq(43) }
|
36
|
+
specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
|
37
|
+
|
38
|
+
context ':readonly' do
|
39
|
+
specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read_before_type_cast).to eq(:world) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#default' do
|
44
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 42) }
|
45
|
+
|
46
|
+
specify { expect(attribute.default).to eq(nil) }
|
47
|
+
specify { expect(attribute(default: 'hello').default).to eq('hello') }
|
48
|
+
specify { expect(attribute(default: -> { value }).default).to eq(42) }
|
49
|
+
specify { expect(attribute(default: ->(object) { object.value }).default).to eq(42) }
|
50
|
+
specify { expect(attribute(default: ->(*args) { args.first.value }).default).to eq(42) }
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#defaultize' do
|
54
|
+
specify { expect(attribute.defaultize(nil)).to be_nil }
|
55
|
+
specify { expect(attribute(default: 'hello').defaultize(nil)).to eq('hello') }
|
56
|
+
specify { expect(attribute(default: 'hello').defaultize('world')).to eq('world') }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#typecast' do
|
60
|
+
specify { expect(attribute.typecast(:hello)).to eq(:hello) }
|
61
|
+
specify { expect(attribute(type: Integer).typecast(42)).to eq(42) }
|
62
|
+
specify { expect(attribute(type: Integer).typecast('42')).to eq(42) }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#enum' do
|
66
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 1..5) }
|
67
|
+
|
68
|
+
specify { expect(attribute.enum).to eq([].to_set) }
|
69
|
+
specify { expect(attribute(enum: []).enum).to eq([].to_set) }
|
70
|
+
specify { expect(attribute(enum: 'hello').enum).to eq(['hello'].to_set) }
|
71
|
+
specify { expect(attribute(enum: ['hello', 'world']).enum).to eq(['hello', 'world'].to_set) }
|
72
|
+
specify { expect(attribute(enum: [1..5]).enum).to eq([1..5].to_set) }
|
73
|
+
specify { expect(attribute(enum: 1..5).enum).to eq((1..5).to_a.to_set) }
|
74
|
+
specify { expect(attribute(enum: -> { 1..5 }).enum).to eq((1..5).to_a.to_set) }
|
75
|
+
specify { expect(attribute(enum: -> { 'hello' }).enum).to eq(['hello'].to_set) }
|
76
|
+
specify { expect(attribute(enum: -> { ['hello', 42] }).enum).to eq(['hello', 42].to_set) }
|
77
|
+
specify { expect(attribute(enum: -> { value }).enum).to eq((1..5).to_a.to_set) }
|
78
|
+
specify { expect(attribute(enum: ->(object) { object.value }).enum).to eq((1..5).to_a.to_set) }
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#enumerize' do
|
82
|
+
specify { expect(attribute.enumerize('anything')).to eq('anything') }
|
83
|
+
specify { expect(attribute(enum: ['hello', 42]).enumerize('hello')).to eq('hello') }
|
84
|
+
specify { expect(attribute(enum: ['hello', 42]).enumerize('world')).to eq(nil) }
|
85
|
+
specify { expect(attribute(enum: -> { 'hello' }).enumerize('hello')).to eq('hello') }
|
86
|
+
specify { expect(attribute(enum: -> { 1..5 }).enumerize(2)).to eq(2) }
|
87
|
+
specify { expect(attribute(enum: -> { 1..5 }).enumerize(42)).to eq(nil) }
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#normalize' do
|
91
|
+
specify { expect(attribute.normalize(' hello ')).to eq(' hello ') }
|
92
|
+
specify { expect(attribute(normalizer: ->(v) { v.strip }).normalize(' hello ')).to eq('hello') }
|
93
|
+
specify { expect(attribute(normalizer: [->(v) { v.strip }, ->(v) { v.first(4) }]).normalize(' hello ')).to eq('hell') }
|
94
|
+
specify { expect(attribute(normalizer: [->(v) { v.first(4) }, ->(v) { v.strip }]).normalize(' hello ')).to eq('hel') }
|
95
|
+
|
96
|
+
context do
|
97
|
+
before { allow_any_instance_of(Dummy).to receive_messages(value: 'value') }
|
98
|
+
let(:other) { 'other' }
|
99
|
+
|
100
|
+
specify { expect(attribute(normalizer: ->(v) { value }).normalize(' hello ')).to eq('value') }
|
101
|
+
specify { expect(attribute(normalizer: ->(v, object) { object.value }).normalize(' hello ')).to eq('value') }
|
102
|
+
specify { expect(attribute(normalizer: ->(v, object) { other }).normalize(' hello ')).to eq('other') }
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'integration' do
|
106
|
+
before do
|
107
|
+
allow(ActiveData).to receive_messages(config: ActiveData::Config.send(:new))
|
108
|
+
ActiveData.normalizer(:strip) do |value|
|
109
|
+
value.strip
|
110
|
+
end
|
111
|
+
ActiveData.normalizer(:trim) do |value, options, attribute|
|
112
|
+
value.first(length || options[:length] || 2)
|
113
|
+
end
|
114
|
+
ActiveData.normalizer(:reset) do |value, options, attribute|
|
115
|
+
empty = value.respond_to?(:empty?) ? value.empty? : value.nil?
|
116
|
+
empty ? attribute.default : value
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
let(:length) { nil }
|
121
|
+
|
122
|
+
specify { expect(attribute(normalizer: :strip).normalize(' hello ')).to eq('hello') }
|
123
|
+
specify { expect(attribute(normalizer: [:strip, :trim]).normalize(' hello ')).to eq('he') }
|
124
|
+
specify { expect(attribute(normalizer: [:trim, :strip]).normalize(' hello ')).to eq('h') }
|
125
|
+
specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }]).normalize(' hello ')).to eq('hell') }
|
126
|
+
specify { expect(attribute(normalizer: {strip: { }, trim: { length: 4 } }).normalize(' hello ')).to eq('hell') }
|
127
|
+
specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }, ->(v) { v.last(2) }])
|
128
|
+
.normalize(' hello ')).to eq('ll') }
|
129
|
+
specify { expect(attribute(normalizer: :reset).normalize('')).to eq(nil) }
|
130
|
+
specify { expect(attribute(normalizer: [:strip, :reset]).normalize(' ')).to eq(nil) }
|
131
|
+
specify { expect(attribute(normalizer: :reset, default: '!!!').normalize(nil)).to eq('!!!') }
|
132
|
+
specify { expect(attribute(normalizer: :reset, default: '!!!').normalize('')).to eq('!!!') }
|
133
|
+
|
134
|
+
context do
|
135
|
+
let(:length) { 3 }
|
136
|
+
|
137
|
+
specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }]).normalize(' hello ')).to eq('hel') }
|
138
|
+
specify { expect(attribute(normalizer: {strip: { }, trim: { length: 4 } }).normalize(' hello ')).to eq('hel') }
|
139
|
+
specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }, ->(v) { v.last(2) }])
|
140
|
+
.normalize(' hello ')).to eq('el') }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Attributes::Base 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::Base, :field, options)
|
10
|
+
Dummy.new.attribute(:field)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#read' do
|
14
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
|
15
|
+
let(:object) { Object.new }
|
16
|
+
|
17
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to be_nil }
|
18
|
+
specify { expect(field.tap { |r| r.write('') }.read).to eq('') }
|
19
|
+
specify { expect(field.tap { |r| r.write(:world) }.read).to eq(:world) }
|
20
|
+
specify { expect(field.tap { |r| r.write(object) }.read).to eq(object) }
|
21
|
+
|
22
|
+
context ':readonly' do
|
23
|
+
specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read).to be_nil }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#read_before_type_cast' do
|
28
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
|
29
|
+
let(:object) { Object.new }
|
30
|
+
|
31
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to be_nil }
|
32
|
+
specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
|
33
|
+
specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
|
34
|
+
specify { expect(field.tap { |r| r.write(object) }.read_before_type_cast).to eq(object) }
|
35
|
+
|
36
|
+
context ':readonly' do
|
37
|
+
specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read_before_type_cast).to be_nil }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#value_present?' do
|
42
|
+
let(:field) { attribute }
|
43
|
+
|
44
|
+
specify { expect(field.tap { |r| r.write(0) }).to be_value_present }
|
45
|
+
specify { expect(field.tap { |r| r.write(42) }).to be_value_present }
|
46
|
+
specify { expect(field.tap { |r| r.write(true) }).to be_value_present }
|
47
|
+
specify { expect(field.tap { |r| r.write(false) }).to be_value_present }
|
48
|
+
specify { expect(field.tap { |r| r.write(nil) }).not_to be_value_present }
|
49
|
+
specify { expect(field.tap { |r| r.write('') }).not_to be_value_present }
|
50
|
+
specify { expect(field.tap { |r| r.write(:world) }).to be_value_present }
|
51
|
+
specify { expect(field.tap { |r| r.write(Object.new) }).to be_value_present }
|
52
|
+
specify { expect(field.tap { |r| r.write([]) }).not_to be_value_present }
|
53
|
+
specify { expect(field.tap { |r| r.write([42]) }).to be_value_present }
|
54
|
+
specify { expect(field.tap { |r| r.write({}) }).not_to be_value_present }
|
55
|
+
specify { expect(field.tap { |r| r.write({hello: 42}) }).to be_value_present }
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#query' do
|
59
|
+
let(:field) { attribute }
|
60
|
+
|
61
|
+
specify { expect(field.tap { |r| r.write(0) }.query).to be(false) }
|
62
|
+
specify { expect(field.tap { |r| r.write(42) }.query).to be(true) }
|
63
|
+
specify { expect(field.tap { |r| r.write(true) }.query).to be(true) }
|
64
|
+
specify { expect(field.tap { |r| r.write(false) }.query).to be(false) }
|
65
|
+
specify { expect(field.tap { |r| r.write(nil) }.query).to be(false) }
|
66
|
+
specify { expect(field.tap { |r| r.write('') }.query).to be(false) }
|
67
|
+
specify { expect(field.tap { |r| r.write(:world) }.query).to be(true) }
|
68
|
+
specify { expect(field.tap { |r| r.write(Object.new) }.query).to be(true) }
|
69
|
+
specify { expect(field.tap { |r| r.write([]) }.query).to be(false) }
|
70
|
+
specify { expect(field.tap { |r| r.write([42]) }.query).to be(true) }
|
71
|
+
specify { expect(field.tap { |r| r.write({}) }.query).to be(false) }
|
72
|
+
specify { expect(field.tap { |r| r.write({hello: 42}) }.query).to be(true) }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#readonly?' do
|
76
|
+
specify { expect(attribute).not_to be_readonly }
|
77
|
+
specify { expect(attribute(readonly: false)).not_to be_readonly }
|
78
|
+
specify { expect(attribute(readonly: true)).to be_readonly }
|
79
|
+
specify { expect(attribute(readonly: -> { false })).not_to be_readonly }
|
80
|
+
specify { expect(attribute(readonly: -> { true })).to be_readonly }
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Attributes::Collection 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::Collection, :field, options)
|
10
|
+
Dummy.new.attribute(:field)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#read' do
|
14
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world, enum: ['hello', '42']) }
|
15
|
+
|
16
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
|
17
|
+
specify { expect(field.tap { |r| r.write([nil]) }.read).to eq([nil]) }
|
18
|
+
specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
|
19
|
+
specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
|
20
|
+
specify { expect(field.tap { |r| r.write([43]) }.read).to eq([nil]) }
|
21
|
+
specify { expect(field.tap { |r| r.write([43, 44]) }.read).to eq([nil]) }
|
22
|
+
specify { expect(field.tap { |r| r.write(['']) }.read).to eq([nil]) }
|
23
|
+
specify { expect(field.tap { |r| r.write(['hello', 42]) }.read).to eq(['hello', '42']) }
|
24
|
+
specify { expect(field.tap { |r| r.write(['hello', false]) }.read).to eq(['hello', nil]) }
|
25
|
+
|
26
|
+
context do
|
27
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world) }
|
28
|
+
|
29
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
|
30
|
+
specify { expect(field.tap { |r| r.write([nil, nil]) }.read).to eq(['world']) }
|
31
|
+
specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
|
32
|
+
specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
|
33
|
+
specify { expect(field.tap { |r| r.write(['']) }.read).to eq(['']) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#read_before_type_cast' do
|
38
|
+
let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
|
39
|
+
|
40
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
|
41
|
+
specify { expect(field.tap { |r| r.write([nil]) }.read_before_type_cast).to eq([:world]) }
|
42
|
+
specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
|
43
|
+
specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
|
44
|
+
specify { expect(field.tap { |r| r.write([43]) }.read_before_type_cast).to eq([43]) }
|
45
|
+
specify { expect(field.tap { |r| r.write([43, 44]) }.read_before_type_cast).to eq([43, 44]) }
|
46
|
+
specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
|
47
|
+
specify { expect(field.tap { |r| r.write(['hello', 42]) }.read_before_type_cast).to eq(['hello', 42]) }
|
48
|
+
specify { expect(field.tap { |r| r.write(['hello', false]) }.read_before_type_cast).to eq(['hello', false]) }
|
49
|
+
|
50
|
+
context do
|
51
|
+
let(:field) { attribute(type: String, default: :world) }
|
52
|
+
|
53
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
|
54
|
+
specify { expect(field.tap { |r| r.write([nil, nil]) }.read_before_type_cast).to eq([:world, :world]) }
|
55
|
+
specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
|
56
|
+
specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
|
57
|
+
specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'integration' do
|
62
|
+
before do
|
63
|
+
stub_model(:post) do
|
64
|
+
collection :tags, String
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(['hello', '42']) }
|
69
|
+
specify { expect(Post.new(tags: ['hello', 42]).tags_before_type_cast).to eq(['hello', 42]) }
|
70
|
+
specify { expect(Post.new.tags?).to eq(false) }
|
71
|
+
specify { expect(Post.new(tags: ['hello', 42]).tags?).to eq(true) }
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Attributes::Dictionary 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::Dictionary, :field, options)
|
10
|
+
Dummy.new.attribute(:field)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#read' do
|
14
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
|
15
|
+
default: :world, enum: ['hello', '42']) }
|
16
|
+
|
17
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
|
18
|
+
specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
|
19
|
+
specify { expect(field.tap { |r| r.write({a: nil}) }.read).to eq({}) }
|
20
|
+
specify { expect(field.tap { |r| r.write({a: ''}) }.read).to eq({}) }
|
21
|
+
specify { expect(field.tap { |r| r.write({a: 1}) }.read).to eq({}) }
|
22
|
+
specify { expect(field.tap { |r| r.write({a: 42}) }.read).to eq({'a' => '42'}) }
|
23
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', b: '42'}) }.read).to eq({'a' => 'hello', 'b' => '42'}) }
|
24
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', b: '1'}) }.read).to eq({'a' => 'hello'}) }
|
25
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', x: '42'}) }.read).to eq({'a' => 'hello', 'x' => '42'}) }
|
26
|
+
|
27
|
+
context do
|
28
|
+
let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
|
29
|
+
default: :world) }
|
30
|
+
|
31
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
|
32
|
+
specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
|
33
|
+
specify { expect(field.tap { |r| r.write({a: 1}) }.read).to eq({'a' => '1'}) }
|
34
|
+
specify { expect(field.tap { |r| r.write({a: nil, b: nil}) }.read).to eq({'a' => 'world', 'b' => 'world'}) }
|
35
|
+
specify { expect(field.tap { |r| r.write({a: ''}) }.read).to eq({'a' => ''}) }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with :keys' do
|
39
|
+
let(:field) { attribute(type: String, keys: ['a', :b]) }
|
40
|
+
|
41
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
|
42
|
+
specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
|
43
|
+
specify { expect(field.tap { |r| r.write({a: 1, 'b' => 2, c: 3, 'd' => 4}) }.read).to eq({'a' => '1', 'b' => '2'}) }
|
44
|
+
specify { expect(field.tap { |r| r.write({a: 1, c: 3, 'd' => 4}) }.read).to eq({'a' => '1'}) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#read_before_type_cast' do
|
49
|
+
let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
|
50
|
+
|
51
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
|
52
|
+
specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
|
53
|
+
specify { expect(field.tap { |r| r.write({a: 1}) }.read_before_type_cast).to eq({'a' => 1}) }
|
54
|
+
specify { expect(field.tap { |r| r.write({a: nil}) }.read_before_type_cast).to eq({'a' => :world}) }
|
55
|
+
specify { expect(field.tap { |r| r.write({a: ''}) }.read_before_type_cast).to eq({'a' => ''}) }
|
56
|
+
specify { expect(field.tap { |r| r.write({a: 42}) }.read_before_type_cast).to eq({'a' => 42}) }
|
57
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', b: '42'}) }.read_before_type_cast).to eq({'a' => 'hello', 'b' => '42'}) }
|
58
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', b: '1'}) }.read_before_type_cast).to eq({'a' => 'hello', 'b' => '1'}) }
|
59
|
+
specify { expect(field.tap { |r| r.write({a: 'hello', x: '42'}) }.read_before_type_cast).to eq({'a' => 'hello', 'x' => '42'}) }
|
60
|
+
|
61
|
+
context do
|
62
|
+
let(:field) { attribute(type: String, default: :world) }
|
63
|
+
|
64
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
|
65
|
+
specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
|
66
|
+
specify { expect(field.tap { |r| r.write({a: 1}) }.read_before_type_cast).to eq({'a' => 1}) }
|
67
|
+
specify { expect(field.tap { |r| r.write({a: nil, b: nil}) }.read_before_type_cast).to eq({'a' => :world, 'b' => :world}) }
|
68
|
+
specify { expect(field.tap { |r| r.write({a: ''}) }.read_before_type_cast).to eq({'a' => ''}) }
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with :keys' do
|
72
|
+
let(:field) { attribute(type: String, keys: ['a', :b]) }
|
73
|
+
|
74
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
|
75
|
+
specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
|
76
|
+
specify { expect(field.tap { |r| r.write({a: 1, 'b' => 2, c: 3, 'd' => 4}) }.read_before_type_cast)
|
77
|
+
.to eq({'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}) }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'integration' do
|
82
|
+
before do
|
83
|
+
stub_model(:post) do
|
84
|
+
dictionary :options, type: String
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
specify { expect(Post.new(options: {a: 'hello', b: 42}).options).to eq({'a' => 'hello', 'b' => '42'}) }
|
89
|
+
specify { expect(Post.new(options: {a: 'hello', b: 42}).options_before_type_cast).to eq({'a' => 'hello', 'b' => 42}) }
|
90
|
+
specify { expect(Post.new.options?).to eq(false) }
|
91
|
+
specify { expect(Post.new(options: {a: 'hello', b: 42}).options?).to eq(true) }
|
92
|
+
end
|
93
|
+
end
|
@@ -1,49 +1,104 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe Localized do
|
5
|
-
|
6
|
-
Class.new do
|
7
|
-
include ActiveData::Model
|
4
|
+
describe ActiveData::Model::Attributes::Localized do
|
5
|
+
before { stub_model(:dummy) }
|
8
6
|
|
9
|
-
|
10
|
-
|
7
|
+
def attribute(*args)
|
8
|
+
options = args.extract_options!
|
9
|
+
reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Localized, :field, options)
|
10
|
+
described_class.new('field', Dummy.new)
|
11
11
|
end
|
12
|
-
let(:translations) { { en: 'Hello', ru: 'Привет' } }
|
13
|
-
before { I18n.locale = :en }
|
14
12
|
|
15
|
-
describe '#
|
16
|
-
|
17
|
-
its(:name_translations) { should == translations.stringify_keys }
|
18
|
-
its(:name) { should == translations[:en] }
|
19
|
-
end
|
13
|
+
describe '#read' do
|
14
|
+
let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
|
17
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read).to eq({ 'en' => 'hello' }) }
|
18
|
+
specify { expect(field.tap { |r| r.write({ en: 42 }) }.read).to eq({ 'en' => '42' }) }
|
19
|
+
specify { expect(field.tap { |r| r.write({ en: 43 }) }.read).to eq({ 'en' => nil }) }
|
20
|
+
specify { expect(field.tap { |r| r.write({ en: '' }) }.read).to eq({ 'en' => nil }) }
|
21
|
+
specify { expect(field.tap { |r| r.write({ en: nil }) }.read).to eq({ 'en' => nil }) }
|
22
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello', ru: 42 }) }.read).to eq({ 'en' => 'hello', 'ru' => '42' }) }
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
context do
|
25
|
+
let(:field) { attribute(type: String, default: :world) }
|
26
|
+
|
27
|
+
specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
|
28
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read).to eq({ 'en' => 'hello' }) }
|
29
|
+
specify { expect(field.tap { |r| r.write({ en: 42 }) }.read).to eq({ 'en' => '42' }) }
|
30
|
+
specify { expect(field.tap { |r| r.write({ en: '' }) }.read).to eq({ 'en' => '' }) }
|
31
|
+
specify { expect(field.tap { |r| r.write({ en: nil }) }.read).to eq({ 'en' => 'world' }) }
|
32
|
+
end
|
31
33
|
end
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
+
describe '#read_before_type_cast' do
|
36
|
+
let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
|
37
|
+
|
38
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
|
39
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read_before_type_cast).to eq({ 'en' => 'hello' }) }
|
40
|
+
specify { expect(field.tap { |r| r.write({ en: 42 }) }.read_before_type_cast).to eq({ 'en' => 42 }) }
|
41
|
+
specify { expect(field.tap { |r| r.write({ en: 43 }) }.read_before_type_cast).to eq({ 'en' => 43 }) }
|
42
|
+
specify { expect(field.tap { |r| r.write({ en: '' }) }.read_before_type_cast).to eq({ 'en' => '' }) }
|
43
|
+
specify { expect(field.tap { |r| r.write({ en: nil }) }.read_before_type_cast).to eq({ 'en' => :world }) }
|
44
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello', ru: 42 }) }.read_before_type_cast).to eq({ 'en' => 'hello', 'ru' => 42 }) }
|
45
|
+
|
35
46
|
context do
|
36
|
-
|
47
|
+
let(:field) { attribute(type: String, default: :world) }
|
48
|
+
|
49
|
+
specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
|
50
|
+
specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read_before_type_cast).to eq({ 'en' => 'hello' }) }
|
51
|
+
specify { expect(field.tap { |r| r.write({ en: 42 }) }.read_before_type_cast).to eq({ 'en' => 42 }) }
|
52
|
+
specify { expect(field.tap { |r| r.write({ en: '' }) }.read_before_type_cast).to eq({ 'en' => '' }) }
|
53
|
+
specify { expect(field.tap { |r| r.write({ en: nil }) }.read_before_type_cast).to eq({ 'en' => :world }) }
|
37
54
|
end
|
55
|
+
end
|
38
56
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
57
|
+
context 'integration' do
|
58
|
+
let(:klass) do
|
59
|
+
Class.new do
|
60
|
+
include ActiveData::Model
|
61
|
+
include ActiveData::Model::Localization
|
62
|
+
|
63
|
+
localized :name, type: String
|
64
|
+
end
|
65
|
+
end
|
66
|
+
let(:translations) { { en: 'Hello', ru: 'Привет' } }
|
67
|
+
before { I18n.locale = :en }
|
68
|
+
|
69
|
+
describe '#name_translations' do
|
70
|
+
subject { klass.new name_translations: translations }
|
71
|
+
its(:name_translations) { should == translations.stringify_keys }
|
72
|
+
its(:name) { should == translations[:en] }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#name' do
|
76
|
+
subject { klass.new name: 'Hello' }
|
77
|
+
its(:name_translations) { should == { 'en' => 'Hello' } }
|
78
|
+
its(:name) { should == 'Hello' }
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#name_before_type_cast' do
|
82
|
+
let(:object) { Object.new }
|
83
|
+
subject { klass.new name: object }
|
84
|
+
its(:name_before_type_cast) { should == object }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'fallbacks' do
|
88
|
+
subject { klass.new name_translations: { ru: 'Привет' } }
|
89
|
+
context do
|
90
|
+
its(:name) { should be_nil }
|
91
|
+
end
|
92
|
+
|
93
|
+
context do
|
94
|
+
before do
|
95
|
+
require "i18n/backend/fallbacks"
|
96
|
+
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
97
|
+
I18n.fallbacks.map(en: :ru)
|
98
|
+
end
|
99
|
+
after { I18n.fallbacks = false }
|
100
|
+
its(:name) { should == 'Привет' }
|
44
101
|
end
|
45
|
-
after { I18n.fallbacks = false }
|
46
|
-
its(:name) { should == 'Привет' }
|
47
102
|
end
|
48
103
|
end
|
49
|
-
end
|
104
|
+
end
|