filter_me 0.1.1 → 0.1.2
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 +82 -17
- data/lib/filter_me/filter/arel_field_filter.rb +1 -1
- data/lib/filter_me/filter/field_validator.rb +1 -1
- data/lib/filter_me/version.rb +1 -1
- data/spec/unit/arel_field_filter_spec.rb +2 -2
- 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: 18b40e6f2a9fcaf467a1f2166f05d0ba68105950
|
4
|
+
data.tar.gz: 0fb38eacbe1d68acaeadc9f45d6e10c8d8da2def
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0440632d4aa92aa9965db9fda965733749ef433bb4bbb2c0f3a437455b64c18b76a51b438bfb600592152cc4c1fcafb9b81a728c7796b2983b540538198ba2b
|
7
|
+
data.tar.gz: 5aefc9980ead5f85a92ac9b255bf00dd2ececb46b5d714e29bca934b383cbad3ffffdbba55c5c0dc3922f75d094d1cdd85ea6631ef1dbb1384fa92f502b2c91d
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@ FilterMe
|
|
5
5
|
|
6
6
|
### A Rails/ActiveRecord filtering gem
|
7
7
|
|
8
|
-
FilterMe provids helpers and classes that
|
8
|
+
FilterMe provids helpers and classes that makes request filtering easy using Ruby classes and object oriented development.
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
``` ruby
|
@@ -17,37 +17,59 @@ gem "filter_me", "0.1.0"
|
|
17
17
|
``` ruby
|
18
18
|
class AccountsFilter < FilterMe::ActiveRecordFilter
|
19
19
|
model Account
|
20
|
-
|
21
|
-
|
22
|
-
field :cost,
|
20
|
+
association :user
|
21
|
+
|
22
|
+
field :cost, :all
|
23
|
+
field :account_type, [:matches] # Symbols are Arel::Predications methods
|
23
24
|
end
|
24
25
|
|
25
26
|
class AccountsController < ApplicationController
|
26
27
|
include FilterMe
|
27
|
-
|
28
|
+
|
28
29
|
def index
|
29
30
|
@accounts = filter_me(Account.all)
|
31
|
+
respond_to do |format|
|
32
|
+
format.json { render json: @accounts }
|
33
|
+
end
|
30
34
|
end
|
31
35
|
end
|
36
|
+
|
32
37
|
```
|
33
38
|
|
34
|
-
|
35
|
-
|
36
|
-
|
39
|
+
Plain request:
|
40
|
+
http://0.0.0.0:3000/accounts.json
|
41
|
+
``` json
|
42
|
+
{"accounts":[
|
43
|
+
{"id":1, "cost":100000, "account_type":"admin"},
|
44
|
+
{"id":2, "cost":50000, "account_type":"paid"},
|
45
|
+
{"id":3, "cost":10000, "account_type":"free"}
|
46
|
+
]}
|
47
|
+
```
|
48
|
+
Performs:
|
49
|
+
``` SQL
|
50
|
+
SELECT "accounts".* FROM "accounts"
|
37
51
|
```
|
38
52
|
|
39
|
-
|
53
|
+
Now with some filtering:
|
54
|
+
http://0.0.0.0:3000/accounts.json?filters%5Baccount_type%5D%5Bmatches%5D=paid
|
55
|
+
``` json
|
56
|
+
{"accounts":[
|
57
|
+
{"id":2, "cost":50000, "account_type":"paid"}
|
58
|
+
]}
|
59
|
+
```
|
60
|
+
Performs:
|
40
61
|
``` SQL
|
41
|
-
SELECT "accounts".* FROM "accounts" WHERE ("accounts"."
|
62
|
+
SELECT "accounts".* FROM "accounts" WHERE ("accounts"."account_type" LIKE 'paid')
|
42
63
|
```
|
43
64
|
|
44
65
|
## Nested Filtering:
|
45
66
|
``` ruby
|
46
67
|
class UsersFilter < FilterMe::ActiveRecordFilter
|
47
68
|
model User
|
48
|
-
|
49
|
-
|
50
|
-
field :username, [:matches, :eq, :not_eq] #
|
69
|
+
association :account
|
70
|
+
|
71
|
+
field :username, [:matches, :eq, :not_eq] # Symbols are Arel::Predications methods
|
72
|
+
field :email, [:matches, :eq, :not_eq] # Symbols are Arel::Predications methods
|
51
73
|
end
|
52
74
|
|
53
75
|
class UsersController < ApplicationController
|
@@ -55,18 +77,61 @@ class UsersController < ApplicationController
|
|
55
77
|
|
56
78
|
def index
|
57
79
|
@users = filter_me(User.all)
|
80
|
+
respond_to do |format|
|
81
|
+
format.json { render json: @users }
|
82
|
+
end
|
58
83
|
end
|
59
84
|
end
|
60
85
|
```
|
61
86
|
|
62
|
-
|
63
|
-
|
64
|
-
|
87
|
+
Plain request:
|
88
|
+
http://0.0.0.0:3000/users.json
|
89
|
+
``` json
|
90
|
+
{"users":[
|
91
|
+
{"id":1, "username":"test1", "email":"test2@test.com", "account": {
|
92
|
+
"id":1, "cost":100000, "account_type":"admin"
|
93
|
+
}},
|
94
|
+
{"id":2, "username":"test2", "email":"test2@test.com", "account":{
|
95
|
+
"id":2, "cost":50000, "account_type":"paid"
|
96
|
+
}},
|
97
|
+
{"id":3, "username":"test3", "email":"test3@spaz.com", "account":{
|
98
|
+
"id":3, "cost":10000, "account_type":"free"
|
99
|
+
}}
|
100
|
+
]}
|
101
|
+
Performs:
|
102
|
+
``` SQL
|
103
|
+
SELECT "users".* FROM "users"
|
104
|
+
```
|
105
|
+
|
106
|
+
Now with some filtering:
|
107
|
+
http://0.0.0.0:3000/users.json?filters%5Baccount%5D%5Bcost%5D%5Blt%5D=50000
|
108
|
+
``` json
|
109
|
+
{"users":[
|
110
|
+
{"id":3, "username":"test3", "email":"test3@spaz.com", "account":{
|
111
|
+
"id":3,"cost":10000, "account_type":"free"
|
112
|
+
}}
|
113
|
+
]}
|
65
114
|
```
|
66
115
|
Performs:
|
67
116
|
``` SQL
|
68
117
|
SELECT "users".* FROM "users" INNER JOIN "accounts" ON "accounts"."user_id" = "users"."id"
|
69
|
-
WHERE ("
|
118
|
+
WHERE ("accounts"."cost" < 50000)
|
119
|
+
```
|
120
|
+
|
121
|
+
Need to provide some top secret super duper special filtering? Go ahead:
|
122
|
+
``` ruby
|
123
|
+
class UsersFilter < FilterMe::ActiveRecordFilter
|
124
|
+
model User
|
125
|
+
|
126
|
+
def special_filter(relation, filters)
|
127
|
+
relation.where(id: filters)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
```
|
131
|
+
http://0.0.0.0:3000/users.json?filters%5Baccount%5D%5Bcost%5D%5Bgteq%5D=50000&filters%5Bspecial_filter%5D%5B%5D=3&filters%5Bspecial_filter%5D%5B%5D=2 Now performs:
|
132
|
+
``` SQL
|
133
|
+
SELECT "users".* FROM "users" INNER JOIN "accounts" ON "accounts"."user_id" = "users"."id"
|
134
|
+
WHERE ("accounts"."cost" >= 50000) AND "users"."id" IN (3, 2)
|
70
135
|
```
|
71
136
|
|
72
137
|
## License
|
@@ -8,7 +8,7 @@ module FilterMe
|
|
8
8
|
@configuration = configuration
|
9
9
|
|
10
10
|
unless validator.valid_filters?(filters)
|
11
|
-
raise FiltersNotWhiteListedError, "The filter types #{validator.invalid_filters(filters)} are not allowed."
|
11
|
+
raise FiltersNotWhiteListedError, "The filter types #{validator.invalid_filters(filters).map { |filter| filter[0] }} are not allowed."
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -21,7 +21,7 @@ module FilterMe
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def valid_filters?(filters)
|
24
|
-
filters.all? { |filter| whitelisted_filters.include? filter[0] }
|
24
|
+
filters.all? { |filter| whitelisted_filters.include?(filter[0]) || whitelisted_filters.include?(filter[0].to_sym) }
|
25
25
|
end
|
26
26
|
|
27
27
|
def invalid_filters(filters)
|
data/lib/filter_me/version.rb
CHANGED
@@ -91,7 +91,7 @@ describe FilterMe::Filter::ArelFieldFilter do
|
|
91
91
|
end
|
92
92
|
|
93
93
|
it "builds the correct arel filter with two filter types of one filter value each" do
|
94
|
-
field_filter = FilterMe::Filter::ArelFieldFilter.new([[
|
94
|
+
field_filter = FilterMe::Filter::ArelFieldFilter.new([["gt", [1]], [:lt, [10]]], configuration)
|
95
95
|
relation_mock = double("relation")
|
96
96
|
|
97
97
|
arel_filter = arel_table[field].lt(10).and(arel_table[field].gt(1))
|
@@ -103,7 +103,7 @@ describe FilterMe::Filter::ArelFieldFilter do
|
|
103
103
|
end
|
104
104
|
|
105
105
|
it "builds the correct arel filter with two filter types of two filter value each" do
|
106
|
-
field_filter = FilterMe::Filter::ArelFieldFilter.new([[:matches, ["%hi%", "%hey%"]], [
|
106
|
+
field_filter = FilterMe::Filter::ArelFieldFilter.new([[:matches, ["%hi%", "%hey%"]], ["lt", ["z", "Z"]]], configuration)
|
107
107
|
relation_mock = double("relation")
|
108
108
|
|
109
109
|
arel_filter = arel_table[field].lt("Z")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filter_me
|
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
|
- Sam Clopton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|