granite-form 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +1 -2
- data/.github/workflows/{ci.yml → ruby.yml} +22 -4
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +3 -3
- data/Appraisals +1 -2
- data/CHANGELOG.md +7 -0
- data/README.md +0 -2
- data/docker-compose.yml +14 -0
- data/gemfiles/rails.5.0.gemfile +0 -1
- data/gemfiles/rails.5.1.gemfile +0 -1
- data/gemfiles/rails.5.2.gemfile +0 -1
- data/granite-form.gemspec +15 -15
- data/lib/granite/form/active_record/associations.rb +1 -1
- data/lib/granite/form/base.rb +1 -2
- data/lib/granite/form/errors.rb +0 -15
- data/lib/granite/form/model/associations/base.rb +0 -4
- data/lib/granite/form/model/associations/collection/embedded.rb +2 -1
- data/lib/granite/form/model/associations/collection/proxy.rb +1 -1
- data/lib/granite/form/model/associations/embeds_any.rb +7 -0
- data/lib/granite/form/model/associations/embeds_many.rb +9 -58
- data/lib/granite/form/model/associations/embeds_one.rb +7 -36
- data/lib/granite/form/model/associations/nested_attributes.rb +5 -5
- data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +0 -4
- data/lib/granite/form/model/associations/persistence_adapters/base.rb +0 -4
- data/lib/granite/form/model/associations/references_many.rb +0 -32
- data/lib/granite/form/model/associations/references_one.rb +0 -28
- data/lib/granite/form/model/associations/reflections/embeds_any.rb +1 -1
- data/lib/granite/form/model/associations/reflections/references_any.rb +0 -4
- data/lib/granite/form/model/associations/reflections/references_one.rb +0 -2
- data/lib/granite/form/model/associations/reflections/singular.rb +0 -8
- data/lib/granite/form/model/associations.rb +0 -6
- data/lib/granite/form/model/attributes/base.rb +1 -1
- data/lib/granite/form/model/attributes/reflections/attribute.rb +0 -6
- data/lib/granite/form/model/attributes/reflections/base.rb +8 -7
- data/lib/granite/form/model/attributes/reflections/reference_one.rb +0 -6
- data/lib/granite/form/model/persistence.rb +1 -19
- data/lib/granite/form/model.rb +0 -2
- data/lib/granite/form/version.rb +1 -1
- data/spec/granite/form/active_record/associations_spec.rb +16 -18
- data/spec/granite/form/model/associations/embeds_many_spec.rb +29 -305
- data/spec/granite/form/model/associations/embeds_one_spec.rb +27 -212
- data/spec/granite/form/model/associations/nested_attributes_spec.rb +0 -95
- data/spec/granite/form/model/associations/references_many_spec.rb +5 -326
- data/spec/granite/form/model/associations/references_one_spec.rb +6 -278
- data/spec/granite/form/model/associations/reflections/embeds_any_spec.rb +1 -2
- data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +18 -26
- data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +16 -23
- data/spec/granite/form/model/associations/reflections/references_many_spec.rb +1 -1
- data/spec/granite/form/model/associations/reflections/references_one_spec.rb +1 -22
- data/spec/granite/form/model/associations/validations_spec.rb +0 -3
- data/spec/granite/form/model/associations_spec.rb +3 -24
- data/spec/granite/form/model/dirty_spec.rb +1 -1
- data/spec/granite/form/model/persistence_spec.rb +0 -2
- data/spec/granite/form/model/validations/associated_spec.rb +2 -4
- data/spec/granite/form/model/validations/nested_spec.rb +2 -4
- data/spec/spec_helper.rb +0 -15
- data/spec/support/active_record.rb +20 -0
- data/spec/support/shared/nested_attribute_examples.rb +3 -21
- metadata +32 -38
- data/.github/workflows/main.yml +0 -29
- data/gemfiles/rails.4.2.gemfile +0 -15
- data/lib/granite/form/model/callbacks.rb +0 -72
- data/lib/granite/form/model/lifecycle.rb +0 -309
- data/spec/granite/form/model/callbacks_spec.rb +0 -337
- data/spec/granite/form/model/lifecycle_spec.rb +0 -356
@@ -14,14 +14,6 @@ module Granite
|
|
14
14
|
def build_#{name} attributes = {}
|
15
15
|
association(:#{name}).build(attributes)
|
16
16
|
end
|
17
|
-
|
18
|
-
def create_#{name} attributes = {}
|
19
|
-
association(:#{name}).create(attributes)
|
20
|
-
end
|
21
|
-
|
22
|
-
def create_#{name}! attributes = {}
|
23
|
-
association(:#{name}).create!(attributes)
|
24
|
-
end
|
25
17
|
RUBY
|
26
18
|
end
|
27
19
|
end
|
@@ -100,12 +100,6 @@ module Granite
|
|
100
100
|
(@_associations ||= {})[reflection.name] ||= reflection.build_association(self)
|
101
101
|
end
|
102
102
|
|
103
|
-
def apply_association_changes!
|
104
|
-
association_names.all? do |name|
|
105
|
-
association(name).apply_changes!
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
103
|
private
|
110
104
|
|
111
105
|
def attributes_for_inspect
|
@@ -104,7 +104,7 @@ module Granite
|
|
104
104
|
if block.arity >= 0 && block.arity <= args.length
|
105
105
|
owner.instance_exec(*args.first(block.arity), &block)
|
106
106
|
else
|
107
|
-
args = block.arity
|
107
|
+
args = block.arity.negative? ? args : args.first(block.arity)
|
108
108
|
yield(*args, owner)
|
109
109
|
end
|
110
110
|
end
|
@@ -4,12 +4,6 @@ module Granite
|
|
4
4
|
module Attributes
|
5
5
|
module Reflections
|
6
6
|
class Attribute < Base
|
7
|
-
def self.build(target, generated_methods, name, *args, &block)
|
8
|
-
attribute = super(target, generated_methods, name, *args, &block)
|
9
|
-
generate_methods name, generated_methods
|
10
|
-
attribute
|
11
|
-
end
|
12
|
-
|
13
7
|
def self.generate_methods(name, target)
|
14
8
|
target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
9
|
def #{name}
|
@@ -7,11 +7,9 @@ module Granite
|
|
7
7
|
attr_reader :name, :options
|
8
8
|
|
9
9
|
class << self
|
10
|
-
def build(_target,
|
11
|
-
|
12
|
-
|
13
|
-
options[:default] = block if block
|
14
|
-
new(name, options)
|
10
|
+
def build(_target, generated_methods, name, *args, &block)
|
11
|
+
generate_methods name, generated_methods
|
12
|
+
new(name, *args, &block)
|
15
13
|
end
|
16
14
|
|
17
15
|
def generate_methods(name, target) end
|
@@ -21,9 +19,12 @@ module Granite
|
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
24
|
-
def initialize(name,
|
22
|
+
def initialize(name, *args, &block)
|
25
23
|
@name = name.to_s
|
26
|
-
|
24
|
+
|
25
|
+
@options = args.extract_options!
|
26
|
+
@options[:type] = args.first if args.first
|
27
|
+
@options[:default] = block if block
|
27
28
|
end
|
28
29
|
|
29
30
|
def build_attribute(owner, raw_value = Granite::Form::UNDEFINED)
|
@@ -4,12 +4,6 @@ module Granite
|
|
4
4
|
module Attributes
|
5
5
|
module Reflections
|
6
6
|
class ReferenceOne < Base
|
7
|
-
def self.build(_target, generated_methods, name, *args)
|
8
|
-
options = args.extract_options!
|
9
|
-
generate_methods name, generated_methods
|
10
|
-
new(name, options)
|
11
|
-
end
|
12
|
-
|
13
7
|
def self.generate_methods(name, target)
|
14
8
|
target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
9
|
def #{name}
|
@@ -26,32 +26,14 @@ module Granite
|
|
26
26
|
!!@persisted
|
27
27
|
end
|
28
28
|
|
29
|
-
def destroyed?
|
30
|
-
!!@destroyed
|
31
|
-
end
|
32
|
-
|
33
29
|
def marked_for_destruction?
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def mark_for_destruction
|
38
|
-
@marked_for_destruction = true
|
39
|
-
end
|
40
|
-
|
41
|
-
def _destroy
|
42
|
-
marked_for_destruction?
|
30
|
+
false
|
43
31
|
end
|
44
32
|
|
45
33
|
private
|
46
34
|
|
47
35
|
def mark_persisted!
|
48
36
|
@persisted = true
|
49
|
-
@destroyed = false
|
50
|
-
end
|
51
|
-
|
52
|
-
def mark_destroyed!
|
53
|
-
@persisted = false
|
54
|
-
@destroyed = true
|
55
37
|
end
|
56
38
|
end
|
57
39
|
end
|
data/lib/granite/form/model.rb
CHANGED
@@ -3,9 +3,7 @@ require 'granite/form/model/attributes'
|
|
3
3
|
require 'granite/form/model/validations'
|
4
4
|
require 'granite/form/model/scopes'
|
5
5
|
require 'granite/form/model/primary'
|
6
|
-
require 'granite/form/model/lifecycle'
|
7
6
|
require 'granite/form/model/persistence'
|
8
|
-
require 'granite/form/model/callbacks'
|
9
7
|
require 'granite/form/model/associations'
|
10
8
|
require 'granite/form/model/localization'
|
11
9
|
require 'granite/form/model/representation'
|
data/lib/granite/form/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Granite::Form::ActiveRecord::Associations do
|
4
4
|
before do
|
5
5
|
stub_model(:project) do
|
6
|
-
include Granite::Form::Model::
|
6
|
+
include Granite::Form::Model::Persistence
|
7
7
|
include Granite::Form::Model::Associations
|
8
8
|
|
9
9
|
attribute :title, String
|
@@ -18,7 +18,8 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
stub_model(:profile) do
|
21
|
-
include Granite::Form::Model::
|
21
|
+
include Granite::Form::Model::Persistence
|
22
|
+
include Granite::Form::Model::Associations
|
22
23
|
|
23
24
|
attribute :first_name, String
|
24
25
|
attribute :last_name, String
|
@@ -89,7 +90,10 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
89
90
|
end
|
90
91
|
|
91
92
|
context 'with profile already set' do
|
92
|
-
before
|
93
|
+
before do
|
94
|
+
user.build_profile(admin: true)
|
95
|
+
user.save!
|
96
|
+
end
|
93
97
|
|
94
98
|
specify do
|
95
99
|
user.profile.admin = false
|
@@ -104,13 +108,9 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
104
108
|
let(:user) { User.create }
|
105
109
|
|
106
110
|
describe '#projects' do
|
107
|
-
specify do
|
108
|
-
expect { user.projects << Project.new }
|
109
|
-
.not_to change { user.read_attribute(:projects) }
|
110
|
-
end
|
111
111
|
specify do
|
112
112
|
expect { user.projects << Project.new(title: 'First') }
|
113
|
-
.
|
113
|
+
.not_to change { user.read_attribute(:projects) }
|
114
114
|
end
|
115
115
|
specify do
|
116
116
|
user.projects << Project.new(title: 'First')
|
@@ -124,15 +124,14 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
124
124
|
|
125
125
|
specify do
|
126
126
|
expect { user.projects << project }
|
127
|
-
.
|
128
|
-
.to([{'title' => 'First', 'author' => {'name' => 'Author'}}])
|
127
|
+
.not_to change { user.read_attribute(:projects) }
|
129
128
|
end
|
130
129
|
specify do
|
131
130
|
expect do
|
132
131
|
user.projects << project
|
133
132
|
user.save
|
134
133
|
end
|
135
|
-
.to change { user.reload.read_attribute(:projects) }.from(
|
134
|
+
.to change { user.reload.read_attribute(:projects) }.from([])
|
136
135
|
.to([{'title' => 'First', 'author' => {'name' => 'Author'}}])
|
137
136
|
end
|
138
137
|
end
|
@@ -150,8 +149,7 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
150
149
|
end
|
151
150
|
specify do
|
152
151
|
expect { user.profile = Profile.new(first_name: 'google.com') }
|
153
|
-
.
|
154
|
-
.to({first_name: 'google.com', last_name: nil, admin: nil}.to_json)
|
152
|
+
.not_to change { user.read_attribute(:profile) }
|
155
153
|
end
|
156
154
|
specify do
|
157
155
|
expect do
|
@@ -196,16 +194,16 @@ describe Granite::Form::ActiveRecord::Associations do
|
|
196
194
|
|
197
195
|
specify { expect(User.reflect_on_association(:projects).klass).to eq(User::Project) }
|
198
196
|
specify { expect(User.new.projects).to eq([]) }
|
199
|
-
specify { expect(User.new.tap { |u| u.projects.
|
200
|
-
specify { expect(User.new.tap { |u| u.projects.
|
197
|
+
specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
|
198
|
+
specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to match([have_attributes(title: 'Project')]) }
|
201
199
|
|
202
200
|
specify { expect(User.reflect_on_association(:profile).klass).to eq(User::Profile) }
|
203
201
|
specify { expect(User.reflect_on_association(:profile).klass).to be < Profile }
|
204
202
|
specify { expect(User.new.profile).to be_nil }
|
205
|
-
specify { expect(User.new.tap { |u| u.
|
203
|
+
specify { expect(User.new.tap { |u| u.build_profile(first_name: 'Profile') }.profile).to be_a(User::Profile) }
|
206
204
|
specify do
|
207
|
-
expect(User.new.tap { |u| u.
|
208
|
-
.to
|
205
|
+
expect(User.new.tap { |u| u.build_profile(first_name: 'Profile') }.profile)
|
206
|
+
.to have_attributes(first_name: 'Profile', last_name: nil, admin: nil, age: nil)
|
209
207
|
end
|
210
208
|
end
|
211
209
|
end
|