filterrific 2.0.0 → 2.0.1
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/CHANGELOG.md +4 -0
- data/README.md +2 -1
- data/lib/filterrific/action_controller_extension.rb +22 -10
- data/lib/filterrific/version.rb +1 -1
- data/spec/filterrific/active_record_extension_spec.rb +12 -12
- data/spec/filterrific/param_set_spec.rb +12 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11a56a1d6587f1b007103d3f23906fe5cda61466
|
4
|
+
data.tar.gz: 7f33fc8e000fe7f7969cc6b8dfd057e6364c0518
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 812ea2580b378e23db574823a3fa561d116773392698a7b5fa51e3bbbc90a7053fe000dc98622e15666d85b6e81a11e4560b08d578e7449e84a3d1de50a2728e
|
7
|
+
data.tar.gz: df6c4d694d297697fd3befc2702af6b65094a0631e457f22732cc44e216fb5c98b29c520a1ca23e754917675f46cca349d69ec6bb87f76ababed04791797bd8d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -28,11 +28,12 @@ Every commit to Filterrific is automatically tested against the following scenar
|
|
28
28
|
|
29
29
|
| Rails version | Ruby environments | Database adapters | Build status |
|
30
30
|
|---------------|--------------------------------|------------------------------------|--------------|
|
31
|
+
| Rails 4.2 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[](https://travis-ci.org/jhund/filterrific_demo)|
|
31
32
|
| Rails 4.1 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[](https://travis-ci.org/jhund/filterrific_demo)|
|
32
33
|
| Rails 4.0 | MRI 1.9.3, 2.0.0, 2.1.2, 2.2.0 | mysql, mysql2, postgresql, sqlite3 |[](https://travis-ci.org/jhund/filterrific_demo)|
|
33
34
|
| Rails 3.2 | MRI 1.9.3, 2.0.0, 2.1.2 | mysql, mysql2, postgresql, sqlite3 |[](https://travis-ci.org/jhund/filterrific_demo)|
|
34
35
|
|
35
|
-
Filterrific version 1.
|
36
|
+
Filterrific version 1.x should work on older versions of Rails and Ruby, however
|
36
37
|
the 1.x branch is not supported any more.
|
37
38
|
|
38
39
|
|
@@ -10,7 +10,7 @@ module Filterrific
|
|
10
10
|
# @param model_class [Class]
|
11
11
|
# @param filterrific_params [Hash] typically the Rails request params under
|
12
12
|
# the :filterrific key (params[:filterrific]), however can be any Hash.
|
13
|
-
# @param opts [Hash]
|
13
|
+
# @param opts [Hash, optional]
|
14
14
|
# @option opts [Array<String>, optional] :available_filters
|
15
15
|
# further restrict which of the filters specified in the model are
|
16
16
|
# available in this context.
|
@@ -24,8 +24,10 @@ module Filterrific
|
|
24
24
|
# these are available in the view to populate select lists and other
|
25
25
|
# dynamic values.
|
26
26
|
# @return [Filterrific::ParamSet]
|
27
|
-
def initialize_filterrific(model_class, filterrific_params, opts)
|
28
|
-
|
27
|
+
def initialize_filterrific(model_class, filterrific_params, opts = {})
|
28
|
+
# We used #deep_stringify_keys, however that breaks on Rails 3.x, so we
|
29
|
+
# went back to #stringify_keys which should be sufficient.
|
30
|
+
f_params = (filterrific_params || {}).stringify_keys
|
29
31
|
opts = opts.stringify_keys
|
30
32
|
pi = opts['persistence_id'] || compute_default_persistence_id
|
31
33
|
|
@@ -35,13 +37,7 @@ module Filterrific
|
|
35
37
|
redirect_to url_for({}) and return false # works with `or return` in calling action.
|
36
38
|
end
|
37
39
|
|
38
|
-
f_params = f_params
|
39
|
-
session[pi].presence || # then try session persisted params
|
40
|
-
opts['default_filter_params'] || # then use passed in opts
|
41
|
-
model_class.filterrific_default_filter_params # finally use model_class defaults
|
42
|
-
|
43
|
-
f_params.deep_stringify_keys!
|
44
|
-
f_params.slice!(opts['available_filters'].map(&:to_s)) if opts['available_filters']
|
40
|
+
f_params = compute_filterrific_params(model_class, f_params, opts)
|
45
41
|
|
46
42
|
filterrific = Filterrific::ParamSet.new(model_class, f_params)
|
47
43
|
filterrific.select_options = opts['select_options']
|
@@ -54,5 +50,21 @@ module Filterrific
|
|
54
50
|
[controller_name, action_name].join('#')
|
55
51
|
end
|
56
52
|
|
53
|
+
# Computes filterrific params using a number of strategies. Limits params
|
54
|
+
# to 'available_filters' if given via opts.
|
55
|
+
# @param model_class [ActiveRecord::Base]
|
56
|
+
# @param filterrific_params [Hash]
|
57
|
+
# @param opts [Hash]
|
58
|
+
def compute_filterrific_params(model_class, filterrific_params, opts)
|
59
|
+
r = (
|
60
|
+
filterrific_params.presence || # start with passed in params
|
61
|
+
session[pi].presence || # then try session persisted params
|
62
|
+
opts['default_filter_params'] || # then use passed in opts
|
63
|
+
model_class.filterrific_default_filter_params # finally use model_class defaults
|
64
|
+
).stringify_keys
|
65
|
+
r.slice!(opts['available_filters'].map(&:to_s)) if opts['available_filters']
|
66
|
+
r
|
67
|
+
end
|
68
|
+
|
57
69
|
end
|
58
70
|
end
|
data/lib/filterrific/version.rb
CHANGED
@@ -9,11 +9,11 @@ module Filterrific
|
|
9
9
|
# Container for test data
|
10
10
|
class TestData
|
11
11
|
|
12
|
-
def self.
|
13
|
-
%w[sorted_by
|
12
|
+
def self.filterrific_available_filters
|
13
|
+
%w[search_query sorted_by with_country_id]
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
16
|
+
def self.filterrific_default_filter_params
|
17
17
|
{ 'sorted_by' => 'name_asc' }
|
18
18
|
end
|
19
19
|
|
@@ -24,8 +24,8 @@ module Filterrific
|
|
24
24
|
let(:filterrific_class){
|
25
25
|
Class.new(ActiveRecord::Base) do
|
26
26
|
filterrific(
|
27
|
-
:
|
28
|
-
:
|
27
|
+
available_filters: TestData.filterrific_available_filters,
|
28
|
+
default_filter_params: TestData.filterrific_default_filter_params
|
29
29
|
)
|
30
30
|
end
|
31
31
|
}
|
@@ -44,19 +44,19 @@ module Filterrific
|
|
44
44
|
|
45
45
|
describe "Filterrific initialization" do
|
46
46
|
|
47
|
-
it "initializes
|
48
|
-
filterrific_class.
|
47
|
+
it "initializes filterrific_available_filters" do
|
48
|
+
filterrific_class.filterrific_available_filters.must_equal(TestData.filterrific_available_filters)
|
49
49
|
end
|
50
50
|
|
51
|
-
it "initializes
|
52
|
-
filterrific_class.
|
51
|
+
it "initializes filterrific_default_filter_params" do
|
52
|
+
filterrific_class.filterrific_default_filter_params.must_equal(TestData.filterrific_default_filter_params)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "raises when no filter_names are given" do
|
56
56
|
proc {
|
57
57
|
Class.new(ActiveRecord::Base) do
|
58
58
|
filterrific(
|
59
|
-
:
|
59
|
+
available_filters: []
|
60
60
|
)
|
61
61
|
end
|
62
62
|
}.must_raise(ArgumentError)
|
@@ -66,8 +66,8 @@ module Filterrific
|
|
66
66
|
proc {
|
67
67
|
Class.new(ActiveRecord::Base) do
|
68
68
|
filterrific(
|
69
|
-
:
|
70
|
-
:
|
69
|
+
available_filters: [:one, :two],
|
70
|
+
default_filter_params:{ three: '' }
|
71
71
|
)
|
72
72
|
end
|
73
73
|
}.must_raise(ArgumentError)
|
@@ -6,17 +6,17 @@ module Filterrific
|
|
6
6
|
# Container for test data
|
7
7
|
class TestData
|
8
8
|
|
9
|
-
def self.
|
9
|
+
def self.filterrific_available_filters
|
10
10
|
%w[
|
11
|
-
filter_proc
|
12
11
|
filter_array_int
|
13
12
|
filter_array_string
|
14
13
|
filter_int
|
14
|
+
filter_proc
|
15
15
|
filter_string
|
16
16
|
]
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.
|
19
|
+
def self.filterrific_default_filter_params
|
20
20
|
{ 'filter_int' => 42 }
|
21
21
|
end
|
22
22
|
|
@@ -32,10 +32,10 @@ module Filterrific
|
|
32
32
|
|
33
33
|
def self.filterrific_params_after_sanitizing
|
34
34
|
{
|
35
|
-
'filter_proc' => 2,
|
36
35
|
'filter_array_int' => [1, 2, 3],
|
37
36
|
'filter_array_string' => %w[one two three],
|
38
37
|
'filter_int' => 42,
|
38
|
+
'filter_proc' => 2,
|
39
39
|
'filter_string' => 'forty-two'
|
40
40
|
}
|
41
41
|
end
|
@@ -43,13 +43,13 @@ module Filterrific
|
|
43
43
|
end
|
44
44
|
|
45
45
|
# Simulates a class that would include the filterrific directive
|
46
|
-
class
|
46
|
+
class ModelClass
|
47
47
|
|
48
|
-
def self.
|
49
|
-
TestData.
|
48
|
+
def self.filterrific_default_filter_params
|
49
|
+
TestData.filterrific_default_filter_params
|
50
50
|
end
|
51
|
-
def self.
|
52
|
-
TestData.
|
51
|
+
def self.filterrific_available_filters
|
52
|
+
TestData.filterrific_available_filters
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
@@ -57,18 +57,18 @@ module Filterrific
|
|
57
57
|
describe ParamSet do
|
58
58
|
|
59
59
|
let(:filterrific_param_set){
|
60
|
-
Filterrific::ParamSet.new(
|
60
|
+
Filterrific::ParamSet.new(ModelClass, TestData.filterrific_params)
|
61
61
|
}
|
62
62
|
|
63
63
|
describe "initialization" do
|
64
64
|
|
65
65
|
it "assigns resource class" do
|
66
|
-
filterrific_param_set.
|
66
|
+
filterrific_param_set.model_class.must_equal(ModelClass)
|
67
67
|
end
|
68
68
|
|
69
69
|
describe "dynamic filter_name attr_accessors" do
|
70
70
|
|
71
|
-
TestData.
|
71
|
+
TestData.filterrific_available_filters.each do |filter_name|
|
72
72
|
|
73
73
|
it "defines a getter for '#{ filter_name }'" do
|
74
74
|
filterrific_param_set.must_respond_to(filter_name)
|