cancan-eager_load 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/cancan-eager_loading.gemspec +28 -0
- data/lib/cancan-eager_load.rb +9 -0
- data/lib/cancan/eager_load/rule_extensions.rb +37 -0
- data/lib/cancan/eager_load/version.rb +7 -0
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 322e66e24a6cb570205bb168d8678822c171677c3707555cbd7804b88c4a06f4
|
4
|
+
data.tar.gz: 505b51f3e13703169b9c69f106a4cab4e1b9eb9fb07a933ed12b5a5fc0460510
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16b01225d9c173864492e7ac92b177b67ab9b996ff8432fdc5fa05664c49f3c2fdaca73f0eeab1e5e3354eb9f7f9ec2ff0c5dec1a948883ba59b3c4506df059d
|
7
|
+
data.tar.gz: 7fc9343396b863f0df453e989ca2788f71310339574444ebdbf9398ec88bd2ec25b8a3601938d6c498e6441d7eda43f2633bc2cb465e3e33f838cd5e064cff47
|
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,46 @@
|
|
1
|
+
Eager load associations to check hash conditions.
|
2
|
+
|
3
|
+
## Why is this needed?
|
4
|
+
|
5
|
+
When checking a user has the ability to perform an action on an instance using a deeply nested hash, it's possible to encounter expensive N+1 queries, which slow your application down.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# ability.rb
|
9
|
+
def initialize(user)
|
10
|
+
return unless user.present?
|
11
|
+
|
12
|
+
# User can see Posts before they're published, of the authors they're mentoring
|
13
|
+
can :show, Post, status: :preview, authors: { mentors: { id: user.id } }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Controllers
|
17
|
+
def show
|
18
|
+
@post = Post.find(params[:id])
|
19
|
+
|
20
|
+
# Causes an N+1 problem - a separate SQL query is made to find the mentors of each author
|
21
|
+
authorize! :show, @post
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
It's possible to audit your code and manually eager load the associtions needed for each permission check, but that's slow, error prone and likely to get out of sync if you redefine your abilities.
|
26
|
+
|
27
|
+
Or you could use `cancan-eager_load` to automatically do it for you.
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
gem 'cancan-eager_load'
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install cancan-eager_load
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
Just use CanCanCan's helper methods as normal: `authorize!`, `can?` and `cannot?`
|
46
|
+
|
@@ -0,0 +1,28 @@
|
|
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 'cancan/eager_load/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'cancan-eager_load'
|
10
|
+
spec.version = CanCan::EagerLoad::VERSION
|
11
|
+
spec.authors = ['Aleck Greenham']
|
12
|
+
spec.email = ['greenhama13@gmail.com']
|
13
|
+
spec.summary = 'Eager load associations to check hash conditions.'
|
14
|
+
spec.description = 'Complex CanCan rule hash conditions can generate a lot of queries when passed an ' \
|
15
|
+
'ActiveRecord instance as a subject. cancan-eager_load parses the hash and adapts it ' \
|
16
|
+
'to eager load the data needed in a single query.'
|
17
|
+
spec.homepage = 'https://github.com/greena13/cancan-eager_load'
|
18
|
+
spec.license = 'MIT'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0")
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 1.8.7'
|
26
|
+
spec.add_dependency 'activerecord-instance_includes', '>= 0.0.1'
|
27
|
+
spec.add_dependency 'cancancan', '>= 2.1.4'
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cancancan'
|
4
|
+
|
5
|
+
module CanCan
|
6
|
+
module RuleExtensions
|
7
|
+
def matches_non_block_conditions(subject)
|
8
|
+
if @conditions.is_a?(Hash)
|
9
|
+
return nested_subject_matches_conditions?(subject) if subject.class == Hash
|
10
|
+
|
11
|
+
unless subject_class?(subject)
|
12
|
+
subject.includes(conditions_to_includes(@conditions))
|
13
|
+
|
14
|
+
return matches_conditions_hash?(subject)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Don't stop at "cannot" definitions when there are conditions.
|
19
|
+
conditions_empty? ? true : @base_behavior
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def conditions_to_includes(target)
|
25
|
+
return unless target.is_a?(Hash)
|
26
|
+
|
27
|
+
target.each_with_object({}) do |key_and_value, memo|
|
28
|
+
key = key_and_value[0]
|
29
|
+
value = key_and_value[1]
|
30
|
+
|
31
|
+
memo[key] = conditions_to_includes(value) || {} if value.is_a?(Hash)
|
32
|
+
|
33
|
+
memo
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cancan-eager_load
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleck Greenham
|
@@ -46,7 +46,16 @@ email:
|
|
46
46
|
executables: []
|
47
47
|
extensions: []
|
48
48
|
extra_rdoc_files: []
|
49
|
-
files:
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rspec"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- cancan-eager_loading.gemspec
|
56
|
+
- lib/cancan-eager_load.rb
|
57
|
+
- lib/cancan/eager_load/rule_extensions.rb
|
58
|
+
- lib/cancan/eager_load/version.rb
|
50
59
|
homepage: https://github.com/greena13/cancan-eager_load
|
51
60
|
licenses:
|
52
61
|
- MIT
|