json_api_preloader 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/lib/json_api_preloader.rb +39 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94b801b1c42dd0c47cc7d8c3960047ebd4bd2d1e1bbfae4dd1f61568162e5326
4
+ data.tar.gz: 69bbed7dcd7583534ae224ef48d57382d65f0412e27fd0a07acc8a74a55d83fe
5
+ SHA512:
6
+ metadata.gz: a87a21c9ac91d7d5c8eaa2a13c8dd6628633d2d17b7e367213d9fbe18c8fdb1a19fdcb810045206b3e627834be0ee9768524a0d5195fc9cee696e1a84fd13c33
7
+ data.tar.gz: 6a33984af2af8b66fb24c0510cd65733011ee0a1f1f8365042f6adbcebaf350506f8ae57d579888418d62a6afb39b9162726413a279a0c41ac6fda9b59b56de2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rubocop'
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiPreloader
4
+ def preload_from_params_for(model_name)
5
+ class_attribute :preloader_configuration
6
+ self.preloader_configuration = {
7
+ name: model_name
8
+ }
9
+ end
10
+
11
+ private
12
+
13
+ def preloaded(key: nil, parent:)
14
+ included = params[:include]
15
+ return {} unless included.presence
16
+
17
+ nested_resources(included: included.split(','), parent: parent)
18
+ end
19
+
20
+ def modify_params(hsh, ary, parent)
21
+ association = ary.shift
22
+ return unless association
23
+
24
+ new_parent = AssociationsChecker.new(parent, association).call
25
+ return unless new_parent
26
+
27
+ modify_params(hsh[association], ary, new_parent)
28
+ end
29
+
30
+
31
+ def nested_resources(included:, parent:)
32
+ default_hash = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
33
+ result = included.each_with_object(default_hash) do |param, hash|
34
+ array = param.split('.').map(&:to_sym)
35
+ modify_params(hash, array, parent)
36
+ end
37
+ result
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_preloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kiryl Karetnikau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: kiryl.karetnikau@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - lib/json_api_preloader.rb
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Preloads associations based on request param `included`
44
+ test_files: []