brita 0.9.3 → 0.10.0

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: f5bb6a00b2f18b8868b43eb4b22803ed179bc63c
4
- data.tar.gz: ff737c18910108bc92517f19f0b7a3fdc14aa0e7
3
+ metadata.gz: 0033a36642e9f1880486826abbdd52952eea56bc
4
+ data.tar.gz: 982111b429409984ed67b8a141658f7ef6f43e2c
5
5
  SHA512:
6
- metadata.gz: fc412bdc2c69093bf76631f2498949c60a7d377640bdbc06bba4d6ae91f77ce97029c9a892aa295361ddd999badc6c63dfff1ebfd69f54e35b0552a4d1c70f10
7
- data.tar.gz: 30830b49f18d75cac7d2249fb9a09e671c4caa1b38ecf3f1a73e3b7514cc2f7049b4ebf77e7f240b147a6ea1d5206f60aa49a9fef4ef29190bbbb78874d8cb5f
6
+ metadata.gz: 12d55c83c7311586ebfcc2c8d7b0e4ce79b3e9bd0a172f8deeaa59b81430ff4db5e2f51aff4447368b74ecbbc36c5c2a7f7303e33e493c7e20c3f161afb349ea
7
+ data.tar.gz: 6c804bfa625e895ee3107bea30fa93441405ad29ff2aab523e260c13e85c519afaea64fc0294a742c35b0d58f1abd2b9c9894247e3a9a3422b580b625ae3bc5d
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/procore/brita.svg?branch=master)](https://travis-ci.org/procore/brita)
4
4
 
5
- A tool to build your own filters!
5
+ A tool to build your own filters and sorts with Rails and Active Record!
6
6
 
7
7
  ## Developer Usage
8
8
  Include Brita in your controllers, and define some filters.
@@ -109,6 +109,48 @@ class PostsController < ApplicationController
109
109
  end
110
110
  ```
111
111
 
112
+ ### Filter on Ranges
113
+ Some parameter types support ranges. Ranges are expected to be a string with the bounding values separated by `...`
114
+
115
+ For example `?filters[price]=3...50` would return records with a price between 3 and 50.
116
+
117
+ The following types support ranges
118
+ * int
119
+ * decimal
120
+ * boolean
121
+ * date
122
+ * time
123
+ * datetime
124
+
125
+ ### Filter on JSON Array
126
+ `int` type filters support sending the values as an array in the URL Query parameters. For example `?filters[id]=[1,2]`. This is a way to keep payloads smaller for GET requests. When URI encoded this will become `filters%5Bid%5D=%5B1,2%5D` which is much smaller the standard format of `filters%5Bid%5D%5B%5D=1&&filters%5Bid%5D%5B%5D=2`.
127
+
128
+ On the server side, the params will be received as:
129
+ ```ruby
130
+ # JSON array encoded result
131
+ "filters"=>{"id"=>"[1,2]"}
132
+
133
+ # standard array format
134
+ "filters"=>{"id"=>["1", "2"]}
135
+ ```
136
+
137
+ Note that this feature cannot currently be wrapped in an array and should not be used in combination with sending array parameters individually.
138
+ * `?filters[id][]=[1,2]` => invalid
139
+ * `?filters[id][]=[1,2]&filters[id][]=3` => invalid
140
+ * `?filters[id]=[1,2]&filters[id]=3` => valid but only 3 is passed through to the server
141
+ * `?filters[id]=[1,2]` => valid
142
+
143
+ #### A note on encoding for JSON Array feature
144
+ JSON arrays contain the reserved characters "`,`" and "`[`" and "`]`". When encoding a JSON array in the URL there are two different ways to handle the encoding. Both ways are supported by Rails.
145
+ For example, lets look at the following filter with a JSON array `?filters[id]=[1,2]`:
146
+ * `?filters%5Bid%5D=%5B1,2%5D`
147
+ * `?filters%5Bid%5D%3D%5B1%2C2%5D`
148
+
149
+ In both cases Rails will correctly decode to the expected result of
150
+ ```ruby
151
+ { "filters" => { "id" => "[1,2]" } }
152
+ ```
153
+
112
154
  ### Sort Types
113
155
  Every sort must have a type, so that Brita knows what to do with it. The current valid sort types are:
114
156
  * int - Sort on an integer column
@@ -187,8 +229,33 @@ Or install it yourself as:
187
229
  $ gem install brita
188
230
  ```
189
231
 
232
+ ## Without Rails
233
+
234
+ We have some future plans to remove the rails dependency so that other frameworks such as Sinatra could leverage this gem.
235
+
190
236
  ## Contributing
191
- Contribution directions go here.
237
+
238
+ Running tests:
239
+ ```bash
240
+ $ bundle exec rake test
241
+ ```
192
242
 
193
243
  ## License
194
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
244
+
245
+ The gem is available as open source under the terms of the [MIT
246
+ License](http://opensource.org/licenses/MIT).
247
+
248
+ ## About Procore
249
+
250
+ <img
251
+ src="https://www.procore.com/images/procore_logo.png"
252
+ alt="Procore Logo"
253
+ width="250px"
254
+ />
255
+
256
+ The Procore Gem is maintained by Procore Technologies.
257
+
258
+ Procore - building the software that builds the world.
259
+
260
+ Learn more about the #1 most widely used construction management software at
261
+ [procore.com](https://www.procore.com/)
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  require "bundler/gem_tasks"
8
8
 
9
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
9
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
10
10
  load "rails/tasks/engine.rake"
11
11
 
12
12
  require "rdoc/task"
@@ -5,6 +5,7 @@ require "brita/sort"
5
5
  require "brita/subset_comparator"
6
6
  require "brita/type_validator"
7
7
  require "brita/parameter"
8
+ require "brita/value_parser"
8
9
  require "brita/scope_handler"
9
10
  require "brita/where_handler"
10
11
  require "brita/validators/valid_int_validator"
@@ -13,7 +13,7 @@ module Brita
13
13
  raise "unknown filter type: #{type}" unless type_validator.valid_type?
14
14
  end
15
15
 
16
- def validation(_)
16
+ def validation(_sort)
17
17
  type_validator.validate
18
18
  end
19
19
 
@@ -51,6 +51,10 @@ module Brita
51
51
 
52
52
  private
53
53
 
54
+ def parameterize(value)
55
+ ValueParser.new(value: value, options: parameter.parse_options).parse
56
+ end
57
+
54
58
  def not_processable?(value)
55
59
  value.nil? && default.nil?
56
60
  end
@@ -65,20 +69,6 @@ module Brita
65
69
  end
66
70
  end
67
71
 
68
- def parameterize(value)
69
- if supports_ranges? && value.to_s.include?("...")
70
- Range.new(*value.split("..."))
71
- elsif type == :boolean
72
- if Rails.version.starts_with?("5")
73
- ActiveRecord::Type::Boolean.new.cast(value)
74
- else
75
- ActiveRecord::Type::Boolean.new.type_cast_from_user(value)
76
- end
77
- else
78
- value
79
- end
80
- end
81
-
82
72
  def valid_scope_params?(scope_params)
83
73
  scope_params.is_a?(Array) && scope_params.all? { |symbol| symbol.is_a?(Symbol) }
84
74
  end
@@ -9,8 +9,12 @@ module Brita
9
9
  @internal_name = internal_name
10
10
  end
11
11
 
12
- def supports_ranges?
13
- ![:string, :text, :scope].include?(type)
12
+ def parse_options
13
+ {
14
+ supports_boolean: supports_boolean?,
15
+ supports_ranges: supports_ranges?,
16
+ supports_json: supports_json?
17
+ }
14
18
  end
15
19
 
16
20
  def handler
@@ -20,5 +24,19 @@ module Brita
20
24
  WhereHandler.new(self)
21
25
  end
22
26
  end
27
+
28
+ private
29
+
30
+ def supports_ranges?
31
+ ![:string, :text, :scope].include?(type)
32
+ end
33
+
34
+ def supports_json?
35
+ type == :int
36
+ end
37
+
38
+ def supports_boolean?
39
+ type == :boolean
40
+ end
23
41
  end
24
42
  end
@@ -63,7 +63,7 @@ module Brita
63
63
  def validation(sort)
64
64
  {
65
65
  inclusion: { in: SubsetComparator.new(sort) },
66
- allow_nil: true,
66
+ allow_nil: true
67
67
  }
68
68
  end
69
69
 
@@ -11,6 +11,10 @@ class ValidIntValidator < ActiveModel::EachValidator
11
11
  end
12
12
 
13
13
  def integer_array?(value)
14
+ if value.is_a?(String)
15
+ value = Brita::ValueParser.new(value: value).array_from_json
16
+ end
17
+
14
18
  value.is_a?(Array) && value.any? && value.all? { |v| integer_or_range?(v) }
15
19
  end
16
20
 
@@ -0,0 +1,61 @@
1
+ module Brita
2
+ class ValueParser
3
+ attr_accessor :value, :supports_boolean, :supports_json, :supports_ranges
4
+ def initialize(value:, options: {})
5
+ @value = value
6
+ @supports_boolean = options.fetch(:supports_boolean, false)
7
+ @supports_ranges = options.fetch(:supports_ranges, false)
8
+ @supports_json = options.fetch(:supports_json, false)
9
+ end
10
+
11
+ def parse
12
+ @_result ||=
13
+ if parse_as_range?
14
+ range_value
15
+ elsif parse_as_boolean?
16
+ boolean_value
17
+ elsif parse_as_json?
18
+ array_from_json
19
+ else
20
+ value
21
+ end
22
+ end
23
+
24
+ def array_from_json
25
+ result = JSON.parse(value)
26
+ if result.is_a?(Array)
27
+ result
28
+ else
29
+ value
30
+ end
31
+ rescue JSON::ParserError
32
+ value
33
+ end
34
+
35
+ private
36
+
37
+ def parse_as_range?
38
+ supports_ranges && value.to_s.include?("...")
39
+ end
40
+
41
+ def range_value
42
+ Range.new(*value.split("..."))
43
+ end
44
+
45
+ def parse_as_json?
46
+ supports_json && value.is_a?(String)
47
+ end
48
+
49
+ def parse_as_boolean?
50
+ supports_boolean
51
+ end
52
+
53
+ def boolean_value
54
+ if Rails.version.starts_with?("5")
55
+ ActiveRecord::Type::Boolean.new.cast(value)
56
+ else
57
+ ActiveRecord::Type::Boolean.new.type_cast_from_user(value)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module Brita
2
- VERSION = "0.9.3".freeze
2
+ VERSION = "0.10.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brita
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Procore Technologies
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-26 00:00:00.000000000 Z
11
+ date: 2018-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +114,7 @@ files:
100
114
  - lib/brita/subset_comparator.rb
101
115
  - lib/brita/type_validator.rb
102
116
  - lib/brita/validators/valid_int_validator.rb
117
+ - lib/brita/value_parser.rb
103
118
  - lib/brita/version.rb
104
119
  - lib/brita/where_handler.rb
105
120
  - lib/tasks/filterable_tasks.rake
@@ -123,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
138
  version: '0'
124
139
  requirements: []
125
140
  rubyforge_project:
126
- rubygems_version: 2.6.13
141
+ rubygems_version: 2.5.1
127
142
  signing_key:
128
143
  specification_version: 4
129
144
  summary: Summary of Brita.