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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c84b3698256472408c03116d1771e9e2fab1116
4
- data.tar.gz: 64a28eb362a0291a0e90193d82d8bd42f44b992e
3
+ metadata.gz: cade992dddbcf4aaed6af0409efc20205c03e48e
4
+ data.tar.gz: 3ac950dbd0d3ac66a9681193733cc9f363c6da20
5
5
  SHA512:
6
- metadata.gz: a34ec016cd428fdbdb066ad44278a0c41ce034a31df9adaf6f36fa0d932b6ee30e58fd7765bc2397003fa2ecff891f2287f926138f827ed8c71465f2c82c42d0
7
- data.tar.gz: be17415132d6c8afa333988ccb97a5177403d2fa1fd085216bbb7b7ee81fbca4ce4b70fe2f1de22a9ac534ce6555332073becba7c51fd85bfd4e97778e06a888
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
- # order.rb
21
- class Order < ActiveRecord::Base
22
- scope :stale, ->(duration) { where(["state='onhold' OR (state != 'done' AND updated_at < ?)", duration.ago]) }
23
- scope :unpaid -> { where(paid: false) }
24
- end
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
- = form_for @order_form do |f|
29
- = f.label :stale, "Stale since more than"
30
- = f.select :stale, [["1 week", 1.week], ["3 weeks", 3.weeks], ["3 months", 3.months]], include_blank: true
31
- = f.label :unpaid
32
- = f.check_box :unpaid
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
- # order_form.rb
37
- class OrderForm
38
- include Virtus.model
39
- include ActiveModel::Model
40
- #
41
- # attribute are the named scopes and their value are :
42
- # - either the value you pass a scope which takes arguments
43
- # - either a Siphon::Nil value to apply (or not) a scope which has no argument
44
- #
45
- attribute :stale, Integer
46
- attribute :unpaid, Siphon::Nil
47
- end
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
- # orders_controller.rb
53
- def search
54
- @order_form = OrderForm.new(params[:order_form])
55
- @orders = siphon(Order.all).scope(@order_form)
56
- end
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
- = form_for @order_form do |f|
80
- = f.label :stale, "Stale since more than"
81
- = f.select :stale, [["1 week", 1], ["3 weeks", 3], ["9 weeks", 9]], include_blank: true
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]: http:://www.coderwall.com/charly
110
+ [2]: https://coderwall.com/p/4zz6ca
@@ -3,8 +3,8 @@ module Siphon
3
3
  # Adapter
4
4
  # ==========
5
5
  #
6
- # use : determine which scope will be called &
7
- # set relevant args to nil for
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.scopes
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 'empty string')
26
- # or if present in formobj but not in form (aka : value is nil)
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
@@ -1,3 +1,3 @@
1
1
  module Siphon
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -23,7 +23,7 @@ class Formobj
23
23
  # super()
24
24
  # end
25
25
 
26
- def scopes
26
+ def _scopes
27
27
  attributes
28
28
  end
29
29
 
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.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-05 00:00:00.000000000 Z
11
+ date: 2013-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport