Permalink
Browse files

Initial commit

  • Loading branch information...
unixcharles committed Oct 22, 2012
0 parents commit bf860fa21899ddc257433618b36a1fab87946e93
Showing with 92 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +19 −0 README.md
  3. +11 −0 activerecord_lax_includes.gemspec
  4. +44 −0 lib/activerecord_lax_includes.rb
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright (c) 2011 Charles Barbier <http://github.com/unixcharles>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
+ActiveRecord-lax-includes
+=========================
+
+By default ActiveRecord will raise an error when trying to eager load association that doesn't exist.
+
+This can be an issue with polymorphism or STI.
+
+See this issue for more details: https://github.com/rails/rails/issues/8005
+
+Usage
+-----
+
+```
+ActiveRecord::lax_includes do
+ # ... record with missing association are filtered out instead of raising an error
+end
+
+# back to normal `Association named '****' was not found; perhaps you misspelled it?` exception.
+```
@@ -0,0 +1,11 @@
+Gem::Specification.new do |s|
+ s.name = 'activerecord_lax_includes'
+ s.version = '0.1.1'
+ s.summary = 'Hotfix nested eager loading for polymorphic and STI relation in ActiveRecord'
+ s.author = 'Charles Barbier'
+ s.email = 'unixcharles@gmail.com'
+ s.homepage = 'http://github.com/unixcharles/active-record-lax-includes'
+
+ s.files = Dir['README.md', 'LICENSE', 'lib/activerecord_lax_includes.rb']
+ s.require_path = 'lib'
+end
@@ -0,0 +1,44 @@
+module ActiveRecordLaxIncludes
+ module Preloader
+ def self.included(base)
+ base.class_eval do
+ alias_method :records_by_reflection_default, :records_by_reflection
+ alias_method :records_by_reflection, :records_by_reflection_with_lax_include
+ end
+ end
+
+ def records_by_reflection_with_lax_include(association)
+ if lax_includes_enabled?
+ filtered_records_by_reflection(association)
+ else
+ records_by_reflection_default(association)
+ end
+ end
+
+ def filtered_records_by_reflection(association)
+ records.select do |record|
+ record.class.reflections[association].present?
+ end.group_by do |record|
+ record.class.reflections[association]
+ end
+ end
+
+ def lax_includes_enabled?
+ !!Thread.current[:active_record_lax_includes_enabled]
+ end
+ end
+
+ module BaseHelper
+ def lax_includes
+ Thread.current[:active_record_lax_includes_enabled] = true
+ yield
+ ensure
+ Thread.current[:active_record_lax_includes_enabled] = false
+ end
+ end
+end
+
+require 'active_record'
+
+ActiveRecord::Associations::Preloader.send(:include, ActiveRecordLaxIncludes::Preloader)
+ActiveRecord.send(:extend, ActiveRecordLaxIncludes::BaseHelper)

0 comments on commit bf860fa

Please sign in to comment.