activerecord-instance_includes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/activerecord-instance_includes.gemspec +26 -0
- data/lib/activerecord-instance_includes.rb +10 -0
- data/lib/activerecord/instance_includes/active_record.rb +12 -0
- data/lib/activerecord/instance_includes/rails3_extensions.rb +12 -0
- data/lib/activerecord/instance_includes/rails4_extensions.rb +12 -0
- data/lib/activerecord/instance_includes/version.rb +5 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e8522d7e3d7c607a0921784a5002886eab1deaaf3cee31aa28ef8c3d78ad85da
|
4
|
+
data.tar.gz: 5a9cfb7da57a9ba25f089e5177bdf0a3f6b253b3129b06a3922da639bc9c696e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b3cdf4c73e714fa95f59ffc9ed0fe044f0a3bb13b796ccb74bfe63bfb84a3d03fcad170564f8bab2617a3e51bcb7f4a46f33902163044f63051ff3841241858
|
7
|
+
data.tar.gz: 20a315d1bf52298f7e197736de8ef0ebc76f64c27272b7379f6ac87df9d88e6800636a097190f54eef5e6b64bf76cc70b43097516a789e2192058b692c445d15
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
spec/tmp
|
16
|
+
spec/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2020 Aleck Greenham
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
Adds includes method to ActiveRecord instances
|
2
|
+
|
3
|
+
## Why is this needed?
|
4
|
+
|
5
|
+
Sometimes you don't have access to the code that instantiates a particular `ActiveRecord` instance, so you can't call `includes` on the class or `ActiveRecord::Relation` instance to eager load associations.
|
6
|
+
|
7
|
+
For example:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# Defines the @post instance variable for us
|
11
|
+
load_and_authorize_resource
|
12
|
+
|
13
|
+
def show
|
14
|
+
render @post
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
Normally this isn't a problem, because if you have a single instance, it's not possible to encounter a N+1 query while working with that instance or any of its direct associations.
|
19
|
+
|
20
|
+
```haml
|
21
|
+
%h1= @post.title
|
22
|
+
|
23
|
+
%ul
|
24
|
+
- @post.authors.each do |author|
|
25
|
+
%li= author.name
|
26
|
+
```
|
27
|
+
|
28
|
+
The problem occurs with more complex (deeply nested) processing, and you have to crawl over the data of nested has_many associations.
|
29
|
+
|
30
|
+
```haml
|
31
|
+
%ul
|
32
|
+
- @post.authors.each do |author|
|
33
|
+
-# Causes a N+1 query problem:
|
34
|
+
%li= "#{author.name} (#{author.nicknames.pluck(:value).join(',')})
|
35
|
+
```
|
36
|
+
|
37
|
+
That's when you need `activerecord-instance_includes`:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
@post.includes(authors: [:nicknames])
|
41
|
+
```
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
gem 'activerecord-instance_includes'
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
$ bundle
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
$ gem install activerecord-instance_includes
|
56
|
+
|
57
|
+
## Usage
|
58
|
+
|
59
|
+
Just call `includes` on ActiveRecord instances in the same way you would normally do so for the ActiveRecord class or `ActiveRecord::Relation` instance.
|
60
|
+
|
61
|
+
The `#includes` method returns the ActiveRecord instance itself so you can chain method calls:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
%ul
|
65
|
+
- @post.includes(authors: [:nicknames]).authors.each do |author|
|
66
|
+
%li= "#{author.name} (#{author.nicknames.pluck(:value).join(',')})
|
67
|
+
```
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'activerecord/instance_includes/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'activerecord-instance_includes'
|
10
|
+
spec.version = ActiveRecordInstanceIncludes::VERSION
|
11
|
+
spec.authors = ['Aleck Greenham']
|
12
|
+
spec.email = ['greenhama13@gmail.com']
|
13
|
+
spec.summary = 'Adds includes method to ActiveRecord instances.'
|
14
|
+
spec.description = 'Provides an #includes method to eager load associations of ActiveRecord instances, ' \
|
15
|
+
'for when you don\'t have access to the class object or ActiveRecord::Relation instance.'
|
16
|
+
spec.homepage = 'https://github.com/greena13/activerecord-instance_includes'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0")
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 1.8.7'
|
25
|
+
spec.add_dependency 'activerecord', '>= 3.0.0'
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module InstanceIncludes
|
5
|
+
autoload :Rails3Extensions, 'activerecord/instance_includes/rails3_extensions'
|
6
|
+
autoload :Rails4Extensions, 'activerecord/instance_includes/rails4_extensions'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'activerecord/instance_includes/active_record'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/lazy_load_hooks'
|
5
|
+
|
6
|
+
ActiveSupport.on_load(:active_record) do
|
7
|
+
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR >= 1
|
8
|
+
include ActiveRecord::InstanceIncludes::Rails3Extensions
|
9
|
+
elsif ActiveRecord::VERSION::MAJOR >= 4
|
10
|
+
include ActiveRecord::InstanceIncludes::Rails4Extensions
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-instance_includes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleck Greenham
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
description: 'Provides an #includes method to eager load associations of ActiveRecord
|
28
|
+
instances, for when you don''t have access to the class object or ActiveRecord::Relation
|
29
|
+
instance.'
|
30
|
+
email:
|
31
|
+
- greenhama13@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".gitignore"
|
37
|
+
- ".rspec"
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- activerecord-instance_includes.gemspec
|
42
|
+
- lib/activerecord-instance_includes.rb
|
43
|
+
- lib/activerecord/instance_includes/active_record.rb
|
44
|
+
- lib/activerecord/instance_includes/rails3_extensions.rb
|
45
|
+
- lib/activerecord/instance_includes/rails4_extensions.rb
|
46
|
+
- lib/activerecord/instance_includes/version.rb
|
47
|
+
homepage: https://github.com/greena13/activerecord-instance_includes
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 1.8.7
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.0.8
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Adds includes method to ActiveRecord instances.
|
70
|
+
test_files: []
|