filter_me 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: 1d20eaf1933fba4c0a52e4c6b04ab9e9bce510c2
4
- data.tar.gz: eafa06100f1fd7cf77332a340f1213f7e344beae
3
+ metadata.gz: 18b40e6f2a9fcaf467a1f2166f05d0ba68105950
4
+ data.tar.gz: 0fb38eacbe1d68acaeadc9f45d6e10c8d8da2def
5
5
  SHA512:
6
- metadata.gz: a5356b764fbe3b6a961fc1dae9a8e7b6a08dce53c4871772a13119fb0623e676acd4fa1074d7b308270d215e6dce9700cbb76ac6590c929643e8770a5891bf86
7
- data.tar.gz: b07bbc44c7a7bd6bcec60cbfa453615e28eba848e8c30cc5d2069ac0edfb98f516ef80d26302bf0db7aa39f6188d34f062d7e6b7b87b43a49abb3b1f62e26f4f
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 provides filtering using Ruby classes and object oriented development
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
- field :type, [:matches, :eq, :not_eq] # Uses arel, so any Arel::Predications method should work
22
- field :cost, [:lt, :gt, :lteq, :gteq, :eq] # Uses arel, so any Arel::Predications method should work
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
- Given a controller that recieves params like the following:
35
- ``` ruby
36
- params # => {filters: {type: {eq: "admin"} } }
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
- The following SQL would be performed (Using ActiveRecord):
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"."type" = "admin")
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
- association :account, :filter_class => AccountsFilter
50
- field :username, [:matches, :eq, :not_eq] # Uses arel, so any Arel::Predications method should work
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
- With the following params:
63
- ``` ruby
64
- params # => {:filters => {:email => {:matches => "%test.com"}, :account => {:cost => {:lt => 100000} } } }
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 ("users"."email" LIKE '%test.com') AND ("accounts"."cost" < 100000)
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)
@@ -1,3 +1,3 @@
1
1
  module FilterMe
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -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([[:gt, [1]], [:lt, [10]]], configuration)
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%"]], [:lt, ["z", "Z"]]], configuration)
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.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-17 00:00:00.000000000 Z
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord