scopie 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +73 -2
- data/lib/scopie/base.rb +11 -1
- data/lib/scopie/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81e654437e834ed3e40a66765512129901f09d97
|
4
|
+
data.tar.gz: ecf9f776853e9bc5f7f4a2eb9c43382a5c6be232
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9507e247d4134b177dbe1d469cbb4108919c78f55e1bf67f91a8093234ef9819c67bf0b19d143739422b4025be0f1180652093549c1fa931adfdb0dcaae8e78
|
7
|
+
data.tar.gz: 2927c25d5d98354ae2e9523fdbf6d09e206204ae2dd8cffb4fec759f94ddb582a9c50ff8f75a48a104b8a148150cfe9ce199982e70286e38e65b31be285225f2
|
data/README.md
CHANGED
@@ -1,2 +1,73 @@
|
|
1
|
-
|
2
|
-
Minimal mapping of incoming parameters to named scopes in your resources through OO design
|
1
|
+
## Scopie
|
2
|
+
Minimal mapping of incoming parameters to named scopes in your resources through OO design
|
3
|
+
|
4
|
+
Scopie 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 < Scopie::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
|
+
def index
|
34
|
+
@graduations = Scopie.apply_scopes(Graduation, method: :index, scopie: Scopies::GraduationsScopie.new).all
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Then for each request:
|
40
|
+
|
41
|
+
```
|
42
|
+
/graduations
|
43
|
+
#=> acts like a normal request
|
44
|
+
|
45
|
+
/graduations?featured=true
|
46
|
+
#=> calls the named scope and bring featured graduations
|
47
|
+
|
48
|
+
/graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
|
49
|
+
#=> brings graduations in the given period
|
50
|
+
|
51
|
+
/graduations?featured=true&by_degree=phd
|
52
|
+
#=> brings featured graduations with phd degree
|
53
|
+
```
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
|
57
|
+
Add `scopie` to your Gemfile or install it from Rubygems.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'scopie
|
61
|
+
```
|
62
|
+
|
63
|
+
## Options
|
64
|
+
|
65
|
+
Scopie supports several options:
|
66
|
+
|
67
|
+
* `:type` - Checks the type of the parameter sent.
|
68
|
+
|
69
|
+
* `:only` - In which actions the scope is applied.
|
70
|
+
|
71
|
+
* `:except` - In which actions the scope is not applied.
|
72
|
+
|
73
|
+
* `:default` - Default value for the scope. Whenever supplied the scope is always called.
|
data/lib/scopie/base.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Scopie::Base
|
4
|
+
TRUE_VALUES = ['true', true, '1', 1]
|
4
5
|
|
5
6
|
def self.scopes_configuration
|
6
7
|
instance_variable_get(:@scopes_configuration) || {}
|
@@ -31,10 +32,19 @@ class Scopie::Base
|
|
31
32
|
private
|
32
33
|
|
33
34
|
def scope_value(scope_name, options, hash)
|
34
|
-
return hash[scope_name] if hash.key?(scope_name)
|
35
|
+
return coerce_value_type(hash[scope_name], options[:type]) if hash.key?(scope_name)
|
35
36
|
options[:default]
|
36
37
|
end
|
37
38
|
|
39
|
+
def coerce_value_type(value, type)
|
40
|
+
return value unless type
|
41
|
+
send("coerce_to_#{type}", value)
|
42
|
+
end
|
43
|
+
|
44
|
+
def coerce_to_boolean(value)
|
45
|
+
TRUE_VALUES.include? value
|
46
|
+
end
|
47
|
+
|
38
48
|
def scope_applicable?(scope_name, options, hash, method)
|
39
49
|
methods_white_list = Array(options[:only])
|
40
50
|
methods_black_list = Array(options[:except])
|
data/lib/scopie/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Kotov
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Minimal mapping of incoming parameters to named scopes in your resources
|
70
84
|
through OO design and pure Ruby classes
|
71
85
|
email:
|