paraphrase 0.8.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 495b5b3b50b22aed23b4b684084c4213dfa6648d
4
- data.tar.gz: 25742c98d2fea7b1b65f64c0694a5b6fa2888005
3
+ metadata.gz: 54d269a79cf5f4ab55d4afe39c4f9e0c4dd6841f
4
+ data.tar.gz: e95b97c71c317d9e6f534bb7dcfb10037cde2bfe
5
5
  SHA512:
6
- metadata.gz: 900028635ad15f53e571fc5af6f589886b4b814930a91f084ce8c387798a379b04d29c8876d373685e9434b33e207f948d87e3ced0e2f0f8f8ba7258f8fc7fce
7
- data.tar.gz: 11deef4d8d8a299018766bd1f7d9ced8dea177594e9a549e0479b0035e1cbe1df9da1f8e82aaa554ceba33492d76371f5d0fe1c685deb6213f0949f4cd3c4472
6
+ metadata.gz: f69b9ef6eb394171092763aa92144d0f044e0f7668c374a4d609da84d0a2e163d39fa2679771fa9a32894150a8a478002d531acd6f3ff139c521742fa5494b75
7
+ data.tar.gz: 93221d4cd686cd4f7a84d958df0608264a4869045ce891239e4dc47d7330c68c2712faf6a049fde322cd3f79fd938e709caf0bea14a96e45822a395ca6d35ce4
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ doc/
3
3
  *.gem
4
4
  vendor/cache/*.gem
5
5
  .yardoc
6
+ coverage
@@ -1,12 +1,17 @@
1
1
  rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
- - 2.1.0
4
+ - 2.1.1
5
5
  - jruby-19mode
6
6
  gemfile:
7
7
  - gemfiles/3.0.gemfile
8
8
  - gemfiles/3.1.gemfile
9
9
  - gemfiles/3.2.gemfile
10
10
  - gemfiles/4.0.gemfile
11
- install: 'bundle install -j 4'
11
+ - gemfiles/4.1.gemfile
12
+ install: 'travis_retry bundle install -j 4'
12
13
  script: 'bundle exec rake'
14
+ env:
15
+ global:
16
+ # Code Climate coverage reporting
17
+ - secure: "VylP1haJogwCq04GRrM3hVfNT4YJiZ4RJoklOQLgqPMRalOCzVPOIRoTz3qpVbdQHf3l18VhVDrDbu2W2qfBKJq7/anIuYYgPd0uRAGliZD4h5B2PgQDqvPeTEftj/g3IYDh6PvZCuhgnxMdldmSnxd9WAjAnkLtjoiLgE6elCE="
data/Appraisals CHANGED
@@ -21,3 +21,9 @@ appraise '4.0' do
21
21
  gem 'activesupport', '~> 4.0'
22
22
  gem 'actionpack', '~> 4.0'
23
23
  end
24
+
25
+ appraise '4.1' do
26
+ gem 'activerecord', '~> 4.1'
27
+ gem 'activesupport', '~> 4.1'
28
+ gem 'actionpack', '~> 4.1'
29
+ end
@@ -1,3 +1,9 @@
1
+ ## 0.9.0
2
+
3
+ * Define methods to process query params on a subclass of `Paraphrase::Params`
4
+ that also handles filtering blank query params.
5
+ * `Query.keys` and `Query#keys` expose the keys that have been mapped.
6
+
1
7
  ## 0.8.0 / 2-5-2014
2
8
 
3
9
  * Remove `ActiveSupport::Notifications`
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # paraphrase
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/ecbypi/paraphrase.png)](https://codeclimate.com/github/ecbypi/paraphrase)
4
+ [![Build Status](https://travis-ci.org/ecbypi/paraphrase.png?branch=master)](https://travis-ci.org/ecbypi/paraphrase)
4
5
 
5
6
  Paraphrase provides a way to map query params to model scopes and
6
7
  only apply scopes when the mapped query params are present.
@@ -29,10 +30,6 @@ be applied to which scopes.
29
30
  class PostQuery < Paraphrase::Query
30
31
  map :author, to: :by_user
31
32
  map :start_date, :end_date, to: :published_within
32
-
33
- def start_date
34
- Time.zone.parse(params[:start_date])
35
- end
36
33
  end
37
34
  ```
38
35
 
@@ -49,16 +46,16 @@ class AdminPostQuery < Paraphrase::Query
49
46
  end
50
47
  ```
51
48
 
52
- In the controller, call `.paraphrase` on your model, passing a hash of query
53
- params. This will filter out the registered query params, calling the scopes
54
- whose inputs are supplied. If a query param for a scope is missing or empty,
55
- the scope is skipped.
49
+ To build the query, call `.paraphrase` on your model. Only scopes whose keys are all
50
+ provided will be applied.
56
51
 
57
- `Paraphrase::Query` will intelligently determine if the value of the query
58
- param is empty. If the value is an array containing empty strings, the empty
59
- strings will be removed before being passed to the scope. If the array is empty
60
- after removing empty strings, the scope will not be called since an empty array
61
- is considered a blank value.
52
+ ```ruby
53
+ # Based on the example `PostQuery` above, this will only apply `Post.by_user`
54
+ # and skip `Post.published_within` since `:end_date` is missing.
55
+ Post.paraphrase(author: 'Jim')
56
+ ```
57
+
58
+ All unregistered keys are filered out of the params that are passed to `.paraphrase`.
62
59
 
63
60
  ```ruby
64
61
  class PostsController < ApplicationController
@@ -72,6 +69,30 @@ class PostsController < ApplicationController
72
69
  end
73
70
  ```
74
71
 
72
+ `Paraphrase::Query` will recursively determine if the value of the query
73
+ param is empty. If the value is an array containing empty strings, the empty
74
+ strings will be removed before being passed to the scope. If the array is empty
75
+ after removing empty strings, the scope will not be called since an empty array
76
+ is considered a blank value.
77
+
78
+ ```ruby
79
+ class UserQuery < Paraphrase::Query
80
+ map :names, to: :with_name
81
+ end
82
+
83
+ class User < ActiveRecord::Base
84
+ def self.with_name(names)
85
+ where(name: names)
86
+ end
87
+ end
88
+
89
+ User.paraphrase(names: ['', 'Jim']).to_sql
90
+ # => SELECT "users".* FROM "users" WHERE "users"."name" IN ['Jim']
91
+
92
+ User.paraphrase(names: ['', '']).to_sql
93
+ # => SELECT "users".* FROM "users"
94
+ ```
95
+
75
96
  You can chain queries on an `ActiveRecord::Relation`. This avoids adding scopes
76
97
  that replicate the functionality of an association like
77
98
  `Post.for_user(user_id)` or allow you to build a default scope.
@@ -100,8 +121,7 @@ end
100
121
 
101
122
  ### Query Class DSL
102
123
 
103
- Scopes are mapped to param keys using the `map` class method provided by
104
- `Paraphrase::Query`. You can specify one or more keys.
124
+ Scopes are mapped to param keys using `map`. You can specify one or more keys.
105
125
 
106
126
  ```ruby
107
127
  class PostQuery < Paraphrase::Query
@@ -121,7 +141,7 @@ end
121
141
  ```
122
142
 
123
143
  If multiple query params are mapped to a scope, but only a subset are required,
124
- use the `:whitelist` option to allow them to be missing. The `:whitelist`
144
+ use the `:whitelist` option to allow them to be blank. The `:whitelist`
125
145
  option can be set to `true`, an individual key or an array of keys.
126
146
 
127
147
  ```ruby
@@ -151,12 +171,11 @@ Post.paraphrase(first_name: 'John', last_name: 'Smith').to_sql
151
171
 
152
172
  ### Boolean Scopes
153
173
 
154
- Some filter records based on a boolean column. It doesn't make sense for these
155
- methods to take any arguments.
174
+ For scopes that filter records based on a boolean column, it doesn't make to
175
+ force the scope to take an argument.
156
176
 
157
- Paraphrase will detect if the method specified takes no arguments. If not, it
158
- will call the method without any arguments, assuming the inputs are present and
159
- valid.
177
+ If the mapped query params are present and a scope takes no arguments,
178
+ `paraphrase` will not attempt to pass those values to the query.
160
179
 
161
180
  ```ruby
162
181
  class PostQuery < Paraphrase::Query
@@ -178,8 +197,7 @@ Post.paraphrase(published: '1').to_sql
178
197
  ### Pre-processing Query Params
179
198
 
180
199
  By default, for each query param specified that maps to a model scope, a method
181
- is defined on the query class that fetches the value for that key. This is used
182
- internally to determine if model scopes need to be applied. To pre-process a
200
+ is defined on the query class that fetches the value for that key. To pre-process a
183
201
  query param, such as an ISO formatted date, override the method in the query
184
202
  class.
185
203
 
@@ -187,12 +205,14 @@ class.
187
205
  class PostQuery < Paraphrase::Query
188
206
  map :start_date, :end_date, to: :published_within
189
207
 
190
- def start_date
191
- @start_date ||= Time.zone.parse(params[:start_date]) rescue nil
192
- end
208
+ class Params < Paraphrase::Params
209
+ def start_date
210
+ @start_date ||= Time.zone.parse(params[:start_date]) rescue nil
211
+ end
193
212
 
194
- def end_date
195
- @start_date ||= Time.zone.parse(params[:end_date]) rescue nil
213
+ def end_date
214
+ @start_date ||= Time.zone.parse(params[:end_date]) rescue nil
215
+ end
196
216
  end
197
217
  end
198
218
 
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/Work/paraphrase
3
3
  specs:
4
- paraphrase (0.8.0)
5
- activemodel (>= 3.0, < 4.1)
6
- activerecord (>= 3.0, < 4.1)
7
- activesupport (>= 3.0, < 4.1)
4
+ paraphrase (0.9.0)
5
+ activemodel (>= 3.0, < 4.2)
6
+ activerecord (>= 3.0, < 4.2)
7
+ activesupport (>= 3.0, < 4.2)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -35,8 +35,11 @@ GEM
35
35
  rake
36
36
  arel (3.0.3)
37
37
  builder (3.0.4)
38
+ codeclimate-test-reporter (0.3.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.0)
39
41
  diff-lcs (1.2.5)
42
+ docile (1.1.3)
40
43
  erubis (2.7.0)
41
44
  hike (1.2.3)
42
45
  i18n (0.6.9)
@@ -62,6 +65,11 @@ GEM
62
65
  rspec-expectations (2.14.4)
63
66
  diff-lcs (>= 1.1.3, < 2.0)
64
67
  rspec-mocks (2.14.4)
68
+ simplecov (0.8.2)
69
+ docile (~> 1.1.0)
70
+ multi_json
71
+ simplecov-html (~> 0.8.0)
72
+ simplecov-html (0.8.0)
65
73
  slop (3.4.7)
66
74
  sprockets (2.2.2)
67
75
  hike (~> 1.2)
@@ -82,6 +90,7 @@ DEPENDENCIES
82
90
  activesupport (~> 3.0)
83
91
  appraisal (~> 0.4)
84
92
  bundler (~> 1.0)
93
+ codeclimate-test-reporter (~> 0.3)
85
94
  paraphrase!
86
95
  pry (~> 0.9)
87
96
  rake (~> 0.9.2)
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/Work/paraphrase
3
3
  specs:
4
- paraphrase (0.8.0)
5
- activemodel (>= 3.0, < 4.1)
6
- activerecord (>= 3.0, < 4.1)
7
- activesupport (>= 3.0, < 4.1)
4
+ paraphrase (0.9.0)
5
+ activemodel (>= 3.0, < 4.2)
6
+ activerecord (>= 3.0, < 4.2)
7
+ activesupport (>= 3.0, < 4.2)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -35,8 +35,11 @@ GEM
35
35
  rake
36
36
  arel (3.0.3)
37
37
  builder (3.0.4)
38
+ codeclimate-test-reporter (0.3.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.0)
39
41
  diff-lcs (1.2.5)
42
+ docile (1.1.3)
40
43
  erubis (2.7.0)
41
44
  hike (1.2.3)
42
45
  i18n (0.6.9)
@@ -62,6 +65,11 @@ GEM
62
65
  rspec-expectations (2.14.4)
63
66
  diff-lcs (>= 1.1.3, < 2.0)
64
67
  rspec-mocks (2.14.4)
68
+ simplecov (0.8.2)
69
+ docile (~> 1.1.0)
70
+ multi_json
71
+ simplecov-html (~> 0.8.0)
72
+ simplecov-html (0.8.0)
65
73
  slop (3.4.7)
66
74
  sprockets (2.2.2)
67
75
  hike (~> 1.2)
@@ -82,6 +90,7 @@ DEPENDENCIES
82
90
  activesupport (~> 3.1)
83
91
  appraisal (~> 0.4)
84
92
  bundler (~> 1.0)
93
+ codeclimate-test-reporter (~> 0.3)
85
94
  paraphrase!
86
95
  pry (~> 0.9)
87
96
  rake (~> 0.9.2)
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/Work/paraphrase
3
3
  specs:
4
- paraphrase (0.8.0)
5
- activemodel (>= 3.0, < 4.1)
6
- activerecord (>= 3.0, < 4.1)
7
- activesupport (>= 3.0, < 4.1)
4
+ paraphrase (0.9.0)
5
+ activemodel (>= 3.0, < 4.2)
6
+ activerecord (>= 3.0, < 4.2)
7
+ activesupport (>= 3.0, < 4.2)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -35,8 +35,11 @@ GEM
35
35
  rake
36
36
  arel (3.0.3)
37
37
  builder (3.0.4)
38
+ codeclimate-test-reporter (0.3.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.0)
39
41
  diff-lcs (1.2.5)
42
+ docile (1.1.3)
40
43
  erubis (2.7.0)
41
44
  hike (1.2.3)
42
45
  i18n (0.6.9)
@@ -62,6 +65,11 @@ GEM
62
65
  rspec-expectations (2.14.4)
63
66
  diff-lcs (>= 1.1.3, < 2.0)
64
67
  rspec-mocks (2.14.4)
68
+ simplecov (0.8.2)
69
+ docile (~> 1.1.0)
70
+ multi_json
71
+ simplecov-html (~> 0.8.0)
72
+ simplecov-html (0.8.0)
65
73
  slop (3.4.7)
66
74
  sprockets (2.2.2)
67
75
  hike (~> 1.2)
@@ -82,6 +90,7 @@ DEPENDENCIES
82
90
  activesupport (~> 3.2)
83
91
  appraisal (~> 0.4)
84
92
  bundler (~> 1.0)
93
+ codeclimate-test-reporter (~> 0.3)
85
94
  paraphrase!
86
95
  pry (~> 0.9)
87
96
  rake (~> 0.9.2)
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/Work/paraphrase
3
3
  specs:
4
- paraphrase (0.8.0)
5
- activemodel (>= 3.0, < 4.1)
6
- activerecord (>= 3.0, < 4.1)
7
- activesupport (>= 3.0, < 4.1)
4
+ paraphrase (0.9.0)
5
+ activemodel (>= 3.0, < 4.2)
6
+ activerecord (>= 3.0, < 4.2)
7
+ activesupport (>= 3.0, < 4.2)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -36,8 +36,11 @@ GEM
36
36
  arel (4.0.1)
37
37
  atomic (1.1.14)
38
38
  builder (3.1.4)
39
+ codeclimate-test-reporter (0.3.0)
40
+ simplecov (>= 0.7.1, < 1.0.0)
39
41
  coderay (1.1.0)
40
42
  diff-lcs (1.2.5)
43
+ docile (1.1.3)
41
44
  erubis (2.7.0)
42
45
  i18n (0.6.9)
43
46
  method_source (0.8.2)
@@ -60,6 +63,11 @@ GEM
60
63
  rspec-expectations (2.14.4)
61
64
  diff-lcs (>= 1.1.3, < 2.0)
62
65
  rspec-mocks (2.14.4)
66
+ simplecov (0.8.2)
67
+ docile (~> 1.1.0)
68
+ multi_json
69
+ simplecov-html (~> 0.8.0)
70
+ simplecov-html (0.8.0)
63
71
  slop (3.4.7)
64
72
  sqlite3 (1.3.8)
65
73
  thread_safe (0.1.3)
@@ -76,6 +84,7 @@ DEPENDENCIES
76
84
  activesupport (~> 4.0)
77
85
  appraisal (~> 0.4)
78
86
  bundler (~> 1.0)
87
+ codeclimate-test-reporter (~> 0.3)
79
88
  paraphrase!
80
89
  pry (~> 0.9)
81
90
  rake (~> 0.9.2)
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.1"
6
+ gem "activesupport", "~> 4.1"
7
+ gem "actionpack", "~> 4.1"
8
+
9
+ gemspec :path=>"../"
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ paraphrase (0.9.0)
5
+ activemodel (>= 3.0, < 4.2)
6
+ activerecord (>= 3.0, < 4.2)
7
+ activesupport (>= 3.0, < 4.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (4.1.0)
13
+ actionview (= 4.1.0)
14
+ activesupport (= 4.1.0)
15
+ rack (~> 1.5.2)
16
+ rack-test (~> 0.6.2)
17
+ actionview (4.1.0)
18
+ activesupport (= 4.1.0)
19
+ builder (~> 3.1)
20
+ erubis (~> 2.7.0)
21
+ activemodel (4.1.0)
22
+ activesupport (= 4.1.0)
23
+ builder (~> 3.1)
24
+ activerecord (4.1.0)
25
+ activemodel (= 4.1.0)
26
+ activesupport (= 4.1.0)
27
+ arel (~> 5.0.0)
28
+ activesupport (4.1.0)
29
+ i18n (~> 0.6, >= 0.6.9)
30
+ json (~> 1.7, >= 1.7.7)
31
+ minitest (~> 5.1)
32
+ thread_safe (~> 0.1)
33
+ tzinfo (~> 1.1)
34
+ appraisal (0.5.2)
35
+ bundler
36
+ rake
37
+ arel (5.0.1.20140414130214)
38
+ builder (3.2.2)
39
+ codeclimate-test-reporter (0.3.0)
40
+ simplecov (>= 0.7.1, < 1.0.0)
41
+ coderay (1.1.0)
42
+ diff-lcs (1.2.5)
43
+ docile (1.1.3)
44
+ erubis (2.7.0)
45
+ i18n (0.6.9)
46
+ json (1.8.1)
47
+ method_source (0.8.2)
48
+ minitest (5.3.3)
49
+ multi_json (1.9.3)
50
+ pry (0.9.12.6)
51
+ coderay (~> 1.0)
52
+ method_source (~> 0.8)
53
+ slop (~> 3.4)
54
+ rack (1.5.2)
55
+ rack-test (0.6.2)
56
+ rack (>= 1.0)
57
+ rake (0.9.6)
58
+ redcarpet (2.1.1)
59
+ rspec (2.14.1)
60
+ rspec-core (~> 2.14.0)
61
+ rspec-expectations (~> 2.14.0)
62
+ rspec-mocks (~> 2.14.0)
63
+ rspec-core (2.14.8)
64
+ rspec-expectations (2.14.5)
65
+ diff-lcs (>= 1.1.3, < 2.0)
66
+ rspec-mocks (2.14.6)
67
+ simplecov (0.8.2)
68
+ docile (~> 1.1.0)
69
+ multi_json
70
+ simplecov-html (~> 0.8.0)
71
+ simplecov-html (0.8.0)
72
+ slop (3.5.0)
73
+ sqlite3 (1.3.8)
74
+ thread_safe (0.3.3)
75
+ tzinfo (1.1.0)
76
+ thread_safe (~> 0.1)
77
+ yard (0.8.7.3)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ actionpack (~> 4.1)
84
+ activerecord (~> 4.1)
85
+ activesupport (~> 4.1)
86
+ appraisal (~> 0.4)
87
+ bundler (~> 1.0)
88
+ codeclimate-test-reporter (~> 0.3)
89
+ paraphrase!
90
+ pry (~> 0.9)
91
+ rake (~> 0.9.2)
92
+ redcarpet (~> 2.1.1)
93
+ rspec (~> 2.14)
94
+ sqlite3 (~> 1.3.6)
95
+ yard (~> 0.7)
@@ -0,0 +1,34 @@
1
+ module Paraphrase
2
+ class Params
3
+ attr_reader :params, :result
4
+
5
+ def initialize(params, keys)
6
+ @params = params.with_indifferent_access.slice(*keys)
7
+
8
+ @result = @params.inject(HashWithIndifferentAccess.new) do |result, (key, value)|
9
+ value = respond_to?(key) ? send(key) : scrub(@params[key])
10
+
11
+ if value.present?
12
+ result[key] = value
13
+ end
14
+
15
+ result
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def scrub(value)
22
+ case value
23
+ when Array
24
+ value.delete_if { |v| scrub(v).blank? }
25
+ when Hash
26
+ value.delete_if { |k, v| scrub(v).blank? }
27
+ when String
28
+ value.strip
29
+ else
30
+ value
31
+ end
32
+ end
33
+ end
34
+ end
@@ -5,6 +5,7 @@ require 'active_support/core_ext/string/inflections'
5
5
  require 'active_support/core_ext/array/extract_options'
6
6
  require 'active_support/hash_with_indifferent_access'
7
7
  require 'paraphrase/active_model'
8
+ require 'paraphrase/params'
8
9
 
9
10
  module Paraphrase
10
11
  class Query
@@ -14,7 +15,7 @@ module Paraphrase
14
15
  class_attribute :scopes, :_source, instance_writer: false
15
16
 
16
17
  # @!attribute [r] params
17
- # @return [HashWithIndifferentAccess] filters parameters based on keys defined in scopes
18
+ # @return [HashWithIndifferentAccess] filtered parameters based on keys defined in scopes
18
19
  #
19
20
  # @!attribute [r] result
20
21
  # @return [ActiveRecord::Relation]
@@ -33,6 +34,20 @@ module Paraphrase
33
34
  self._source = name.to_s
34
35
  end
35
36
 
37
+ # Keys being mapped to scopes
38
+ #
39
+ # @return [Array<Symbol>]
40
+ def self.keys
41
+ scopes.flat_map(&:keys)
42
+ end
43
+
44
+ # Returns the class for processing and filtering query params.
45
+ def self.param_processor
46
+ self::Params
47
+ rescue NameError
48
+ Paraphrase::Params
49
+ end
50
+
36
51
  # Add a {Scope} instance to {Query#scopes}. Defines a reader for each key
37
52
  # to read from {Query#params}.
38
53
  #
@@ -48,7 +63,7 @@ module Paraphrase
48
63
  scopes << Scope.new(keys, options)
49
64
 
50
65
  keys.each do |key|
51
- define_method(key) { params[key] } unless method_defined?(key)
66
+ define_method(key) { params[key] }
52
67
  end
53
68
  end
54
69
 
@@ -58,18 +73,13 @@ module Paraphrase
58
73
  # @param [Hash] params query parameters
59
74
  # @param [ActiveRecord::Relation] relation object to apply methods to
60
75
  def initialize(params = {}, relation = source)
61
- keys = scopes.map(&:keys).flatten.map(&:to_s)
62
-
63
- @params = params.with_indifferent_access.slice(*keys)
64
- scrub_params!
76
+ @params = process_params(params)
65
77
 
66
- @result = scopes.inject(relation) do |r, scope|
67
- scope.chain(self, r)
78
+ @result = scopes.inject(relation) do |result, scope|
79
+ scope.chain(@params, result)
68
80
  end
69
81
  end
70
82
 
71
- alias :[] :send
72
-
73
83
  # Return an `ActiveRecord::Relation` corresponding to the source class
74
84
  # determined from the `_source` class attribute or the name of the query
75
85
  # class.
@@ -82,25 +92,15 @@ module Paraphrase
82
92
  end
83
93
  end
84
94
 
85
- private
86
-
87
- def scrub_params!
88
- params.delete_if { |key, value| scrub(value) }
95
+ # @see Query.keys
96
+ def keys
97
+ self.class.keys
89
98
  end
90
99
 
91
- def scrub(value)
92
- value = case value
93
- when Array
94
- value.delete_if { |v| scrub(v) }
95
- when Hash
96
- value.delete_if { |k, v| scrub(v) }
97
- when String
98
- value.strip
99
- else
100
- value
101
- end
100
+ private
102
101
 
103
- value.blank?
102
+ def process_params(params)
103
+ self.class.param_processor.new(params, keys).result
104
104
  end
105
105
  end
106
106
  end
@@ -13,11 +13,11 @@ module Paraphrase
13
13
  # @return [Array<Symbol>] keys required for query
14
14
  attr_reader :keys, :name, :required_keys
15
15
 
16
- # @param [Symbol] name name of the scope
16
+ # @param [Symbol] keys query params to be mapped to the scope
17
17
  # @param [Hash] options options to configure {Scope Scope} instance
18
- # @option options [Symbol, Array<Symbol>] :to param key(s) to extract values from
19
- # @option options [true, Symbol, Array<Symbol>] :require lists all or a
20
- # subset of param keys as required
18
+ # @option options [Symbol, Array<Symbol>] :to scope to map query params to
19
+ # @option options [true, Symbol, Array<Symbol>] :whitelist lists all or a
20
+ # subset of param keys as optional
21
21
  def initialize(keys, options)
22
22
  @keys = keys
23
23
  @name = options[:to]
@@ -34,18 +34,18 @@ module Paraphrase
34
34
  # values are missing. Detects if the scope takes no arguments to determine
35
35
  # if values should be passed to the scope.
36
36
  #
37
- # @param [Paraphrase::Query] query instance of {Query} class
37
+ # @param [Hash] params filtered params from the query
38
38
  # @param [ActiveRecord::Relation] relation scope chain
39
39
  # @return [ActiveRecord::Relation]
40
- def chain(query, relation)
41
- if required_keys.all? { |key| query[key] }
40
+ def chain(params, relation)
41
+ if required_keys.all? { |key| params[key] }
42
42
  klass = relation.respond_to?(:klass) ? relation.klass : relation
43
43
  arity = klass.method(name).arity
44
44
 
45
45
  if arity == 0
46
46
  relation.send(name)
47
47
  else
48
- values = keys.map { |key| query[key] }
48
+ values = keys.map { |key| params[key] }
49
49
  relation.send(name, *values)
50
50
  end
51
51
  else
@@ -1,3 +1,3 @@
1
1
  module Paraphrase
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -24,26 +24,24 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.required_ruby_version = '>= 1.9.3'
26
26
 
27
- gem.add_dependency 'activerecord', '>= 3.0', '< 4.1'
28
- gem.add_dependency 'activesupport', '>= 3.0', '< 4.1'
29
- gem.add_dependency 'activemodel', '>= 3.0', '< 4.1'
27
+ gem.add_dependency 'activerecord', '>= 3.0', '< 4.2'
28
+ gem.add_dependency 'activesupport', '>= 3.0', '< 4.2'
29
+ gem.add_dependency 'activemodel', '>= 3.0', '< 4.2'
30
30
 
31
- gem.add_development_dependency 'actionpack', '>= 3.0', '< 4.1'
31
+ gem.add_development_dependency 'actionpack', '>= 3.0', '< 4.2'
32
32
  gem.add_development_dependency 'bundler', '~> 1.0'
33
33
  gem.add_development_dependency 'yard', '~> 0.7'
34
34
  gem.add_development_dependency 'rspec', '~> 2.14'
35
35
  gem.add_development_dependency 'rake', '~> 0.9.2'
36
36
  gem.add_development_dependency 'appraisal', '~> 0.4'
37
37
  gem.add_development_dependency 'pry', '~> 0.9'
38
-
39
- if RUBY_PLATFORM != 'java'
40
- gem.add_development_dependency 'redcarpet', '~> 2.1.1'
41
- end
38
+ gem.add_development_dependency 'codeclimate-test-reporter', '~> 0.3'
42
39
 
43
40
  if RUBY_PLATFORM == 'java'
44
41
  gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
45
42
  gem.add_development_dependency 'jdbc-sqlite3'
46
43
  else
47
44
  gem.add_development_dependency 'sqlite3', '~> 1.3.6'
45
+ gem.add_development_dependency 'redcarpet', '~> 2.1.1'
48
46
  end
49
47
  end
@@ -9,12 +9,14 @@ module Paraphrase
9
9
  map :authors, to: :by_users
10
10
  map :start_date, :end_date, to: :published_between
11
11
 
12
- def start_date
13
- @start_date ||= Time.parse(params[:start_date]) rescue nil
14
- end
12
+ class Params < Paraphrase::Params
13
+ def start_date
14
+ @start_date ||= Time.parse(params[:start_date]) rescue nil
15
+ end
15
16
 
16
- def end_date
17
- @end_date ||= Time.parse(params[:end_date]) rescue nil
17
+ def end_date
18
+ @end_date ||= Time.parse(params[:end_date]) rescue nil
19
+ end
18
20
  end
19
21
  end
20
22
 
@@ -55,16 +57,6 @@ module Paraphrase
55
57
  end
56
58
  end
57
59
 
58
- describe '#[]' do
59
- it 'retreives values from #params or uses custom reader if defined' do
60
- query = PostQuery.new(title: 'Morning Joe', start_date: '2010-10-30', end_date: 'foo')
61
-
62
- expect(query[:title]).to eq 'Morning Joe'
63
- expect(query[:start_date]).to eq Time.local(2010, 10, 30)
64
- expect(query[:end_date]).to be_nil
65
- end
66
- end
67
-
68
60
  describe "#params" do
69
61
  it "filters out params not specified in scopes" do
70
62
  query = PostQuery.new(nickname: 'bill', title: 'william')
@@ -1,8 +1,15 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require 'rspec'
2
5
  require 'pry'
3
6
  require 'paraphrase'
4
7
  require 'active_record'
5
8
 
9
+ RSpec.configure do |config|
10
+ config.order = 'random'
11
+ end
12
+
6
13
  I18n.enforce_available_locales = false
7
14
 
8
15
  ActiveRecord::Base.establish_connection(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paraphrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Gutierrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-05 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '3.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '4.1'
22
+ version: '4.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '3.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '4.1'
32
+ version: '4.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '3.0'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '4.1'
42
+ version: '4.2'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,7 +49,7 @@ dependencies:
49
49
  version: '3.0'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '4.1'
52
+ version: '4.2'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: activemodel
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -59,7 +59,7 @@ dependencies:
59
59
  version: '3.0'
60
60
  - - "<"
61
61
  - !ruby/object:Gem::Version
62
- version: '4.1'
62
+ version: '4.2'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -69,7 +69,7 @@ dependencies:
69
69
  version: '3.0'
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
- version: '4.1'
72
+ version: '4.2'
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: actionpack
75
75
  requirement: !ruby/object:Gem::Requirement
@@ -79,7 +79,7 @@ dependencies:
79
79
  version: '3.0'
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
- version: '4.1'
82
+ version: '4.2'
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
@@ -89,7 +89,7 @@ dependencies:
89
89
  version: '3.0'
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
- version: '4.1'
92
+ version: '4.2'
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: bundler
95
95
  requirement: !ruby/object:Gem::Requirement
@@ -175,19 +175,19 @@ dependencies:
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0.9'
177
177
  - !ruby/object:Gem::Dependency
178
- name: redcarpet
178
+ name: codeclimate-test-reporter
179
179
  requirement: !ruby/object:Gem::Requirement
180
180
  requirements:
181
181
  - - "~>"
182
182
  - !ruby/object:Gem::Version
183
- version: 2.1.1
183
+ version: '0.3'
184
184
  type: :development
185
185
  prerelease: false
186
186
  version_requirements: !ruby/object:Gem::Requirement
187
187
  requirements:
188
188
  - - "~>"
189
189
  - !ruby/object:Gem::Version
190
- version: 2.1.1
190
+ version: '0.3'
191
191
  - !ruby/object:Gem::Dependency
192
192
  name: sqlite3
193
193
  requirement: !ruby/object:Gem::Requirement
@@ -202,6 +202,20 @@ dependencies:
202
202
  - - "~>"
203
203
  - !ruby/object:Gem::Version
204
204
  version: 1.3.6
205
+ - !ruby/object:Gem::Dependency
206
+ name: redcarpet
207
+ requirement: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - "~>"
210
+ - !ruby/object:Gem::Version
211
+ version: 2.1.1
212
+ type: :development
213
+ prerelease: false
214
+ version_requirements: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - "~>"
217
+ - !ruby/object:Gem::Version
218
+ version: 2.1.1
205
219
  description: "\n Map query params to model scopes, pairing
206
220
  one or\n more keys to a scope. Parameters can be required,
207
221
  or\n whitelisted providing fine tuned control over how\n
@@ -229,9 +243,12 @@ files:
229
243
  - gemfiles/3.2.gemfile.lock
230
244
  - gemfiles/4.0.gemfile
231
245
  - gemfiles/4.0.gemfile.lock
246
+ - gemfiles/4.1.gemfile
247
+ - gemfiles/4.1.gemfile.lock
232
248
  - lib/paraphrase.rb
233
249
  - lib/paraphrase/active_model.rb
234
250
  - lib/paraphrase/errors.rb
251
+ - lib/paraphrase/params.rb
235
252
  - lib/paraphrase/query.rb
236
253
  - lib/paraphrase/rails.rb
237
254
  - lib/paraphrase/scope.rb