filterrific 2.0.3 → 2.0.4

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: 4b083d6ced83900d819a27094c641df15c8b8eaa
4
- data.tar.gz: b6792d8a295048001d3def4112b6917edb263293
3
+ metadata.gz: 36dc749c095650e566eacee010e3021ee5aad1c3
4
+ data.tar.gz: dedcfb34441effe7dfde8fbb610b0dedcfcab461
5
5
  SHA512:
6
- metadata.gz: c780219e349ac9e7a8aae20db84476c086a1650de029d176268757a5331c3e5924694ff9c856281cc59ed8058d41432f0a675942d2f6b5ac113abf3231310adc
7
- data.tar.gz: 17e8b121fe5e15ec56a4d34dfae5fbd7906ffb5ebdc506c96a3ba357a405fd1c299f55c54c8750a023586c5a91e1874327add1c8b96512fb08df5294d29eb252
6
+ metadata.gz: c92f6d1eae2d2a91ad1b00bc9cfb5c6bc2e84ea0889748042b4b295ede9a9741a14232e565b8ffad66a9ac7fd897f3f5e9e8140762649bbb6f155b99967845d1
7
+ data.tar.gz: caf3aca751fbc4d9dbe894530ac03ef4024d2d69ca1569fa29081ed89ddd361cb3e40c4f09738204a6c0a73a7f6898595242c6475a7d9493eca5338578055e7d
@@ -39,6 +39,16 @@
39
39
  },
40
40
  )
41
41
 
42
+
43
+ ### 2.0.4
44
+
45
+ * Objectify nested params so that they render correctly on the form when
46
+ restored from session or URL params.
47
+ * Don't convert strings that almost look like integers to integers. Example:
48
+ `"0123"` as part of a phone number should not be converted to `123`.
49
+
50
+
51
+
42
52
  ### 2.0.3
43
53
 
44
54
  * Cleaned up obsolete option names
@@ -34,7 +34,7 @@ module Filterrific
34
34
  if (f_params.delete('reset_filterrific'))
35
35
  # Reset query and session_persisted params
36
36
  session[pi] = nil
37
- redirect_to url_for({}) and return false # works with `or return` in calling action.
37
+ redirect_to url_for({}) and return false # requires `or return` in calling action.
38
38
  end
39
39
 
40
40
  f_params = compute_filterrific_params(model_class, f_params, opts, pi)
@@ -13,9 +13,9 @@ module Filterrific
13
13
 
14
14
  # Initializes a new Filterrific::ParamSet. This is the core of Filterrific
15
15
  # where all the action happens.
16
- # @param a_model_class [Class] the class you want to filter records of
17
- # @param filterrific_params [Hash, optional] the filter params, uses
18
- # model_class' default_settings
16
+ # @param a_model_class [Class] the class you want to filter records of.
17
+ # @param filterrific_params [Hash, optional] the filter params, falls back
18
+ # to model_class' default_settings.
19
19
  # @return [Filterrific::ParamSet]
20
20
  def initialize(a_model_class, filterrific_params = {})
21
21
  self.model_class = a_model_class
@@ -41,6 +41,13 @@ module Filterrific
41
41
  model_class.filterrific_find(self)
42
42
  end
43
43
 
44
+ # Returns true if this instance of Filterrific has params other than the
45
+ # defaults.
46
+ # @return [Boolean]
47
+ def has_filters_applied?
48
+ sdf
49
+ end
50
+
44
51
  # Returns Filterrific::ParamSet as hash (used for URL params and serialization)
45
52
  # @return [Hash] with stringified keys
46
53
  def to_hash
@@ -53,6 +60,9 @@ module Filterrific
53
60
  when param_value.is_a?(Proc)
54
61
  # evaluate Proc so it can be serialized
55
62
  h[filter_name] = param_value.call
63
+ when param_value.is_a?(OpenStruct)
64
+ # convert OpenStruct to hash
65
+ h[filter_name] = param_value.marshal_dump
56
66
  else
57
67
  h[filter_name] = param_value
58
68
  end
@@ -69,18 +79,23 @@ module Filterrific
69
79
  protected
70
80
 
71
81
  # Conditions params: Evaluates Procs and type casts integer values.
82
+ # Why are we type casting integers?
72
83
  # @param fp [Hash] the filterrific params hash
73
84
  # @return[Hash] the conditioned params hash
74
85
  def condition_filterrific_params(fp)
75
86
  fp.each do |key, val|
76
87
  case
77
88
  when val.is_a?(Proc)
78
- # evaulate Procs
89
+ # evaluate Procs
79
90
  fp[key] = val.call
80
91
  when val.is_a?(Array)
81
92
  # type cast integers in the array
82
- fp[key] = fp[key].map { |e| e =~ /^\d+$/ ? e.to_i : e }
83
- when val =~ /^\d+$/
93
+ fp[key] = fp[key].map { |e| e =~ /^[1-9]\d*$/ ? e.to_i : e }
94
+ when val.is_a?(Hash)
95
+ # type cast Hash to OpenStruct so that nested params render correctly
96
+ # in the form
97
+ fp[key] = OpenStruct.new(fp[key])
98
+ when val =~ /^[1-9]\d*$/
84
99
  # type cast integer
85
100
  fp[key] = fp[key].to_i
86
101
  end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Filterrific
4
- VERSION = "2.0.3"
4
+ VERSION = "2.0.4"
5
5
  end
@@ -8,8 +8,10 @@ module Filterrific
8
8
 
9
9
  def self.filterrific_available_filters
10
10
  %w[
11
+ filter_almost_int
11
12
  filter_array_int
12
13
  filter_array_string
14
+ filter_hash
13
15
  filter_int
14
16
  filter_proc
15
17
  filter_string
@@ -22,21 +24,37 @@ module Filterrific
22
24
 
23
25
  def self.filterrific_params
24
26
  {
25
- 'filter_proc' => lambda { 1 + 1 },
27
+ 'filter_almost_int' => '042',
26
28
  'filter_array_int' => %w[1 2 3],
27
29
  'filter_array_string' => %w[one two three],
30
+ 'filter_hash' => { a: 1, b: 2 },
28
31
  'filter_int' => '42',
29
- 'filter_string' => 'forty-two'
32
+ 'filter_proc' => lambda { 1 + 1 },
33
+ 'filter_string' => 'forty-two',
30
34
  }
31
35
  end
32
36
 
33
- def self.filterrific_params_after_sanitizing
37
+ def self.filterrific_params_after_conditioning
34
38
  {
39
+ 'filter_almost_int' => '042',
35
40
  'filter_array_int' => [1, 2, 3],
36
41
  'filter_array_string' => %w[one two three],
42
+ 'filter_hash' => OpenStruct.new(a: 1, b: 2),
37
43
  'filter_int' => 42,
38
44
  'filter_proc' => 2,
39
- 'filter_string' => 'forty-two'
45
+ 'filter_string' => 'forty-two',
46
+ }
47
+ end
48
+
49
+ def self.filterrific_params_as_hash
50
+ {
51
+ 'filter_almost_int' => '042',
52
+ 'filter_array_int' => [1, 2, 3],
53
+ 'filter_array_string' => %w[one two three],
54
+ 'filter_hash' => { a: 1, b: 2 },
55
+ 'filter_int' => 42,
56
+ 'filter_proc' => 2,
57
+ 'filter_string' => 'forty-two',
40
58
  }
41
59
  end
42
60
 
@@ -82,8 +100,8 @@ module Filterrific
82
100
 
83
101
  TestData.filterrific_params.keys.each do |key|
84
102
 
85
- it "assigns sanitized param to '#{ key }' attr" do
86
- filterrific_param_set.send(key).must_equal(TestData.filterrific_params_after_sanitizing[key])
103
+ it "assigns conditioned param to '#{ key }' attr" do
104
+ filterrific_param_set.send(key).must_equal(TestData.filterrific_params_after_conditioning[key])
87
105
  end
88
106
 
89
107
  end
@@ -102,7 +120,7 @@ module Filterrific
102
120
 
103
121
  it "returns all filterrific_params as hash" do
104
122
  filterrific_param_set.to_hash.must_equal(
105
- TestData.filterrific_params_after_sanitizing
123
+ TestData.filterrific_params_as_hash
106
124
  )
107
125
  end
108
126
 
@@ -112,7 +130,7 @@ module Filterrific
112
130
 
113
131
  it "returns all filterrific_params as json string" do
114
132
  filterrific_param_set.to_json.must_equal(
115
- TestData.filterrific_params_after_sanitizing.to_json
133
+ TestData.filterrific_params_as_hash.to_json
116
134
  )
117
135
  end
118
136
 
@@ -142,5 +160,26 @@ module Filterrific
142
160
  end
143
161
  end
144
162
 
163
+ describe "#condition_filterrific_params" do
164
+
165
+ [
166
+ [{ a_proc: lambda { 1 + 1 } }, { a_proc: 2 }],
167
+ [{ an_array: [1, 'a'] }, { an_array: [1, 'a'] }],
168
+ [{ a_hash: { 'a' => 1, 'b' => 2 } }, { a_hash: OpenStruct.new({ 'a' => 1, 'b' => 2 }) }],
169
+ [{ a_string_that_looks_like_int: '123' }, { a_string_that_looks_like_int: 123 }],
170
+ [{ a_string_that_almost_looks_like_int: '0123' }, { a_string_that_almost_looks_like_int: '0123' }],
171
+ [{ an_array_with_almost_int: ['0123', '123'] }, { an_array_with_almost_int: ['0123', 123] }],
172
+ [{ a_string: 'abc' }, { a_string: 'abc' }],
173
+ ].each do |test_params, xpect|
174
+ it "Handles #{ test_params.inspect }" do
175
+ filterrific_param_set.send(
176
+ :condition_filterrific_params,
177
+ test_params
178
+ ).must_equal(xpect)
179
+ end
180
+ end
181
+
182
+ end
183
+
145
184
  end
146
185
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterrific
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jo Hund
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails