foobara-active-record-type 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 671d17e08f4547b324b61474ef20d10e7057b063af31b4fad297b621f05e16dd
4
- data.tar.gz: 7cf66a0c5f9b4302f24f5059a50a9f2979adc0f9b870e627c213402fd6884192
3
+ metadata.gz: 3499a493db193b496a20ae4dd0d1dd1d7ad5193701346ee2de9abc397b107ff9
4
+ data.tar.gz: 59db77d01419543a0230aecb0d5f49aa6f1ba541bb820d7ffd743ee41211d2ea
5
5
  SHA512:
6
- metadata.gz: b9adb3df88d04f1d5215785eac7eb71a8ab8efdd7d88c02d24aaaf7de1c1e7a13dfacab73cb0af114c74074b1ab075284118acf8a432b045e92270106fcca56a
7
- data.tar.gz: 3524d0cf555774eb8d46afd200a98fa9d4c0c81c198936810e882d8a8d8a603035b2081b6b392f9e1c33a545cfc96a414792d9ede6e421fa4bdf4b0096e96df8
6
+ metadata.gz: a72802f7ae7ef65f5174285ae4dde77f7876b3463ea3d6a69bc7366a91838b5e1a22b94a8c6352b3725755195121855f391a30163378ceb065cfd16c6d9b9622
7
+ data.tar.gz: b49ed2b61b22d34e1b0179c841e2deb05ac5b0886fa71826bcb781f1572e6c52323ed729cd2ae7b058d5e078a184fb872e8dff4329fdd8184c557f7ef15332f0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.0.3]
2
+
3
+ - Fix problem where the first time a type is declared via an
4
+ active record class it has a complex declaration hash instead of a simple
5
+ registered class lookup
6
+
1
7
  ## [0.0.1]
2
8
 
3
9
  - Release as a gem
@@ -2,7 +2,7 @@ module ActiveRecordFoobaraMethods
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  class_methods do
5
- attr_accessor :foobara_type
5
+ attr_accessor :foobara_type, :foobara_attributes_type
6
6
  end
7
7
  end
8
8
 
@@ -7,16 +7,20 @@ module Foobara
7
7
  end
8
8
 
9
9
  def desugarize(active_record_class)
10
- if active_record_class.superclass != ActiveRecord::Base
11
- # this will register a foobara type for the base class
12
- type_for_declaration(active_record_class.superclass)
10
+ active_record_superclass = active_record_class.superclass
11
+
12
+ if active_record_superclass != ActiveRecord::Base
13
+ if active_record_superclass.attribute_names.include?(active_record_class.primary_key)
14
+ # this will register a foobara type for the base class
15
+ type_for_declaration(active_record_class.superclass)
16
+ end
13
17
  end
14
18
 
15
19
  {
16
20
  type: :active_record,
17
21
  model_class: active_record_class.name,
18
22
  name: active_record_class.name,
19
- model_base_class: active_record_class.superclass.name,
23
+ model_base_class: active_record_superclass.name,
20
24
  model_module: Util.module_for(active_record_class)&.name,
21
25
  primary_key: active_record_class.primary_key,
22
26
  attributes_declaration: active_record_class_to_attributes_declaration(active_record_class)
@@ -13,7 +13,9 @@ module Foobara
13
13
  handler = handler_for_class(TypeDeclarations::Handlers::ExtendAttributesTypeDeclaration)
14
14
  attributes_type_declaration = type.declaration_data[:attributes_declaration]
15
15
 
16
- type.element_types = handler.process_value!(attributes_type_declaration).element_types
16
+ active_record_class.foobara_attributes_type = handler.process_value!(attributes_type_declaration)
17
+
18
+ type.element_types = active_record_class.foobara_attributes_type.element_types
17
19
  type_name = type.declaration_data[:name]
18
20
 
19
21
  # We don't want to check that the active record is valid as if it were a model
@@ -5,13 +5,14 @@ module Foobara
5
5
  module RegisteredTypeDeclaration
6
6
  module Desugarizers
7
7
  class RegisteredActiveRecordBaseClassDesugarizer < TypeDeclarations::Desugarizer
8
- def applicable?(klass)
9
- klass.is_a?(Class) && klass < ActiveRecord::Base && type_registered?(klass.name)
8
+ def applicable?(sugary_type_declaration)
9
+ sugary_type_declaration.is_a?(Class) && sugary_type_declaration < ActiveRecord::Base &&
10
+ sugary_type_declaration.foobara_type
10
11
  end
11
12
 
12
13
  def desugarize(active_record_class)
13
14
  {
14
- type: active_record_class.name
15
+ type: active_record_class.foobara_type.foobara_manifest_reference.to_sym
15
16
  }
16
17
  end
17
18
 
@@ -0,0 +1,36 @@
1
+ module Foobara
2
+ module ActiveRecordType
3
+ class ExtendActiveRecordTypeDeclaration < TypeDeclarations::Handlers::ExtendDetachedEntityTypeDeclaration
4
+ module TypeDeclarationExtension
5
+ module RegisteredTypeDeclaration
6
+ module Desugarizers
7
+ class UnregisteredActiveRecordBaseClassDesugarizer < TypeDeclarations::Desugarizer
8
+ def applicable?(sugary_type_declaration)
9
+ sugary_type_declaration.is_a?(Class) && sugary_type_declaration < ActiveRecord::Base &&
10
+ !sugary_type_declaration.foobara_type
11
+ end
12
+
13
+ # We will create the foobara type from the active record class and then the
14
+ # RegisteredActiveRecordBaseClassDesugarizer
15
+ # will handle it properly.
16
+ # This will keep declarations using just the active record class simple in the first place they are used
17
+ # by acting as if it were a registered type at the time even though it wasn't yet.
18
+ def desugarize(active_record_class)
19
+ handler = handler_for_class(Foobara::ActiveRecordType::ExtendActiveRecordTypeDeclaration)
20
+ handler.process_value!(active_record_class)
21
+
22
+ {
23
+ type: active_record_class.foobara_type.foobara_manifest_reference.to_sym
24
+ }
25
+ end
26
+
27
+ def priority
28
+ Priority::FIRST - 3
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-active-record-type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-04 00:00:00.000000000 Z
10
+ date: 2025-01-18 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -58,8 +58,9 @@ files:
58
58
  - src/extend_active_record_type_declaration/hash_desugarizer.rb
59
59
  - src/extend_active_record_type_declaration/model_class_desugarizer.rb
60
60
  - src/extend_active_record_type_declaration/primary_key_desugarizer.rb
61
- - src/extend_active_record_type_declaration/registered_active_record_base_class_desugarizer.rb
62
61
  - src/extend_active_record_type_declaration/to_type_transformer.rb
62
+ - src/extend_active_record_type_declaration/type_declaration_extension/registered_type_declaration/desugarizers/registered_active_record_base_class_desugarizer.rb
63
+ - src/extend_active_record_type_declaration/type_declaration_extension/registered_type_declaration/desugarizers/unregistered_active_record_base_class_desugarizer.rb
63
64
  - src/extend_active_record_type_declaration/validate_primary_key_is_symbol.rb
64
65
  - src/extend_active_record_type_declaration/validate_primary_key_present.rb
65
66
  - src/extend_active_record_type_declaration/validate_primary_key_references_attribute.rb