granite-form 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- 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 +15 -0
- data/README.md +6 -15
- 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 +7 -7
- 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/embeds_many.rb +1 -1
- data/lib/granite/form/model/associations/reflections/embeds_one.rb +11 -1
- data/lib/granite/form/model/associations/reflections/references_any.rb +0 -4
- data/lib/granite/form/model/associations/reflections/singular.rb +0 -22
- data/lib/granite/form/model/associations.rb +0 -6
- data/lib/granite/form/model/attributes/attribute.rb +3 -21
- data/lib/granite/form/model/attributes/base.rb +5 -23
- data/lib/granite/form/model/attributes/reference_many.rb +1 -1
- data/lib/granite/form/model/attributes/reference_one.rb +1 -1
- data/lib/granite/form/model/attributes/reflections/attribute.rb +2 -8
- data/lib/granite/form/model/attributes/reflections/base/build_type_definition.rb +38 -0
- data/lib/granite/form/model/attributes/reflections/base.rb +20 -17
- data/lib/granite/form/model/attributes/reflections/collection/build_type_definition.rb +19 -0
- data/lib/granite/form/model/attributes/reflections/dictionary/build_type_definition.rb +19 -0
- data/lib/granite/form/model/attributes/reflections/dictionary.rb +0 -3
- data/lib/granite/form/model/attributes/reflections/reference_one.rb +0 -6
- data/lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb +73 -0
- data/lib/granite/form/model/attributes/reflections/represents.rb +10 -2
- data/lib/granite/form/model/attributes/represents.rb +22 -37
- data/lib/granite/form/model/attributes.rb +10 -2
- data/lib/granite/form/model/persistence.rb +1 -19
- data/lib/granite/form/model/representation.rb +1 -0
- data/lib/granite/form/model/validations.rb +6 -0
- data/lib/granite/form/model.rb +1 -3
- data/lib/granite/form/types/active_support/time_zone.rb +2 -0
- data/lib/granite/form/types/array.rb +2 -0
- data/lib/granite/form/types/big_decimal.rb +2 -0
- data/lib/granite/form/types/boolean.rb +2 -0
- data/lib/granite/form/types/collection.rb +11 -0
- data/lib/granite/form/types/date.rb +2 -0
- data/lib/granite/form/types/date_time.rb +2 -0
- data/lib/granite/form/types/dictionary.rb +23 -0
- data/lib/granite/form/types/float.rb +2 -0
- data/lib/granite/form/types/has_subtype.rb +18 -0
- data/lib/granite/form/types/hash_with_action_controller_parameters.rb +2 -0
- data/lib/granite/form/types/integer.rb +2 -0
- data/lib/granite/form/types/object.rb +28 -0
- data/lib/granite/form/types/string.rb +2 -0
- data/lib/granite/form/types/time.rb +2 -0
- data/lib/granite/form/types/uuid.rb +2 -0
- data/lib/granite/form/types.rb +3 -0
- data/lib/granite/form/util.rb +55 -0
- data/lib/granite/form/version.rb +1 -1
- data/lib/granite/form.rb +1 -0
- 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 +7 -279
- 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/attributes/attribute_spec.rb +0 -29
- data/spec/granite/form/model/attributes/reflections/attribute_spec.rb +0 -9
- data/spec/granite/form/model/attributes/reflections/base/build_type_definition_spec.rb +27 -0
- data/spec/granite/form/model/attributes/reflections/base_spec.rb +16 -10
- data/spec/granite/form/model/attributes/reflections/collection/build_type_definition_spec.rb +24 -0
- data/spec/granite/form/model/attributes/reflections/dictionary/build_type_definition_spec.rb +24 -0
- data/spec/granite/form/model/attributes/reflections/dictionary_spec.rb +0 -6
- data/spec/granite/form/model/attributes/reflections/represents/build_type_definition_spec.rb +129 -0
- data/spec/granite/form/model/attributes/reflections/represents_spec.rb +43 -20
- data/spec/granite/form/model/attributes/represents_spec.rb +78 -55
- data/spec/granite/form/model/attributes_spec.rb +84 -23
- data/spec/granite/form/model/dirty_spec.rb +1 -7
- data/spec/granite/form/model/persistence_spec.rb +0 -2
- data/spec/granite/form/model/representation_spec.rb +4 -7
- data/spec/granite/form/model/validations/associated_spec.rb +2 -4
- data/spec/granite/form/model/validations/nested_spec.rb +2 -4
- data/spec/granite/form/model/validations_spec.rb +28 -1
- data/spec/granite/form/types/collection_spec.rb +22 -0
- data/spec/granite/form/types/dictionary_spec.rb +32 -0
- data/spec/granite/form/types/has_subtype_spec.rb +20 -0
- data/spec/granite/form/types/object_spec.rb +50 -4
- data/spec/granite/form/util_spec.rb +108 -0
- data/spec/spec_helper.rb +0 -15
- data/spec/support/active_record.rb +23 -0
- data/spec/support/shared/nested_attribute_examples.rb +3 -21
- metadata +56 -51
- data/.github/workflows/main.yml +0 -29
- data/gemfiles/rails.4.2.gemfile +0 -15
- data/lib/granite/form/model/attributes/collection.rb +0 -19
- data/lib/granite/form/model/attributes/dictionary.rb +0 -28
- data/lib/granite/form/model/attributes/localized.rb +0 -44
- data/lib/granite/form/model/attributes/reflections/localized.rb +0 -45
- data/lib/granite/form/model/callbacks.rb +0 -72
- data/lib/granite/form/model/lifecycle.rb +0 -309
- data/lib/granite/form/model/localization.rb +0 -26
- data/spec/granite/form/model/attributes/collection_spec.rb +0 -72
- data/spec/granite/form/model/attributes/dictionary_spec.rb +0 -100
- data/spec/granite/form/model/attributes/localized_spec.rb +0 -103
- data/spec/granite/form/model/attributes/reflections/localized_spec.rb +0 -37
- data/spec/granite/form/model/callbacks_spec.rb +0 -337
- data/spec/granite/form/model/lifecycle_spec.rb +0 -356
@@ -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
|
@@ -12,6 +12,12 @@ module Granite
|
|
12
12
|
alias_method :validate, :valid?
|
13
13
|
end
|
14
14
|
|
15
|
+
class_methods do
|
16
|
+
def validates_presence?(attr)
|
17
|
+
_validators[attr.to_sym].grep(ActiveModel::Validations::PresenceValidator).present?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
15
21
|
def validate!(context = nil)
|
16
22
|
valid?(context) || raise_validation_error
|
17
23
|
end
|
data/lib/granite/form/model.rb
CHANGED
@@ -3,11 +3,8 @@ 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
|
-
require 'granite/form/model/localization'
|
11
8
|
require 'granite/form/model/representation'
|
12
9
|
require 'granite/form/model/dirty'
|
13
10
|
|
@@ -24,6 +21,7 @@ module Granite
|
|
24
21
|
include ActiveModel::Serialization
|
25
22
|
include ActiveModel::Serializers::JSON
|
26
23
|
|
24
|
+
include Util
|
27
25
|
include Conventions
|
28
26
|
include Attributes
|
29
27
|
include Validations
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Granite
|
4
|
+
module Form
|
5
|
+
module Types
|
6
|
+
class Dictionary < HasSubtype
|
7
|
+
def prepare(value)
|
8
|
+
value = to_hash(value)
|
9
|
+
value = value.stringify_keys.slice(*reflection.keys) if reflection.keys.present?
|
10
|
+
value.transform_values { |v| subtype_definition.prepare(v) }.with_indifferent_access
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def to_hash(value)
|
16
|
+
Hash[value]
|
17
|
+
rescue ArgumentError
|
18
|
+
{}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Granite
|
2
|
+
module Form
|
3
|
+
module Types
|
4
|
+
class HasSubtype
|
5
|
+
attr_reader :subtype_definition
|
6
|
+
delegate :reflection, :owner, :type, :name, :enum, to: :subtype_definition
|
7
|
+
|
8
|
+
def initialize(subtype_definition)
|
9
|
+
@subtype_definition = subtype_definition
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_duplicate(*args)
|
13
|
+
self.class.new(subtype_definition.build_duplicate(*args))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -13,6 +13,29 @@ module Granite
|
|
13
13
|
@owner = owner
|
14
14
|
end
|
15
15
|
|
16
|
+
def build_duplicate(reflection, owner)
|
17
|
+
self.class.new(type, reflection, owner)
|
18
|
+
end
|
19
|
+
|
20
|
+
def prepare(value)
|
21
|
+
enumerize(ensure_type(value))
|
22
|
+
end
|
23
|
+
|
24
|
+
def enum
|
25
|
+
source = owner.evaluate(reflection.enum)
|
26
|
+
|
27
|
+
case source
|
28
|
+
when ::Range
|
29
|
+
source.to_a
|
30
|
+
when ::Set
|
31
|
+
source
|
32
|
+
else
|
33
|
+
::Array.wrap(source)
|
34
|
+
end.to_set
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
16
39
|
def ensure_type(value)
|
17
40
|
if value.instance_of?(type)
|
18
41
|
value
|
@@ -24,6 +47,11 @@ module Granite
|
|
24
47
|
def typecast(value)
|
25
48
|
value if value.is_a?(type)
|
26
49
|
end
|
50
|
+
|
51
|
+
def enumerize(value)
|
52
|
+
set = enum if reflection.enum
|
53
|
+
value if !set || (set.none? || set.include?(value))
|
54
|
+
end
|
27
55
|
end
|
28
56
|
end
|
29
57
|
end
|
data/lib/granite/form/types.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'granite/form/types/has_subtype'
|
3
4
|
require 'granite/form/types/object'
|
4
5
|
require 'granite/form/types/array'
|
5
6
|
require 'granite/form/types/big_decimal'
|
6
7
|
require 'granite/form/types/boolean'
|
8
|
+
require 'granite/form/types/collection'
|
7
9
|
require 'granite/form/types/date'
|
8
10
|
require 'granite/form/types/date_time'
|
11
|
+
require 'granite/form/types/dictionary'
|
9
12
|
require 'granite/form/types/hash_with_action_controller_parameters'
|
10
13
|
require 'granite/form/types/float'
|
11
14
|
require 'granite/form/types/integer'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Granite
|
2
|
+
module Form
|
3
|
+
module Util
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Evaluates value and returns result based on what was passed:
|
7
|
+
# - if Proc was passed, then executes it in context of self
|
8
|
+
# - if Symbol was passed, then calls a method with that name and returns result
|
9
|
+
# - otherwise just returns the value itself
|
10
|
+
# @param value [Object] value to evaluate
|
11
|
+
# @return [Object] result of evaluation
|
12
|
+
def evaluate(value, *args)
|
13
|
+
value.is_a?(Symbol) ? evaluate_symbol(value, *args) : evaluate_if_proc(value, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Evaluates value and returns result based on what was passed:
|
17
|
+
# - if Proc was passed, then executes it in context of self
|
18
|
+
# - otherwise just returns the value itself
|
19
|
+
# @param value [Object] value to evaluate
|
20
|
+
# @return [Object] result of evaluation
|
21
|
+
def evaluate_if_proc(value, *args)
|
22
|
+
value.is_a?(Proc) ? evaluate_proc(value, *args) : value
|
23
|
+
end
|
24
|
+
|
25
|
+
# Evaluates `if` or `unless` conditions present in the supplied
|
26
|
+
# `options` being it a symbol or callable.
|
27
|
+
#
|
28
|
+
# @param [Hash] options The method options to evaluate.
|
29
|
+
# @option options :if method name or callable
|
30
|
+
# @option options :unless method name or callable
|
31
|
+
# @return [Boolean] whether conditions are satisfied
|
32
|
+
def conditions_satisfied?(**options)
|
33
|
+
raise ArgumentError, 'You cannot specify both if and unless' if options.key?(:if) && options.key?(:unless)
|
34
|
+
|
35
|
+
if options.key?(:if)
|
36
|
+
evaluate(options[:if])
|
37
|
+
elsif options.key?(:unless)
|
38
|
+
!evaluate(options[:unless])
|
39
|
+
else
|
40
|
+
true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def evaluate_proc(value, *args)
|
47
|
+
instance_exec(*args, &value)
|
48
|
+
end
|
49
|
+
|
50
|
+
def evaluate_symbol(value, *args)
|
51
|
+
__send__(value, *args)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/granite/form/version.rb
CHANGED
data/lib/granite/form.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
|