active_record_custom_preloader 0.4.2 → 0.5.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
  SHA1:
3
- metadata.gz: a3ae2d7473982e7d6c8a0acb437c9035639b612a
4
- data.tar.gz: 569cdbd12ef58432b9ec7943483a1ecc0e674b53
3
+ metadata.gz: f437b6601b911a6dc53a3e9a87ef86a44c031bf7
4
+ data.tar.gz: 01733c6190ca58c13e606371eaf445178727a87c
5
5
  SHA512:
6
- metadata.gz: 739c08fcfd37f34beade62def759197b85ca34f64751aa0ee860b5b843689353cfb7ebc9e134b97d9807db78d410274e537275483a65396eeae6988df96ec1c8
7
- data.tar.gz: b30be1a80b7fc0c691fca81178754dcad5674abd3dd1b05f54e9a0f118179b445196023892c00c69bc8de61bc334a3217088e559b8f966a36bcdda0654cea361
6
+ metadata.gz: f129529340548f1f9dec261bb9f8fced68425adb513c936b5ccbe85e789db6b860f092099e83a48d4584de05ddb42e95349f713f8ac67faf8a86f2923ec3de0c
7
+ data.tar.gz: 39b22ccbcb98ba60478cdc9a5ee7b2f034948b4a5bb4b4a1202df2d239587e067bee18493c8ee9b9fd1cc39c3db817f8b8ec650c884b1ff908d2e5db16004277
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordCustomPreloader
2
- VERSION = '0.4.2'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/concern'
3
+
4
+ # Usage example:
5
+ #
6
+ # class Employee < ApplicationRecord
7
+ # # columns: id, name, department_ids
8
+ # add_custom_loader :_departments, class_name: 'EmployeeDepartmentPreloader'
9
+ # end
10
+ #
11
+ # class Department < ApplicationRecord
12
+ # # columns: id, name
13
+ # end
14
+ #
15
+ # class EmployeeDepartmentPreloader < ActiveRecordCustomPreloader::Preloader
16
+ # include ActiveRecordCustomPreloader::WithArrayForeignKeysLoading
17
+ # self.model_class_name = 'Department'
18
+ # self.association_foreign_keys_names = :department_ids
19
+ # self.keep_sorting = true
20
+ # end
21
+ #
22
+ module ActiveRecordCustomPreloader
23
+ module WithArrayForeignKeysLoading
24
+ extend ActiveSupport::Concern
25
+
26
+ included do
27
+ # set to true if you want associated records to be sorted in same way as ids.
28
+ # [optional] (default false)
29
+ class_attribute :keep_sorting, instance_writer: false
30
+ self.keep_sorting = false
31
+
32
+ # model class of association records
33
+ # [required]
34
+ class_attribute :model_class_name, instance_writer: false
35
+
36
+ # Name of foreign keys array which link association record to parent record.
37
+ # Should returns symbol name of a column or parent record instance method.
38
+ # [required]
39
+ class_attribute :association_foreign_keys_name, instance_writer: false
40
+
41
+ private :fetch_association, :associations_by_parent_record
42
+ end
43
+
44
+ # returns associations for provided parent_record.
45
+ # array for has_many and model or nil for has_one.
46
+ def associations_by_parent_record(parent_record, association_records)
47
+ ids = parent_record.public_send(association_foreign_keys_name)
48
+ result = association_records.select { |r| ids.include?(r.id) }
49
+ result.sort_by! { |r| ids.index r.id } if keep_sorting
50
+ result
51
+ end
52
+
53
+ def fetch_association(parent_records)
54
+ ids = parent_records.map(&association_foreign_keys_name).flatten.uniq
55
+ associations_scope.where(id: ids).to_a
56
+ end
57
+
58
+ # default scope for association records.
59
+ # you can override it for example to preload some values to association records.
60
+ def associations_scope
61
+ model_class_name.constantize.all
62
+ end
63
+
64
+ def preload(parent_records)
65
+ association_records = fetch_association(parent_records)
66
+ parent_records.each do |parent_record|
67
+ value = associations_by_parent_record(parent_record, association_records)
68
+ parent_record._set_custom_preloaded_value(name, value)
69
+ end
70
+ end
71
+
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_custom_preloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-15 00:00:00.000000000 Z
11
+ date: 2019-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -119,6 +119,7 @@ files:
119
119
  - lib/active_record_custom_preloader/railtie.rb
120
120
  - lib/active_record_custom_preloader/relation_patch.rb
121
121
  - lib/active_record_custom_preloader/version.rb
122
+ - lib/active_record_custom_preloader/with_array_foreign_keys_loading.rb
122
123
  - lib/active_record_custom_preloader/with_context_dependent_loading.rb
123
124
  - lib/active_record_custom_preloader/with_multiple_foreign_keys_loading.rb
124
125
  homepage: https://github.com/senid231/active_record_custom_preloader