granite-form 0.5.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +26 -48
- data/.rubocop_todo.yml +304 -27
- data/CHANGELOG.md +14 -2
- data/granite-form.gemspec +2 -1
- data/lib/granite/form/active_record/associations.rb +4 -3
- data/lib/granite/form/config.rb +1 -1
- data/lib/granite/form/errors.rb +34 -32
- data/lib/granite/form/extensions.rb +2 -1
- data/lib/granite/form/model/associations/base.rb +6 -2
- data/lib/granite/form/model/associations/collection/embedded.rb +1 -1
- data/lib/granite/form/model/associations/collection/proxy.rb +3 -3
- data/lib/granite/form/model/associations/embeds_any.rb +1 -1
- data/lib/granite/form/model/associations/embeds_many.rb +15 -11
- data/lib/granite/form/model/associations/embeds_one.rb +9 -8
- data/lib/granite/form/model/associations/nested_attributes.rb +60 -32
- data/lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb +2 -1
- data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +7 -6
- data/lib/granite/form/model/associations/persistence_adapters/base.rb +8 -4
- data/lib/granite/form/model/associations/references_any.rb +1 -1
- data/lib/granite/form/model/associations/references_many.rb +3 -2
- data/lib/granite/form/model/associations/references_one.rb +1 -1
- data/lib/granite/form/model/associations/reflections/base.rb +3 -2
- data/lib/granite/form/model/associations/reflections/embeds_any.rb +4 -4
- data/lib/granite/form/model/associations/reflections/embeds_many.rb +4 -1
- data/lib/granite/form/model/associations/reflections/embeds_one.rb +4 -1
- data/lib/granite/form/model/associations/reflections/references_any.rb +6 -6
- data/lib/granite/form/model/associations/reflections/references_many.rb +1 -1
- data/lib/granite/form/model/associations/reflections/references_one.rb +1 -1
- data/lib/granite/form/model/associations/validations.rb +6 -6
- data/lib/granite/form/model/associations.rb +6 -4
- data/lib/granite/form/model/attributes/attribute.rb +1 -0
- data/lib/granite/form/model/attributes/base.rb +9 -7
- data/lib/granite/form/model/attributes/reference_one.rb +1 -1
- data/lib/granite/form/model/attributes/reflections/base/build_type_definition.rb +2 -1
- data/lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb +2 -2
- data/lib/granite/form/model/attributes/represents.rb +1 -1
- data/lib/granite/form/model/attributes.rb +31 -14
- data/lib/granite/form/model/conventions.rb +1 -1
- data/lib/granite/form/model/persistence.rb +1 -1
- data/lib/granite/form/model/primary.rb +1 -1
- data/lib/granite/form/model/representation.rb +4 -4
- data/lib/granite/form/model/scopes.rb +5 -5
- data/lib/granite/form/model/validations.rb +4 -3
- data/lib/granite/form/types/active_support/time_zone.rb +1 -1
- data/lib/granite/form/types/array.rb +1 -1
- data/lib/granite/form/types/big_decimal.rb +1 -1
- data/lib/granite/form/types/boolean.rb +1 -1
- data/lib/granite/form/types/date.rb +1 -1
- data/lib/granite/form/types/date_time.rb +1 -1
- data/lib/granite/form/types/dictionary.rb +1 -1
- data/lib/granite/form/types/float.rb +1 -1
- data/lib/granite/form/types/has_subtype.rb +1 -0
- data/lib/granite/form/types/hash_with_action_controller_parameters.rb +2 -2
- data/lib/granite/form/types/integer.rb +1 -1
- data/lib/granite/form/types/object.rb +2 -1
- data/lib/granite/form/types/string.rb +1 -1
- data/lib/granite/form/types/time.rb +1 -1
- data/lib/granite/form/types/uuid.rb +1 -1
- data/lib/granite/form/util.rb +1 -1
- data/lib/granite/form/version.rb +1 -1
- data/spec/granite/form/active_record/associations_spec.rb +35 -13
- data/spec/granite/form/config_spec.rb +8 -4
- data/spec/granite/form/model/associations/embeds_many_spec.rb +99 -51
- data/spec/granite/form/model/associations/embeds_one_spec.rb +48 -25
- data/spec/granite/form/model/associations/persistence_adapters/active_record_spec.rb +12 -7
- data/spec/granite/form/model/associations/references_many_spec.rb +51 -10
- data/spec/granite/form/model/associations/references_one_spec.rb +17 -6
- data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +51 -16
- data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +19 -9
- data/spec/granite/form/model/associations/reflections/references_many_spec.rb +67 -15
- data/spec/granite/form/model/associations/reflections/references_one_spec.rb +34 -11
- data/spec/granite/form/model/associations/validations_spec.rb +16 -5
- data/spec/granite/form/model/associations_spec.rb +28 -9
- data/spec/granite/form/model/attributes/attribute_spec.rb +33 -11
- data/spec/granite/form/model/attributes/base_spec.rb +9 -3
- data/spec/granite/form/model/attributes/reflections/attribute_spec.rb +1 -0
- data/spec/granite/form/model/attributes/reflections/base_spec.rb +1 -0
- data/spec/granite/form/model/attributes/reflections/represents/build_type_definition_spec.rb +3 -1
- data/spec/granite/form/model/attributes/reflections/represents_spec.rb +2 -2
- data/spec/granite/form/model/attributes/represents_spec.rb +2 -2
- data/spec/granite/form/model/attributes_spec.rb +97 -36
- data/spec/granite/form/model/dirty_spec.rb +3 -0
- data/spec/granite/form/model/persistence_spec.rb +15 -5
- data/spec/granite/form/model/primary_spec.rb +17 -2
- data/spec/granite/form/model/representation_spec.rb +13 -3
- data/spec/granite/form/model/scopes_spec.rb +8 -3
- data/spec/granite/form/model/validations/associated_spec.rb +20 -6
- data/spec/granite/form/model/validations/nested_spec.rb +30 -14
- data/spec/granite/form/model/validations_spec.rb +1 -1
- data/spec/granite/form/model_spec.rb +1 -0
- data/spec/granite/form/types/collection_spec.rb +2 -1
- data/spec/granite/form/types/date_spec.rb +1 -1
- data/spec/granite/form/types/date_time_spec.rb +0 -2
- data/spec/granite/form/types/dictionary_spec.rb +1 -0
- data/spec/granite/form/types/has_subtype_spec.rb +6 -1
- data/spec/granite/form/types/hash_with_action_controller_parameters_spec.rb +1 -1
- data/spec/granite/form/types/object_spec.rb +2 -0
- data/spec/granite/form/types/time_spec.rb +0 -2
- data/spec/granite/form/util_spec.rb +6 -3
- data/spec/support/active_record.rb +13 -0
- data/spec/support/shared/nested_attribute_examples.rb +110 -54
- metadata +19 -6
- data/.github/CODEOWNERS +0 -1
@@ -4,6 +4,7 @@ module Granite
|
|
4
4
|
module Attributes
|
5
5
|
class Base
|
6
6
|
attr_reader :type_definition
|
7
|
+
|
7
8
|
delegate :type, :reflection, :owner, :enum, to: :type_definition
|
8
9
|
delegate :name, :readonly, to: :reflection
|
9
10
|
|
@@ -20,6 +21,7 @@ module Granite
|
|
20
21
|
|
21
22
|
def write(value)
|
22
23
|
return if readonly?
|
24
|
+
|
23
25
|
write_value value
|
24
26
|
end
|
25
27
|
|
@@ -57,12 +59,12 @@ module Granite
|
|
57
59
|
|
58
60
|
def inspect_attribute
|
59
61
|
value = case read
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
when Date, Time, DateTime
|
63
|
+
%("#{read.to_formatted_s(:db)}")
|
64
|
+
else
|
65
|
+
inspection = read.inspect
|
66
|
+
inspection.size > 100 ? inspection.truncate(50) : inspection
|
67
|
+
end
|
66
68
|
"#{name}: #{value}"
|
67
69
|
end
|
68
70
|
|
@@ -89,7 +91,7 @@ module Granite
|
|
89
91
|
end
|
90
92
|
end
|
91
93
|
|
92
|
-
|
94
|
+
private
|
93
95
|
|
94
96
|
def remove_variable(*names)
|
95
97
|
names.flatten.each do |name|
|
@@ -6,6 +6,7 @@ module Granite
|
|
6
6
|
class Base
|
7
7
|
class BuildTypeDefinition
|
8
8
|
attr_reader :owner, :reflection
|
9
|
+
|
9
10
|
delegate :name, to: :reflection
|
10
11
|
|
11
12
|
def initialize(owner, reflection)
|
@@ -19,7 +20,7 @@ module Granite
|
|
19
20
|
type_definition_for(type)
|
20
21
|
end
|
21
22
|
|
22
|
-
|
23
|
+
private
|
23
24
|
|
24
25
|
def type
|
25
26
|
reflection.options[:type]
|
@@ -19,7 +19,7 @@ module Granite
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
private
|
23
23
|
|
24
24
|
def reference
|
25
25
|
owner.__send__(reflection.reference)
|
@@ -61,7 +61,7 @@ module Granite
|
|
61
61
|
|
62
62
|
def convert_active_model_type_to_definition(attribute_type)
|
63
63
|
type = attribute_type.try(:value_class) ||
|
64
|
-
|
64
|
+
Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
|
65
65
|
type_definition_for(type) if type
|
66
66
|
end
|
67
67
|
end
|
@@ -16,16 +16,20 @@ module Granite
|
|
16
16
|
extend ActiveSupport::Concern
|
17
17
|
|
18
18
|
included do
|
19
|
-
class_attribute :_attributes, :_attribute_aliases, :_sanitize,
|
19
|
+
class_attribute :_attributes, :_attribute_aliases, :_sanitize,
|
20
|
+
:mass_assignment_strict_mode,
|
21
|
+
instance_reader: false, instance_writer: false
|
20
22
|
self._attributes = {}
|
21
23
|
self._attribute_aliases = {}
|
22
24
|
self._sanitize = true
|
25
|
+
self.mass_assignment_strict_mode = false
|
23
26
|
|
24
27
|
delegate :attribute_names, :has_attribute?, to: 'self.class'
|
25
28
|
|
26
29
|
%w[attribute collection dictionary].each do |kind|
|
27
30
|
define_singleton_method kind do |*args, &block|
|
28
|
-
add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args,
|
31
|
+
add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args,
|
32
|
+
&block)
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
@@ -34,7 +38,7 @@ module Granite
|
|
34
38
|
def add_attribute(reflection_class, *args, &block)
|
35
39
|
reflection = reflection_class.build(self, generated_attributes_methods, *args, &block)
|
36
40
|
self._attributes = _attributes.merge(reflection.name => reflection)
|
37
|
-
should_define_dirty =
|
41
|
+
should_define_dirty = dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base
|
38
42
|
define_dirty(reflection.name, generated_attributes_methods) if should_define_dirty
|
39
43
|
reflection
|
40
44
|
end
|
@@ -42,7 +46,12 @@ module Granite
|
|
42
46
|
def alias_attribute(alias_name, attribute_name)
|
43
47
|
reflection = reflect_on_attribute(attribute_name)
|
44
48
|
raise ArgumentError, "Unable to alias undefined attribute `#{attribute_name}` on #{self}" unless reflection
|
45
|
-
|
49
|
+
|
50
|
+
if reflection.class == Granite::Form::Model::Attributes::Reflections::Base
|
51
|
+
raise ArgumentError,
|
52
|
+
"Unable to alias base attribute `#{attribute_name}`"
|
53
|
+
end
|
54
|
+
|
46
55
|
reflection.class.generate_methods alias_name, generated_attributes_methods
|
47
56
|
self._attribute_aliases = _attribute_aliases.merge(alias_name.to_s => reflection.name)
|
48
57
|
define_dirty alias_name, generated_attributes_methods if dirty?
|
@@ -85,7 +94,7 @@ module Granite
|
|
85
94
|
self._sanitize = previous_sanitize
|
86
95
|
end
|
87
96
|
|
88
|
-
|
97
|
+
private
|
89
98
|
|
90
99
|
def original_inspect
|
91
100
|
Object.method(:inspect).unbind.bind(self).call
|
@@ -101,7 +110,7 @@ module Granite
|
|
101
110
|
def generated_attributes_methods
|
102
111
|
@generated_attributes_methods ||=
|
103
112
|
const_set(:GeneratedAttributesMethods, Module.new)
|
104
|
-
|
113
|
+
.tap { |proxy| include proxy }
|
105
114
|
end
|
106
115
|
|
107
116
|
def inverted_attribute_aliases
|
@@ -117,14 +126,15 @@ module Granite
|
|
117
126
|
end
|
118
127
|
|
119
128
|
def ==(other)
|
120
|
-
super || other.instance_of?(self.class) && other.attributes(false) == attributes(false)
|
129
|
+
super || (other.instance_of?(self.class) && other.attributes(false) == attributes(false))
|
121
130
|
end
|
122
131
|
|
123
|
-
|
132
|
+
alias eql? ==
|
124
133
|
|
125
134
|
def attribute(name)
|
126
135
|
reflection = self.class.reflect_on_attribute(name)
|
127
136
|
return unless reflection
|
137
|
+
|
128
138
|
initial_value = @initial_attributes.to_h.fetch(reflection.name, Granite::Form::UNDEFINED)
|
129
139
|
@_attributes ||= {}
|
130
140
|
@_attributes[reflection.name] ||= reflection.build_attribute(self, initial_value)
|
@@ -134,13 +144,13 @@ module Granite
|
|
134
144
|
attribute(name).write(value)
|
135
145
|
end
|
136
146
|
|
137
|
-
|
147
|
+
alias []= write_attribute
|
138
148
|
|
139
149
|
def read_attribute(name)
|
140
150
|
attribute(name).read
|
141
151
|
end
|
142
152
|
|
143
|
-
|
153
|
+
alias [] read_attribute
|
144
154
|
|
145
155
|
def read_attribute_before_type_cast(name)
|
146
156
|
attribute(name).read_before_type_cast
|
@@ -162,7 +172,7 @@ module Granite
|
|
162
172
|
assign_attributes(attrs)
|
163
173
|
end
|
164
174
|
|
165
|
-
|
175
|
+
alias update_attributes update
|
166
176
|
|
167
177
|
def assign_attributes(attrs)
|
168
178
|
attrs.each do |name, value|
|
@@ -172,12 +182,13 @@ module Granite
|
|
172
182
|
if respond_to?("#{name}=") && !sanitize_value
|
173
183
|
public_send("#{name}=", value)
|
174
184
|
else
|
175
|
-
|
185
|
+
attribute_type = sanitize_value ? 'primary' : 'undefined'
|
186
|
+
report_unknown_attribute(attribute_type, name)
|
176
187
|
end
|
177
188
|
end
|
178
189
|
end
|
179
190
|
|
180
|
-
|
191
|
+
alias attributes= assign_attributes
|
181
192
|
|
182
193
|
def sync_attributes
|
183
194
|
attribute_names.each do |name|
|
@@ -198,7 +209,13 @@ module Granite
|
|
198
209
|
super
|
199
210
|
end
|
200
211
|
|
201
|
-
|
212
|
+
private
|
213
|
+
|
214
|
+
def report_unknown_attribute(attribute_type, name)
|
215
|
+
raise ActiveModel::UnknownAttributeError.new(self, name.to_s) if self.class.mass_assignment_strict_mode
|
216
|
+
|
217
|
+
logger.debug("Ignoring #{attribute_type} `#{name}` attribute value for #{self} during mass-assignment")
|
218
|
+
end
|
202
219
|
|
203
220
|
def attributes_for_inspect
|
204
221
|
attribute_names(false).map do |name|
|
@@ -29,7 +29,7 @@ module Granite
|
|
29
29
|
alias_attribute :primary_attribute, _primary_name
|
30
30
|
end
|
31
31
|
|
32
|
-
|
32
|
+
alias primary_attribute primary
|
33
33
|
|
34
34
|
def has_primary_attribute? # rubocop:disable Naming/PredicateName
|
35
35
|
has_attribute? _primary_name
|
@@ -19,7 +19,7 @@ module Granite
|
|
19
19
|
attrs = attrs.to_unsafe_hash if attrs.respond_to?(:to_unsafe_hash)
|
20
20
|
attrs = attrs.stringify_keys
|
21
21
|
represented_attrs = self.class.represented_names_and_aliases
|
22
|
-
|
22
|
+
.each_with_object({}) do |name, result|
|
23
23
|
result[name] = attrs.delete(name) if attrs.key?(name)
|
24
24
|
end
|
25
25
|
|
@@ -29,7 +29,7 @@ module Granite
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
|
32
|
+
alias attributes= assign_attributes
|
33
33
|
end
|
34
34
|
|
35
35
|
module ClassMethods
|
@@ -53,9 +53,9 @@ module Granite
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
|
56
|
+
private
|
57
57
|
|
58
|
-
def run_validations!
|
58
|
+
def run_validations! # :nodoc:
|
59
59
|
super
|
60
60
|
emerge_represented_attributes_errors!
|
61
61
|
errors.empty?
|
@@ -26,11 +26,14 @@ module Granite
|
|
26
26
|
|
27
27
|
unless trust && source.is_a?(self.class)
|
28
28
|
source.each do |entity|
|
29
|
-
|
29
|
+
unless entity.is_a?(self.class._scope_model)
|
30
|
+
raise AssociationTypeMismatch.new(self.class._scope_model,
|
31
|
+
entity.class)
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
33
|
-
super
|
36
|
+
super(source)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
@@ -38,7 +41,6 @@ module Granite
|
|
38
41
|
super || self.class._scope_model.respond_to?(method)
|
39
42
|
end
|
40
43
|
|
41
|
-
# rubocop:disable Style/MethodMissing
|
42
44
|
# rubocop-0.52.1 doesn't understand that `#respond_to_missing?` is defined above
|
43
45
|
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
|
44
46
|
def method_missing(method, *args, **kwargs, &block)
|
@@ -76,8 +78,6 @@ module Granite
|
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|
79
|
-
# rubocop:enable Style/MethodMissing
|
80
|
-
|
81
81
|
def with_scope
|
82
82
|
previous_scope = self.class._scope_model.current_scope
|
83
83
|
self.class._scope_model.current_scope = self
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require_relative 'validations/nested'
|
2
|
+
require_relative 'validations/associated'
|
3
|
+
|
1
4
|
module Granite
|
2
5
|
module Form
|
3
6
|
module Model
|
@@ -22,7 +25,7 @@ module Granite
|
|
22
25
|
valid?(context) || raise_validation_error
|
23
26
|
end
|
24
27
|
|
25
|
-
|
28
|
+
protected
|
26
29
|
|
27
30
|
def raise_validation_error
|
28
31
|
raise Granite::Form::ValidationError, self
|
@@ -31,5 +34,3 @@ module Granite
|
|
31
34
|
end
|
32
35
|
end
|
33
36
|
end
|
34
|
-
|
35
|
-
Dir[File.dirname(__FILE__) + '/validations/*.rb'].each { |file| require file }
|
@@ -4,13 +4,13 @@ module Granite
|
|
4
4
|
module Form
|
5
5
|
module Types
|
6
6
|
class HashWithActionControllerParameters < Object
|
7
|
-
|
7
|
+
private
|
8
8
|
|
9
9
|
def typecast(value)
|
10
10
|
case value
|
11
11
|
when ActionController::Parameters
|
12
12
|
value.to_h if value.permitted?
|
13
|
-
when ::Hash
|
13
|
+
when ::Hash
|
14
14
|
value
|
15
15
|
end
|
16
16
|
end
|
@@ -5,6 +5,7 @@ module Granite
|
|
5
5
|
module Types
|
6
6
|
class Object
|
7
7
|
attr_reader :reflection, :owner, :type
|
8
|
+
|
8
9
|
delegate :name, to: :reflection
|
9
10
|
|
10
11
|
def initialize(type, reflection, owner)
|
@@ -34,7 +35,7 @@ module Granite
|
|
34
35
|
end.to_set
|
35
36
|
end
|
36
37
|
|
37
|
-
|
38
|
+
private
|
38
39
|
|
39
40
|
def ensure_type(value)
|
40
41
|
if value.instance_of?(type)
|
data/lib/granite/form/util.rb
CHANGED
data/lib/granite/form/version.rb
CHANGED