jsonapi-preloadable 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jsonapi-preloadable.rb +63 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1154f9d3a7b5d25aa0e414997f45c0905192163c6e9c8e00f44e381dcd95dbbd
4
+ data.tar.gz: 42523dbf58be58588b243c03496a00872940d8ecab1b807cb76c27b95ff431be
5
+ SHA512:
6
+ metadata.gz: da1801edd0e20ffe81d4b531ce5c0b38ff8ba9328a580e88f3c4c007405c94a2512bee5f412ecf021fdc415aac8bb500325ce330e2a1424e1cd647f0f47d3056
7
+ data.tar.gz: 705061743f776632c191218f05f07d36d4cfe3c4eb2bc68d0d8e31bbb0a0d0b94378621567987283452152964cae4adab8b52d8e377234bc5011a197cbd9399b
@@ -0,0 +1,63 @@
1
+ module Preloadable
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ class PreloadableAttributeConfig
7
+ attr_writer :attrs, :defaults
8
+
9
+ def attrs
10
+ @attrs ||= {}
11
+ end
12
+
13
+ def defaults
14
+ @defaults ||= Set.new
15
+ end
16
+
17
+ def add(*args)
18
+ if args.size == 1
19
+ # plain declaration
20
+ # config.add relationship: :nested
21
+ options = args.first
22
+ attrs[options.keys.first] = options
23
+ else
24
+ # aliased declaration
25
+ # config.add :alias, relationship: :nested
26
+ key, value = args
27
+ attrs[key] = value
28
+ end
29
+ end
30
+
31
+ def preload(*keys)
32
+ keys = [keys] if keys.instance_of?(Symbol)
33
+ defaults.merge(keys)
34
+ end
35
+ end
36
+
37
+ module ClassMethods
38
+ def preloadable_config
39
+ @preloadable_config ||= PreloadableAttributeConfig.new
40
+ end
41
+
42
+ def preloadable(&block)
43
+ block.call(preloadable_config)
44
+ end
45
+
46
+ def records(options = {})
47
+ attrs = preloadable_config.defaults.dup
48
+
49
+ context_args = options.dig(:context, :preload)
50
+ if context_args.present?
51
+ attrs.merge(context_args.split(",").map(&:to_sym))
52
+ end
53
+
54
+ return super if attrs.empty?
55
+
56
+ includable = preloadable_config.attrs.map do |key, value|
57
+ value if attrs.include?(key)
58
+ end.compact
59
+
60
+ super.includes(includable)
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi-preloadable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Arturo Puente
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Handles ActiveRecord chained relationship preloading in JSONAPI:Resources
14
+ email: arturopuentevc@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/jsonapi-preloadable.rb
20
+ homepage: https://github.com/arturopuente/jsonapi-preloadable
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Handles ActiveRecord chained relationship preloading in JSONAPI:Resources
43
+ test_files: []