recordselect 4.1.0 → 4.1.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/README +27 -0
- data/lib/record_select/conditions.rb +7 -1
- data/lib/record_select/config.rb +25 -4
- data/lib/record_select/version.rb +1 -1
- data/lib/record_select.rb +15 -1
- data/test/test_configuration.rb +198 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9004f2682a4156b93ecbadc269e53a2c8c1c7b03ca2f22b80f0ff40e727e3c69
|
|
4
|
+
data.tar.gz: 1e33c470ca134cd5338025144408eeea80650969b3a321308428b6405f142016
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a43027fd27673c1a0887c334dbba2448fad6590c440bed5496873b8449a130e1fbb77ad54f68c94721fc096e4d661bff0fa0a4203cacaacb1ed40d2fe4f05e65
|
|
7
|
+
data.tar.gz: 243ea04d2e3b29a937f3cea800743fa2e0cbf5257d3f7147aa6bf8b30ed4eb9f6a604a9f4c1b5dcd162db237d265dca7911f9855106ed6e2f0efd95c9b6c64ce
|
data/README
CHANGED
|
@@ -43,6 +43,33 @@ import 'record_select'
|
|
|
43
43
|
|
|
44
44
|
Either using assets pipeline or importmap, it will be loaded when requiring or importing active_scaffold, so no need to add it to application.js, importmap.rb or manifest.js
|
|
45
45
|
|
|
46
|
+
= CONTROLLER CONFIGURATION
|
|
47
|
+
|
|
48
|
+
Every controller must explicitly supply +search_on+. There is no automatic search across model columns:
|
|
49
|
+
|
|
50
|
+
```rb
|
|
51
|
+
record_select search_on: [:name, :email]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Use +search_on: []+ for a custom search implementation or when assigning search fields later. With an empty array, the built-in text search adds no conditions.
|
|
55
|
+
|
|
56
|
+
URL parameters matching model columns can also filter the records. Limit these separately from text search using global defaults, for example in an initializer:
|
|
57
|
+
|
|
58
|
+
```rb
|
|
59
|
+
RecordSelect.allowed_columns = [:id, :company_id]
|
|
60
|
+
RecordSelect.excluded_columns = [:password_digest, :reset_password_token]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Global defaults are captured and normalized when each controller calls +record_select+. Set them before controller configuration; later global changes do not affect existing configurations.
|
|
64
|
+
|
|
65
|
+
Override either default for a controller:
|
|
66
|
+
|
|
67
|
+
```rb
|
|
68
|
+
record_select search_on: [:name], allowed_columns: [:company_id, :active], excluded_columns: [:active]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Exclusions take precedence over allowed columns. Symbols and strings are accepted. In 4.1.1, both settings default to +nil+, preserving filtering on all model columns with a deprecation warning when a column filter is used. The warning is emitted on every request that applies automatic URL column filters. In 4.2.0, +allowed_columns+ will default to +[]+, disabling automatic URL column filtering. An empty allowed list disables all automatic URL column filters. Controller options replace the corresponding global list; explicit +allowed_columns: nil+ allows all model columns and +excluded_columns: []+ clears global exclusions for that controller. To explicitly retain unrestricted filtering without a warning, set +allowed_columns: nil, excluded_columns: []+ (or the corresponding global settings). These settings do not restrict the trusted SQL expressions in +search_on+ or custom controller conditions.
|
|
72
|
+
|
|
46
73
|
= CALLBACKS
|
|
47
74
|
|
|
48
75
|
Register callbacks in a JavaScript file and pass their names to the helpers:
|
|
@@ -54,6 +54,8 @@ module RecordSelect
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
def build_record_select_conditions(tokens, operator, search_pattern)
|
|
57
|
+
return if record_select_config.search_on.empty?
|
|
58
|
+
|
|
57
59
|
where_clauses = record_select_config.search_on.collect { |sql| "#{sql} #{operator} ?" }
|
|
58
60
|
phrase = "(#{where_clauses.join(' OR ')})"
|
|
59
61
|
sql = ([phrase] * tokens.length).join(' AND ')
|
|
@@ -68,9 +70,13 @@ module RecordSelect
|
|
|
68
70
|
ignored_columns = %w[controller action page search update]
|
|
69
71
|
params.each do |field, value|
|
|
70
72
|
next if field.in? ignored_columns
|
|
73
|
+
next unless record_select_config.column_allowed?(field)
|
|
71
74
|
column = record_select_config.model.columns_hash[field]
|
|
72
|
-
|
|
75
|
+
next unless column
|
|
76
|
+
|
|
77
|
+
conditions << record_select_condition_for_column(column, value)
|
|
73
78
|
end
|
|
79
|
+
record_select_config.warn_unconfigured_column_filters unless conditions.empty?
|
|
74
80
|
conditions
|
|
75
81
|
end
|
|
76
82
|
|
data/lib/record_select/config.rb
CHANGED
|
@@ -4,10 +4,16 @@ module RecordSelect
|
|
|
4
4
|
# a write-once configuration object
|
|
5
5
|
class Config
|
|
6
6
|
def initialize(klass, options = {})
|
|
7
|
+
raise ArgumentError, 'record_select requires :search_on (use [] for custom or deferred searches)' if options[:search_on].nil?
|
|
8
|
+
|
|
7
9
|
@klass = klass
|
|
8
10
|
@notify = block_given? ? proc : options[:notify]
|
|
9
11
|
@per_page = options[:per_page]
|
|
10
|
-
@search_on = [options[:search_on]].flatten
|
|
12
|
+
@search_on = [options[:search_on]].flatten
|
|
13
|
+
allowed_columns = options.fetch(:allowed_columns) { RecordSelect.allowed_columns }
|
|
14
|
+
excluded_columns = options.fetch(:excluded_columns) { RecordSelect.excluded_columns }
|
|
15
|
+
@allowed_columns = Array(allowed_columns).map(&:to_s) unless allowed_columns.nil?
|
|
16
|
+
@excluded_columns = Array(excluded_columns).map(&:to_s) unless excluded_columns.nil?
|
|
11
17
|
@order_by = options[:order_by]
|
|
12
18
|
@full_text_search = options[:full_text_search]
|
|
13
19
|
@label = options[:label]
|
|
@@ -18,7 +24,7 @@ module RecordSelect
|
|
|
18
24
|
@toggle_search_mode = options[:toggle_search_mode]
|
|
19
25
|
end
|
|
20
26
|
|
|
21
|
-
attr_reader :include, :joins, :left_joins
|
|
27
|
+
attr_reader :include, :joins, :left_joins, :allowed_columns, :excluded_columns
|
|
22
28
|
|
|
23
29
|
def pagination?
|
|
24
30
|
@pagination
|
|
@@ -42,8 +48,23 @@ module RecordSelect
|
|
|
42
48
|
|
|
43
49
|
# A collection of fields to search. This is essentially raw SQL, so you could search on "CONCAT(first_name, ' ', last_name)" if you wanted to.
|
|
44
50
|
# NOTE: this does *NO* default transforms (such as LOWER()), that's left entirely up to you.
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
attr_reader :search_on
|
|
52
|
+
|
|
53
|
+
def warn_unconfigured_column_filters
|
|
54
|
+
return unless allowed_columns.nil? && excluded_columns.nil?
|
|
55
|
+
|
|
56
|
+
RecordSelect.deprecator.warn(
|
|
57
|
+
"Automatic URL filtering on all model columns is deprecated and can expose hidden values. " \
|
|
58
|
+
"Configure allowed_columns to list permitted filter columns, or [] to disable URL column filtering. " \
|
|
59
|
+
"Set allowed_columns: nil and excluded_columns: [] to explicitly retain unrestricted filtering " \
|
|
60
|
+
"without this warning. Automatic URL column filtering will be disabled by default in RecordSelect 4.2.0."
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def column_allowed?(name)
|
|
65
|
+
name = name.to_s
|
|
66
|
+
allowed = allowed_columns
|
|
67
|
+
(allowed.nil? || allowed.include?(name)) && (excluded_columns.nil? || !excluded_columns.include?(name))
|
|
47
68
|
end
|
|
48
69
|
|
|
49
70
|
def order_by
|
data/lib/record_select.rb
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'active_support/deprecation'
|
|
4
|
+
|
|
3
5
|
module RecordSelect
|
|
6
|
+
class << self
|
|
7
|
+
attr_accessor :allowed_columns, :excluded_columns
|
|
8
|
+
end
|
|
9
|
+
self.allowed_columns = nil
|
|
10
|
+
self.excluded_columns = nil
|
|
11
|
+
|
|
12
|
+
def self.deprecator
|
|
13
|
+
@deprecator ||= ActiveSupport::Deprecation.new('4.2.0', 'RecordSelect')
|
|
14
|
+
end
|
|
15
|
+
|
|
4
16
|
def self.callback_registry?
|
|
5
17
|
true
|
|
6
18
|
end
|
|
@@ -17,7 +29,9 @@ module RecordSelect
|
|
|
17
29
|
# +per_page+:: records to show per page when browsing
|
|
18
30
|
# +notify+:: a method name to invoke when a record has been selected.
|
|
19
31
|
# +order_by+:: a SQL string to order the search results
|
|
20
|
-
# +search_on+::
|
|
32
|
+
# +search_on+:: required searchable field or array of fields; [] disables the built-in text search
|
|
33
|
+
# +allowed_columns+:: URL parameter filter columns; nil allows all model columns. Overrides RecordSelect.allowed_columns.
|
|
34
|
+
# +excluded_columns+:: URL parameter filter columns to ignore, even if allowed. Overrides RecordSelect.excluded_columns.
|
|
21
35
|
# +full_text_search+:: a boolean for whether to use a %?% search pattern or not. default is false.
|
|
22
36
|
# +label+:: a proc that accepts a record as argument and returns an option label. default is to call record.to_label instead.
|
|
23
37
|
# +include+:: as for ActiveRecord::Base#find. can help with search conditions or just help optimize rendering the results.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
require 'minitest/autorun'
|
|
5
|
+
require 'active_support'
|
|
6
|
+
require 'active_support/test_case'
|
|
7
|
+
require 'active_support/core_ext/object/inclusion'
|
|
8
|
+
require 'record_select'
|
|
9
|
+
require 'record_select/config'
|
|
10
|
+
require 'record_select/conditions'
|
|
11
|
+
|
|
12
|
+
class RecordSelectConfigurationTest < ActiveSupport::TestCase
|
|
13
|
+
class FilterController
|
|
14
|
+
include RecordSelect::Conditions
|
|
15
|
+
attr_accessor :params, :record_select_config
|
|
16
|
+
|
|
17
|
+
def filters
|
|
18
|
+
record_select_conditions_from_params
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def text_conditions
|
|
22
|
+
build_record_select_conditions(['alice'], 'LIKE', '?%')
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def record_select_condition_for_column(column, value)
|
|
26
|
+
{column.name => value}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def setup
|
|
31
|
+
@allowed_columns = RecordSelect.allowed_columns
|
|
32
|
+
@excluded_columns = RecordSelect.excluded_columns
|
|
33
|
+
RecordSelect.allowed_columns = nil
|
|
34
|
+
RecordSelect.excluded_columns = []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def teardown
|
|
38
|
+
RecordSelect.allowed_columns = @allowed_columns
|
|
39
|
+
RecordSelect.excluded_columns = @excluded_columns
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def config(options = {})
|
|
43
|
+
RecordSelect::Config.new('user', {search_on: [:name]}.merge(options))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def controller(options = {})
|
|
47
|
+
instance = FilterController.new
|
|
48
|
+
instance.record_select_config = config(options)
|
|
49
|
+
columns = %w[id name company_id password_digest action].to_h { |name| [name, Struct.new(:name).new(name)] }
|
|
50
|
+
model = Struct.new(:columns_hash).new(columns)
|
|
51
|
+
instance.record_select_config.define_singleton_method(:model) { model }
|
|
52
|
+
instance.params = {'id' => '1', 'company_id' => '2', 'password_digest' => 'secret', 'action' => 'browse', 'unknown' => 'ignored'}
|
|
53
|
+
instance
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_search_on_is_required
|
|
57
|
+
error = assert_raise(ArgumentError) { RecordSelect::Config.new('user') }
|
|
58
|
+
assert_match(/requires :search_on/, error.message)
|
|
59
|
+
assert_raise(ArgumentError) { config(search_on: nil) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_search_on_accepts_a_field_or_an_explicit_empty_array
|
|
63
|
+
assert_equal [:name], config(search_on: :name).search_on
|
|
64
|
+
assert_equal [], config(search_on: []).search_on
|
|
65
|
+
assert_nil controller(search_on: []).text_conditions
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_text_search_preserves_explicit_fields
|
|
69
|
+
assert_equal ['(name LIKE ? OR email LIKE ?)', 'alice%', 'alice%'], controller(search_on: [:name, :email]).text_conditions
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_default_filters_preserve_model_columns_and_ignore_reserved_and_unknown_parameters
|
|
73
|
+
assert_equal [{'id' => '1'}, {'company_id' => '2'}, {'password_digest' => 'secret'}], controller.filters
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_global_allowed_columns_limit_filters
|
|
77
|
+
RecordSelect.allowed_columns = [:company_id]
|
|
78
|
+
assert_equal [{'company_id' => '2'}], controller.filters
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def test_global_exclusions_limit_filters
|
|
82
|
+
RecordSelect.excluded_columns = [:password_digest, 'id']
|
|
83
|
+
assert_equal [{'company_id' => '2'}], controller.filters
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_controller_options_override_global_lists_and_exclusions_win
|
|
87
|
+
RecordSelect.allowed_columns = [:password_digest]
|
|
88
|
+
RecordSelect.excluded_columns = [:company_id]
|
|
89
|
+
instance = controller(allowed_columns: [:id, 'company_id'], excluded_columns: ['id'])
|
|
90
|
+
assert_equal [{'company_id' => '2'}], instance.filters
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_overriding_one_list_preserves_the_other_global_default
|
|
94
|
+
RecordSelect.excluded_columns = [:id]
|
|
95
|
+
assert_equal [{'company_id' => '2'}], controller(allowed_columns: [:id, :company_id]).filters
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def test_explicit_nil_and_empty_exclusions_clear_global_restrictions
|
|
99
|
+
RecordSelect.allowed_columns = []
|
|
100
|
+
RecordSelect.excluded_columns = [:company_id]
|
|
101
|
+
assert_equal [{'id' => '1'}, {'company_id' => '2'}, {'password_digest' => 'secret'}], controller(allowed_columns: nil, excluded_columns: []).filters
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_empty_allowed_columns_disable_parameter_filters
|
|
105
|
+
assert_equal [], controller(allowed_columns: []).filters
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_defaults_leave_both_column_policies_unconfigured
|
|
109
|
+
assert_nil @allowed_columns
|
|
110
|
+
assert_nil @excluded_columns
|
|
111
|
+
RecordSelect.excluded_columns = nil
|
|
112
|
+
assert_nil config.allowed_columns
|
|
113
|
+
assert_nil config.excluded_columns
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_explicit_nil_exclusions_override_global_exclusions_and_warn
|
|
117
|
+
RecordSelect.excluded_columns = [:password_digest]
|
|
118
|
+
instance = controller(excluded_columns: nil)
|
|
119
|
+
warnings = capture_filter_warnings { assert_equal 3, instance.filters.length }
|
|
120
|
+
assert_equal 1, warnings.length
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def test_unconfigured_filters_warn_each_time_filters_are_built
|
|
124
|
+
RecordSelect.excluded_columns = nil
|
|
125
|
+
instance = controller
|
|
126
|
+
warnings = capture_filter_warnings do
|
|
127
|
+
assert_equal 3, instance.filters.length
|
|
128
|
+
instance.filters
|
|
129
|
+
end
|
|
130
|
+
assert_equal 2, warnings.length
|
|
131
|
+
assert_match(/allowed_columns/, warnings.first)
|
|
132
|
+
assert_match(/excluded_columns/, warnings.first)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def test_unconfigured_filters_do_not_warn_for_reserved_or_unknown_parameters
|
|
136
|
+
RecordSelect.excluded_columns = nil
|
|
137
|
+
instance = controller
|
|
138
|
+
instance.params = {'action' => 'browse', 'unknown' => 'ignored'}
|
|
139
|
+
assert_empty capture_filter_warnings { assert_equal [], instance.filters }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def test_explicit_filter_policies_do_not_warn
|
|
143
|
+
RecordSelect.excluded_columns = nil
|
|
144
|
+
[{allowed_columns: []}, {allowed_columns: [:company_id]},
|
|
145
|
+
{excluded_columns: []}, {excluded_columns: [:password_digest]}].each do |options|
|
|
146
|
+
assert_empty capture_filter_warnings { controller(options).filters }
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def test_global_explicit_compatibility_does_not_warn
|
|
151
|
+
RecordSelect.allowed_columns = nil
|
|
152
|
+
RecordSelect.excluded_columns = []
|
|
153
|
+
assert_empty capture_filter_warnings { assert_equal 3, controller.filters.length }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def test_global_settings_are_captured_when_the_controller_is_configured
|
|
157
|
+
RecordSelect.allowed_columns = [:id, :company_id]
|
|
158
|
+
RecordSelect.excluded_columns = :id
|
|
159
|
+
instance = controller
|
|
160
|
+
RecordSelect.allowed_columns = []
|
|
161
|
+
RecordSelect.excluded_columns = [:company_id]
|
|
162
|
+
|
|
163
|
+
assert_equal ['id', 'company_id'], instance.record_select_config.allowed_columns
|
|
164
|
+
assert_equal ['id'], instance.record_select_config.excluded_columns
|
|
165
|
+
assert_equal [{'company_id' => '2'}], instance.filters
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def test_normalized_settings_do_not_share_the_source_arrays
|
|
169
|
+
allowed = [:id, 'company_id']
|
|
170
|
+
excluded = [:id]
|
|
171
|
+
instance = controller(allowed_columns: allowed, excluded_columns: excluded)
|
|
172
|
+
allowed.clear
|
|
173
|
+
excluded << :company_id
|
|
174
|
+
|
|
175
|
+
assert_equal [{'company_id' => '2'}], instance.filters
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def test_parameter_restrictions_do_not_limit_text_search
|
|
179
|
+
instance = controller(allowed_columns: [], excluded_columns: [:name])
|
|
180
|
+
assert_equal ['(name LIKE ?)', 'alice%'], instance.text_conditions
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
private
|
|
184
|
+
|
|
185
|
+
def capture_filter_warnings
|
|
186
|
+
deprecator = RecordSelect.deprecator
|
|
187
|
+
original_behavior = deprecator.behavior
|
|
188
|
+
original_silenced = deprecator.silenced
|
|
189
|
+
warnings = []
|
|
190
|
+
deprecator.silenced = false
|
|
191
|
+
deprecator.behavior = proc { |message, *| warnings << message }
|
|
192
|
+
yield
|
|
193
|
+
warnings
|
|
194
|
+
ensure
|
|
195
|
+
deprecator.behavior = original_behavior
|
|
196
|
+
deprecator.silenced = original_silenced
|
|
197
|
+
end
|
|
198
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: recordselect
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.1.
|
|
4
|
+
version: 4.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sergio Cambra
|
|
@@ -62,6 +62,7 @@ files:
|
|
|
62
62
|
- lib/record_select/version.rb
|
|
63
63
|
- lib/recordselect.rb
|
|
64
64
|
- test/recordselect_test.rb
|
|
65
|
+
- test/test_configuration.rb
|
|
65
66
|
- vendor/assets/javascripts/jquery.visible.min.js
|
|
66
67
|
homepage: http://github.com/scambra/recordselect
|
|
67
68
|
licenses:
|
|
@@ -86,3 +87,4 @@ specification_version: 4
|
|
|
86
87
|
summary: RecordSelect widget as a replacement for massive drop down lists
|
|
87
88
|
test_files:
|
|
88
89
|
- test/recordselect_test.rb
|
|
90
|
+
- test/test_configuration.rb
|