jpie 3.9.0 → 3.10.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: 4f3af561d7afa748d2a1095a6b3c83fe8ab42b12a6fb88655f67c023e72779ba
4
- data.tar.gz: ea3c315885d7b2ae18f328bb6659c796e64e82b62b87ae479fd7d3faa0ef4f09
3
+ metadata.gz: 637098cfaf33f60d61c1669573a007686467f5218a1b20aff8701886b3fa79d5
4
+ data.tar.gz: 95e995bb47caddc5f3ae867b0e4d3aa2b5fb2de9f40225bba36a7edb527b62b0
5
5
  SHA512:
6
- metadata.gz: edd52e432cec91efbd1487ebb8ad340fbe560879415ae6af400290b68e5c5bfa3db6f7afd967077bbf6a5509992329b412f865af88db19f80accadb8903f44e5
7
- data.tar.gz: 1f9854c359c421410f19645ae03659137059e92f8bc88a1b52b3e79ac2906abe5747c859a46ed02a312ba8ab37e7f661fbce1c16e42a2a29e73e5446122889b1
6
+ metadata.gz: ccbdca591a2dcd1a6745f72beb4f477777ac96bfbbf0f3973c56bb4d2f7e63ebecc4bd20feab45d345e887a4d2903aa97c2bce3efa30a36f26403d771311d3fb
7
+ data.tar.gz: 3a6f363d40e998ce65c9805ed06fcf7a2fea1b5e2aac13fd14360f0b8250937b9a00302b230e2a2931d32b8991e55ffdfd93c779cc3534f54f7ed06a1e0755bf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jpie (3.9.0)
4
+ jpie (3.10.0)
5
5
  actionpack (~> 8.1, >= 8.1.0)
6
6
  pg_query (>= 4)
7
7
  prosopite (>= 1)
@@ -70,13 +70,16 @@ module JSONAPI
70
70
  end
71
71
 
72
72
  def determine_sti_class
73
- JSONAPI::ResourceLoader.find(jsonapi_type || resource_type).model_class
73
+ JSONAPI::ResourceLoader.find(jsonapi_type || resource_type, namespace: jsonapi_namespace).model_class
74
74
  rescue JSONAPI::ResourceLoader::MissingResourceClass
75
75
  model_class
76
76
  end
77
77
 
78
+ # The namespace decides which resource defines the writable fields. Resolving the
79
+ # type alone returns the flat resource, so a namespaced endpoint over a flat model
80
+ # would accept every field the flat resource allows.
78
81
  def determine_sti_resource_class
79
- JSONAPI::ResourceLoader.find(jsonapi_type || resource_type)
82
+ JSONAPI::ResourceLoader.find(jsonapi_type || resource_type, namespace: jsonapi_namespace)
80
83
  rescue JSONAPI::ResourceLoader::MissingResourceClass
81
84
  @resource_class
82
85
  end
@@ -8,6 +8,11 @@ module JSONAPI
8
8
 
9
9
  def serialize_resource(resource)
10
10
  includes = parse_include_param
11
+ # On show and update the record arrives through `load_resource_record`, which loaded
12
+ # the include tree via `scope_with_includes`. Each included resource's own `records`
13
+ # preloads still have to be applied, or every included record pays them one query at
14
+ # a time. On create the tree is unloaded, so this finds no targets and does nothing.
15
+ preload_included_resource_associations([resource], includes)
11
16
  cache = build_include_filter_cache([resource], includes)
12
17
  serializer = JSONAPI::Serializer.new(resource, authorization_context: self, include_filter_cache: cache,
13
18
  namespace: jsonapi_namespace,)
@@ -111,8 +111,14 @@ module JSONAPI
111
111
  handler.call(controller: authorization_context, scope:, action: :index, model_class:)
112
112
  end
113
113
 
114
+ # An attachment reaches the serializer as loaded blobs, never as a relation, so it
115
+ # takes the same route as any other loaded association: the filter cache applies the
116
+ # configured authorization_scope for ActiveStorage::Blob. Without it an attachment
117
+ # sideload carried no authorization of its own.
114
118
  def get_active_storage_records(current_record, association_name)
115
- JSONAPI::ActiveStorage::Serialization.blobs_for(association_name, current_record)
119
+ blobs = JSONAPI::ActiveStorage::Serialization.blobs_for(association_name, current_record)
120
+
121
+ include_filter_cache.filter(blobs, ::ActiveStorage::Blob)
116
122
  end
117
123
 
118
124
  def serialize_and_process_record(related_record, path_to_record, ctx, parent_record: nil, association_name: nil)
@@ -20,6 +20,23 @@ module JSONAPI
20
20
 
21
21
  private
22
22
 
23
+ # A belongs_to may name a key that does not match the association
24
+ # (belongs_to :author, foreign_key: "user_id"), so ask the reflection.
25
+ def association_foreign_key(association_name, param_name)
26
+ belongs_to_reflection(association_name)&.foreign_key&.to_s || "#{param_name}_id"
27
+ end
28
+
29
+ def association_foreign_type(association_name, param_name)
30
+ belongs_to_reflection(association_name)&.foreign_type&.to_s || "#{param_name}_type"
31
+ end
32
+
33
+ def belongs_to_reflection(association_name)
34
+ return nil unless @model_class
35
+
36
+ association = @model_class.reflect_on_association(association_name.to_sym)
37
+ association if association&.belongs_to?
38
+ end
39
+
23
40
  def normalize_relationship_value(value)
24
41
  ParamHelpers.ensure_object!(value, "relationship")
25
42
  end
@@ -32,8 +49,10 @@ module JSONAPI
32
49
  if active_storage_attachment?(association_name)
33
50
  attrs[association_name.to_s] = nil
34
51
  else
35
- attrs["#{param_name}_id"] = nil
36
- attrs["#{param_name}_type"] = nil if polymorphic_association?(association_name)
52
+ attrs[association_foreign_key(association_name, param_name)] = nil
53
+ if polymorphic_association?(association_name)
54
+ attrs[association_foreign_type(association_name, param_name)] = nil
55
+ end
37
56
  end
38
57
  end
39
58
 
@@ -96,8 +115,8 @@ module JSONAPI
96
115
 
97
116
  def process_polymorphic_relationship(attrs, association_name, param_name, id, type)
98
117
  class_name = validate_and_get_class_name(type, association_name)
99
- attrs["#{param_name}_id"] = id
100
- attrs["#{param_name}_type"] = class_name
118
+ attrs[association_foreign_key(association_name, param_name)] = id
119
+ attrs[association_foreign_type(association_name, param_name)] = class_name
101
120
  end
102
121
 
103
122
  def validate_and_get_class_name(type, association_name)
@@ -112,7 +131,7 @@ module JSONAPI
112
131
 
113
132
  def process_non_polymorphic_relationship(attrs, association_name, param_name, id, type)
114
133
  validate_relationship_type(association_name, type)
115
- attrs["#{param_name}_id"] = id
134
+ attrs[association_foreign_key(association_name, param_name)] = id
116
135
  end
117
136
  end
118
137
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONAPI
4
- VERSION = "3.9.0"
4
+ VERSION = "3.10.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpie
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.9.0
4
+ version: 3.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp