jpie 1.1.3 → 1.2.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: ca98d7927dfa30e20390066f51aae1a468287cebefc663d9f597da31b32ed621
4
- data.tar.gz: ae87502673d1d04a991ce28acdc1e64fa434f6a357dd424b86b4158b9904f4e2
3
+ metadata.gz: 654010b56dc91dbc6edffc3c54891d7d6cb7eab738f685cbbe0e793aa9d26b13
4
+ data.tar.gz: d400665267796d760e1051697a46bb067c414142be5372e1abb2ac1254e8aaa7
5
5
  SHA512:
6
- metadata.gz: 5a3cb096bfcf3f94279d2942f286556f3e97e4b5cab8028838c7f4d2b25bd581282ef71da6110e755f984dea45cfbbe824cfb44d1f958ac98bf2610774015eea
7
- data.tar.gz: 4c70ef1b739652c73ca5d5f04e25a3152b09a382cbe9a714fb9d844fafe60354930db43139fb265024cff0fc1b2f24331dfb3b0deff72cd3d2ee1c28b0d3ec61
6
+ metadata.gz: a4081700e8daa2cd9e4b209d7cf2d1d0464b44d3678fb9095adc995fe5e075ad5fa00b64af8bc7bdb9a53177f64ba74e999e85f61d56c9c82d2908bede76b579
7
+ data.tar.gz: 6c786599fbfda71820206e62c17dbbb9ed86d28858ae905293b20a9426e3cf64da76dba69c8ffaa7dfff5193f4e42ba203a09ba635943f8ac6212cea95bdfada
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jpie (1.1.3)
4
+ jpie (1.2.0)
5
5
  actionpack (~> 8.0, >= 8.0.0)
6
6
  rails (~> 8.0, >= 8.0.0)
7
7
 
@@ -49,15 +49,15 @@ module JSONAPI
49
49
  end
50
50
 
51
51
  def preload_collection(includes)
52
- return model_class.all if includes.empty?
52
+ return resource_class.records if includes.empty?
53
53
 
54
- model_class.all.includes(includes)
54
+ resource_class.records.includes(includes)
55
55
  end
56
56
 
57
57
  def preload_single(includes)
58
- return model_class.find(params[:id]) if includes.empty?
58
+ return resource_class.records.find(params[:id]) if includes.empty?
59
59
 
60
- model_class.includes(includes).find(params[:id])
60
+ resource_class.records.includes(includes).find(params[:id])
61
61
  end
62
62
  end
63
63
  end
@@ -10,15 +10,27 @@ module JSONAPI
10
10
  def load_jsonapi_resource
11
11
  @resource_name = params[:resource_type]&.singularize
12
12
  @jsonapi_namespace = params[:jsonapi_namespace].presence
13
- @resource_class = JSONAPI::ResourceLoader.find(@resource_name, namespace: @jsonapi_namespace) if @resource_name
14
- @model_class = @resource_class.model_class if @resource_class
15
- @resource = @model_class.find(params[:id]) if params[:id] && @model_class
13
+ load_resource_and_model_class
14
+ load_resource_record
16
15
  rescue JSONAPI::ResourceLoader::MissingResourceClass
17
16
  @resource_class = nil
18
17
  rescue ActiveRecord::RecordNotFound
19
18
  render_record_not_found
20
19
  end
21
20
 
21
+ def load_resource_and_model_class
22
+ return unless @resource_name
23
+
24
+ @resource_class = JSONAPI::ResourceLoader.find(@resource_name, namespace: @jsonapi_namespace)
25
+ @model_class = @resource_class.model_class if @resource_class
26
+ end
27
+
28
+ def load_resource_record
29
+ return unless params[:id] && @resource_class
30
+
31
+ @resource = @resource_class.records.find(params[:id])
32
+ end
33
+
22
34
  def render_record_not_found
23
35
  render_jsonapi_error(
24
36
  status: 404,
@@ -34,7 +34,7 @@ module JSONAPI
34
34
  end
35
35
 
36
36
  def index
37
- scope = apply_authorization_scope(@preloaded_resources || model_class.all, action: :index)
37
+ scope = apply_authorization_scope(@preloaded_resources || resource_class.records, action: :index)
38
38
  query = build_query(scope)
39
39
  @total_count = query.total_count
40
40
  @pagination_applied = query.pagination_applied
@@ -50,6 +50,10 @@ module JSONAPI
50
50
  rescue StandardError
51
51
  safe_model_class
52
52
  end
53
+
54
+ def records
55
+ model_class.all
56
+ end
53
57
  end
54
58
  end
55
59
  end
@@ -46,8 +46,20 @@ module JSONAPI
46
46
  return get_active_storage_records(current_record, association_name)
47
47
  end
48
48
 
49
- related = current_record.public_send(association_name)
50
- Array(related)
49
+ get_association_records(current_record, association_name)
50
+ end
51
+
52
+ def get_association_records(current_record, association_name)
53
+ association = current_record.association(association_name)
54
+ related_klass = association.klass
55
+ return [] if related_klass.blank?
56
+
57
+ build_scoped_relation(related_klass, association).to_a
58
+ end
59
+
60
+ def build_scoped_relation(related_klass, association)
61
+ related_base_scope = ResourceLoader.find_for_model(related_klass).records
62
+ related_base_scope.merge(association.scope)
51
63
  end
52
64
 
53
65
  def get_active_storage_records(current_record, association_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONAPI
4
- VERSION = "1.1.3"
4
+ VERSION = "1.2.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: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp