activerecord_lax_includes 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +18 -0
  2. data/README.md +19 -0
  3. data/lib/activerecord_lax_includes.rb +44 -0
  4. metadata +47 -0
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,44 @@
1
+ module ActiveRecordLaxIncludes
2
+ module Preloader
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method :records_by_reflection_default, :records_by_reflection
6
+ alias_method :records_by_reflection, :records_by_reflection_with_lax_include
7
+ end
8
+ end
9
+
10
+ def records_by_reflection_with_lax_include(association)
11
+ if lax_includes_enabled?
12
+ filtered_records_by_reflection(association)
13
+ else
14
+ records_by_reflection_default(association)
15
+ end
16
+ end
17
+
18
+ def filtered_records_by_reflection(association)
19
+ records.select do |record|
20
+ record.class.reflections[association].present?
21
+ end.group_by do |record|
22
+ record.class.reflections[association]
23
+ end
24
+ end
25
+
26
+ def lax_includes_enabled?
27
+ !!Thread.current[:active_record_lax_includes_enabled]
28
+ end
29
+ end
30
+
31
+ module BaseHelper
32
+ def lax_includes
33
+ Thread.current[:active_record_lax_includes_enabled] = true
34
+ yield
35
+ ensure
36
+ Thread.current[:active_record_lax_includes_enabled] = false
37
+ end
38
+ end
39
+ end
40
+
41
+ require 'active_record'
42
+
43
+ ActiveRecord::Associations::Preloader.send(:include, ActiveRecordLaxIncludes::Preloader)
44
+ ActiveRecord.send(:extend, ActiveRecordLaxIncludes::BaseHelper)
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_lax_includes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Barbier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: unixcharles@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - LICENSE
22
+ - lib/activerecord_lax_includes.rb
23
+ homepage: http://github.com/unixcharles/active-record-lax-includes
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Hotfix nested eager loading for polymorphic and STI relation in ActiveRecord
47
+ test_files: []