scopable 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/scopable.rb +12 -12
- data/lib/scopable/version.rb +1 -1
- data/spec/scopable_spec.rb +23 -12
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8489a22edabcb084113a419241d84225a8f054e4
|
4
|
+
data.tar.gz: 4ec1c76ec10aaa7fbd19f5394700d354489bb8e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12ccb068f7e55bafd9d517f8ae620e5dc6121c61950eb704699231510fc5abd9c88ac014bb3ce071f7557c89edad07f08efdd0c9a4bda5c3b8bf518aea36a12c
|
7
|
+
data.tar.gz: 82ca0836921ed85397ba15108e3e5bb0844926902b9f9e458cc066cef57e2b4836fe402e9bde69332311e072a3bfacd4acb68130b666303d6a23a0df1149c487
|
data/lib/scopable.rb
CHANGED
@@ -7,48 +7,48 @@ module Scopable
|
|
7
7
|
|
8
8
|
def scoped(model, params)
|
9
9
|
scopes.reduce(model) do |relation, scope|
|
10
|
-
name,
|
10
|
+
name, options = *scope
|
11
11
|
|
12
12
|
# Controller actions where this scope should be applied.
|
13
13
|
# Accepts either a literal value or a lambda. nil with disable the option.
|
14
|
-
only =
|
14
|
+
only = options[:only]
|
15
15
|
only = instance_exec(&only) if only.respond_to?(:call)
|
16
16
|
|
17
17
|
# Enfore :only option.
|
18
|
-
|
18
|
+
next relation unless only.nil? || Array.wrap(only).map(&:to_s).include?(action_name)
|
19
19
|
|
20
20
|
# Controller actions where this scope should be ignored.
|
21
21
|
# Accepts either a literal value or a lambda. nil with disable the option.
|
22
|
-
except =
|
22
|
+
except = options[:except]
|
23
23
|
except = instance_exec(&except) if except.respond_to?(:call)
|
24
24
|
|
25
25
|
# Enfore :except option.
|
26
|
-
|
26
|
+
next relation if except.present? && Array.wrap(except).map(&:to_s).include?(action_name)
|
27
27
|
|
28
28
|
# Name of the request parameters which value will be used in this scope.
|
29
29
|
# Defaults to the name of the scope.
|
30
|
-
param =
|
30
|
+
param = options.fetch(:param, name)
|
31
31
|
|
32
32
|
# Use the value from the request parameter or fall back to the default.
|
33
33
|
value = params[param]
|
34
34
|
|
35
35
|
# If parameter is not present use the :default option.
|
36
36
|
# Accepts either a literal value or a lambda.
|
37
|
-
value =
|
37
|
+
value = options[:default] if value.nil?
|
38
38
|
|
39
39
|
# Forces the scope to use the given value given in the :force option.
|
40
40
|
# Accepts either a literal value or a lambda.
|
41
|
-
value =
|
41
|
+
value = options[:force] if options.key?(:force)
|
42
42
|
|
43
43
|
# If either :default or :force options were procs, evaluate them.
|
44
44
|
value = instance_exec(&value) if value.respond_to?(:call)
|
45
45
|
|
46
46
|
# The :required option makes sure there's a value present, otherwise return an empty scope (Model#none).
|
47
|
-
required =
|
47
|
+
required = options[:required]
|
48
48
|
required = instance_exec(&required) if required.respond_to?(:call)
|
49
49
|
|
50
50
|
# Enforce the :required option.
|
51
|
-
|
51
|
+
break relation.none if required && value.nil?
|
52
52
|
|
53
53
|
# Parses values like 'on/off', 'true/false', and 'yes/no' to an actual boolean value.
|
54
54
|
case value.to_s
|
@@ -61,10 +61,10 @@ module Scopable
|
|
61
61
|
# For advanced scopes that require more than a method call on the model.
|
62
62
|
# When a block is given, it is ran no matter the scope value.
|
63
63
|
# The proc will be given the model being scoped and the resulting value from the options above, and it'll be executed inside the context of the controller's action.
|
64
|
-
block =
|
64
|
+
block = options[:block]
|
65
65
|
|
66
66
|
if block.nil? && value.nil?
|
67
|
-
|
67
|
+
next relation
|
68
68
|
end
|
69
69
|
|
70
70
|
case
|
data/lib/scopable/version.rb
CHANGED
data/spec/scopable_spec.rb
CHANGED
@@ -51,43 +51,54 @@ describe Scopable do
|
|
51
51
|
#
|
52
52
|
# Test two optional scopes, with 0, 1, and 2 matching parameters.
|
53
53
|
#
|
54
|
-
describe 'with two optional
|
54
|
+
describe 'with two optional scopes' do
|
55
55
|
let :controller do
|
56
56
|
Class.new(Controller) do
|
57
|
-
scope :
|
58
|
-
scope :
|
57
|
+
scope :color
|
58
|
+
scope :size
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
context '
|
62
|
+
context 'without parameters' do
|
63
63
|
subject :action do
|
64
64
|
controller.new
|
65
65
|
end
|
66
66
|
|
67
|
-
it 'should skip the
|
67
|
+
it 'should skip the scopes' do
|
68
68
|
expect(action.relation.scopes).to be_empty
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
context 'with
|
72
|
+
context 'with parameter matching the first scope' do
|
73
73
|
subject :action do
|
74
|
-
controller.new(nil,
|
74
|
+
controller.new(nil, color: 'black')
|
75
75
|
end
|
76
76
|
|
77
|
-
it 'should apply
|
77
|
+
it 'should apply only the first scope' do
|
78
78
|
expect(action.relation.scopes.size).to eq(1)
|
79
|
-
expect(action.relation.scopes).to include(
|
79
|
+
expect(action.relation.scopes).to include(color: 'black')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'with parameter matching the second scope' do
|
84
|
+
subject :action do
|
85
|
+
controller.new(nil, size: 'm')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should apply only the second scope' do
|
89
|
+
expect(action.relation.scopes.size).to eq(1)
|
90
|
+
expect(action.relation.scopes).to include(size: 'm')
|
80
91
|
end
|
81
92
|
end
|
82
93
|
|
83
|
-
context 'with
|
94
|
+
context 'with parameters matching both scopes' do
|
84
95
|
subject :action do
|
85
|
-
controller.new(nil,
|
96
|
+
controller.new(nil, size: 'm', color: 'black')
|
86
97
|
end
|
87
98
|
|
88
99
|
it 'should apply both scopes' do
|
89
100
|
expect(action.relation.scopes.size).to eq(2)
|
90
|
-
expect(action.relation.scopes).to include(
|
101
|
+
expect(action.relation.scopes).to include(size: 'm', color: 'black')
|
91
102
|
end
|
92
103
|
end
|
93
104
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Corenzan
|
@@ -14,56 +14,56 @@ dependencies:
|
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.7'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.7'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description:
|
@@ -73,11 +73,11 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- .codeclimate.yml
|
77
|
-
- .gitignore
|
78
|
-
- .rspec
|
79
|
-
- .rubocop.yml
|
80
|
-
- .travis.yml
|
76
|
+
- ".codeclimate.yml"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- ".travis.yml"
|
81
81
|
- Gemfile
|
82
82
|
- LICENSE.txt
|
83
83
|
- README.md
|
@@ -99,17 +99,17 @@ require_paths:
|
|
99
99
|
- lib
|
100
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
106
|
requirements:
|
107
|
-
- -
|
107
|
+
- - ">="
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
111
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.6.8
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
115
|
summary: Apply or skip model scopes based on options and request parameters.
|