Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unixcharles committed Oct 22, 2012
0 parents commit bf860fa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 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.
19 changes: 19 additions & 0 deletions README.md
@@ -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.
```
11 changes: 11 additions & 0 deletions activerecord_lax_includes.gemspec
@@ -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
44 changes: 44 additions & 0 deletions lib/activerecord_lax_includes.rb
@@ -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.