granite 0.14.1 → 0.15.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6a9f12a8fab90e99e863a20803400744a5eb9f0393d4fd8e9242e6d2eca542b
4
- data.tar.gz: 54c451903bc4b9b9cebbe7fd07201e8492f0fbaab301d2cbae05e4f7ecc9c6e5
3
+ metadata.gz: 2cf5813f23b963898bbdd4f55d35b2f0699428c657413ee6f18d56713ffcf659
4
+ data.tar.gz: 48a04a7a60bf1819222728acf33c8bf0a73149d9b9d83b2ea04bc70a2182ec04
5
5
  SHA512:
6
- metadata.gz: bae2c1833fe6f5e04f31a3974c6d7d94afe0a71dca159b73fd53350d30c97fcac26b67330484ade77ee08e1b1bbce0d14cba44e37408dcdde0cccf9085927152
7
- data.tar.gz: 618880a543e9c33b448990ebbf3947fab1bbf1ddcad7797dc22f4c22e6f7fbc025ffdfd224b8d321c091bcac07517ad222938cdb68fc76a63e320cc5a5f17768
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
- if self.class._subject.blank?
36
- super
37
- return
38
- end
43
+ return super unless self.class.subject? # rubocop:disable Lint/ReturnInVoidContext
39
44
 
40
- reflection = self.class.reflect_on_association(self.class._subject)
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 ActiveData::AssociationTypeMismatch
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
- ActiveData.config.logger.error "Unhandled error in callback: #{error.inspect}\n#{error.backtrace.join("\n")}"
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
@@ -1,4 +1,4 @@
1
- require 'active_data'
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 ActiveData
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 ActiveData::ValidationError do |e|
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.added?(error.attribute, error)
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 'active_data/model'
2
- require 'active_data/model/primary'
3
- require 'active_data/model/lifecycle'
4
- require 'active_data/model/associations'
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 ActiveData when building data objects (e.g. when using
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 ActiveData::Model
18
- include ActiveData::Model::Representation
19
- include ActiveData::Model::Dirty
20
- include ActiveData::Model::Associations
21
- include ActiveData::Model::Primary
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 < ActiveData::Model::Attributes::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
- active_data_type || type_from_type_for_attribute || super
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 = ActiveData.typecaster(type_class.ancestors.grep(Class))
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 active_data_type
67
- return nil unless reference.is_a?(ActiveData::Model)
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
- ActiveData::Model::Attributes::ReferenceMany,
75
- ActiveData::Model::Attributes::Collection,
76
- ActiveData::Model::Attributes::Dictionary
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 ActiveData
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(name.to_s)
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
- ActiveData::Model::Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
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 < ActiveData::Model::Attributes::Reflections::Represents
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
@@ -1,7 +1,7 @@
1
- require 'active_data'
1
+ require 'granite/form'
2
2
 
3
- ActiveData.typecaster('Granite::Action::Types::Collection') do |value, attribute|
4
- typecaster = ActiveData.typecaster(attribute.type.subtype)
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)
@@ -1,3 +1,3 @@
1
1
  module Granite
2
- VERSION = '0.14.1'.freeze
2
+ VERSION = '0.15.0'.freeze
3
3
  end
data/lib/granite.rb CHANGED
@@ -28,4 +28,4 @@ require 'granite/routing'
28
28
  require 'granite/typecasters'
29
29
  require 'granite/rails' if defined?(::Rails)
30
30
 
31
- ActiveData.base_concern = Granite::Base
31
+ Granite::Form.base_concern = Granite::Base
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.14.1
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-08-26 00:00:00.000000000 Z
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: active_data
34
+ name: granite-form
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 1.2.0
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: 1.2.0
46
+ version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
49
  requirement: !ruby/object:Gem::Requirement