granite-form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +13 -0
  3. data/.github/workflows/ci.yml +35 -0
  4. data/.github/workflows/main.yml +29 -0
  5. data/.gitignore +21 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +64 -0
  8. data/.rubocop_todo.yml +48 -0
  9. data/Appraisals +8 -0
  10. data/CHANGELOG.md +73 -0
  11. data/Gemfile +8 -0
  12. data/Guardfile +77 -0
  13. data/LICENSE +22 -0
  14. data/README.md +429 -0
  15. data/Rakefile +6 -0
  16. data/gemfiles/rails.4.2.gemfile +15 -0
  17. data/gemfiles/rails.5.0.gemfile +15 -0
  18. data/gemfiles/rails.5.1.gemfile +15 -0
  19. data/gemfiles/rails.5.2.gemfile +15 -0
  20. data/gemfiles/rails.6.0.gemfile +14 -0
  21. data/gemfiles/rails.6.1.gemfile +14 -0
  22. data/gemfiles/rails.7.0.gemfile +14 -0
  23. data/granite-form.gemspec +31 -0
  24. data/lib/granite/form/active_record/associations.rb +57 -0
  25. data/lib/granite/form/active_record/nested_attributes.rb +20 -0
  26. data/lib/granite/form/base.rb +15 -0
  27. data/lib/granite/form/config.rb +42 -0
  28. data/lib/granite/form/errors.rb +111 -0
  29. data/lib/granite/form/extensions.rb +36 -0
  30. data/lib/granite/form/model/associations/base.rb +97 -0
  31. data/lib/granite/form/model/associations/collection/embedded.rb +14 -0
  32. data/lib/granite/form/model/associations/collection/proxy.rb +35 -0
  33. data/lib/granite/form/model/associations/embeds_any.rb +19 -0
  34. data/lib/granite/form/model/associations/embeds_many.rb +152 -0
  35. data/lib/granite/form/model/associations/embeds_one.rb +112 -0
  36. data/lib/granite/form/model/associations/nested_attributes.rb +215 -0
  37. data/lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb +33 -0
  38. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +68 -0
  39. data/lib/granite/form/model/associations/persistence_adapters/base.rb +55 -0
  40. data/lib/granite/form/model/associations/references_any.rb +43 -0
  41. data/lib/granite/form/model/associations/references_many.rb +113 -0
  42. data/lib/granite/form/model/associations/references_one.rb +88 -0
  43. data/lib/granite/form/model/associations/reflections/base.rb +92 -0
  44. data/lib/granite/form/model/associations/reflections/embeds_any.rb +52 -0
  45. data/lib/granite/form/model/associations/reflections/embeds_many.rb +17 -0
  46. data/lib/granite/form/model/associations/reflections/embeds_one.rb +19 -0
  47. data/lib/granite/form/model/associations/reflections/references_any.rb +65 -0
  48. data/lib/granite/form/model/associations/reflections/references_many.rb +30 -0
  49. data/lib/granite/form/model/associations/reflections/references_one.rb +32 -0
  50. data/lib/granite/form/model/associations/reflections/singular.rb +37 -0
  51. data/lib/granite/form/model/associations/validations.rb +41 -0
  52. data/lib/granite/form/model/associations.rb +120 -0
  53. data/lib/granite/form/model/attributes/attribute.rb +75 -0
  54. data/lib/granite/form/model/attributes/base.rb +134 -0
  55. data/lib/granite/form/model/attributes/collection.rb +19 -0
  56. data/lib/granite/form/model/attributes/dictionary.rb +28 -0
  57. data/lib/granite/form/model/attributes/localized.rb +44 -0
  58. data/lib/granite/form/model/attributes/reference_many.rb +21 -0
  59. data/lib/granite/form/model/attributes/reference_one.rb +52 -0
  60. data/lib/granite/form/model/attributes/reflections/attribute.rb +61 -0
  61. data/lib/granite/form/model/attributes/reflections/base.rb +62 -0
  62. data/lib/granite/form/model/attributes/reflections/collection.rb +12 -0
  63. data/lib/granite/form/model/attributes/reflections/dictionary.rb +15 -0
  64. data/lib/granite/form/model/attributes/reflections/localized.rb +45 -0
  65. data/lib/granite/form/model/attributes/reflections/reference_many.rb +12 -0
  66. data/lib/granite/form/model/attributes/reflections/reference_one.rb +49 -0
  67. data/lib/granite/form/model/attributes/reflections/represents.rb +56 -0
  68. data/lib/granite/form/model/attributes/represents.rb +67 -0
  69. data/lib/granite/form/model/attributes.rb +204 -0
  70. data/lib/granite/form/model/callbacks.rb +72 -0
  71. data/lib/granite/form/model/conventions.rb +40 -0
  72. data/lib/granite/form/model/dirty.rb +84 -0
  73. data/lib/granite/form/model/lifecycle.rb +309 -0
  74. data/lib/granite/form/model/localization.rb +26 -0
  75. data/lib/granite/form/model/persistence.rb +59 -0
  76. data/lib/granite/form/model/primary.rb +59 -0
  77. data/lib/granite/form/model/representation.rb +101 -0
  78. data/lib/granite/form/model/scopes.rb +118 -0
  79. data/lib/granite/form/model/validations/associated.rb +22 -0
  80. data/lib/granite/form/model/validations/nested.rb +56 -0
  81. data/lib/granite/form/model/validations.rb +29 -0
  82. data/lib/granite/form/model.rb +33 -0
  83. data/lib/granite/form/railtie.rb +9 -0
  84. data/lib/granite/form/undefined_class.rb +11 -0
  85. data/lib/granite/form/version.rb +5 -0
  86. data/lib/granite/form.rb +163 -0
  87. data/spec/lib/granite/form/active_record/associations_spec.rb +211 -0
  88. data/spec/lib/granite/form/active_record/nested_attributes_spec.rb +15 -0
  89. data/spec/lib/granite/form/config_spec.rb +66 -0
  90. data/spec/lib/granite/form/model/associations/embeds_many_spec.rb +706 -0
  91. data/spec/lib/granite/form/model/associations/embeds_one_spec.rb +533 -0
  92. data/spec/lib/granite/form/model/associations/nested_attributes_spec.rb +119 -0
  93. data/spec/lib/granite/form/model/associations/persistence_adapters/active_record_spec.rb +58 -0
  94. data/spec/lib/granite/form/model/associations/references_many_spec.rb +572 -0
  95. data/spec/lib/granite/form/model/associations/references_one_spec.rb +445 -0
  96. data/spec/lib/granite/form/model/associations/reflections/embeds_any_spec.rb +42 -0
  97. data/spec/lib/granite/form/model/associations/reflections/embeds_many_spec.rb +145 -0
  98. data/spec/lib/granite/form/model/associations/reflections/embeds_one_spec.rb +117 -0
  99. data/spec/lib/granite/form/model/associations/reflections/references_many_spec.rb +303 -0
  100. data/spec/lib/granite/form/model/associations/reflections/references_one_spec.rb +287 -0
  101. data/spec/lib/granite/form/model/associations/validations_spec.rb +137 -0
  102. data/spec/lib/granite/form/model/associations_spec.rb +198 -0
  103. data/spec/lib/granite/form/model/attributes/attribute_spec.rb +186 -0
  104. data/spec/lib/granite/form/model/attributes/base_spec.rb +97 -0
  105. data/spec/lib/granite/form/model/attributes/collection_spec.rb +72 -0
  106. data/spec/lib/granite/form/model/attributes/dictionary_spec.rb +100 -0
  107. data/spec/lib/granite/form/model/attributes/localized_spec.rb +103 -0
  108. data/spec/lib/granite/form/model/attributes/reflections/attribute_spec.rb +72 -0
  109. data/spec/lib/granite/form/model/attributes/reflections/base_spec.rb +56 -0
  110. data/spec/lib/granite/form/model/attributes/reflections/collection_spec.rb +37 -0
  111. data/spec/lib/granite/form/model/attributes/reflections/dictionary_spec.rb +43 -0
  112. data/spec/lib/granite/form/model/attributes/reflections/localized_spec.rb +37 -0
  113. data/spec/lib/granite/form/model/attributes/reflections/represents_spec.rb +70 -0
  114. data/spec/lib/granite/form/model/attributes/represents_spec.rb +85 -0
  115. data/spec/lib/granite/form/model/attributes_spec.rb +350 -0
  116. data/spec/lib/granite/form/model/callbacks_spec.rb +337 -0
  117. data/spec/lib/granite/form/model/conventions_spec.rb +11 -0
  118. data/spec/lib/granite/form/model/dirty_spec.rb +84 -0
  119. data/spec/lib/granite/form/model/lifecycle_spec.rb +356 -0
  120. data/spec/lib/granite/form/model/persistence_spec.rb +46 -0
  121. data/spec/lib/granite/form/model/primary_spec.rb +84 -0
  122. data/spec/lib/granite/form/model/representation_spec.rb +139 -0
  123. data/spec/lib/granite/form/model/scopes_spec.rb +86 -0
  124. data/spec/lib/granite/form/model/typecasting_spec.rb +193 -0
  125. data/spec/lib/granite/form/model/validations/associated_spec.rb +102 -0
  126. data/spec/lib/granite/form/model/validations/nested_spec.rb +164 -0
  127. data/spec/lib/granite/form/model/validations_spec.rb +31 -0
  128. data/spec/lib/granite/form/model_spec.rb +10 -0
  129. data/spec/lib/granite/form_spec.rb +11 -0
  130. data/spec/shared/nested_attribute_examples.rb +332 -0
  131. data/spec/spec_helper.rb +50 -0
  132. data/spec/support/model_helpers.rb +10 -0
  133. data/spec/support/muffle_helper.rb +7 -0
  134. metadata +403 -0
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
4
+ before do
5
+ stub_model(:author) do
6
+ include Granite::Form::Model::Lifecycle
7
+ attribute :name, String
8
+ end
9
+
10
+ stub_model(:book) do
11
+ include Granite::Form::Model::Associations
12
+
13
+ attribute :title, String
14
+ embeds_one :author
15
+ end
16
+ end
17
+ let(:book) { Book.new }
18
+
19
+ specify { expect(book.author).to be_nil }
20
+
21
+ context ':read, :write' do
22
+ before do
23
+ stub_model(:book) do
24
+ include Granite::Form::Model::Persistence
25
+ include Granite::Form::Model::Associations
26
+
27
+ attribute :title
28
+ embeds_one :author,
29
+ read: lambda { |reflection, object|
30
+ value = object.read_attribute(reflection.name)
31
+ JSON.parse(value) if value.present?
32
+ },
33
+ write: lambda { |reflection, object, value|
34
+ object.write_attribute(reflection.name, value ? value.to_json : nil)
35
+ }
36
+ end
37
+ end
38
+
39
+ let(:book) { Book.instantiate author: {name: 'Duke'}.to_json }
40
+ let(:author) { Author.new(name: 'Rick') }
41
+
42
+ specify do
43
+ expect { book.author = author }
44
+ .to change { book.read_attribute(:author) }
45
+ .from({name: 'Duke'}.to_json).to({name: 'Rick'}.to_json)
46
+ end
47
+ end
48
+
49
+ describe '#author=' do
50
+ let(:author) { Author.new name: 'Author' }
51
+ specify { expect { book.author = author }.to change { book.author }.from(nil).to(author) }
52
+ specify { expect { book.author = 'string' }.to raise_error Granite::Form::AssociationTypeMismatch }
53
+
54
+ context do
55
+ let(:other) { Author.new name: 'Other' }
56
+ before { book.author = other }
57
+ specify { expect { book.author = author }.to change { book.author }.from(other).to(author) }
58
+ specify { expect { book.author = nil }.to change { book.author }.from(other).to(nil) }
59
+ end
60
+ end
61
+
62
+ describe '#build_author' do
63
+ let(:author) { Author.new name: 'Author' }
64
+ specify { expect(book.build_author(name: 'Author')).to eq(author) }
65
+ specify { expect { book.build_author(name: 'Author') }.to change { book.author }.from(nil).to(author) }
66
+ end
67
+
68
+ describe '#create_author' do
69
+ let(:author) { Author.new name: 'Author' }
70
+ specify { expect(book.create_author(name: 'Author')).to eq(author) }
71
+ specify { expect { book.create_author(name: 'Author') }.to change { book.author }.from(nil).to(author) }
72
+ end
73
+
74
+ describe '#create_author!' do
75
+ let(:author) { Author.new name: 'Author' }
76
+ specify { expect(book.create_author!(name: 'Author')).to eq(author) }
77
+ specify { expect { book.create_author!(name: 'Author') }.to change { book.author }.from(nil).to(author) }
78
+ end
79
+
80
+ context 'on the fly' do
81
+ context do
82
+ before do
83
+ stub_model(:book) do
84
+ include Granite::Form::Model::Associations
85
+
86
+ attribute :title, String
87
+ embeds_one :author do
88
+ attribute :name, String
89
+ end
90
+ end
91
+ end
92
+
93
+ specify { expect(Book.reflect_on_association(:author).klass).to eq(Book::Author) }
94
+ specify { expect(Book.new.author).to be_nil }
95
+ specify { expect(Book.new.tap { |b| b.create_author(name: 'Author') }.author).to be_a(Book::Author) }
96
+ specify { expect(Book.new.tap { |b| b.create_author(name: 'Author') }.read_attribute(:author)).to eq('name' => 'Author') }
97
+ end
98
+
99
+ context do
100
+ before do
101
+ stub_model(:book) do
102
+ include Granite::Form::Model::Associations
103
+
104
+ attribute :title, String
105
+ embeds_one :author, class_name: 'Author' do
106
+ attribute :age, Integer
107
+ end
108
+ end
109
+ end
110
+
111
+ specify { expect(Book.reflect_on_association(:author).klass).to eq(Book::Author) }
112
+ specify { expect(Book.new.author).to be_nil }
113
+ specify { expect(Book.new.tap { |b| b.create_author(name: 'Author') }.author).to be_a(Book::Author) }
114
+ specify { expect(Book.new.tap { |b| b.create_author(name: 'Author') }.read_attribute(:author)).to eq('name' => 'Author', 'age' => nil) }
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,303 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Associations::Reflections::ReferencesMany do
4
+ before do
5
+ stub_class(:author, ActiveRecord::Base) do
6
+ scope :name_starts_with_a, -> { name_starts_with('a') }
7
+ scope :name_starts_with, ->(letter) { where("name LIKE '#{letter}%'") }
8
+ end
9
+
10
+ stub_model(:book) do
11
+ include Granite::Form::Model::Associations
12
+
13
+ attribute :title, String
14
+ references_many :authors
15
+ end
16
+ end
17
+
18
+ let(:author) { Author.create!(name: 'Rick') }
19
+ let(:other) { Author.create!(name: 'John') }
20
+ let(:book) { Book.new }
21
+ let(:book_with_author) { Book.new(authors: [author]) }
22
+
23
+ specify { expect(book.authors).to be_empty }
24
+
25
+ context ':class_name' do
26
+ before do
27
+ stub_model(:book) do
28
+ include Granite::Form::Model::Associations
29
+
30
+ attribute :title, String
31
+ references_many :creators, class_name: 'Author'
32
+ end
33
+ end
34
+
35
+ let(:book) { Book.new }
36
+
37
+ specify do
38
+ expect { book.creators << author }
39
+ .to change { book.creators }.from([]).to([author])
40
+ end
41
+ specify do
42
+ expect { book.creators << author }
43
+ .to change { book.creator_ids }.from([]).to([author.id])
44
+ end
45
+ end
46
+
47
+ describe ':primary_key' do
48
+ before do
49
+ stub_model(:book) do
50
+ include Granite::Form::Model::Associations
51
+ collection :author_names, String
52
+ references_many :authors, primary_key: 'name'
53
+ end
54
+ end
55
+
56
+ let(:author) { Author.create!(name: 'Rick') }
57
+
58
+ specify do
59
+ expect { book.author_names = [author.name] }
60
+ .to change { book.authors }.from([]).to([author])
61
+ end
62
+ specify do
63
+ expect { book.authors = [author] }
64
+ .to change { book.author_names }.from([]).to([author.name])
65
+ end
66
+ end
67
+
68
+ describe ':reference_key' do
69
+ before do
70
+ stub_model(:book) do
71
+ include Granite::Form::Model::Associations
72
+ references_many :authors, reference_key: 'identify'
73
+ end
74
+ end
75
+
76
+ let(:author) { Author.create!(name: 'Rick') }
77
+
78
+ specify do
79
+ expect { book.identify = [author.id] }
80
+ .to change { book.authors }.from([]).to([author])
81
+ end
82
+ specify do
83
+ expect { book.authors = [author] }
84
+ .to change { book.identify }.from([]).to([author.id])
85
+ end
86
+ end
87
+
88
+ describe ':default' do
89
+ shared_examples_for :persisted_default do |default|
90
+ before do
91
+ stub_model(:book) do
92
+ include Granite::Form::Model::Associations
93
+ references_many :authors
94
+ references_many :owners, class_name: 'Author', default: default
95
+ end
96
+ end
97
+
98
+ let(:author) { Author.create! }
99
+ let(:other) { Author.create! }
100
+ let(:book) { Book.new(authors: [author]) }
101
+
102
+ specify { expect(book.owner_ids).to eq([author.id]) }
103
+ specify { expect(book.owners).to eq([author]) }
104
+ specify { expect { book.owners = [other] }.to change { book.owner_ids }.from([author.id]).to([other.id]) }
105
+ specify { expect { book.owners = [other] }.to change { book.owners }.from([author]).to([other]) }
106
+ specify { expect { book.owner_ids = [other.id] }.to change { book.owner_ids }.from([author.id]).to([other.id]) }
107
+ specify { expect { book.owner_ids = [other.id] }.to change { book.owners }.from([author]).to([other]) }
108
+ specify { expect { book.owners = [] }.to change { book.owner_ids }.from([author.id]).to([]) }
109
+ specify { expect { book.owners = [] }.to change { book.owners }.from([author]).to([]) }
110
+ specify { expect { book.owner_ids = [] }.not_to change { book.owner_ids }.from([author.id]) }
111
+ specify { expect { book.owner_ids = [] }.not_to change { book.owners }.from([author]) }
112
+ specify { expect { book.owner_ids = [nil] }.to change { book.owner_ids }.from([author.id]).to([]) }
113
+ specify { expect { book.owner_ids = [nil] }.to change { book.owners }.from([author]).to([]) }
114
+ specify { expect { book.owner_ids = [''] }.to change { book.owner_ids }.from([author.id]).to([]) }
115
+ specify { expect { book.owner_ids = [''] }.to change { book.owners }.from([author]).to([]) }
116
+ specify { expect { book.owner_ids = nil }.not_to change { book.owner_ids }.from([author.id]) }
117
+ specify { expect { book.owner_ids = nil }.not_to change { book.owners }.from([author]) }
118
+ specify { expect { book.owner_ids = '' }.to change { book.owner_ids }.from([author.id]).to([]) }
119
+ specify { expect { book.owner_ids = '' }.to change { book.owners }.from([author]).to([]) }
120
+ end
121
+
122
+ it_behaves_like :persisted_default, -> { authors.map(&:id) }
123
+ it_behaves_like :persisted_default, -> { authors }
124
+
125
+ shared_examples_for :new_record_default do |default|
126
+ before do
127
+ stub_model(:book) do
128
+ include Granite::Form::Model::Associations
129
+ references_many :authors
130
+ references_many :owners, class_name: 'Author', default: default
131
+ end
132
+ end
133
+
134
+ let(:author) { Author.create! }
135
+ let(:book) { Book.new }
136
+
137
+ specify { expect(book.owner_ids).to eq([nil]) }
138
+ specify { expect(book.owners).to match([an_instance_of(Author).and(have_attributes(name: 'Author'))]) }
139
+ specify { expect { book.owners = [other] }.to change { book.owner_ids }.from([nil]).to([other.id]) }
140
+ specify { expect { book.owners = [other] }.to change { book.owners }.from([an_instance_of(Author)]).to([other]) }
141
+ specify { expect { book.owner_ids = [other.id] }.to change { book.owner_ids }.from([nil]).to([other.id]) }
142
+ specify { expect { book.owner_ids = [other.id] }.to change { book.owners }.from([an_instance_of(Author)]).to([other]) }
143
+ specify { expect { book.owners = [] }.to change { book.owner_ids }.from([nil]).to([]) }
144
+ specify { expect { book.owners = [] }.to change { book.owners }.from([an_instance_of(Author)]).to([]) }
145
+ specify { expect { book.owner_ids = [] }.not_to change { book.owner_ids }.from([nil]) }
146
+ specify { expect { book.owner_ids = [] }.not_to change { book.owners }.from([an_instance_of(Author)]) }
147
+ specify { expect { book.owner_ids = [nil] }.to change { book.owner_ids }.from([nil]).to([]) }
148
+ specify { expect { book.owner_ids = [nil] }.to change { book.owners }.from([an_instance_of(Author)]).to([]) }
149
+ specify { expect { book.owner_ids = [''] }.to change { book.owner_ids }.from([nil]).to([]) }
150
+ specify { expect { book.owner_ids = [''] }.to change { book.owners }.from([an_instance_of(Author)]).to([]) }
151
+ specify { expect { book.owner_ids = nil }.not_to change { book.owner_ids }.from([nil]) }
152
+ specify { expect { book.owner_ids = nil }.not_to change { book.owners }.from([an_instance_of(Author)]) }
153
+ specify { expect { book.owner_ids = '' }.to change { book.owner_ids }.from([nil]).to([]) }
154
+ specify { expect { book.owner_ids = '' }.to change { book.owners }.from([an_instance_of(Author)]).to([]) }
155
+ end
156
+
157
+ it_behaves_like :new_record_default, name: 'Author'
158
+ it_behaves_like :new_record_default, -> { Author.new(name: 'Author') }
159
+ end
160
+
161
+ describe 'Book.inspect' do
162
+ specify { expect(Book.inspect).to eq('Book(authors: ReferencesMany(Author), title: String, author_ids: (reference))') }
163
+ end
164
+
165
+ describe '#scope' do
166
+ before do
167
+ stub_model(:book) do
168
+ include Granite::Form::Model::Associations
169
+ references_many :authors, ->(owner) { name_starts_with(owner.letter) }
170
+ attribute :letter, String
171
+ end
172
+ end
173
+
174
+ let(:book) { Book.new(letter: 'a') }
175
+ let!(:author1) { Author.create!(name: 'Rick') }
176
+ let!(:author2) { Author.create!(name: 'Aaron') }
177
+
178
+ specify do
179
+ expect { book.authors = [author1, author2] }
180
+ .to change { book.authors }.from([]).to([author1, author2])
181
+ end
182
+ specify do
183
+ expect { book.authors = [author1, author2] }
184
+ .to change { book.author_ids }.from([]).to([author1.id, author2.id])
185
+ end
186
+
187
+ specify do
188
+ expect { book.author_ids = [author1.id, author2.id] }
189
+ .to change { book.authors }.from([]).to([author2])
190
+ end
191
+ specify do
192
+ expect { book.author_ids = [author1.id, author2.id] }
193
+ .to change { book.author_ids }.from([]).to([author2.id])
194
+ end
195
+
196
+ specify do
197
+ expect { book.authors = [author1, author2] }
198
+ .to change { book.authors.reload }.from([]).to([author2])
199
+ end
200
+ specify do
201
+ expect { book.authors = [author1, author2] }
202
+ .to change {
203
+ book.authors.reload
204
+ book.author_ids
205
+ }.from([]).to([author2.id])
206
+ end
207
+
208
+ context do
209
+ let(:book2) { Book.new(letter: 'r') }
210
+
211
+ specify 'scope is not cached too much' do
212
+ expect { book.author_ids = [author1.id, author2.id] }
213
+ .to change { book.authors }.from([]).to([author2])
214
+ expect { book2.author_ids = [author1.id, author2.id] }
215
+ .to change { book2.authors }.from([]).to([author1])
216
+ end
217
+ end
218
+ end
219
+
220
+ describe '#author' do
221
+ it { expect(book.authors).not_to respond_to(:build) }
222
+ it { expect(book.authors).not_to respond_to(:create) }
223
+ it { expect(book.authors).not_to respond_to(:create!) }
224
+
225
+ describe '#clear' do
226
+ it { expect { book_with_author.authors.clear }.to change { book_with_author.authors }.from([author]).to([]) }
227
+ end
228
+
229
+ describe '#reload' do
230
+ before { book.authors << author.tap { |a| a.name = 'Don Juan' } }
231
+ it { expect { book.authors.reload }.to change { book.authors.map(&:name) }.from(['Don Juan']).to(['Rick']) }
232
+ end
233
+
234
+ describe '#concat' do
235
+ it { expect { book.authors.concat author }.to change { book.authors }.from([]).to([author]) }
236
+ it { expect { book.authors << author << other }.to change { book.authors }.from([]).to([author, other]) }
237
+ context 'no duplication' do
238
+ before { book.authors << author }
239
+ it { expect { book.authors.concat author }.not_to change { book.authors }.from([author]) }
240
+ end
241
+ end
242
+
243
+ context 'scope missing method delegation' do
244
+ it { expect(book_with_author.authors.scope).to be_a ActiveRecord::Relation }
245
+ it { expect(book_with_author.authors.where(name: 'John')).to be_a ActiveRecord::Relation }
246
+ it { expect(book_with_author.authors.name_starts_with_a).to be_a ActiveRecord::Relation }
247
+ end
248
+ end
249
+
250
+ describe '#author_ids' do
251
+ it { expect(book_with_author.author_ids).to eq([author.id]) }
252
+ xit { expect { book_with_author.author_ids << other.id }.to change { book_with_author.authors }.from([author]).to([author, other]) }
253
+ it { expect { book_with_author.author_ids = [other.id] }.to change { book_with_author.authors }.from([author]).to([other]) }
254
+ end
255
+
256
+ describe '#authors=' do
257
+ specify { expect { book.authors = [author] }.to change { book.authors }.from([]).to([author]) }
258
+ specify { expect { book.authors = ['string'] }.to raise_error Granite::Form::AssociationTypeMismatch }
259
+
260
+ context do
261
+ before { book.authors = [other] }
262
+ specify { expect { book.authors = [author] }.to change { book.authors }.from([other]).to([author]) }
263
+ specify { expect { book.authors = [author] }.to change { book.author_ids }.from([other.id]).to([author.id]) }
264
+ specify { expect { book.authors = [] }.to change { book.authors }.from([other]).to([]) }
265
+ specify { expect { book.authors = [] }.to change { book.author_ids }.from([other.id]).to([]) }
266
+ end
267
+
268
+ context 'model not persisted' do
269
+ let(:author) { Author.new }
270
+ specify { expect { book.authors = [author, other] }.to change { book.authors }.from([]).to([author, other]) }
271
+ specify { expect { book.authors = [author, other] }.to change { book.author_ids }.from([]).to([nil, other.id]) }
272
+
273
+ context do
274
+ before { book.authors = [author, other] }
275
+ specify do
276
+ expect { author.save! }.to change { book.author_ids }.from([nil, other.id])
277
+ .to(match([be_a(Integer), other.id]))
278
+ end
279
+ specify { expect { author.save! }.not_to change { book.authors } }
280
+ end
281
+ end
282
+ end
283
+
284
+ describe '#author_ids=' do
285
+ specify { expect { book.author_ids = [author.id] }.to change { book.author_ids }.from([]).to([author.id]) }
286
+ specify { expect { book.author_ids = [author.id] }.to change { book.authors }.from([]).to([author]) }
287
+ specify { expect { book.author_ids = [author] }.to change { book.authors }.from([]).to([author]) }
288
+
289
+ specify { expect { book.author_ids = [author.id.next.to_s] }.not_to change { book.author_ids }.from([]) }
290
+ specify { expect { book.author_ids = [author.id.next.to_s] }.not_to change { book.authors }.from([]) }
291
+
292
+ specify { expect { book.author_ids = [author.id.next.to_s, author.id] }.to change { book.author_ids }.from([]).to([author.id]) }
293
+ specify { expect { book.author_ids = [author.id.next.to_s, author.id] }.to change { book.authors }.from([]).to([author]) }
294
+
295
+ context do
296
+ before { book.authors = [other] }
297
+ specify { expect { book.author_ids = [author.id] }.to change { book.author_ids }.from([other.id]).to([author.id]) }
298
+ specify { expect { book.author_ids = [author.id] }.to change { book.authors }.from([other]).to([author]) }
299
+ specify { expect { book.author_ids = [] }.to change { book.author_ids }.from([other.id]).to([]) }
300
+ specify { expect { book.author_ids = [] }.to change { book.authors }.from([other]).to([]) }
301
+ end
302
+ end
303
+ end