scopie_rails 0.2.0 → 0.3.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 +74 -1
- data/lib/scopie_rails.rb +4 -4
- data/lib/scopie_rails/controller.rb +1 -0
- data/lib/scopie_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e56563f6e72158575391b7da2f3a82e22737a46
|
4
|
+
data.tar.gz: 21af9647d32257149b14ddd4878720039d461dc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8de2a74842924f0a452664058ee620a676132c75e2dede33bfa10d364a88cf2f173aa3c311dd7f7688f4bc8a3673129cabc4092809d13a1db3369d9f7c12d68
|
7
|
+
data.tar.gz: 0fda698f097b7d25b4e3728e83bfe8ea5536e0f06bc5c7f08615f483be4406035b09fad9b23155fa24f08918e80ad3c9f09ec79b18462a504b54bd46f02e8bd5
|
data/README.md
CHANGED
@@ -1,2 +1,75 @@
|
|
1
|
-
|
1
|
+
## ScopieRails
|
2
2
|
Scopie for Rails
|
3
|
+
|
4
|
+
ScopieRails allows you to map incoming controller parameters to named scopes in your resources.
|
5
|
+
Imagine the following model called graduations:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class Graduation < ActiveRecord::Base
|
9
|
+
scope :featured, -> { where(:featured => true) }
|
10
|
+
scope :by_degree, -> degree { where(:degree => degree) }
|
11
|
+
scope :by_period, -> started_at, ended_at { where("started_at = ? AND ended_at = ?", started_at, ended_at) }
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
You can use those named scopes as filters by declaring them on your scopie:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Scopies::GraduationsScopie < ScopieRails::Base
|
19
|
+
has_scope :featured, type: :boolean
|
20
|
+
has_scope :by_degree
|
21
|
+
has_scope :by_period
|
22
|
+
|
23
|
+
def by_period(scope, value, _hash)
|
24
|
+
scope.by_period(value[:started_at], value[:ended_at])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Now, if you want to apply them to an specific resource, you just need to call `apply_scopes`:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class GraduationsController < ApplicationController
|
33
|
+
include ScopieRails::Controller
|
34
|
+
|
35
|
+
def index
|
36
|
+
@graduations = apply_scopes(Graduation).all
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Then for each request:
|
42
|
+
|
43
|
+
```
|
44
|
+
/graduations
|
45
|
+
#=> acts like a normal request
|
46
|
+
|
47
|
+
/graduations?featured=true
|
48
|
+
#=> calls the named scope and bring featured graduations
|
49
|
+
|
50
|
+
/graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
|
51
|
+
#=> brings graduations in the given period
|
52
|
+
|
53
|
+
/graduations?featured=true&by_degree=phd
|
54
|
+
#=> brings featured graduations with phd degree
|
55
|
+
```
|
56
|
+
|
57
|
+
## Installation
|
58
|
+
|
59
|
+
Add `scopie_rails` to your Gemfile or install it from Rubygems.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
gem 'scopie_rails'
|
63
|
+
```
|
64
|
+
|
65
|
+
## Options
|
66
|
+
|
67
|
+
Scopie supports several options:
|
68
|
+
|
69
|
+
* `:type` - Checks the type of the parameter sent.
|
70
|
+
|
71
|
+
* `:only` - In which actions the scope is applied.
|
72
|
+
|
73
|
+
* `:except` - In which actions the scope is not applied.
|
74
|
+
|
75
|
+
* `:default` - Default value for the scope. Whenever supplied the scope is always called.
|
data/lib/scopie_rails.rb
CHANGED
@@ -8,10 +8,10 @@ require 'active_support/core_ext/string/inflections'
|
|
8
8
|
|
9
9
|
module ScopieRails
|
10
10
|
|
11
|
-
SCOPIE_PREFIX = 'Scopies'
|
12
|
-
SCOPIE_SUFFIX = 'Scopie'
|
13
|
-
CLASS_NAME_DELIMETER = '::'
|
14
|
-
CONTROLLER_DELIMETER = 'Controller'
|
11
|
+
SCOPIE_PREFIX = 'Scopies'
|
12
|
+
SCOPIE_SUFFIX = 'Scopie'
|
13
|
+
CLASS_NAME_DELIMETER = '::'
|
14
|
+
CONTROLLER_DELIMETER = 'Controller'
|
15
15
|
|
16
16
|
require 'scopie_rails/base'
|
17
17
|
require 'scopie_rails/controller'
|
data/lib/scopie_rails/version.rb
CHANGED