activerecord_lax_includes_2 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dddb790f06ae995ae5e40aacdad8b9a61f999eb8
4
+ data.tar.gz: d1f2cf79d137b4d7146d5b02261b603c7508650c
5
+ SHA512:
6
+ metadata.gz: 7010c841be8f792ccd3dc7b6c120bde80f21ab40d88da5b09a15e3d4101a6720640a7d8fe3dd8b29308f38de83e28f8314df0afbb06380ace5a264065268e5b0
7
+ data.tar.gz: 758441502e72068c61952b76f8f0657dd1d351fd1ccd88315adf8b0300387a311f148abfb3af8ab503dac2d0f8125fa87a0f0581d6b32e2d7a61b37fcf97b829
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 Charles Barbier <http://github.com/unixcharles>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ ActiveRecord-lax-includes
2
+ =========================
3
+
4
+ By default ActiveRecord will raise an error when trying to eager load association that doesn't exist.
5
+
6
+ This can be an issue with polymorphism or STI.
7
+
8
+ See this issue for more details: https://github.com/rails/rails/issues/8005
9
+
10
+ Usage
11
+ -----
12
+
13
+ ```
14
+ ActiveRecord::lax_includes do
15
+ # ... record with missing association are filtered out instead of raising an error
16
+ end
17
+
18
+ # back to normal `Association named '****' was not found; perhaps you misspelled it?` exception.
19
+ ```
@@ -0,0 +1,103 @@
1
+ module ActiveRecordLaxIncludes
2
+ def lax_includes
3
+ Thread.current[:active_record_lax_includes_enabled] = true
4
+ yield
5
+ ensure
6
+ Thread.current[:active_record_lax_includes_enabled] = false
7
+ end
8
+
9
+ def lax_includes_enabled?
10
+ result = Thread.current[:active_record_lax_includes_enabled]
11
+ if result.nil?
12
+ result = Rails.configuration.respond_to?(:active_record_lax_includes_enabled) &&
13
+ Rails.configuration.active_record_lax_includes_enabled
14
+ end
15
+ result
16
+ end
17
+
18
+ module Base
19
+ def association(name)
20
+ association = association_instance_get(name)
21
+
22
+ if association.nil?
23
+ if reflection = self.class._reflect_on_association(name)
24
+ association = reflection.association_class.new(self, reflection)
25
+ association_instance_set(name, association)
26
+ elsif !ActiveRecord.lax_includes_enabled?
27
+ raise ActiveRecord::AssociationNotFoundError.new(self, name)
28
+ end
29
+ end
30
+
31
+ association
32
+ end
33
+ end
34
+
35
+ module Preloader
36
+ private
37
+
38
+ def preloaders_on(association, records, scope, options = {})
39
+ case association
40
+ when Hash
41
+ preloaders_for_hash(association, records, scope, options)
42
+ when Symbol
43
+ preloaders_for_one(association, records, scope, options)
44
+ when String
45
+ preloaders_for_one(association.to_sym, records, scope, options)
46
+ else
47
+ raise ArgumentError, "#{association.inspect} was not recognised for preload"
48
+ end
49
+ end
50
+
51
+ def preloaders_for_hash(association, records, scope, options = {})
52
+ association.flat_map { |parent, child|
53
+ loaders = preloaders_for_one parent, records, scope, options
54
+ polymorphic = options[:polymorphic] || loaders.any? do |l|
55
+ l.respond_to?(:reflection) && l.reflection.polymorphic?
56
+ end
57
+
58
+ recs = loaders.flat_map(&:preloaded_records).uniq
59
+ loaders.concat Array.wrap(child).flat_map { |assoc|
60
+ preloaders_on assoc, recs, scope, polymorphic: polymorphic
61
+ }
62
+ loaders
63
+ }
64
+ end
65
+
66
+ def preloaders_for_one(association, records, scope, options = {})
67
+ grouped = grouped_records(association, records)
68
+ puts "association: #{association.inspect}"
69
+ puts "records: #{records.inspect}"
70
+ puts "scope: #{scope.inspect}"
71
+ puts "grouped: #{grouped.inspect}"
72
+ puts "options: #{options.inspect}"
73
+ if !ActiveRecord.lax_includes_enabled? && records.any? && grouped.none? && !options[:polymorphic]
74
+ raise ActiveRecord::AssociationNotFoundError.new(records.first.class, association)
75
+ end
76
+
77
+ grouped.flat_map do |reflection, klasses|
78
+ klasses.map do |rhs_klass, rs|
79
+ loader = preloader_for(reflection, rs, rhs_klass).new(rhs_klass, rs, reflection, scope)
80
+ loader.run self
81
+ loader
82
+ end
83
+ end
84
+ end
85
+
86
+ def grouped_records(association, records)
87
+ h = {}
88
+ records.each do |record|
89
+ if record && assoc = record.association(association)
90
+ klasses = h[assoc.reflection] ||= {}
91
+ (klasses[assoc.klass] ||= []) << record
92
+ end
93
+ end
94
+ h
95
+ end
96
+ end
97
+ end
98
+
99
+ require 'active_record'
100
+
101
+ ActiveRecord.send(:extend, ActiveRecordLaxIncludes)
102
+ ActiveRecord::Base.send(:prepend, ActiveRecordLaxIncludes::Base)
103
+ ActiveRecord::Associations::Preloader.send(:prepend, ActiveRecordLaxIncludes::Preloader)
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_lax_includes_2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.9
5
+ platform: ruby
6
+ authors:
7
+ - Charles Barbier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: unixcharles@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/activerecord_lax_includes.rb
22
+ homepage: http://github.com/unixcharles/active-record-lax-includes
23
+ licenses: []
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
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hotfix nested eager loading for polymorphic and STI relation in ActiveRecord
45
+ test_files: []