granite-form 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -2
  3. data/.github/workflows/{ci.yml → ruby.yml} +22 -4
  4. data/.rubocop.yml +1 -1
  5. data/.rubocop_todo.yml +3 -3
  6. data/Appraisals +1 -2
  7. data/CHANGELOG.md +7 -0
  8. data/README.md +0 -2
  9. data/docker-compose.yml +14 -0
  10. data/gemfiles/rails.5.0.gemfile +0 -1
  11. data/gemfiles/rails.5.1.gemfile +0 -1
  12. data/gemfiles/rails.5.2.gemfile +0 -1
  13. data/granite-form.gemspec +15 -15
  14. data/lib/granite/form/active_record/associations.rb +1 -1
  15. data/lib/granite/form/base.rb +1 -2
  16. data/lib/granite/form/errors.rb +0 -15
  17. data/lib/granite/form/model/associations/base.rb +0 -4
  18. data/lib/granite/form/model/associations/collection/embedded.rb +2 -1
  19. data/lib/granite/form/model/associations/collection/proxy.rb +1 -1
  20. data/lib/granite/form/model/associations/embeds_any.rb +7 -0
  21. data/lib/granite/form/model/associations/embeds_many.rb +9 -58
  22. data/lib/granite/form/model/associations/embeds_one.rb +7 -36
  23. data/lib/granite/form/model/associations/nested_attributes.rb +5 -5
  24. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +0 -4
  25. data/lib/granite/form/model/associations/persistence_adapters/base.rb +0 -4
  26. data/lib/granite/form/model/associations/references_many.rb +0 -32
  27. data/lib/granite/form/model/associations/references_one.rb +0 -28
  28. data/lib/granite/form/model/associations/reflections/embeds_any.rb +1 -1
  29. data/lib/granite/form/model/associations/reflections/references_any.rb +0 -4
  30. data/lib/granite/form/model/associations/reflections/references_one.rb +0 -2
  31. data/lib/granite/form/model/associations/reflections/singular.rb +0 -8
  32. data/lib/granite/form/model/associations.rb +0 -6
  33. data/lib/granite/form/model/attributes/base.rb +1 -1
  34. data/lib/granite/form/model/attributes/reflections/attribute.rb +0 -6
  35. data/lib/granite/form/model/attributes/reflections/base.rb +8 -7
  36. data/lib/granite/form/model/attributes/reflections/reference_one.rb +0 -6
  37. data/lib/granite/form/model/persistence.rb +1 -19
  38. data/lib/granite/form/model.rb +0 -2
  39. data/lib/granite/form/version.rb +1 -1
  40. data/spec/granite/form/active_record/associations_spec.rb +16 -18
  41. data/spec/granite/form/model/associations/embeds_many_spec.rb +29 -305
  42. data/spec/granite/form/model/associations/embeds_one_spec.rb +27 -212
  43. data/spec/granite/form/model/associations/nested_attributes_spec.rb +0 -95
  44. data/spec/granite/form/model/associations/references_many_spec.rb +5 -326
  45. data/spec/granite/form/model/associations/references_one_spec.rb +6 -278
  46. data/spec/granite/form/model/associations/reflections/embeds_any_spec.rb +1 -2
  47. data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +18 -26
  48. data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +16 -23
  49. data/spec/granite/form/model/associations/reflections/references_many_spec.rb +1 -1
  50. data/spec/granite/form/model/associations/reflections/references_one_spec.rb +1 -22
  51. data/spec/granite/form/model/associations/validations_spec.rb +0 -3
  52. data/spec/granite/form/model/associations_spec.rb +3 -24
  53. data/spec/granite/form/model/dirty_spec.rb +1 -1
  54. data/spec/granite/form/model/persistence_spec.rb +0 -2
  55. data/spec/granite/form/model/validations/associated_spec.rb +2 -4
  56. data/spec/granite/form/model/validations/nested_spec.rb +2 -4
  57. data/spec/spec_helper.rb +0 -15
  58. data/spec/support/active_record.rb +20 -0
  59. data/spec/support/shared/nested_attribute_examples.rb +3 -21
  60. metadata +32 -38
  61. data/.github/workflows/main.yml +0 -29
  62. data/gemfiles/rails.4.2.gemfile +0 -15
  63. data/lib/granite/form/model/callbacks.rb +0 -72
  64. data/lib/granite/form/model/lifecycle.rb +0 -309
  65. data/spec/granite/form/model/callbacks_spec.rb +0 -337
  66. data/spec/granite/form/model/lifecycle_spec.rb +0 -356
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  describe Granite::Form::Model::Associations::Reflections::EmbedsMany do
4
4
  before do
5
5
  stub_model(:project) do
6
- include Granite::Form::Model::Lifecycle
6
+ include Granite::Form::Model::Persistence
7
+ include Granite::Form::Model::Associations
7
8
  attribute :title, String
8
9
  end
9
10
  stub_model(:user) do
@@ -24,24 +25,28 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsMany do
24
25
  attribute :name
25
26
  embeds_many :projects,
26
27
  read: lambda { |reflection, object|
27
- value = object.read_attribute(reflection.name)
28
+ value = object.instance_variable_get("@_value_#{reflection.name}")
28
29
  JSON.parse(value) if value.present?
29
30
  },
30
31
  write: lambda { |reflection, object, value|
31
- object.write_attribute(reflection.name, value.to_json)
32
+ value = value.to_json if value
33
+ object.instance_variable_set("@_value_#{reflection.name}", value)
32
34
  }
33
35
  end
34
36
  end
35
37
 
36
- let(:user) { User.instantiate name: 'Rick', projects: [{title: 'Genesis'}].to_json }
38
+ let(:user) { User.new }
37
39
  let(:new_project1) { Project.new(title: 'Project 1') }
38
40
  let(:new_project2) { Project.new(title: 'Project 2') }
39
41
 
40
42
  specify do
41
- expect { user.projects.concat([new_project1, new_project2]) }
42
- .to change { user.read_attribute(:projects) }
43
- .from([{title: 'Genesis'}].to_json)
44
- .to([{title: 'Genesis'}, {title: 'Project 1'}, {title: 'Project 2'}].to_json)
43
+ expect do
44
+ user.projects = [new_project1, new_project2]
45
+ user.association(:projects).sync
46
+ end
47
+ .to change { user.projects(true) }
48
+ .from([])
49
+ .to([have_attributes(title: 'Project 1'), have_attributes(title: 'Project 2')])
45
50
  end
46
51
  end
47
52
 
@@ -54,23 +59,10 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsMany do
54
59
  specify { expect { user.projects.build(title: 'Project') }.to change { user.projects }.from([]).to([project]) }
55
60
  end
56
61
 
57
- describe '#create' do
58
- let(:project) { Project.new title: 'Project' }
59
- specify { expect(user.projects.create(title: 'Project')).to eq(project) }
60
- specify { expect { user.projects.create(title: 'Project') }.to change { user.projects }.from([]).to([project]) }
61
- end
62
-
63
- describe '#create!' do
64
- let(:project) { Project.new title: 'Project' }
65
- specify { expect(user.projects.create!(title: 'Project')).to eq(project) }
66
- specify { expect { user.projects.create!(title: 'Project') }.to change { user.projects }.from([]).to([project]) }
67
- end
68
-
69
62
  describe '#reload' do
70
63
  let(:project) { Project.new title: 'Project' }
71
64
  before do
72
- user.update(projects: [project])
73
- user.apply_association_changes!
65
+ user.write_attribute(:projects, [{title: 'Project'}])
74
66
  end
75
67
  before { user.projects.build }
76
68
 
@@ -120,8 +112,8 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsMany do
120
112
 
121
113
  specify { expect(User.reflect_on_association(:projects).klass).to eq(User::Project) }
122
114
  specify { expect(User.new.projects).to eq([]) }
123
- specify { expect(User.new.tap { |u| u.projects.create(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
124
- specify { expect(User.new.tap { |u| u.projects.create(title: 'Project') }.read_attribute(:projects)).to eq([{'title' => 'Project'}]) }
115
+ specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
116
+ specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to match([have_attributes(title: 'Project')]) }
125
117
  end
126
118
 
127
119
  context do
@@ -138,8 +130,8 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsMany do
138
130
 
139
131
  specify { expect(User.reflect_on_association(:projects).klass).to eq(User::Project) }
140
132
  specify { expect(User.new.projects).to eq([]) }
141
- specify { expect(User.new.tap { |u| u.projects.create(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
142
- specify { expect(User.new.tap { |u| u.projects.create(title: 'Project') }.read_attribute(:projects)).to eq([{'title' => 'Project', 'value' => nil}]) }
133
+ specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
134
+ specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to match([have_attributes(title: 'Project', value: nil)]) }
143
135
  end
144
136
  end
145
137
  end
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
4
4
  before do
5
5
  stub_model(:author) do
6
- include Granite::Form::Model::Lifecycle
6
+ include Granite::Form::Model::Persistence
7
+ include Granite::Form::Model::Associations
7
8
  attribute :name, String
8
9
  end
9
10
 
@@ -27,22 +28,26 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
27
28
  attribute :title
28
29
  embeds_one :author,
29
30
  read: lambda { |reflection, object|
30
- value = object.read_attribute(reflection.name)
31
+ value = object.instance_variable_get("@_value_#{reflection.name}")
31
32
  JSON.parse(value) if value.present?
32
33
  },
33
34
  write: lambda { |reflection, object, value|
34
- object.write_attribute(reflection.name, value ? value.to_json : nil)
35
+ value = value.to_json if value
36
+ object.instance_variable_set("@_value_#{reflection.name}", value)
35
37
  }
36
38
  end
37
39
  end
38
40
 
39
- let(:book) { Book.instantiate author: {name: 'Duke'}.to_json }
41
+ let(:book) { Book.new }
40
42
  let(:author) { Author.new(name: 'Rick') }
41
43
 
42
44
  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)
45
+ expect do
46
+ book.author = author
47
+ book.association(:author).sync
48
+ end
49
+ .to change { book.author(true) }
50
+ .from(nil).to(have_attributes(name: 'Rick'))
46
51
  end
47
52
  end
48
53
 
@@ -65,18 +70,6 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
65
70
  specify { expect { book.build_author(name: 'Author') }.to change { book.author }.from(nil).to(author) }
66
71
  end
67
72
 
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
73
  context 'on the fly' do
81
74
  context do
82
75
  before do
@@ -92,8 +85,8 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
92
85
 
93
86
  specify { expect(Book.reflect_on_association(:author).klass).to eq(Book::Author) }
94
87
  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') }
88
+ specify { expect(Book.new.tap { |b| b.build_author(name: 'Author') }.author).to be_a(Book::Author) }
89
+ specify { expect(Book.new.tap { |b| b.build_author(name: 'Author') }.author).to have_attributes(name: 'Author') }
97
90
  end
98
91
 
99
92
  context do
@@ -110,8 +103,8 @@ describe Granite::Form::Model::Associations::Reflections::EmbedsOne do
110
103
 
111
104
  specify { expect(Book.reflect_on_association(:author).klass).to eq(Book::Author) }
112
105
  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) }
106
+ specify { expect(Book.new.tap { |b| b.build_author(name: 'Author') }.author).to be_a(Book::Author) }
107
+ specify { expect(Book.new.tap { |b| b.build_author(name: 'Author') }.author).to have_attributes(name: 'Author', age: nil) }
115
108
  end
116
109
  end
117
110
  end
@@ -4,7 +4,7 @@ describe Granite::Form::Model::Associations::Reflections::ReferencesMany do
4
4
  before do
5
5
  stub_class(:author, ActiveRecord::Base) do
6
6
  scope :name_starts_with_a, -> { name_starts_with('a') }
7
- scope :name_starts_with, ->(letter) { where("name LIKE '#{letter}%'") }
7
+ scope :name_starts_with, ->(letter) { where("name ILIKE '#{letter}%'") }
8
8
  end
9
9
 
10
10
  stub_model(:book) do
@@ -4,7 +4,7 @@ describe Granite::Form::Model::Associations::Reflections::ReferencesOne do
4
4
  before do
5
5
  stub_class(:author, ActiveRecord::Base) do
6
6
  scope :name_starts_with_a, -> { name_starts_with('a') }
7
- scope :name_starts_with, ->(letter) { where("name LIKE '#{letter}%'") }
7
+ scope :name_starts_with, ->(letter) { where("name ILIKE '#{letter}%'") }
8
8
  end
9
9
 
10
10
  stub_model(:book) do
@@ -263,25 +263,4 @@ describe Granite::Form::Model::Associations::Reflections::ReferencesOne do
263
263
  specify { expect { book.author_id = nil }.to change { book.author }.from(other).to(nil) }
264
264
  end
265
265
  end
266
-
267
- describe '#build_author' do
268
- specify { expect(book.build_author(name: 'Author')).to be_a(Author) }
269
- specify { expect(book.build_author(name: 'Author')).not_to be_persisted }
270
- specify { expect { book.build_author(name: 'Author') }.to change { book.author }.from(nil).to(an_instance_of(Author)) }
271
- specify { expect { book.build_author(name: 'Author') }.not_to change { book.author_id }.from(nil) }
272
- end
273
-
274
- describe '#create_author' do
275
- specify { expect(book.create_author(name: 'Author')).to be_a(Author) }
276
- specify { expect(book.create_author(name: 'Author')).to be_persisted }
277
- specify { expect { book.create_author(name: 'Author') }.to change { book.author }.from(nil).to(an_instance_of(Author)) }
278
- specify { expect { book.create_author(name: 'Author') }.to change { book.author_id }.from(nil).to(be_a(Integer)) }
279
- end
280
-
281
- describe '#create_author!' do
282
- specify { expect(book.create_author!(name: 'Author')).to be_a(Author) }
283
- specify { expect(book.create_author!(name: 'Author')).to be_persisted }
284
- specify { expect { book.create_author!(name: 'Author') }.to change { book.author }.from(nil).to(an_instance_of(Author)) }
285
- specify { expect { book.create_author!(name: 'Author') }.to change { book.author_id }.from(nil).to(be_a(Integer)) }
286
- end
287
266
  end
@@ -3,7 +3,6 @@ require 'spec_helper'
3
3
  describe Granite::Form::Model::Associations::Validations do
4
4
  before do
5
5
  stub_model(:project) do
6
- include Granite::Form::Model::Lifecycle
7
6
  include Granite::Form::Model::Associations
8
7
  include Granite::Form::Model::Associations::Validations
9
8
 
@@ -17,8 +16,6 @@ describe Granite::Form::Model::Associations::Validations do
17
16
  end
18
17
 
19
18
  stub_model(:profile) do
20
- include Granite::Form::Model::Lifecycle
21
-
22
19
  attribute :first_name, String
23
20
  attribute :last_name, String
24
21
  validates :first_name, presence: true
@@ -7,7 +7,7 @@ describe Granite::Form::Model::Associations do
7
7
  include Granite::Form::Model::Associations
8
8
  end
9
9
  stub_model(:project) do
10
- include Granite::Form::Model::Lifecycle
10
+ include Granite::Form::Model::Persistence
11
11
  end
12
12
  stub_model(:user, Nobody) do
13
13
  include Granite::Form::Model::Associations
@@ -65,7 +65,7 @@ describe Granite::Form::Model::Associations do
65
65
  context do
66
66
  before do
67
67
  stub_model(:project) do
68
- include Granite::Form::Model::Lifecycle
68
+ include Granite::Form::Model::Persistence
69
69
  include Granite::Form::Model::Associations
70
70
 
71
71
  attribute :title, String
@@ -80,7 +80,7 @@ describe Granite::Form::Model::Associations do
80
80
  end
81
81
 
82
82
  stub_model(:profile) do
83
- include Granite::Form::Model::Lifecycle
83
+ include Granite::Form::Model::Persistence
84
84
 
85
85
  attribute :first_name, String
86
86
  attribute :last_name, String
@@ -160,27 +160,6 @@ describe Granite::Form::Model::Associations do
160
160
  specify { expect(user.association_names).to eq(%i[profile projects]) }
161
161
  end
162
162
 
163
- describe '#apply_association_changes!' do
164
- let(:profile) { Profile.new first_name: 'Name' }
165
- let(:project) { Project.new title: 'Project' }
166
- let(:user) { User.new(profile: profile, projects: [project]) }
167
- before { project.build_author(name: 'Author') }
168
-
169
- specify do
170
- expect { user.apply_association_changes! }.to change { user.attributes['profile'] }
171
- .from(nil).to('first_name' => 'Name', 'last_name' => nil)
172
- end
173
- specify do
174
- expect { user.apply_association_changes! }.to change { user.attributes['projects'] }
175
- .from(nil).to([{'title' => 'Project', 'author' => {'name' => 'Author'}}])
176
- end
177
-
178
- context do
179
- let(:project) { Project.new }
180
- specify { expect { user.apply_association_changes! }.to raise_error Granite::Form::AssociationChangesNotApplied }
181
- end
182
- end
183
-
184
163
  describe '#instantiate' do
185
164
  before { User.send(:include, Granite::Form::Model::Persistence) }
186
165
  let(:profile) { Profile.new first_name: 'Name' }
@@ -30,7 +30,7 @@ describe Granite::Form::Model::Dirty do
30
30
  let(:other_author) { Author.create!(name: 'Other') }
31
31
 
32
32
  specify { expect(Model.new.changes).to eq({}) }
33
- specify { expect(Model.new.tap { |m| m.create_something(value: 'Value') }.changes).to eq({}) }
33
+ specify { expect(Model.new.tap { |m| m.build_something(value: 'Value') }.changes).to eq({}) }
34
34
 
35
35
  specify { expect(Model.new(author: author).changes).to eq('author_id' => [nil, author.id]) }
36
36
  specify { expect(Model.new(author_id: author.id).changes).to eq('author_id' => [nil, author.id]) }
@@ -11,12 +11,10 @@ describe Granite::Form::Model::Persistence do
11
11
  end
12
12
 
13
13
  specify { expect(model.new).not_to be_persisted }
14
- specify { expect(model.new).not_to be_destroyed }
15
14
 
16
15
  describe '#instantiate' do
17
16
  specify { expect(model.instantiate({})).to be_an_instance_of model }
18
17
  specify { expect(model.instantiate({})).to be_persisted }
19
- specify { expect(model.instantiate({})).not_to be_destroyed }
20
18
 
21
19
  context do
22
20
  subject(:instance) { model.instantiate(name: 'Hello', foo: 'Bar') }
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe Granite::Form::Model::Validations::AssociatedValidator do
4
4
  before do
5
5
  stub_model(:validated_assoc) do
6
- include Granite::Form::Model
7
- include Granite::Form::Model::Lifecycle
6
+ include Granite::Form::Model::Persistence
8
7
 
9
8
  attribute :name, String
10
9
 
@@ -12,8 +11,7 @@ describe Granite::Form::Model::Validations::AssociatedValidator do
12
11
  end
13
12
 
14
13
  stub_model(:unvalidated_assoc) do
15
- include Granite::Form::Model
16
- include Granite::Form::Model::Lifecycle
14
+ include Granite::Form::Model::Persistence
17
15
 
18
16
  attribute :name, String
19
17
  end
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe Granite::Form::Model::Validations::NestedValidator do
4
4
  before do
5
5
  stub_model(:validated_assoc) do
6
- include Granite::Form::Model
7
- include Granite::Form::Model::Lifecycle
6
+ include Granite::Form::Model::Persistence
8
7
  include Granite::Form::Model::Primary
9
8
 
10
9
  primary :id, Integer
@@ -14,8 +13,7 @@ describe Granite::Form::Model::Validations::NestedValidator do
14
13
  end
15
14
 
16
15
  stub_model(:unvalidated_assoc) do
17
- include Granite::Form::Model
18
- include Granite::Form::Model::Lifecycle
16
+ include Granite::Form::Model::Persistence
19
17
 
20
18
  attribute :name, String
21
19
  end
data/spec/spec_helper.rb CHANGED
@@ -10,21 +10,6 @@ require 'database_cleaner'
10
10
 
11
11
  Dir[File.join(__dir__, 'support', '**', '*.rb')].sort.each { |f| require f }
12
12
 
13
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
14
- ActiveRecord::Base.logger = Logger.new('/dev/null')
15
-
16
- ActiveRecord::Schema.define do
17
- create_table :users do |t|
18
- t.column :email, :string
19
- t.column :projects, :text
20
- t.column :profile, :text
21
- end
22
-
23
- create_table :authors do |t|
24
- t.column :name, :string
25
- end
26
- end
27
-
28
13
  RSpec.configure do |config|
29
14
  config.mock_with :rspec
30
15
  config.order = :random
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ adapter: 'postgresql',
3
+ database: 'granite',
4
+ username: 'granite',
5
+ password: 'granite',
6
+ host: 'localhost'
7
+ )
8
+ ActiveRecord::Base.logger = Logger.new('/dev/null')
9
+
10
+ ActiveRecord::Schema.define do
11
+ create_table :users, force: :cascade do |t|
12
+ t.column :email, :string
13
+ t.column :projects, :text
14
+ t.column :profile, :text
15
+ end
16
+
17
+ create_table :authors, force: :cascade do |t|
18
+ t.column :name, :string
19
+ end
20
+ end
@@ -4,7 +4,7 @@ shared_examples 'nested attributes' do
4
4
  before do
5
5
  stub_model :project do
6
6
  include Granite::Form::Model::Primary
7
- include Granite::Form::Model::Lifecycle
7
+ include Granite::Form::Model::Associations
8
8
 
9
9
  primary :slug, String
10
10
  attribute :title, String
@@ -12,7 +12,7 @@ shared_examples 'nested attributes' do
12
12
 
13
13
  stub_model :profile do
14
14
  include Granite::Form::Model::Primary
15
- include Granite::Form::Model::Lifecycle
15
+ include Granite::Form::Model::Associations
16
16
 
17
17
  primary :identifier
18
18
  attribute :first_name, String
@@ -49,14 +49,12 @@ shared_examples 'nested attributes' do
49
49
  specify do
50
50
  expect do
51
51
  user.profile_attributes = {first_name: 'User 1', _destroy: '1'}
52
- user.save { true }
53
52
  end.not_to change { user.profile.first_name }
54
53
  end
55
54
  specify { expect { user.profile_attributes = {identifier: profile.identifier.to_s, first_name: 'User 1', _destroy: '1'} }.to change { user.profile.first_name }.to('User 1') }
56
55
  specify do
57
56
  expect do
58
57
  user.profile_attributes = {identifier: profile.identifier.to_s, first_name: 'User 1', _destroy: '1'}
59
- user.save { true }
60
58
  end.to change { user.profile.first_name }.to('User 1')
61
59
  end
62
60
 
@@ -67,14 +65,12 @@ shared_examples 'nested attributes' do
67
65
  specify do
68
66
  expect do
69
67
  user.profile_attributes = {first_name: 'User 1', _destroy: '1'}
70
- user.save { true }
71
68
  end.not_to change { user.profile.first_name }
72
69
  end
73
- specify { expect { user.profile_attributes = {identifier: profile.identifier.to_s, first_name: 'User 1', _destroy: '1'} }.to change { user.profile.first_name }.to('User 1') }
70
+ specify { expect { user.profile_attributes = {identifier: profile.identifier.to_s, first_name: 'User 1', _destroy: '1'} }.to change { user.profile }.to(nil) }
74
71
  specify do
75
72
  expect do
76
73
  user.profile_attributes = {identifier: profile.identifier.to_s, first_name: 'User 1', _destroy: '1'}
77
- user.save { true }
78
74
  end.to change { user.profile }.to(nil)
79
75
  end
80
76
  end
@@ -92,8 +88,6 @@ shared_examples 'nested attributes' do
92
88
  context 'not primary' do
93
89
  before do
94
90
  stub_model :profile do
95
- include Granite::Form::Model::Lifecycle
96
-
97
91
  attribute :identifier, Integer
98
92
  attribute :first_name, String
99
93
  end
@@ -245,7 +239,6 @@ shared_examples 'nested attributes' do
245
239
  {slug: projects.first.slug.to_i, title: 'Project 3', _destroy: '1'},
246
240
  {title: 'Project 4', _destroy: '1'}
247
241
  ]
248
- user.save { true }
249
242
  end
250
243
  .to change { user.projects.map(&:title) }.to(['Project 3', 'Project 2'])
251
244
  end
@@ -259,16 +252,6 @@ shared_examples 'nested attributes' do
259
252
  {slug: projects.first.slug.to_i, title: 'Project 3', _destroy: '1'},
260
253
  {title: 'Project 4', _destroy: '1'}
261
254
  ]
262
- end
263
- .to change { user.projects.map(&:title) }.to(['Project 3', 'Project 2'])
264
- end
265
- specify do
266
- expect do
267
- user.projects_attributes = [
268
- {slug: projects.first.slug.to_i, title: 'Project 3', _destroy: '1'},
269
- {title: 'Project 4', _destroy: '1'}
270
- ]
271
- user.save { true }
272
255
  end
273
256
  .to change { user.projects.map(&:title) }.to(['Project 2'])
274
257
  end
@@ -303,7 +286,6 @@ shared_examples 'nested attributes' do
303
286
  before do
304
287
  stub_model :project do
305
288
  include Granite::Form::Model::Primary
306
- include Granite::Form::Model::Lifecycle
307
289
 
308
290
  attribute :slug, String
309
291
  attribute :title, String