active_data 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +13 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +53 -0
  5. data/.rvmrc +1 -1
  6. data/.travis.yml +15 -2
  7. data/Appraisals +1 -1
  8. data/CHANGELOG.md +31 -0
  9. data/Guardfile +8 -8
  10. data/README.md +256 -0
  11. data/Rakefile +2 -4
  12. data/active_data.gemspec +8 -7
  13. data/gemfiles/rails.4.0.gemfile +1 -1
  14. data/gemfiles/rails.4.1.gemfile +1 -1
  15. data/gemfiles/rails.4.2.gemfile +1 -1
  16. data/gemfiles/rails.5.0.gemfile +1 -1
  17. data/gemfiles/rails.5.1.gemfile +14 -0
  18. data/lib/active_data/active_record/associations.rb +18 -13
  19. data/lib/active_data/active_record/nested_attributes.rb +8 -14
  20. data/lib/active_data/base.rb +13 -0
  21. data/lib/active_data/config.rb +4 -4
  22. data/lib/active_data/errors.rb +29 -13
  23. data/lib/active_data/extensions.rb +22 -21
  24. data/lib/active_data/model/associations/base.rb +22 -6
  25. data/lib/active_data/model/associations/embeds_any.rb +17 -0
  26. data/lib/active_data/model/associations/embeds_many.rb +29 -19
  27. data/lib/active_data/model/associations/embeds_one.rb +30 -26
  28. data/lib/active_data/model/associations/nested_attributes.rb +82 -50
  29. data/lib/active_data/model/associations/persistence_adapters/active_record/referenced_proxy.rb +31 -0
  30. data/lib/active_data/model/associations/persistence_adapters/active_record.rb +66 -0
  31. data/lib/active_data/model/associations/persistence_adapters/base.rb +53 -0
  32. data/lib/active_data/model/associations/references_any.rb +41 -0
  33. data/lib/active_data/model/associations/references_many.rb +51 -37
  34. data/lib/active_data/model/associations/references_one.rb +43 -41
  35. data/lib/active_data/model/associations/reflections/base.rb +19 -29
  36. data/lib/active_data/model/associations/reflections/embeds_any.rb +43 -0
  37. data/lib/active_data/model/associations/reflections/embeds_many.rb +3 -13
  38. data/lib/active_data/model/associations/reflections/embeds_one.rb +5 -37
  39. data/lib/active_data/model/associations/reflections/references_any.rb +62 -0
  40. data/lib/active_data/model/associations/reflections/references_many.rb +7 -7
  41. data/lib/active_data/model/associations/reflections/references_one.rb +9 -7
  42. data/lib/active_data/model/associations/reflections/singular.rb +35 -0
  43. data/lib/active_data/model/associations/validations.rb +2 -27
  44. data/lib/active_data/model/associations.rb +12 -10
  45. data/lib/active_data/model/attributes/attribute.rb +10 -10
  46. data/lib/active_data/model/attributes/base.rb +8 -7
  47. data/lib/active_data/model/attributes/localized.rb +4 -4
  48. data/lib/active_data/model/attributes/reference_many.rb +6 -8
  49. data/lib/active_data/model/attributes/reference_one.rb +17 -9
  50. data/lib/active_data/model/attributes/reflections/attribute.rb +2 -2
  51. data/lib/active_data/model/attributes/reflections/base.rb +8 -11
  52. data/lib/active_data/model/attributes/reflections/localized.rb +2 -2
  53. data/lib/active_data/model/attributes/reflections/reference_one.rb +11 -22
  54. data/lib/active_data/model/attributes/reflections/represents.rb +5 -6
  55. data/lib/active_data/model/attributes/represents.rb +6 -5
  56. data/lib/active_data/model/attributes.rb +33 -87
  57. data/lib/active_data/model/callbacks.rb +6 -7
  58. data/lib/active_data/model/conventions.rb +2 -0
  59. data/lib/active_data/model/dirty.rb +4 -4
  60. data/lib/active_data/model/lifecycle.rb +18 -20
  61. data/lib/active_data/model/localization.rb +5 -2
  62. data/lib/active_data/model/persistence.rb +2 -2
  63. data/lib/active_data/model/primary.rb +19 -14
  64. data/lib/active_data/model/representation.rb +81 -0
  65. data/lib/active_data/model/scopes.rb +22 -12
  66. data/lib/active_data/model/validations/associated.rb +3 -2
  67. data/lib/active_data/model/validations/nested.rb +6 -1
  68. data/lib/active_data/model/validations.rb +3 -3
  69. data/lib/active_data/model.rb +2 -1
  70. data/lib/active_data/undefined_class.rb +9 -0
  71. data/lib/active_data/version.rb +1 -1
  72. data/lib/active_data.rb +40 -17
  73. data/spec/lib/active_data/active_record/associations_spec.rb +107 -45
  74. data/spec/lib/active_data/active_record/nested_attributes_spec.rb +1 -2
  75. data/spec/lib/active_data/config_spec.rb +37 -15
  76. data/spec/lib/active_data/model/associations/embeds_many_spec.rb +475 -172
  77. data/spec/lib/active_data/model/associations/embeds_one_spec.rb +353 -96
  78. data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +108 -12
  79. data/spec/lib/active_data/model/associations/persistence_adapters/active_record_spec.rb +58 -0
  80. data/spec/lib/active_data/model/associations/references_many_spec.rb +440 -64
  81. data/spec/lib/active_data/model/associations/references_one_spec.rb +347 -36
  82. data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +8 -7
  83. data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +7 -6
  84. data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +81 -33
  85. data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +116 -37
  86. data/spec/lib/active_data/model/associations/validations_spec.rb +27 -43
  87. data/spec/lib/active_data/model/associations_spec.rb +34 -25
  88. data/spec/lib/active_data/model/attributes/attribute_spec.rb +26 -23
  89. data/spec/lib/active_data/model/attributes/base_spec.rb +5 -6
  90. data/spec/lib/active_data/model/attributes/collection_spec.rb +7 -8
  91. data/spec/lib/active_data/model/attributes/dictionary_spec.rb +40 -33
  92. data/spec/lib/active_data/model/attributes/localized_spec.rb +27 -28
  93. data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +6 -6
  94. data/spec/lib/active_data/model/attributes/represents_spec.rb +10 -78
  95. data/spec/lib/active_data/model/attributes_spec.rb +150 -45
  96. data/spec/lib/active_data/model/callbacks_spec.rb +69 -70
  97. data/spec/lib/active_data/model/conventions_spec.rb +0 -1
  98. data/spec/lib/active_data/model/dirty_spec.rb +22 -13
  99. data/spec/lib/active_data/model/lifecycle_spec.rb +49 -23
  100. data/spec/lib/active_data/model/persistence_spec.rb +5 -6
  101. data/spec/lib/active_data/model/representation_spec.rb +126 -0
  102. data/spec/lib/active_data/model/scopes_spec.rb +1 -3
  103. data/spec/lib/active_data/model/typecasting_spec.rb +6 -5
  104. data/spec/lib/active_data/model/validations/associated_spec.rb +26 -18
  105. data/spec/lib/active_data/model/validations/nested_spec.rb +89 -18
  106. data/spec/lib/active_data/model_spec.rb +1 -2
  107. data/spec/lib/active_data_spec.rb +0 -1
  108. data/spec/shared/nested_attribute_examples.rb +332 -0
  109. data/spec/spec_helper.rb +3 -0
  110. data/spec/support/model_helpers.rb +2 -2
  111. data/spec/support/muffle_helper.rb +7 -0
  112. metadata +52 -18
  113. data/lib/active_data/model/associations/collection/referenced.rb +0 -26
  114. data/lib/active_data/model/associations/reflections/reference_reflection.rb +0 -45
  115. data/spec/lib/active_data/model/nested_attributes.rb +0 -202
@@ -5,12 +5,12 @@ describe ActiveData::Model::Attributes::Attribute do
5
5
 
6
6
  def attribute(*args)
7
7
  options = args.extract_options!
8
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Attribute, :field, {type: Object}.merge(options))
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Attribute, :field, {type: Object}.merge(options))
9
9
  Dummy.new.attribute(:field)
10
10
  end
11
11
 
12
12
  describe '#read' do
13
- let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v ? v.strip : v }, default: :world, enum: %w[hello 42 world]) }
14
14
 
15
15
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq('world') }
16
16
  specify { expect(field.tap { |r| r.write(:world) }.read).to eq('world') }
@@ -26,7 +26,7 @@ describe ActiveData::Model::Attributes::Attribute do
26
26
  end
27
27
 
28
28
  describe '#read_before_type_cast' do
29
- let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
29
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.strip }, default: :world, enum: %w[hello 42 world]) }
30
30
 
31
31
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
32
32
  specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
@@ -54,6 +54,7 @@ describe ActiveData::Model::Attributes::Attribute do
54
54
  specify { expect(attribute.defaultize(nil)).to be_nil }
55
55
  specify { expect(attribute(default: 'hello').defaultize(nil)).to eq('hello') }
56
56
  specify { expect(attribute(default: 'hello').defaultize('world')).to eq('world') }
57
+ specify { expect(attribute(default: false, type: Boolean).defaultize(nil)).to eq(false) }
57
58
  end
58
59
 
59
60
  describe '#typecast' do
@@ -68,7 +69,7 @@ describe ActiveData::Model::Attributes::Attribute do
68
69
  specify { expect(attribute.enum).to eq([].to_set) }
69
70
  specify { expect(attribute(enum: []).enum).to eq([].to_set) }
70
71
  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: %w[hello world]).enum).to eq(%w[hello world].to_set) }
72
73
  specify { expect(attribute(enum: [1..5]).enum).to eq([1..5].to_set) }
73
74
  specify { expect(attribute(enum: 1..5).enum).to eq((1..5).to_a.to_set) }
74
75
  specify { expect(attribute(enum: -> { 1..5 }).enum).to eq((1..5).to_a.to_set) }
@@ -97,21 +98,19 @@ describe ActiveData::Model::Attributes::Attribute do
97
98
  before { allow_any_instance_of(Dummy).to receive_messages(value: 'value') }
98
99
  let(:other) { 'other' }
99
100
 
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') }
101
+ specify { expect(attribute(normalizer: ->(_v) { value }).normalize(' hello ')).to eq('value') }
102
+ specify { expect(attribute(normalizer: ->(_v, object) { object.value }).normalize(' hello ')).to eq('value') }
103
+ specify { expect(attribute(normalizer: ->(_v, _object) { other }).normalize(' hello ')).to eq('other') }
103
104
  end
104
105
 
105
106
  context 'integration' do
106
107
  before do
107
108
  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|
109
+ ActiveData.normalizer(:strip) { |value, _, _| value.strip }
110
+ ActiveData.normalizer(:trim) do |value, options, _attribute|
112
111
  value.first(length || options[:length] || 2)
113
112
  end
114
- ActiveData.normalizer(:reset) do |value, options, attribute|
113
+ ActiveData.normalizer(:reset) do |value, _options, attribute|
115
114
  empty = value.respond_to?(:empty?) ? value.empty? : value.nil?
116
115
  empty ? attribute.default : value
117
116
  end
@@ -120,24 +119,28 @@ describe ActiveData::Model::Attributes::Attribute do
120
119
  let(:length) { nil }
121
120
 
122
121
  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') }
122
+ specify { expect(attribute(normalizer: %i[strip trim]).normalize(' hello ')).to eq('he') }
123
+ specify { expect(attribute(normalizer: %i[trim strip]).normalize(' hello ')).to eq('h') }
124
+ specify { expect(attribute(normalizer: [:strip, {trim: {length: 4}}]).normalize(' hello ')).to eq('hell') }
125
+ specify { expect(attribute(normalizer: {strip: {}, trim: {length: 4}}).normalize(' hello ')).to eq('hell') }
126
+ specify do
127
+ expect(attribute(normalizer: [:strip, {trim: {length: 4}}, ->(v) { v.last(2) }])
128
+ .normalize(' hello ')).to eq('ll')
129
+ end
129
130
  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: %i[strip reset]).normalize(' ')).to eq(nil) }
131
132
  specify { expect(attribute(normalizer: :reset, default: '!!!').normalize(nil)).to eq('!!!') }
132
133
  specify { expect(attribute(normalizer: :reset, default: '!!!').normalize('')).to eq('!!!') }
133
134
 
134
135
  context do
135
136
  let(:length) { 3 }
136
137
 
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') }
138
+ specify { expect(attribute(normalizer: [:strip, {trim: {length: 4}}]).normalize(' hello ')).to eq('hel') }
139
+ specify { expect(attribute(normalizer: {strip: {}, trim: {length: 4}}).normalize(' hello ')).to eq('hel') }
140
+ specify do
141
+ expect(attribute(normalizer: [:strip, {trim: {length: 4}}, ->(v) { v.last(2) }])
142
+ .normalize(' hello ')).to eq('el')
143
+ end
141
144
  end
142
145
  end
143
146
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Attributes::Base do
@@ -6,12 +5,12 @@ describe ActiveData::Model::Attributes::Base do
6
5
 
7
6
  def attribute(*args)
8
7
  options = args.extract_options!
9
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Base, :field, options)
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Base, :field, options)
10
9
  Dummy.new.attribute(:field)
11
10
  end
12
11
 
13
12
  describe '#read' do
14
- let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v ? v.strip : v }, default: :world, enum: %w[hello 42 world]) }
15
14
  let(:object) { Object.new }
16
15
 
17
16
  specify { expect(field.tap { |r| r.write(nil) }.read).to be_nil }
@@ -25,7 +24,7 @@ describe ActiveData::Model::Attributes::Base do
25
24
  end
26
25
 
27
26
  describe '#read_before_type_cast' do
28
- let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
27
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.strip }, default: :world, enum: %w[hello 42 world]) }
29
28
  let(:object) { Object.new }
30
29
 
31
30
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to be_nil }
@@ -52,7 +51,7 @@ describe ActiveData::Model::Attributes::Base do
52
51
  specify { expect(field.tap { |r| r.write([]) }).not_to be_value_present }
53
52
  specify { expect(field.tap { |r| r.write([42]) }).to be_value_present }
54
53
  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 }
54
+ specify { expect(field.tap { |r| r.write(hello: 42) }).to be_value_present }
56
55
  end
57
56
 
58
57
  describe '#query' do
@@ -69,7 +68,7 @@ describe ActiveData::Model::Attributes::Base do
69
68
  specify { expect(field.tap { |r| r.write([]) }.query).to be(false) }
70
69
  specify { expect(field.tap { |r| r.write([42]) }.query).to be(true) }
71
70
  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) }
71
+ specify { expect(field.tap { |r| r.write(hello: 42) }.query).to be(true) }
73
72
  end
74
73
 
75
74
  describe '#readonly?' do
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Attributes::Collection do
@@ -6,12 +5,12 @@ describe ActiveData::Model::Attributes::Collection do
6
5
 
7
6
  def attribute(*args)
8
7
  options = args.extract_options!
9
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Collection, :field, options)
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Collection, :field, options)
10
9
  Dummy.new.attribute(:field)
11
10
  end
12
11
 
13
12
  describe '#read' do
14
- let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world, enum: ['hello', '42']) }
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world, enum: %w[hello 42]) }
15
14
 
16
15
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
17
16
  specify { expect(field.tap { |r| r.write([nil]) }.read).to eq([nil]) }
@@ -20,11 +19,11 @@ describe ActiveData::Model::Attributes::Collection do
20
19
  specify { expect(field.tap { |r| r.write([43]) }.read).to eq([nil]) }
21
20
  specify { expect(field.tap { |r| r.write([43, 44]) }.read).to eq([nil]) }
22
21
  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']) }
22
+ specify { expect(field.tap { |r| r.write(['hello', 42]) }.read).to eq(%w[hello 42]) }
24
23
  specify { expect(field.tap { |r| r.write(['hello', false]) }.read).to eq(['hello', nil]) }
25
24
 
26
25
  context do
27
- let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world) }
26
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world) }
28
27
 
29
28
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
30
29
  specify { expect(field.tap { |r| r.write([nil, nil]) }.read).to eq(['world']) }
@@ -35,7 +34,7 @@ describe ActiveData::Model::Attributes::Collection do
35
34
  end
36
35
 
37
36
  describe '#read_before_type_cast' do
38
- let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
37
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
39
38
 
40
39
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
41
40
  specify { expect(field.tap { |r| r.write([nil]) }.read_before_type_cast).to eq([:world]) }
@@ -51,7 +50,7 @@ describe ActiveData::Model::Attributes::Collection do
51
50
  let(:field) { attribute(type: String, default: :world) }
52
51
 
53
52
  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]) }
53
+ specify { expect(field.tap { |r| r.write([nil, nil]) }.read_before_type_cast).to eq(%i[world world]) }
55
54
  specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
56
55
  specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
57
56
  specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
@@ -65,7 +64,7 @@ describe ActiveData::Model::Attributes::Collection do
65
64
  end
66
65
  end
67
66
 
68
- specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(['hello', '42']) }
67
+ specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(%w[hello 42]) }
69
68
  specify { expect(Post.new(tags: ['hello', 42]).tags_before_type_cast).to eq(['hello', 42]) }
70
69
  specify { expect(Post.new.tags?).to eq(false) }
71
70
  specify { expect(Post.new(tags: ['hello', 42]).tags?).to eq(true) }
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Attributes::Dictionary do
@@ -6,33 +5,39 @@ describe ActiveData::Model::Attributes::Dictionary do
6
5
 
7
6
  def attribute(*args)
8
7
  options = args.extract_options!
9
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Dictionary, :field, options)
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Dictionary, :field, options)
10
9
  Dummy.new.attribute(:field)
11
10
  end
12
11
 
13
12
  describe '#read' do
14
- let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
15
- default: :world, enum: ['hello', '42']) }
13
+ let(:field) do
14
+ attribute(type: String,
15
+ normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
16
+ default: :world, enum: %w[hello 42])
17
+ end
16
18
 
17
19
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
18
20
  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'}) }
21
+ specify { expect(field.tap { |r| r.write(a: nil) }.read).to eq({}) }
22
+ specify { expect(field.tap { |r| r.write(a: '') }.read).to eq({}) }
23
+ specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq({}) }
24
+ specify { expect(field.tap { |r| r.write(a: 42) }.read).to eq('a' => '42') }
25
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read).to eq('a' => 'hello', 'b' => '42') }
26
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read).to eq('a' => 'hello') }
27
+ specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read).to eq('a' => 'hello', 'x' => '42') }
26
28
 
27
29
  context do
28
- let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
29
- default: :world) }
30
+ let(:field) do
31
+ attribute(type: String,
32
+ normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
33
+ default: :world)
34
+ end
30
35
 
31
36
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
32
37
  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' => ''}) }
38
+ specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq('a' => '1') }
39
+ specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read).to eq('a' => 'world', 'b' => 'world') }
40
+ specify { expect(field.tap { |r| r.write(a: '') }.read).to eq('a' => '') }
36
41
  end
37
42
 
38
43
  context 'with :keys' do
@@ -40,32 +45,32 @@ describe ActiveData::Model::Attributes::Dictionary do
40
45
 
41
46
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
42
47
  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'}) }
48
+ specify { expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read).to eq('a' => '1', 'b' => '2') }
49
+ specify { expect(field.tap { |r| r.write(a: 1, c: 3, 'd' => 4) }.read).to eq('a' => '1') }
45
50
  end
46
51
  end
47
52
 
48
53
  describe '#read_before_type_cast' do
49
- let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
54
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
50
55
 
51
56
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
52
57
  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'}) }
58
+ specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
59
+ specify { expect(field.tap { |r| r.write(a: nil) }.read_before_type_cast).to eq('a' => :world) }
60
+ specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
61
+ specify { expect(field.tap { |r| r.write(a: 42) }.read_before_type_cast).to eq('a' => 42) }
62
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '42') }
63
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '1') }
64
+ specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read_before_type_cast).to eq('a' => 'hello', 'x' => '42') }
60
65
 
61
66
  context do
62
67
  let(:field) { attribute(type: String, default: :world) }
63
68
 
64
69
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
65
70
  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' => ''}) }
71
+ specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
72
+ specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read_before_type_cast).to eq('a' => :world, 'b' => :world) }
73
+ specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
69
74
  end
70
75
 
71
76
  context 'with :keys' do
@@ -73,8 +78,10 @@ describe ActiveData::Model::Attributes::Dictionary do
73
78
 
74
79
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
75
80
  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}) }
81
+ specify do
82
+ expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read_before_type_cast)
83
+ .to eq('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4)
84
+ end
78
85
  end
79
86
  end
80
87
 
@@ -85,8 +92,8 @@ describe ActiveData::Model::Attributes::Dictionary do
85
92
  end
86
93
  end
87
94
 
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}) }
95
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options).to eq('a' => 'hello', 'b' => '42') }
96
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options_before_type_cast).to eq('a' => 'hello', 'b' => 42) }
90
97
  specify { expect(Post.new.options?).to eq(false) }
91
98
  specify { expect(Post.new(options: {a: 'hello', b: 42}).options?).to eq(true) }
92
99
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Attributes::Localized do
@@ -6,51 +5,51 @@ describe ActiveData::Model::Attributes::Localized do
6
5
 
7
6
  def attribute(*args)
8
7
  options = args.extract_options!
9
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Localized, :field, options)
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Localized, :field, options)
10
9
  described_class.new('field', Dummy.new)
11
10
  end
12
11
 
13
12
  describe '#read' do
14
- let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
13
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
15
14
 
16
15
  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' }) }
16
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
17
+ specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
18
+ specify { expect(field.tap { |r| r.write(en: 43) }.read).to eq('en' => nil) }
19
+ specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => nil) }
20
+ specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => nil) }
21
+ specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read).to eq('en' => 'hello', 'ru' => '42') }
23
22
 
24
23
  context do
25
24
  let(:field) { attribute(type: String, default: :world) }
26
25
 
27
26
  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' }) }
27
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
28
+ specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
29
+ specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => '') }
30
+ specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => 'world') }
32
31
  end
33
32
  end
34
33
 
35
34
  describe '#read_before_type_cast' do
36
- let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
35
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
37
36
 
38
37
  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 }) }
38
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
39
+ specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
40
+ specify { expect(field.tap { |r| r.write(en: 43) }.read_before_type_cast).to eq('en' => 43) }
41
+ specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
42
+ specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
43
+ specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read_before_type_cast).to eq('en' => 'hello', 'ru' => 42) }
45
44
 
46
45
  context do
47
46
  let(:field) { attribute(type: String, default: :world) }
48
47
 
49
48
  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 }) }
49
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
50
+ specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
51
+ specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
52
+ specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
54
53
  end
55
54
  end
56
55
 
@@ -63,7 +62,7 @@ describe ActiveData::Model::Attributes::Localized do
63
62
  localized :name, type: String
64
63
  end
65
64
  end
66
- let(:translations) { { en: 'Hello', ru: 'Привет' } }
65
+ let(:translations) { {en: 'Hello', ru: 'Привет'} }
67
66
  before { I18n.locale = :en }
68
67
 
69
68
  describe '#name_translations' do
@@ -74,7 +73,7 @@ describe ActiveData::Model::Attributes::Localized do
74
73
 
75
74
  describe '#name' do
76
75
  subject { klass.new name: 'Hello' }
77
- its(:name_translations) { should == { 'en' => 'Hello' } }
76
+ its(:name_translations) { should == {'en' => 'Hello'} }
78
77
  its(:name) { should == 'Hello' }
79
78
  end
80
79
 
@@ -85,14 +84,14 @@ describe ActiveData::Model::Attributes::Localized do
85
84
  end
86
85
 
87
86
  context 'fallbacks' do
88
- subject { klass.new name_translations: { ru: 'Привет' } }
87
+ subject { klass.new name_translations: {ru: 'Привет'} }
89
88
  context do
90
89
  its(:name) { should be_nil }
91
90
  end
92
91
 
93
92
  context do
94
93
  before do
95
- require "i18n/backend/fallbacks"
94
+ require 'i18n/backend/fallbacks'
96
95
  I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
97
96
  I18n.fallbacks.map(en: :ru)
98
97
  end
@@ -40,7 +40,7 @@ describe ActiveData::Model::Attributes::Reflections::Attribute do
40
40
  describe '#defaultizer' do
41
41
  specify { expect(reflection.defaultizer).to be_nil }
42
42
  specify { expect(reflection(default: 42).defaultizer).to eq(42) }
43
- specify { expect(reflection(default: ->{ }).defaultizer).to be_a Proc }
43
+ specify { expect(reflection(default: -> {}).defaultizer).to be_a Proc }
44
44
  end
45
45
 
46
46
  describe '#typecaster' do
@@ -58,15 +58,15 @@ describe ActiveData::Model::Attributes::Reflections::Attribute do
58
58
  describe '#enumerizer' do
59
59
  specify { expect(reflection.enumerizer).to be_nil }
60
60
  specify { expect(reflection(enum: 42).enumerizer).to eq(42) }
61
- specify { expect(reflection(enum: ->{ }).enumerizer).to be_a Proc }
61
+ specify { expect(reflection(enum: -> {}).enumerizer).to be_a Proc }
62
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) }
63
+ specify { expect(reflection(in: -> {}).enumerizer).to be_a Proc }
64
+ specify { expect(reflection(enum: 42, in: -> {}).enumerizer).to eq(42) }
65
65
  end
66
66
 
67
67
  describe '#normalizers' do
68
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 }
69
+ specify { expect(reflection(normalizer: -> {}).normalizers).to be_a Array }
70
+ specify { expect(reflection(normalizer: -> {}).normalizers.first).to be_a Proc }
71
71
  end
72
72
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Attributes::Represents do
@@ -6,7 +5,7 @@ describe ActiveData::Model::Attributes::Represents do
6
5
 
7
6
  def attribute(*args)
8
7
  options = args.extract_options!
9
- reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Represents, :full_name, options.reverse_merge(of: :subject))
8
+ Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Represents, :full_name, options.reverse_merge(of: :subject))
10
9
  Dummy.new.attribute(:full_name)
11
10
  end
12
11
 
@@ -16,6 +15,13 @@ describe ActiveData::Model::Attributes::Represents do
16
15
  end
17
16
  end
18
17
 
18
+ describe '#new' do
19
+ before { attribute(:full_name) }
20
+ let(:attributes) { {foo: 'bar'} }
21
+
22
+ specify { expect { Dummy.new(attributes) }.to_not change { attributes } }
23
+ end
24
+
19
25
  describe '#write' do
20
26
  subject { Subject.new }
21
27
  before { allow_any_instance_of(Dummy).to receive_messages(value: 42, subject: subject) }
@@ -27,7 +33,7 @@ describe ActiveData::Model::Attributes::Represents do
27
33
  describe '#read' do
28
34
  subject { Subject.new(full_name: :hello) }
29
35
  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]) }
36
+ let(:field) { attribute(normalizer: ->(v) { v && v.is_a?(String) ? v.strip : v }, default: :world, enum: ['hello', '42', 'world', :world]) }
31
37
 
32
38
  specify { expect(field.read).to eq('hello') }
33
39
  specify { expect(field.tap { |r| r.write(nil) }.read).to eq(:world) }
@@ -54,7 +60,7 @@ describe ActiveData::Model::Attributes::Represents do
54
60
  describe '#read_before_type_cast' do
55
61
  subject { Subject.new(full_name: :hello) }
56
62
  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']) }
63
+ let(:field) { attribute(normalizer: ->(v) { v.strip }, default: :world, enum: %w[hello 42 world]) }
58
64
 
59
65
  specify { expect(field.read_before_type_cast).to eq(:hello) }
60
66
  specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
@@ -76,78 +82,4 @@ describe ActiveData::Model::Attributes::Represents do
76
82
  specify { expect { sleep(0.01) }.not_to change { field.read_before_type_cast } }
77
83
  end
78
84
  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
85
  end