associated_scope 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 90fe7a6c03d83ef569250401d70ff8075b9b0dddf6824c6e88bba5123618c6e8
4
+ data.tar.gz: 02d9c730096399e617ea1f29833ebde7e199f599a158d2a96ff325c2dff18598
5
+ SHA512:
6
+ metadata.gz: de948d684eda4124f2cd07a16d30bf8863f21505affb5303d07baf7f124ac13318f7a713c6ab187925d23714d315305ad38ad2916ec2eccdaf6449b211ef9be5
7
+ data.tar.gz: caf89a01b9269c716317fd4a9c897622ae74b8ad8045ca3a96f32fd729f61a2c94ac6da8b97d4561fbb88b3a0d59e9d1910269badaa78faeeee9d1520b5318ae
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "associated_scope/version"
4
+
5
+ module AssociatedScope
6
+ module RelationExtension
7
+ def load(&block)
8
+ if loaded?
9
+ super
10
+ else
11
+ myblock1 = myblock
12
+
13
+ super(&myblock1)
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.included(base)
19
+ base.extend ParentClassMethods1
20
+ end
21
+
22
+ module ParentClassMethods1
23
+ def extended(base)
24
+ klass = base.respond_to?(:klass) ? base.klass : base.class
25
+ associated_scope_args2 = associated_scope_args
26
+ associated_scope_args.each do |name, args|
27
+ source = args[:source]
28
+ reflection = klass._reflect_on_association(source)
29
+ raise AssociationNotFoundError.new(self, source) unless reflection
30
+
31
+ reflection = reflection.dup
32
+ parent_scope = reflection.scope
33
+ associated_scope_arg = args[:scope]
34
+ myscope = proc do
35
+ instance_exec(&parent_scope).merge!(instance_exec(&associated_scope_arg))
36
+ end
37
+ reflection.define_singleton_method(:name) do
38
+ name
39
+ end
40
+ reflection.define_singleton_method(:scope) do
41
+ myscope
42
+ end
43
+
44
+ associated_scope_args[name].merge! reflection: reflection
45
+ end
46
+
47
+ associated_methods = @associated_methods
48
+ myblock = proc do |record|
49
+ record.define_singleton_method(:association) do |name|
50
+ association = association_instance_get(name)
51
+ return association unless association.nil?
52
+
53
+ args = associated_scope_args2[name]
54
+ return super(name) unless args
55
+
56
+ reflection = args[:reflection]
57
+ association = reflection.association_class.new(self, reflection)
58
+
59
+ association_instance_set(name, association)
60
+ association.association_scope
61
+ association
62
+ end
63
+
64
+ associated_scope_args2.keys.each do |name|
65
+ record.define_singleton_method(name) do
66
+ association(name).reader
67
+ end
68
+ end
69
+
70
+ record.instance_eval(&associated_methods)
71
+ end
72
+
73
+ if base.is_a?(ActiveRecord::Base)
74
+ myblock.call(base)
75
+ else
76
+ base.extend RelationExtension
77
+ base.define_singleton_method(:myblock) { myblock }
78
+ end
79
+ end
80
+
81
+ def associated_scope_args
82
+ @associated_scope_args ||= {}
83
+ end
84
+
85
+ def associated_scope(name, scope, source:)
86
+ associated_scope_args[name] = { scope: scope, source: source }
87
+ end
88
+
89
+ def associated_methods(&block)
90
+ @associated_methods = block
91
+ end
92
+
93
+ def associated_scope1(name, scope, source:)
94
+ reflection = _reflect_on_association(source)
95
+ raise AssociationNotFoundError.new(self, source) unless reflection
96
+
97
+ reflection = reflection.dup
98
+ parent_scope = reflection.scope
99
+ myscope = proc { instance_exec(&parent_scope).merge!(instance_exec(&scope)) }
100
+ reflection.define_singleton_method(:name) do
101
+ name
102
+ end
103
+ reflection.define_singleton_method(:scope) do
104
+ myscope
105
+ end
106
+
107
+ associated_scope_args[name] = { scope: scope, reflection: reflection }
108
+ define_method(name) do
109
+ association(name).reader
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AssociatedScope
4
+ VERSION = "0.0.1"
5
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: associated_scope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Serg Tyatin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: ActiveRecord preloads works only with model associations, this gem allow
42
+ to create dynamic preloadable scope. That helps to solve N+1 query problem.
43
+ email:
44
+ - 700@2rba.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/associated_scope.rb
50
+ - lib/associated_scope/version.rb
51
+ homepage: https://github.com/2rba/associated_scope
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.8
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Preloadable ActiveRecord scope
74
+ test_files: []