scopie 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +52 -11
- data/lib/scopie/base.rb +17 -9
- data/lib/scopie/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a712c48ae4d6541f9de46f7161944622035f5dca
|
4
|
+
data.tar.gz: a3ab1963d878729f75219ee9f8a48891c208abba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13f994ef566f8d582a1e28f1213a25463b0aed03ec3c0131fc97760bed5897768835e7eb1fa5e12f4feb4966f0e799ddbb655f74658e205914f2466191cfc296
|
7
|
+
data.tar.gz: 0e37d4f423664bceb1d44c9f3e2db4906fd4f591040ae8814662289364a08787f369eca26e4d4572e236725fc3c6d9bcb5552cb08ad8f28f76403db442138375
|
data/README.md
CHANGED
@@ -1,36 +1,72 @@
|
|
1
1
|
## Scopie
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/beorc/scopie)
|
4
|
+
|
2
5
|
Minimal mapping of incoming parameters to named scopes in your resources through OO design
|
3
6
|
|
4
7
|
Scopie allows you to map incoming controller parameters to named scopes in your resources.
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
It is similar to [has_scope](http://github.com/plataformatec/has_scope).
|
10
|
+
|
11
|
+
The key differences are:
|
12
|
+
|
13
|
+
* Dedicated class where the scopes are defined, so that your controller will be skinny.
|
14
|
+
* It doesn't depend from ActiveSupport or ActionPack. Please have, a look at [scopie_rails](http://github.com/beorc/scopie_rails) if you are using Ruby on Rails framework.
|
15
|
+
* To override default mapping behavior you don't need to pass a block - just define a method with the same name as scope.
|
16
|
+
* You can DRY your custom scopes mapping logic by using helper methods defined in scopie class and use the same scopie class in multiple controllers.
|
11
17
|
|
12
18
|
Imagine the following model called graduations:
|
13
19
|
|
14
20
|
```ruby
|
15
21
|
class Graduation < ActiveRecord::Base
|
16
|
-
|
17
|
-
scope :
|
18
|
-
scope :
|
22
|
+
|
23
|
+
scope :featured, -> { where(featured: true) }
|
24
|
+
scope :by_degree, -> (degree) { where(degree: degree) }
|
25
|
+
scope :by_period, -> (started_at, ended_at) { where('started_at = ? AND ended_at = ?', started_at, ended_at) }
|
26
|
+
|
27
|
+
scope :created_at_greater_than, ->(date) { where('entities.created_at >= ?', date.beginning_of_day) }
|
28
|
+
scope :created_at_less_than, ->(date) { where('entities.created_at <= ?', date.end_of_day) }
|
29
|
+
scope :updated_at_greater_than, ->(date) { where('entities.updated_at >= ?', date.beginning_of_day) }
|
30
|
+
scope :updated_at_less_than, ->(date) { where('entities.updated_at <= ?', date.end_of_day) }
|
31
|
+
|
19
32
|
end
|
20
33
|
```
|
21
34
|
|
22
35
|
You can use those named scopes as filters by declaring them on your scopie:
|
23
36
|
|
24
37
|
```ruby
|
25
|
-
class
|
38
|
+
class GraduationsScopie < Scopie::Base
|
39
|
+
|
26
40
|
has_scope :featured, type: :boolean
|
27
41
|
has_scope :by_degree, :by_period
|
42
|
+
|
43
|
+
has_scope :created_at_greater_than, in: :created_at, as: :start_at
|
44
|
+
has_scope :created_at_less_than, in: :created_at, as: :end_at
|
45
|
+
|
28
46
|
has_scope :page, default: 1
|
29
47
|
has_scope :per, default: 30
|
30
48
|
|
31
49
|
def by_period(scope, value, _hash)
|
32
|
-
|
50
|
+
started_at = value[:started_at]
|
51
|
+
ended_at = value[:ended_at]
|
52
|
+
|
53
|
+
started_at && ended_at && scope.by_period(started_at, ended_at)
|
33
54
|
end
|
55
|
+
|
56
|
+
def created_at_greater_than(scope, value, _hash)
|
57
|
+
scope.created_at_greater_than(parse_date(value))
|
58
|
+
end
|
59
|
+
|
60
|
+
def created_at_less_than(scope, value, _hash)
|
61
|
+
scope.created_at_less_than(parse_date(value))
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def parse_date(value)
|
67
|
+
Date.parse(value)
|
68
|
+
end
|
69
|
+
|
34
70
|
end
|
35
71
|
```
|
36
72
|
|
@@ -38,9 +74,11 @@ Now, if you want to apply them to an specific resource, you just need to call `a
|
|
38
74
|
|
39
75
|
```ruby
|
40
76
|
class GraduationsController < ApplicationController
|
77
|
+
|
41
78
|
def index
|
42
79
|
@graduations = Scopie.apply_scopes(Graduation, method: :index, scopie: Scopies::GraduationsScopie.new).all
|
43
80
|
end
|
81
|
+
|
44
82
|
end
|
45
83
|
```
|
46
84
|
|
@@ -56,6 +94,9 @@ Then for each request:
|
|
56
94
|
/graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
|
57
95
|
#=> brings graduations in the given period
|
58
96
|
|
97
|
+
/graduations?created_at[start_at]=2016-06-01&created_at[end_at]=2016-06-02
|
98
|
+
#=> brings graduations created in the given period
|
99
|
+
|
59
100
|
/graduations?featured=true&by_degree=phd
|
60
101
|
#=> brings featured graduations with phd degree
|
61
102
|
```
|
@@ -72,7 +113,7 @@ gem 'scopie'
|
|
72
113
|
|
73
114
|
Scopie supports several options:
|
74
115
|
|
75
|
-
* `:type` -
|
116
|
+
* `:type` - Coerces the type of the parameter sent. Available options: boolean, integer, date.
|
76
117
|
|
77
118
|
* `:only` - In which actions the scope is applied.
|
78
119
|
|
data/lib/scopie/base.rb
CHANGED
@@ -30,9 +30,10 @@ class Scopie::Base
|
|
30
30
|
|
31
31
|
def current_scopes(hash, method = nil)
|
32
32
|
scopes = scopes_configuration.map do |scope_name, options|
|
33
|
-
next unless scope_applicable?(scope_name, options, hash, method)
|
34
33
|
value = scope_value(scope_name, options, hash)
|
35
|
-
|
34
|
+
next unless scope_applicable?(value, options, method)
|
35
|
+
|
36
|
+
[scope_name, coerce_value_type(value, options[:type])]
|
36
37
|
end
|
37
38
|
|
38
39
|
scopes.compact!
|
@@ -43,10 +44,10 @@ class Scopie::Base
|
|
43
44
|
|
44
45
|
def apply_scope(scope_name, target, value, hash)
|
45
46
|
result = if respond_to?(scope_name)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
public_send(scope_name, target, value, hash)
|
48
|
+
else
|
49
|
+
target.public_send(scope_name, value)
|
50
|
+
end
|
50
51
|
|
51
52
|
result || target
|
52
53
|
end
|
@@ -61,7 +62,7 @@ class Scopie::Base
|
|
61
62
|
key_name = key_name(scope_name, options)
|
62
63
|
reduced_hash = reduced_hash(hash, options)
|
63
64
|
|
64
|
-
return
|
65
|
+
return reduced_hash[key_name] if reduced_hash.key?(key_name)
|
65
66
|
options[:default]
|
66
67
|
end
|
67
68
|
|
@@ -74,10 +75,17 @@ class Scopie::Base
|
|
74
75
|
TRUE_VALUES.include? value
|
75
76
|
end
|
76
77
|
|
77
|
-
def
|
78
|
+
def coerce_to_integer(value)
|
79
|
+
Integer(value)
|
80
|
+
end
|
81
|
+
|
82
|
+
def coerce_to_date(value)
|
83
|
+
Date.parse(value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def scope_applicable?(value, options, method)
|
78
87
|
return false unless method_applicable?(method, options)
|
79
88
|
|
80
|
-
value = scope_value(scope_name, options, hash)
|
81
89
|
is_value_present = value.respond_to?(:empty?) ? !value.empty? : !!value
|
82
90
|
|
83
91
|
is_value_present || !!options[:allow_blank]
|
data/lib/scopie/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Kotov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|