scopie_rails 0.7.0 → 0.8.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 +46 -7
- data/lib/scopie_rails/engine.rb +3 -0
- data/lib/scopie_rails/version.rb +2 -1
- data/lib/scopie_rails.rb +1 -3
- metadata +6 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb41290b07a829393e1f6a02793b320afdce972
|
4
|
+
data.tar.gz: 5152212ca1822242d3aed6b7a8fa144e6ddc2502
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80512b1d6eb25594df3a92956eb3d28a2df5923a5e4f9eeb77a84f92ee8eab383704f07cd76c370aa1387a031925b695213da6dc0aa469e4acdde79844411db1
|
7
|
+
data.tar.gz: 2540e408c676d420b38a5a45aabd63835aa7fe579c68fac8cf9352d6cd6b91e0b756ec42151492b6bfc9c04245d87dddba8a62309bf880ce1d026c586bd2860e
|
data/README.md
CHANGED
@@ -1,33 +1,69 @@
|
|
1
1
|
## ScopieRails
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/beorc/scopie_rails)
|
4
|
+
|
2
5
|
[Scopie][s] for Rails
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
It is similar to [has_scope](http://github.com/plataformatec/has_scope).
|
8
|
+
|
9
|
+
The key differences are:
|
10
|
+
|
11
|
+
* Dedicated class where the scopes are defined, so that your controller will be skinny.
|
12
|
+
* To override default mapping behavior you don't need to pass a block - just define a method with the same name as scope.
|
13
|
+
* 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.
|
8
14
|
|
9
15
|
Imagine the following model called graduations:
|
10
16
|
|
11
17
|
```ruby
|
12
18
|
class Graduation < ActiveRecord::Base
|
19
|
+
|
13
20
|
scope :featured, -> { where(featured: true) }
|
14
21
|
scope :by_degree, -> (degree) { where(degree: degree) }
|
15
22
|
scope :by_period, -> (started_at, ended_at) { where('started_at = ? AND ended_at = ?', started_at, ended_at) }
|
23
|
+
|
24
|
+
scope :created_at_greater_than, ->(date) { where('entities.created_at >= ?', date.beginning_of_day) }
|
25
|
+
scope :created_at_less_than, ->(date) { where('entities.created_at <= ?', date.end_of_day) }
|
26
|
+
scope :updated_at_greater_than, ->(date) { where('entities.updated_at >= ?', date.beginning_of_day) }
|
27
|
+
scope :updated_at_less_than, ->(date) { where('entities.updated_at <= ?', date.end_of_day) }
|
28
|
+
|
16
29
|
end
|
17
30
|
```
|
18
31
|
|
19
32
|
You can use those named scopes as filters by declaring them on your scopie:
|
20
33
|
|
21
34
|
```ruby
|
22
|
-
class
|
35
|
+
class GraduationsScopie < ScopieRails::Base
|
36
|
+
|
23
37
|
has_scope :featured, type: :boolean
|
24
38
|
has_scope :by_degree, :by_period
|
39
|
+
|
40
|
+
has_scope :created_at_greater_than, in: :created_at, as: :start_at
|
41
|
+
has_scope :created_at_less_than, in: :created_at, as: :end_at
|
42
|
+
|
25
43
|
has_scope :page, default: 1
|
26
44
|
has_scope :per, default: 30
|
27
45
|
|
28
46
|
def by_period(scope, value, _hash)
|
29
|
-
|
47
|
+
started_at = value[:started_at]
|
48
|
+
ended_at = value[:ended_at]
|
49
|
+
|
50
|
+
started_at && ended_at && scope.by_period(started_at, ended_at)
|
51
|
+
end
|
52
|
+
|
53
|
+
def created_at_greater_than(scope, value, _hash)
|
54
|
+
scope.created_at_greater_than(parse_date(value))
|
55
|
+
end
|
56
|
+
|
57
|
+
def created_at_less_than(scope, value, _hash)
|
58
|
+
scope.created_at_less_than(parse_date(value))
|
30
59
|
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def parse_date(value)
|
64
|
+
Date.parse(value)
|
65
|
+
end
|
66
|
+
|
31
67
|
end
|
32
68
|
```
|
33
69
|
|
@@ -55,6 +91,9 @@ Then for each request:
|
|
55
91
|
/graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
|
56
92
|
#=> brings graduations in the given period
|
57
93
|
|
94
|
+
/graduations?created_at[start_at]=2016-06-01&created_at[end_at]=2016-06-02
|
95
|
+
#=> brings graduations created in the given period
|
96
|
+
|
58
97
|
/graduations?featured=true&by_degree=phd
|
59
98
|
#=> brings featured graduations with phd degree
|
60
99
|
```
|
@@ -71,7 +110,7 @@ gem 'scopie_rails'
|
|
71
110
|
|
72
111
|
Scopie supports several options:
|
73
112
|
|
74
|
-
* `:type` - Coerces the type of the parameter sent.
|
113
|
+
* `:type` - Coerces the type of the parameter sent. Available options: boolean, integer, date.
|
75
114
|
|
76
115
|
* `:only` - In which actions the scope is applied.
|
77
116
|
|
data/lib/scopie_rails/engine.rb
CHANGED
data/lib/scopie_rails/version.rb
CHANGED
data/lib/scopie_rails.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopie_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.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: scopie
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3.2'
|
34
34
|
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
36
|
version: '5.1'
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirements:
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
43
|
+
version: '3.2'
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '5.1'
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '3.2'
|
54
54
|
- - "<"
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: '5.1'
|
@@ -60,24 +60,10 @@ dependencies:
|
|
60
60
|
requirements:
|
61
61
|
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: '
|
63
|
+
version: '3.2'
|
64
64
|
- - "<"
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '5.1'
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
|
-
name: railties
|
69
|
-
requirement: !ruby/object:Gem::Requirement
|
70
|
-
requirements:
|
71
|
-
- - ">="
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '0'
|
74
|
-
type: :runtime
|
75
|
-
prerelease: false
|
76
|
-
version_requirements: !ruby/object:Gem::Requirement
|
77
|
-
requirements:
|
78
|
-
- - ">="
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version: '0'
|
81
67
|
- !ruby/object:Gem::Dependency
|
82
68
|
name: rspec
|
83
69
|
requirement: !ruby/object:Gem::Requirement
|