granite 0.14.1 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/granite/action/subject.rb +11 -6
- data/lib/granite/action/transaction_manager.rb +1 -1
- data/lib/granite/action.rb +4 -4
- data/lib/granite/base.rb +10 -10
- data/lib/granite/represents/attribute.rb +21 -11
- data/lib/granite/represents/reflection.rb +1 -1
- data/lib/granite/typecasters.rb +3 -3
- data/lib/granite/version.rb +1 -1
- data/lib/granite.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cf5813f23b963898bbdd4f55d35b2f0699428c657413ee6f18d56713ffcf659
|
4
|
+
data.tar.gz: 48a04a7a60bf1819222728acf33c8bf0a73149d9b9d83b2ea04bc70a2182ec04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b182f94aa37d25756606fe9384a5ea98963279f8a58f84a66a7660fd21a3076b46f6965d56e26d5bc75357431f9bec2dfabb639bd198b0d00529d2567dd21628
|
7
|
+
data.tar.gz: 814dbec1866a2845d869489d82051feb017e4c850460fef1af69f13d31cf6c40fb66dbddec52529e84dfd292acce8bf73bb4479f9f8e2fca42c586c9dd12b2a2
|
@@ -29,15 +29,20 @@ module Granite
|
|
29
29
|
|
30
30
|
self._subject = name
|
31
31
|
end
|
32
|
+
|
33
|
+
def subject?
|
34
|
+
_subject.present?
|
35
|
+
end
|
36
|
+
|
37
|
+
def subject_reflection
|
38
|
+
reflect_on_association(_subject)
|
39
|
+
end
|
32
40
|
end
|
33
41
|
|
34
42
|
def initialize(*args)
|
35
|
-
|
36
|
-
super
|
37
|
-
return
|
38
|
-
end
|
43
|
+
return super unless self.class.subject? # rubocop:disable Lint/ReturnInVoidContext
|
39
44
|
|
40
|
-
reflection = self.class.
|
45
|
+
reflection = self.class.subject_reflection
|
41
46
|
attributes = extract_initialize_attributes(args)
|
42
47
|
|
43
48
|
subject_attributes = extract_subject_attributes!(attributes, reflection)
|
@@ -61,7 +66,7 @@ module Granite
|
|
61
66
|
|
62
67
|
self.subject = args.first unless args.empty?
|
63
68
|
fail SubjectNotFoundError, self.class unless subject
|
64
|
-
rescue
|
69
|
+
rescue Granite::Form::AssociationTypeMismatch
|
65
70
|
raise SubjectTypeMismatchError.new(self.class, args.first.class.name, reflection.klass)
|
66
71
|
end
|
67
72
|
|
@@ -75,7 +75,7 @@ module Granite
|
|
75
75
|
|
76
76
|
def log_errors(errors)
|
77
77
|
errors.each do |error|
|
78
|
-
|
78
|
+
Granite::Form.config.logger.error "Unhandled error in callback: #{error.inspect}\n#{error.backtrace.join("\n")}"
|
79
79
|
end
|
80
80
|
end
|
81
81
|
end
|
data/lib/granite/action.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'granite/form'
|
2
2
|
require 'active_record/errors'
|
3
3
|
require 'active_record/validations'
|
4
4
|
require 'active_support/callbacks'
|
@@ -27,7 +27,7 @@ module Granite
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# We are using a lot of stacked additional logic for `assign_attributes`
|
30
|
-
# At least, represented and nested attributes modules in
|
30
|
+
# At least, represented and nested attributes modules in Granite::Form
|
31
31
|
# are having such a method redefiniions. Both are prepended to the
|
32
32
|
# Granite action, so we have to prepend our patch as well in order
|
33
33
|
# to put it above all other, so it will handle the attributes first.
|
@@ -55,7 +55,7 @@ module Granite
|
|
55
55
|
merge_errors(e.record.errors)
|
56
56
|
end
|
57
57
|
|
58
|
-
handle_exception
|
58
|
+
handle_exception Granite::Form::ValidationError do |e|
|
59
59
|
merge_errors(e.model.errors)
|
60
60
|
end
|
61
61
|
|
@@ -72,7 +72,7 @@ module Granite
|
|
72
72
|
else
|
73
73
|
def merge_errors(other_errors)
|
74
74
|
other_errors.each do |error|
|
75
|
-
errors.import(error) unless errors.
|
75
|
+
errors.import(error) unless errors.messages[error.attribute].include?(error.message)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
data/lib/granite/base.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
1
|
+
require 'granite/form/model'
|
2
|
+
require 'granite/form/model/primary'
|
3
|
+
require 'granite/form/model/lifecycle'
|
4
|
+
require 'granite/form/model/associations'
|
5
5
|
|
6
6
|
require 'granite/translations'
|
7
7
|
require 'granite/represents'
|
8
8
|
require 'granite/assign_data'
|
9
9
|
|
10
10
|
module Granite
|
11
|
-
# Base included in Granite::Action, but also used by
|
11
|
+
# Base included in Granite::Action, but also used by Granite::Form when building data objects (e.g. when using
|
12
12
|
# embeds_many)
|
13
13
|
module Base
|
14
14
|
extend ActiveSupport::Concern
|
15
15
|
|
16
16
|
include ActiveSupport::Callbacks
|
17
|
-
include
|
18
|
-
include
|
19
|
-
include
|
20
|
-
include
|
21
|
-
include
|
17
|
+
include Granite::Form::Model
|
18
|
+
include Granite::Form::Model::Representation
|
19
|
+
include Granite::Form::Model::Dirty
|
20
|
+
include Granite::Form::Model::Associations
|
21
|
+
include Granite::Form::Model::Primary
|
22
22
|
include ActiveModel::Validations::Callbacks
|
23
23
|
|
24
24
|
include Granite::Util
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module Granite
|
2
2
|
module Represents
|
3
|
-
class Attribute <
|
3
|
+
class Attribute < Granite::Form::Model::Attributes::Attribute
|
4
|
+
types = {}
|
5
|
+
types[ActiveRecord::Enum::EnumType] = String if defined?(ActiveRecord)
|
6
|
+
TYPES = types.freeze
|
4
7
|
delegate :writer, :reader, :reader_before_type_cast, to: :reflection
|
5
8
|
|
6
9
|
def initialize(*_args)
|
@@ -23,13 +26,13 @@ module Granite
|
|
23
26
|
def type
|
24
27
|
return reflection.options[:type] if reflection.options[:type].present?
|
25
28
|
|
26
|
-
|
29
|
+
granite_form_type || type_from_type_for_attribute || super
|
27
30
|
end
|
28
31
|
|
29
32
|
def typecaster
|
30
33
|
@typecaster ||= begin
|
31
34
|
type_class = type.instance_of?(Class) ? type : type.class
|
32
|
-
@typecaster =
|
35
|
+
@typecaster = Granite::Form.typecaster(type_class.ancestors.grep(Class))
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
@@ -63,36 +66,43 @@ module Granite
|
|
63
66
|
end
|
64
67
|
end
|
65
68
|
|
66
|
-
def
|
67
|
-
return nil unless reference.is_a?(
|
69
|
+
def granite_form_type
|
70
|
+
return nil unless reference.is_a?(Granite::Form::Model)
|
68
71
|
|
69
72
|
reference_attribute = reference.attribute(name)
|
70
73
|
|
71
74
|
return nil if reference_attribute.nil?
|
72
75
|
|
73
76
|
return Granite::Action::Types::Collection.new(reference_attribute.type) if [
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
Granite::Form::Model::Attributes::ReferenceMany,
|
78
|
+
Granite::Form::Model::Attributes::Collection,
|
79
|
+
Granite::Form::Model::Attributes::Dictionary
|
77
80
|
].any? { |klass| reference_attribute.is_a? klass }
|
78
81
|
|
79
|
-
reference_attribute.type # TODO: create `type_for_attribute` method inside of
|
82
|
+
reference_attribute.type # TODO: create `type_for_attribute` method inside of Granite::Form
|
80
83
|
end
|
81
84
|
|
82
85
|
def type_from_type_for_attribute
|
83
86
|
return nil unless reference.respond_to?(:type_for_attribute)
|
84
87
|
|
85
|
-
attribute_type = reference.type_for_attribute(
|
88
|
+
attribute_type = reference.type_for_attribute(attribute_name.to_s)
|
86
89
|
|
90
|
+
return TYPES[attribute_type.class] if TYPES.key?(attribute_type.class)
|
87
91
|
return Granite::Action::Types::Collection.new(convert_type_to_value_class(attribute_type.subtype)) if attribute_type.respond_to?(:subtype)
|
88
92
|
|
89
93
|
convert_type_to_value_class(attribute_type)
|
90
94
|
end
|
91
95
|
|
96
|
+
def attribute_name
|
97
|
+
return name if ActiveModel.version >= Gem::Version.new('6.1.0')
|
98
|
+
|
99
|
+
reference.class.attribute_aliases[name.to_s] || name
|
100
|
+
end
|
101
|
+
|
92
102
|
def convert_type_to_value_class(attribute_type)
|
93
103
|
return attribute_type.value_class if attribute_type.respond_to?(:value_class)
|
94
104
|
|
95
|
-
|
105
|
+
Granite::Form::Model::Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
|
96
106
|
end
|
97
107
|
end
|
98
108
|
end
|
@@ -2,7 +2,7 @@ require 'granite/represents/attribute'
|
|
2
2
|
|
3
3
|
module Granite
|
4
4
|
module Represents
|
5
|
-
class Reflection <
|
5
|
+
class Reflection < Granite::Form::Model::Attributes::Reflections::Represents
|
6
6
|
class << self
|
7
7
|
def build(target, generated_methods, name, *args, &block)
|
8
8
|
options = args.last
|
data/lib/granite/typecasters.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require '
|
1
|
+
require 'granite/form'
|
2
2
|
|
3
|
-
|
4
|
-
typecaster =
|
3
|
+
Granite::Form.typecaster('Granite::Action::Types::Collection') do |value, attribute|
|
4
|
+
typecaster = Granite::Form.typecaster(attribute.type.subtype)
|
5
5
|
if value.respond_to? :transform_values
|
6
6
|
value.transform_values { |v| typecaster.call(v, attribute) }
|
7
7
|
elsif value.respond_to?(:map)
|
data/lib/granite/version.rb
CHANGED
data/lib/granite.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: granite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toptal Engineering
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -31,19 +31,19 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: granite-form
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: '0'
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activesupport
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|