siphon 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -31
- data/lib/siphon/adapter.rb +6 -6
- data/lib/siphon/version.rb +1 -1
- data/spec/class/formobj.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: cade992dddbcf4aaed6af0409efc20205c03e48e
|
4
|
+
data.tar.gz: 3ac950dbd0d3ac66a9681193733cc9f363c6da20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6d8a170387014c505b1c1f31dd42d7dc5e4b49455f9d8851c80da5a911e74ac8c0a2aeb4b294dff56c2d7d989fd1efdba8f9b1cc045d3fe00c0ce307776bdba
|
7
|
+
data.tar.gz: ce48cab6fc0e4174fe8f20479daa7f23b118c2574281f3e0881799cbf98b3485f02c6859d98221fdb9f82ded4d6b3e6ba0cd0d19c88303a654dd9c7b03bafb16
|
data/README.md
CHANGED
@@ -17,43 +17,52 @@ Here's how it works :
|
|
17
17
|
|
18
18
|
### The Scopes :
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
```ruby
|
21
|
+
# order.rb
|
22
|
+
class Order < ActiveRecord::Base
|
23
|
+
scope :stale, ->(duration) { where(["state='onhold' OR (state != 'done' AND updated_at < ?)", duration.ago]) }
|
24
|
+
scope :unpaid -> { where(paid: false) }
|
25
|
+
end
|
26
|
+
```
|
25
27
|
|
26
28
|
### The Form :
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
```ruby
|
31
|
+
= form_for @order_form do |f|
|
32
|
+
= f.label :stale, "Stale since more than"
|
33
|
+
= f.select :stale, [["1 week", 1.week], ["3 weeks", 3.weeks], ["3 months", 3.months]], include_blank: true
|
34
|
+
= f.label :unpaid
|
35
|
+
= f.check_box :unpaid
|
36
|
+
```
|
37
|
+
|
33
38
|
|
34
39
|
### The Form Object:
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
```ruby
|
42
|
+
# order_form.rb
|
43
|
+
class OrderForm
|
44
|
+
include Virtus.model
|
45
|
+
include ActiveModel::Model
|
46
|
+
#
|
47
|
+
# attribute are the named scopes and their value are :
|
48
|
+
# - either the value you pass a scope which takes arguments
|
49
|
+
# - either a Siphon::Nil value to apply (or not) a scope which has no argument
|
50
|
+
#
|
51
|
+
attribute :stale, Integer
|
52
|
+
attribute :unpaid, Siphon::Nil
|
53
|
+
end
|
54
|
+
```
|
48
55
|
|
49
56
|
|
50
57
|
### Aaaaand... TADA siphon :
|
51
58
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
59
|
+
```ruby
|
60
|
+
# orders_controller.rb
|
61
|
+
def search
|
62
|
+
@order_form = OrderForm.new(params[:order_form])
|
63
|
+
@orders = siphon(Order.all).scope(@order_form)
|
64
|
+
end
|
65
|
+
```
|
57
66
|
|
58
67
|
Here's how it works : it takes an initial ActiveRelation (Order.all) and then from a FormObject (@order_form) it will apply or not a set of scopes with typecasted arguments.
|
59
68
|
|
@@ -76,9 +85,11 @@ So the values have two distinctive roles .
|
|
76
85
|
|
77
86
|
In the case of `stale` which takes a duration :
|
78
87
|
|
79
|
-
|
80
|
-
|
81
|
-
|
88
|
+
```ruby
|
89
|
+
= form_for @order_form do |f|
|
90
|
+
= f.label :stale, "Stale since more than"
|
91
|
+
= f.select :stale, [["1 week", 1], ["3 weeks", 3], include_blank: true
|
92
|
+
```
|
82
93
|
|
83
94
|
If you select a duration the scope will be called and the argument will be turned into an integer and passed as an arg.
|
84
95
|
But if you select the blank option the value of params[:stale] will be an empty string. Siphon knows it should be an integer thanks to the formobject and concludes this means no values are passed to the scope and therefore shouldn't be called.
|
@@ -96,4 +107,4 @@ That's all!
|
|
96
107
|
5. Create new Pull Request
|
97
108
|
|
98
109
|
[1]: https://github.com/solnic/virtus
|
99
|
-
[2]:
|
110
|
+
[2]: https://coderwall.com/p/4zz6ca
|
data/lib/siphon/adapter.rb
CHANGED
@@ -3,8 +3,8 @@ module Siphon
|
|
3
3
|
# Adapter
|
4
4
|
# ==========
|
5
5
|
#
|
6
|
-
# use : determine which scope will be called &
|
7
|
-
# set
|
6
|
+
# use : determine which scope will not be called &
|
7
|
+
# set args to nil for 'argless' scopes
|
8
8
|
#
|
9
9
|
#
|
10
10
|
class Adapter
|
@@ -12,7 +12,7 @@ module Siphon
|
|
12
12
|
def initialize(formobj)
|
13
13
|
@formobj = formobj
|
14
14
|
|
15
|
-
@scopes_hash = @formobj.
|
15
|
+
@scopes_hash = @formobj.attributes
|
16
16
|
@argless = argless_list(formobj)
|
17
17
|
end
|
18
18
|
|
@@ -22,9 +22,9 @@ module Siphon
|
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
25
|
-
# if scope is present in form but with no value (aka
|
26
|
-
# or if present in formobj but not in form (aka :
|
27
|
-
# don't apply the scope && reject from scopes_hash
|
25
|
+
# if scope is present in form but with no value (aka: an empty string)
|
26
|
+
# or if present in formobj but not in form (aka : a nil value)
|
27
|
+
# don't apply the scope && reject them from scopes_hash
|
28
28
|
def filterout_empty_string_and_nil
|
29
29
|
@scopes_hash.delete_if { |scope, arg| ["", nil].include? @formobj[scope] }
|
30
30
|
end
|
data/lib/siphon/version.rb
CHANGED
data/spec/class/formobj.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: siphon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Sistovaris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|