gauze 0.0.3 → 0.0.4
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 +50 -1
- data/lib/gauze/base.rb +39 -7
- data/lib/gauze/version.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: 527dae15f5adffa62ef1b3325a193b704a8894c2
|
4
|
+
data.tar.gz: 96b137d7878a66b32b8a52abddfd9ffb4bc178ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78c419437f8fa15ca7f28fbc26c690b26fac850841d26f8ee19a49f1f5c31cc1d4ad400e53b044294f871210dd7704061075a30821ea0f52c2200ee0c375e51b
|
7
|
+
data.tar.gz: 4257f394a2244ef6a9ef140aa9f7a275b018572fedbb055b91ed512b3e11554849d6ab6809edee3b050908aadf17bb2903813f06af8ed8cc9b8d1f926a3840e6
|
data/README.md
CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
|
|
28
28
|
## Usage
|
29
29
|
|
30
30
|
Create a class that your controller can find.
|
31
|
-
Define filters using the Gauze DSL.
|
31
|
+
Define filters & sorters using the Gauze DSL.
|
32
32
|
|
33
33
|
The filter method accepts 4 arguments.
|
34
34
|
|
@@ -56,6 +56,17 @@ Then in your controller:
|
|
56
56
|
end
|
57
57
|
```
|
58
58
|
|
59
|
+
The sorter method accepts 2 arguments.
|
60
|
+
|
61
|
+
`sorter :param_key, :column_name`
|
62
|
+
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class EventFilters < Gauze::Base
|
66
|
+
sorter :event_name, :name
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
59
70
|
Because **Gauze** uses AREL to construct queries, you can use `.build` just like any other scope. You can pass relational objects & chain more relations as you need.
|
60
71
|
```ruby
|
61
72
|
def index
|
@@ -64,6 +75,44 @@ def index
|
|
64
75
|
end
|
65
76
|
```
|
66
77
|
|
78
|
+
### Using Joins
|
79
|
+
|
80
|
+
It is possible to filter & sort across joined tables.
|
81
|
+
|
82
|
+
To start, you need to let **Gauze** know that you are going to make use of a join.
|
83
|
+
Simply add the `needs` method to your gauze classes.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class CustomerFilters < Gauze::Base
|
87
|
+
needs :person
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
This can also be a hash or array, a-la activerecord:
|
92
|
+
```ruby
|
93
|
+
needs person: :location, orders: {transaction: :charge}
|
94
|
+
```
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
needs :person, :location
|
98
|
+
```
|
99
|
+
|
100
|
+
After you have all of your gauze needs met, you simply use a hash style syntax to filter across the join.
|
101
|
+
```ruby
|
102
|
+
class CustomerFilters < Gauze::Base
|
103
|
+
needs :person
|
104
|
+
filter :name, {person: :last_name}, :matches, -> val {"%#{val}%"}
|
105
|
+
sorter :last_name, {person: :last_name}
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
|
110
|
+
## TO-DO
|
111
|
+
- [ ] Write tests.
|
112
|
+
- [ ] Add controller hooks.
|
113
|
+
|
114
|
+
|
115
|
+
|
67
116
|
## Contributing
|
68
117
|
|
69
118
|
1. Fork it ( https://github.com/TheKidCoder/gauze/fork )
|
data/lib/gauze/base.rb
CHANGED
@@ -17,8 +17,13 @@ module Gauze
|
|
17
17
|
@filters.push param_key: param_key, column: column_name, method: arel_method, preprocessor: preprocessor
|
18
18
|
end
|
19
19
|
|
20
|
+
def self.sorter(param_key, column)
|
21
|
+
@sorters ||= []
|
22
|
+
@sorters.push param_key: param_key, column: column
|
23
|
+
end
|
24
|
+
|
20
25
|
def self.build(resource, params = {})
|
21
|
-
new(resource, params).
|
26
|
+
new(resource, params).build
|
22
27
|
end
|
23
28
|
|
24
29
|
def initialize(resource, params = {})
|
@@ -26,13 +31,17 @@ module Gauze
|
|
26
31
|
@params = params.symbolize_keys
|
27
32
|
end
|
28
33
|
|
29
|
-
def
|
34
|
+
def build
|
30
35
|
wheres = applied_filters.map {|obj| build_arel_filter(obj)}
|
31
36
|
_query = @resource
|
32
37
|
wheres.each {|node| _query = _query.where(node)}
|
33
38
|
|
34
|
-
if
|
35
|
-
_query = _query.joins(
|
39
|
+
if get_klass_var(:@join_stanza).present?
|
40
|
+
_query = _query.joins(get_klass_var(:@join_stanza))
|
41
|
+
end
|
42
|
+
|
43
|
+
if get_klass_var(:@sorters).present?
|
44
|
+
_query = build_order_query(_query)
|
36
45
|
end
|
37
46
|
|
38
47
|
return _query
|
@@ -42,18 +51,41 @@ module Gauze
|
|
42
51
|
filter_val = @params[filter_hash[:param_key]]
|
43
52
|
filter_val = filter_hash[:preprocessor].call(filter_val) if filter_hash[:preprocessor]
|
44
53
|
|
45
|
-
|
54
|
+
if filter_hash[:column].is_a?(Hash)
|
55
|
+
arel_column_from_hash(filter_hash[:column]).method(filter_hash[:method]).call(filter_val)
|
56
|
+
else
|
57
|
+
@resource.arel_table[filter_hash[:column]].method(filter_hash[:method]).call(filter_val)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_order_query(query)
|
62
|
+
return query unless @params[:sort].present?
|
63
|
+
sort_column = get_klass_var(:@sorters).find {|h| h[:param_key].to_s == @params[:sort].underscore}
|
64
|
+
return query unless sort_column.present?
|
65
|
+
|
66
|
+
if sort_column[:column].is_a?(Hash)
|
67
|
+
query.order(arel_column_from_hash(sort_column[:column]))
|
68
|
+
else
|
69
|
+
query.order(sort_column[:column])
|
70
|
+
end
|
46
71
|
end
|
47
72
|
|
48
73
|
private
|
49
|
-
def
|
74
|
+
def get_klass_var(var)
|
75
|
+
self.class.instance_variable_get(var)
|
76
|
+
end
|
77
|
+
|
78
|
+
def arel_column_from_hash(hash_param)
|
79
|
+
raise ArgumentError, "Hash can only have one key." if hash_param.length > 1
|
80
|
+
_resource = hash_param.keys.first.to_s.classify.constantize
|
81
|
+
_resource.arel_table[hash_param.values.first]
|
50
82
|
end
|
51
83
|
|
52
84
|
def applied_filters
|
53
85
|
_filters = []
|
54
86
|
@params.each do |k,v|
|
55
87
|
next unless v.present?
|
56
|
-
next unless filter =
|
88
|
+
next unless filter = get_klass_var(:@filters).find {|obj| obj[:param_key] == k}
|
57
89
|
_filters.push filter
|
58
90
|
end
|
59
91
|
|
data/lib/gauze/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gauze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Ostrowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|