recordselect 4.1.2 → 4.2.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 +4 -4
- data/README +49 -7
- data/app/views/record_select/_list.html.erb +2 -2
- data/app/views/record_select/_search.html.erb +1 -1
- data/lib/record_select/actions.rb +24 -20
- data/lib/record_select/conditions.rb +35 -24
- data/lib/record_select/config.rb +23 -19
- data/lib/record_select/engine.rb +2 -1
- data/lib/record_select/extensions/active_record.rb +3 -3
- data/lib/record_select/extensions/localization.rb +3 -2
- data/lib/record_select/extensions/routing_mapper.rb +1 -1
- data/lib/record_select/form_builder.rb +5 -5
- data/lib/record_select/helpers/record_select_helper.rb +104 -77
- data/lib/record_select/version.rb +2 -2
- data/lib/record_select.rb +21 -14
- metadata +5 -8
- data/test/recordselect_test.rb +0 -32
- data/test/test_configuration.rb +0 -165
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 47eef2709830ca3ca9a143a394c0ebd565399585008be456a9a866b6b07fc5f7
|
|
4
|
+
data.tar.gz: c9e794ed963a29992694a8275e9e6d96c99eaad933c666522e78d581b5064de6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b8ff74f5c153ea59cae7dc3bf5ca6e48d96a7a2a3eb92f415c2d8f3279c66233dae142fcf76fc65d3ff64fc392737a94109aa6d9950edbf9b6806e22282cf605
|
|
7
|
+
data.tar.gz: 544d9e0a115ddd8abbf610ddfcdfd7b422d3df3a4630c2d368f0e373c45f95c044b18fe6ee523d894d7e7bf56b2e8b022f00c719f8fd9cc33c571bce6476d284
|
data/README
CHANGED
|
@@ -53,6 +53,32 @@ record_select search_on: [:name, :email]
|
|
|
53
53
|
|
|
54
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
55
|
|
|
56
|
+
The text search operator defaults to +ILIKE+ on PostgreSQL and +LIKE+ on other adapters. Set +RecordSelect.like_operator+ in an initializer to override it globally:
|
|
57
|
+
|
|
58
|
+
```rb
|
|
59
|
+
RecordSelect.like_operator = 'LIKE'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Override the global setting per controller with +like_operator+. Either setting accepts a string or a callable that receives each configured +search_on+ entry unchanged and the configured model, and returns its SQL operator:
|
|
63
|
+
|
|
64
|
+
```rb
|
|
65
|
+
record_select search_on: [:name, :email],
|
|
66
|
+
like_operator: ->(column, _model) { column == :email ? 'LIKE' : 'ILIKE' }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A global callable can inspect model column metadata, including collation:
|
|
70
|
+
|
|
71
|
+
```rb
|
|
72
|
+
RecordSelect.like_operator = lambda do |column, model|
|
|
73
|
+
collation = model.columns_hash[column.to_s]&.collation
|
|
74
|
+
collation == 'case_insensitive' ? 'LIKE' : 'ILIKE'
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Use the collation names and operators supported by your database. The +columns_hash+ lookup above expects simple column names; qualified names and SQL expressions need separate handling.
|
|
79
|
+
|
|
80
|
+
The global setting is captured when the controller is configured. An explicit +like_operator: nil+ restores the adapter default for that controller. This setting controls the SQL operator; prefix and full-text wildcard behavior stays the same.
|
|
81
|
+
|
|
56
82
|
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
83
|
|
|
58
84
|
```rb
|
|
@@ -68,13 +94,7 @@ Override either default for a controller:
|
|
|
68
94
|
record_select search_on: [:name], allowed_columns: [:company_id, :active], excluded_columns: [:active]
|
|
69
95
|
```
|
|
70
96
|
|
|
71
|
-
Exclusions take precedence over allowed columns. Symbols and strings are accepted. +allowed_columns+
|
|
72
|
-
|
|
73
|
-
To acknowledge disabled URL column filtering globally:
|
|
74
|
-
|
|
75
|
-
```rb
|
|
76
|
-
RecordSelect.excluded_columns = []
|
|
77
|
-
```
|
|
97
|
+
Exclusions take precedence over allowed columns. Symbols and strings are accepted. Both +allowed_columns+ and +excluded_columns+ default to +[]+, disabling automatic URL column filters without requiring explicit configuration. An explicit +excluded_columns: nil+ is treated as +[]+ and clears any global exclusions for that controller; it does not enable filtering by itself.
|
|
78
98
|
|
|
79
99
|
To enable only specific filters for a controller:
|
|
80
100
|
|
|
@@ -105,3 +125,25 @@ JavaScript source strings are no longer accepted for +onselect+ or +onchange+. J
|
|
|
105
125
|
|
|
106
126
|
= DEPENDENCIES
|
|
107
127
|
This depends on the excellent Paginator gem by Bruce Williams. This simple gem is available at paginator.rubyforge.org.
|
|
128
|
+
|
|
129
|
+
= RUNNING TESTS
|
|
130
|
+
|
|
131
|
+
Run the Ruby and rendered-view tests with:
|
|
132
|
+
|
|
133
|
+
bundle exec rake test
|
|
134
|
+
|
|
135
|
+
Run the JavaScript behavior tests with:
|
|
136
|
+
|
|
137
|
+
bundle exec rake test:javascript
|
|
138
|
+
|
|
139
|
+
The JavaScript suite uses Capybara, Selenium, and headless Chrome or Chromium.
|
|
140
|
+
It serves the actual RecordSelect, jQuery, and jquery-ujs assets from a small
|
|
141
|
+
local Rack application, including real AJAX search and pagination responses.
|
|
142
|
+
No host application or database is required. A compatible ChromeDriver must
|
|
143
|
+
be on PATH, or Selenium Manager must be able to obtain one. Set CHROME_BIN to
|
|
144
|
+
use a specific browser executable, for example:
|
|
145
|
+
|
|
146
|
+
CHROME_BIN=/usr/bin/chromium bundle exec rake test:javascript
|
|
147
|
+
|
|
148
|
+
Both suites run in CI. COVERAGE=true measures Ruby and ERB coverage; it does
|
|
149
|
+
not measure JavaScript coverage.
|
|
@@ -11,7 +11,7 @@ next_url = url_for(pagination_url_params.merge(page: page.next.number)) if page.
|
|
|
11
11
|
<%= content_tag :li, class: 'found', data: {searching: rs_(:searching)} do %>
|
|
12
12
|
<%= rs_(:records_found,
|
|
13
13
|
count: page.pager.count,
|
|
14
|
-
model: record_select_config.model.model_name.human(:
|
|
14
|
+
model: record_select_config.model.model_name.human(count: page.pager.count).downcase) %>
|
|
15
15
|
<% if record_select_config.toggle_search_mode? %>
|
|
16
16
|
<% rs_mode = full_text_search? ? 'begins' : 'contains' %>
|
|
17
17
|
<%= link_to rs_(rs_mode), pagination_url_params.merge(rs_mode: rs_mode),
|
|
@@ -20,7 +20,7 @@ next_url = url_for(pagination_url_params.merge(page: page.next.number)) if page.
|
|
|
20
20
|
<% end %>
|
|
21
21
|
<% if page.prev? -%>
|
|
22
22
|
<li class="pagination previous">
|
|
23
|
-
<%= link_to image_tag('record_select/previous.gif', alt: rs_(:previous))
|
|
23
|
+
<%= link_to safe_join([image_tag('record_select/previous.gif', alt: rs_(:previous)), rs_(:previous_items, count: page.pager.per_page)], ' '),
|
|
24
24
|
prev_url, method: :get, remote: true %>
|
|
25
25
|
</li>
|
|
26
26
|
<% end -%>
|
|
@@ -5,5 +5,5 @@ url_options = params.permit(*permit_fields).merge(controller: controller, action
|
|
|
5
5
|
<%= form_tag url_options, {method: :get, remote: true, id: record_select_search_id} -%>
|
|
6
6
|
<%= text_field_tag 'search', params[:search], autocomplete: 'off', class: 'text-input' %>
|
|
7
7
|
<%= hidden_field_tag 'rs_mode', params[:rs_mode] if record_select_config.toggle_search_mode? %>
|
|
8
|
-
<%= submit_tag 'search', class:
|
|
8
|
+
<%= submit_tag 'search', class: 'search_submit' %>
|
|
9
9
|
</form>
|
|
@@ -15,19 +15,7 @@ module RecordSelect
|
|
|
15
15
|
end
|
|
16
16
|
@page = pager.page(params[:page] || 1)
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
wants.html { render_record_select :partial => 'browse'}
|
|
20
|
-
wants.js {
|
|
21
|
-
if params[:update]
|
|
22
|
-
render_record_select :template => 'browse'
|
|
23
|
-
else
|
|
24
|
-
render_record_select :partial => 'browse'
|
|
25
|
-
end
|
|
26
|
-
}
|
|
27
|
-
wants.yaml {}
|
|
28
|
-
wants.xml {}
|
|
29
|
-
wants.json {}
|
|
30
|
-
end
|
|
18
|
+
record_select_browse_response
|
|
31
19
|
end
|
|
32
20
|
|
|
33
21
|
# :method => :post
|
|
@@ -40,22 +28,22 @@ module RecordSelect
|
|
|
40
28
|
elsif record_select_config.notify
|
|
41
29
|
send(record_select_config.notify, record)
|
|
42
30
|
end
|
|
43
|
-
render :
|
|
31
|
+
render nothing: true
|
|
44
32
|
end
|
|
45
33
|
|
|
46
34
|
protected
|
|
47
35
|
|
|
48
|
-
def record_select_config
|
|
36
|
+
def record_select_config # :nodoc:
|
|
49
37
|
self.class.record_select_config
|
|
50
38
|
end
|
|
51
39
|
|
|
52
|
-
def render_record_select(options = {})
|
|
53
|
-
[
|
|
54
|
-
if options[template_name]
|
|
40
|
+
def render_record_select(options = {}) # :nodoc:
|
|
41
|
+
%i[template partial].each do |template_name|
|
|
42
|
+
if options[template_name]
|
|
55
43
|
options[template_name] = File.join(record_select_views_path, options[template_name])
|
|
56
44
|
end
|
|
57
45
|
end
|
|
58
|
-
if block_given?
|
|
46
|
+
if block_given?
|
|
59
47
|
yield options
|
|
60
48
|
else
|
|
61
49
|
render options
|
|
@@ -64,6 +52,22 @@ module RecordSelect
|
|
|
64
52
|
|
|
65
53
|
private
|
|
66
54
|
|
|
55
|
+
def record_select_browse_response
|
|
56
|
+
respond_to do |wants|
|
|
57
|
+
wants.html { render_record_select partial: 'browse' }
|
|
58
|
+
wants.js do
|
|
59
|
+
if params[:update]
|
|
60
|
+
render_record_select template: 'browse'
|
|
61
|
+
else
|
|
62
|
+
render_record_select partial: 'browse'
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
wants.any(:yaml, :xml, :json) do
|
|
66
|
+
# Use Rails default template rendering for these formats.
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
67
71
|
def record_select_query
|
|
68
72
|
query = record_select_model
|
|
69
73
|
user_includes = record_select_includes
|
|
@@ -74,7 +78,7 @@ module RecordSelect
|
|
|
74
78
|
end
|
|
75
79
|
|
|
76
80
|
def record_select_views_path
|
|
77
|
-
|
|
81
|
+
'record_select'
|
|
78
82
|
end
|
|
79
83
|
end
|
|
80
84
|
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
module RecordSelect
|
|
4
4
|
module Conditions
|
|
5
5
|
protected
|
|
6
|
+
|
|
6
7
|
# returns the combination of all conditions.
|
|
7
8
|
# conditions come from:
|
|
8
9
|
# * current search (params[:search])
|
|
@@ -25,43 +26,52 @@ module RecordSelect
|
|
|
25
26
|
def record_select_includes; end
|
|
26
27
|
|
|
27
28
|
def record_select_like_operator
|
|
28
|
-
@
|
|
29
|
+
@record_select_like_operator ||= ::ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' ? 'ILIKE' : 'LIKE'
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
# define special list of selected fields,
|
|
32
|
-
# mainly to define extra fields that can be used for
|
|
33
|
+
# mainly to define extra fields that can be used for
|
|
33
34
|
# specialized sorting.
|
|
34
|
-
def record_select_select
|
|
35
|
-
end
|
|
35
|
+
def record_select_select; end
|
|
36
36
|
|
|
37
37
|
# generate conditions from params[:search]
|
|
38
38
|
# override this if you want to customize the search routine
|
|
39
39
|
def record_select_conditions_from_search
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
build_record_select_conditions(tokens, record_select_like_operator, search_pattern)
|
|
40
|
+
return unless params[:search] && !params[:search].strip.empty?
|
|
41
|
+
|
|
42
|
+
if full_text_search?
|
|
43
|
+
tokens = params[:search].strip.split
|
|
44
|
+
search_pattern = '%?%'
|
|
45
|
+
else
|
|
46
|
+
tokens = [params[:search].strip]
|
|
47
|
+
search_pattern = '?%'
|
|
49
48
|
end
|
|
49
|
+
like_operator = record_select_config.like_operator || record_select_like_operator
|
|
50
|
+
build_record_select_conditions(tokens, like_operator, search_pattern)
|
|
50
51
|
end
|
|
51
52
|
|
|
52
53
|
def full_text_search?
|
|
53
|
-
record_select_config.full_text_search? && params[:rs_mode].blank? || params[:rs_mode] == 'contains'
|
|
54
|
+
(record_select_config.full_text_search? && params[:rs_mode].blank?) || params[:rs_mode] == 'contains'
|
|
54
55
|
end
|
|
55
|
-
|
|
56
|
-
def build_record_select_conditions(tokens, operator, search_pattern)
|
|
57
|
-
return if record_select_config.search_on.empty?
|
|
58
56
|
|
|
59
|
-
|
|
57
|
+
def build_record_select_conditions(tokens, like_operator, search_pattern)
|
|
58
|
+
columns = record_select_config.search_on
|
|
59
|
+
return if columns.empty?
|
|
60
|
+
|
|
61
|
+
where_clauses = columns.collect do |column|
|
|
62
|
+
operator =
|
|
63
|
+
if like_operator.respond_to?(:call)
|
|
64
|
+
like_operator.call(column, record_select_config.model)
|
|
65
|
+
else
|
|
66
|
+
like_operator
|
|
67
|
+
end
|
|
68
|
+
"#{column} #{operator} ?"
|
|
69
|
+
end
|
|
60
70
|
phrase = "(#{where_clauses.join(' OR ')})"
|
|
61
71
|
sql = ([phrase] * tokens.length).join(' AND ')
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
[sql, *
|
|
72
|
+
|
|
73
|
+
values = tokens.collect { |token| [search_pattern.sub('?', token)] * columns.length }.flatten
|
|
74
|
+
[sql, *values]
|
|
65
75
|
end
|
|
66
76
|
|
|
67
77
|
# generate conditions from the url parameters (e.g. users/browse?group_id=5)
|
|
@@ -71,6 +81,7 @@ module RecordSelect
|
|
|
71
81
|
params.each do |field, value|
|
|
72
82
|
next if field.in? ignored_columns
|
|
73
83
|
next unless record_select_config.column_allowed?(field)
|
|
84
|
+
|
|
74
85
|
column = record_select_config.model.columns_hash[field]
|
|
75
86
|
next unless column
|
|
76
87
|
|
|
@@ -97,10 +108,10 @@ module RecordSelect
|
|
|
97
108
|
model = record_select_config.model
|
|
98
109
|
if value.is_a? Array
|
|
99
110
|
{column.name => value}
|
|
100
|
-
elsif value.blank?
|
|
111
|
+
elsif value.blank? && column.null
|
|
101
112
|
{column.name => nil}
|
|
102
|
-
elsif [
|
|
103
|
-
column_name = model.quoted_table_name
|
|
113
|
+
elsif %i[string text].include? column.type
|
|
114
|
+
column_name = "#{model.quoted_table_name}.#{model.connection.quote_column_name(column.name)}"
|
|
104
115
|
["LOWER(#{column_name}) LIKE ?", value]
|
|
105
116
|
else
|
|
106
117
|
{column.name => record_select_type_cast(column, value)}
|
data/lib/record_select/config.rb
CHANGED
|
@@ -4,22 +4,17 @@ module RecordSelect
|
|
|
4
4
|
# a write-once configuration object
|
|
5
5
|
class Config
|
|
6
6
|
def initialize(klass, options = {})
|
|
7
|
-
|
|
7
|
+
if options[:search_on].nil?
|
|
8
|
+
raise ArgumentError,
|
|
9
|
+
'record_select requires :search_on (use [] for custom or deferred searches)'
|
|
10
|
+
end
|
|
8
11
|
|
|
9
12
|
@klass = klass
|
|
10
13
|
@notify = block_given? ? proc : options[:notify]
|
|
11
14
|
@per_page = options[:per_page]
|
|
12
15
|
@search_on = [options[:search_on]].flatten
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if excluded_columns.nil?
|
|
16
|
-
raise ArgumentError, 'record_select requires explicit :excluded_columns (use [] for no exclusions). ' \
|
|
17
|
-
'URL column filters are disabled by default; set :allowed_columns to the required columns. ' \
|
|
18
|
-
'To explicitly restore unrestricted legacy filtering, use allowed_columns: nil, excluded_columns: []. ' \
|
|
19
|
-
'This is unsafe: filtering on hidden model columns can disclose sensitive values. Prefer an explicit allowlist.'
|
|
20
|
-
end
|
|
21
|
-
@allowed_columns = Array(allowed_columns).map(&:to_s) unless allowed_columns.nil?
|
|
22
|
-
@excluded_columns = Array(excluded_columns).map(&:to_s)
|
|
16
|
+
@like_operator = options.fetch(:like_operator) { RecordSelect.like_operator }
|
|
17
|
+
configure_column_filters(options)
|
|
23
18
|
@order_by = options[:order_by]
|
|
24
19
|
@full_text_search = options[:full_text_search]
|
|
25
20
|
@label = options[:label]
|
|
@@ -48,18 +43,20 @@ module RecordSelect
|
|
|
48
43
|
|
|
49
44
|
# The method name or proc to notify of a selection event.
|
|
50
45
|
# May not matter if the selection event is intercepted client-side.
|
|
51
|
-
|
|
52
|
-
@notify
|
|
53
|
-
end
|
|
46
|
+
attr_reader :notify
|
|
54
47
|
|
|
55
|
-
# A collection of fields to search. This is essentially raw SQL, so you could search on "CONCAT(first_name, ' ',
|
|
48
|
+
# A collection of fields to search. This is essentially raw SQL, so you could search on "CONCAT(first_name, ' ',
|
|
49
|
+
# last_name)" if you wanted to.
|
|
56
50
|
# NOTE: this does *NO* default transforms (such as LOWER()), that's left entirely up to you.
|
|
57
51
|
attr_reader :search_on
|
|
58
52
|
|
|
53
|
+
# SQL operator or callable receiving a search_on entry and the model; nil uses the adapter default.
|
|
54
|
+
attr_reader :like_operator
|
|
55
|
+
|
|
59
56
|
def column_allowed?(name)
|
|
60
57
|
name = name.to_s
|
|
61
58
|
allowed = allowed_columns
|
|
62
|
-
(allowed.nil? || allowed.include?(name)) &&
|
|
59
|
+
(allowed.nil? || allowed.include?(name)) && excluded_columns.exclude?(name)
|
|
63
60
|
end
|
|
64
61
|
|
|
65
62
|
def order_by
|
|
@@ -92,14 +89,21 @@ module RecordSelect
|
|
|
92
89
|
# </div>
|
|
93
90
|
#
|
|
94
91
|
def label
|
|
95
|
-
@label ||= proc {|r| r.to_label}
|
|
92
|
+
@label ||= proc { |r| r.to_label }
|
|
96
93
|
end
|
|
97
94
|
|
|
98
95
|
protected
|
|
99
96
|
|
|
100
97
|
# A singularized underscored version of the model we're browsing
|
|
101
|
-
|
|
102
|
-
|
|
98
|
+
attr_reader :klass
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def configure_column_filters(options)
|
|
103
|
+
allowed_columns = options.fetch(:allowed_columns) { RecordSelect.allowed_columns }
|
|
104
|
+
excluded_columns = options.fetch(:excluded_columns) { RecordSelect.excluded_columns }
|
|
105
|
+
@allowed_columns = Array(allowed_columns).map(&:to_s) unless allowed_columns.nil?
|
|
106
|
+
@excluded_columns = Array(excluded_columns).map(&:to_s)
|
|
103
107
|
end
|
|
104
108
|
end
|
|
105
109
|
end
|
data/lib/record_select/engine.rb
CHANGED
|
@@ -11,7 +11,8 @@ module RecordSelect
|
|
|
11
11
|
initializer 'record_select.action_view' do
|
|
12
12
|
ActiveSupport.on_load :action_view do
|
|
13
13
|
include RecordSelectHelper
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
ActionView::Helpers::FormBuilder.include RecordSelect::FormBuilder
|
|
15
16
|
end
|
|
16
17
|
end
|
|
17
18
|
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
# own system.
|
|
6
6
|
class Object
|
|
7
7
|
def rs_(key, options = {})
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
if key.present?
|
|
9
|
+
defaults = {scope: [:record_select], default: key.is_a?(String) ? key : key.to_s.titleize}
|
|
10
|
+
text = I18n.t(key.to_s, **defaults, **options)
|
|
10
11
|
# text = nil if text.include?('translation missing:')
|
|
11
12
|
end
|
|
12
13
|
text ||= key
|
|
@@ -6,9 +6,9 @@ module RecordSelect
|
|
|
6
6
|
reflection = @object.class.reflect_on_association(association)
|
|
7
7
|
form_name = form_name_for_association(reflection)
|
|
8
8
|
current = @object.send(association)
|
|
9
|
-
options[:id] ||= "#{@object_name.gsub(/[^-a-zA-Z0-9:.]/,
|
|
9
|
+
options[:id] ||= "#{@object_name.gsub(/[^-a-zA-Z0-9:.]/, '_').sub(/_$/, '')}_#{association}"
|
|
10
10
|
|
|
11
|
-
if [
|
|
11
|
+
if %i[has_one belongs_to].include? reflection.macro
|
|
12
12
|
@template.record_select_field(form_name, current || reflection.klass.new, options)
|
|
13
13
|
else
|
|
14
14
|
options[:controller] ||= reflection.klass.to_s.pluralize.underscore
|
|
@@ -19,9 +19,9 @@ module RecordSelect
|
|
|
19
19
|
private
|
|
20
20
|
|
|
21
21
|
def form_name_for_association(reflection)
|
|
22
|
-
key_name =
|
|
23
|
-
key_name +=
|
|
24
|
-
|
|
22
|
+
key_name = reflection.options[:foreign_key] || reflection.association_foreign_key
|
|
23
|
+
key_name += 's' unless %i[has_one belongs_to].include? reflection.macro
|
|
24
|
+
"#{@object_name}[#{key_name}]"
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
end
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
module RecordSelectHelper
|
|
4
4
|
# If you use :params option, define this helper in your controller helpers
|
|
5
5
|
# to return array with those params, so _list partial permits them
|
|
6
|
-
def permit_rs_browse_params
|
|
7
|
-
end
|
|
6
|
+
def permit_rs_browse_params; end
|
|
8
7
|
|
|
9
8
|
def record_select_js(type:, id:, url:, options:)
|
|
10
9
|
javascript_tag("new RecordSelect.#{type}(#{id.to_json}, #{url.to_json}, #{options.to_json});", nonce: true)
|
|
@@ -13,15 +12,14 @@ module RecordSelectHelper
|
|
|
13
12
|
# Adds a link on the page that toggles a RecordSelect widget from the given controller.
|
|
14
13
|
#
|
|
15
14
|
# *Options*
|
|
16
|
-
# +onselect+:: Name of a callback registered with RecordSelect.registerCallback. If the callback returns false, the
|
|
17
|
-
#
|
|
15
|
+
# +onselect+:: Name of a callback registered with RecordSelect.registerCallback. If the callback returns false, the
|
|
16
|
+
# dialog will *not* close automatically.
|
|
17
|
+
# +params+:: Extra URL parameters. If any parameter is a column name, the parameter will be used as a search term
|
|
18
|
+
# to filter the result set.
|
|
18
19
|
# +html+:: Options for A tag
|
|
19
20
|
# +rs+:: Options for RecordSelect constructor
|
|
20
21
|
def link_to_record_select(name, controller, options = {})
|
|
21
|
-
options
|
|
22
|
-
options[:params].merge!(:controller => controller, :action => :browse)
|
|
23
|
-
options[:html] ||= {}
|
|
24
|
-
options[:html][:id] ||= "rs_#{rand(9999)}"
|
|
22
|
+
prepare_record_select_link_options(controller, options)
|
|
25
23
|
js = options.include?(:js) ? options[:js] : request.xhr?
|
|
26
24
|
|
|
27
25
|
controller = assert_controller_responds(options[:params][:controller])
|
|
@@ -40,78 +38,59 @@ module RecordSelectHelper
|
|
|
40
38
|
#
|
|
41
39
|
# *Arguments*
|
|
42
40
|
# +name+:: the input name that will be used to submit the selected record's id.
|
|
43
|
-
# +current+:: the currently selected object. provide a new record if there're none currently selected and you have not
|
|
41
|
+
# +current+:: the currently selected object. provide a new record if there're none currently selected and you have not
|
|
42
|
+
# passed the optional :controller argument.
|
|
44
43
|
#
|
|
45
44
|
# *Options*
|
|
46
|
-
# +controller+:: The controller configured to provide the result set. Optional if you have standard resource
|
|
45
|
+
# +controller+:: The controller configured to provide the result set. Optional if you have standard resource
|
|
46
|
+
# controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the
|
|
47
|
+
# class of +current+ (the second argument)
|
|
47
48
|
# +params+:: A hash of extra URL parameters
|
|
48
49
|
# +id+:: The id to use for the input. Defaults based on the input's name.
|
|
49
50
|
# +field_name+:: The name to use for the text input. Defaults to '', so field is not submitted.
|
|
50
51
|
# +rs+:: Options for RecordSelect constructor
|
|
51
52
|
def record_select_field(name, current, options = {})
|
|
52
53
|
options[:controller] ||= current.class.to_s.pluralize.underscore
|
|
53
|
-
options
|
|
54
|
-
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
55
|
-
options[:class] = options[:class] ? options[:class].dup : String.new
|
|
56
|
-
options[:class] << ' recordselect'
|
|
54
|
+
prepare_record_select_field_options(name, options)
|
|
57
55
|
options[:clear_button] = true unless options.include? :clear_button
|
|
58
56
|
js = options.include?(:js) ? options.delete(:js) : request.xhr?
|
|
59
57
|
|
|
60
58
|
controller = assert_controller_responds(options.delete(:controller))
|
|
61
|
-
|
|
62
|
-
record_select_options = {id: record_select_id(controller.controller_path)}
|
|
63
|
-
record_select_options[:field_name] = options.delete(:field_name) if options[:field_name]
|
|
59
|
+
record_select_options = record_select_single_options(current, controller, options)
|
|
64
60
|
clear_button_class = 'clear-input-button'
|
|
65
|
-
if current
|
|
66
|
-
record_select_options[:id] = current.id
|
|
67
|
-
record_select_options[:label] = label_for_field(current, controller)
|
|
68
|
-
clear_button_class += ' enabled'
|
|
69
|
-
end
|
|
70
|
-
record_select_options.merge! options.delete(:rs) if options[:rs]
|
|
71
|
-
|
|
72
|
-
clear_button = options.delete(:clear_button)
|
|
73
|
-
options[:autocomplete] = 'off'
|
|
74
|
-
url = url_for({action: :browse, controller: controller.controller_path}.merge(params))
|
|
75
|
-
rs_data = {type: 'Single', id: options[:id], url: url, options: record_select_options}
|
|
76
|
-
options[:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js
|
|
77
|
-
html = text_field_tag(name, nil, options)
|
|
78
|
-
html << button_tag('x', type: :button, class: clear_button_class, aria_label: 'Clear input', title: 'Clear input') if clear_button
|
|
79
|
-
html << record_select_js(**rs_data) if js
|
|
61
|
+
clear_button_class += ' enabled' if current && !current.new_record?
|
|
80
62
|
|
|
81
|
-
|
|
63
|
+
rs_data = record_select_field_data('Single', controller, options, record_select_options, javascript: js)
|
|
64
|
+
render_record_select_single_field(name, options, rs_data, clear_button_class: clear_button_class, javascript: js)
|
|
82
65
|
end
|
|
83
66
|
|
|
84
67
|
# Adds a RecordSelect-based form field. The field is autocompleted.
|
|
85
68
|
#
|
|
86
69
|
# *Arguments*
|
|
87
70
|
# +name+:: the input name that will be used to submit the selected value.
|
|
88
|
-
# +current+:: the current object. provide a new record if there're none currently selected and you have not passed the
|
|
71
|
+
# +current+:: the current object. provide a new record if there're none currently selected and you have not passed the
|
|
72
|
+
# optional :controller argument.
|
|
89
73
|
#
|
|
90
74
|
# *Options*
|
|
91
|
-
# +controller+:: The controller configured to provide the result set. Optional if you have standard resource
|
|
75
|
+
# +controller+:: The controller configured to provide the result set. Optional if you have standard resource
|
|
76
|
+
# controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the
|
|
77
|
+
# class of +current+ (the second argument)
|
|
92
78
|
# +params+:: A hash of extra URL parameters
|
|
93
79
|
# +id+:: The id to use for the input. Defaults based on the input's name.
|
|
94
80
|
# +rs+:: Options for RecordSelect constructor
|
|
95
81
|
def record_select_autocomplete(name, current, options = {})
|
|
96
82
|
options[:controller] ||= current.class.to_s.pluralize.underscore
|
|
97
|
-
options
|
|
98
|
-
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
99
|
-
options[:class] = options[:class] ? options[:class].dup : String.new
|
|
100
|
-
options[:class] << ' recordselect'
|
|
83
|
+
prepare_record_select_field_options(name, options)
|
|
101
84
|
js = options.include?(:js) ? options.delete(:js) : request.xhr?
|
|
102
85
|
|
|
103
86
|
controller = assert_controller_responds(options.delete(:controller))
|
|
104
|
-
params = options.delete(:params)
|
|
105
87
|
record_select_options = {id: record_select_id(controller.controller_path), label: options.delete(:label)}
|
|
106
88
|
if current
|
|
107
89
|
record_select_options[:label] ||= label_for_field(current, controller)
|
|
108
90
|
end
|
|
109
91
|
record_select_options.merge! options.delete(:rs) if options[:rs]
|
|
110
92
|
|
|
111
|
-
|
|
112
|
-
url = url_for({action: :browse, controller: controller.controller_path}.merge(params))
|
|
113
|
-
rs_data = {type: 'Autocomplete', id: options[:id], url: url, options: record_select_options}
|
|
114
|
-
options[:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js
|
|
93
|
+
rs_data = record_select_field_data('Autocomplete', controller, options, record_select_options, javascript: js)
|
|
115
94
|
html = text_field_tag(name, nil, options)
|
|
116
95
|
html << record_select_js(**rs_data) if js
|
|
117
96
|
|
|
@@ -121,7 +100,8 @@ module RecordSelectHelper
|
|
|
121
100
|
# Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.
|
|
122
101
|
#
|
|
123
102
|
# *Arguments*
|
|
124
|
-
# +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to
|
|
103
|
+
# +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to
|
|
104
|
+
# the name.
|
|
125
105
|
# +current+:: pass a collection of existing associated records
|
|
126
106
|
#
|
|
127
107
|
# *Options*
|
|
@@ -131,40 +111,30 @@ module RecordSelectHelper
|
|
|
131
111
|
# +rs+:: Options for RecordSelect constructor
|
|
132
112
|
def record_multi_select_field(name, current, options = {})
|
|
133
113
|
options[:controller] ||= current.first.class.to_s.pluralize.underscore
|
|
134
|
-
options
|
|
135
|
-
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
136
|
-
options[:class] = options[:class] ? options[:class].dup : String.new
|
|
137
|
-
options[:class] << ' recordselect'
|
|
114
|
+
prepare_record_select_field_options(name, options)
|
|
138
115
|
options.delete(:name)
|
|
139
116
|
js = options.include?(:js) ? options.delete(:js) : request.xhr?
|
|
140
117
|
|
|
141
118
|
controller = assert_controller_responds(options.delete(:controller))
|
|
142
|
-
params = options.delete(:params)
|
|
143
119
|
record_select_options = {id: record_select_id(controller.controller_path)}
|
|
144
|
-
record_select_options[:current] = current.
|
|
120
|
+
record_select_options[:current] = current.map do |record|
|
|
121
|
+
{id: record.id, label: label_for_field(record, controller)}
|
|
122
|
+
end
|
|
145
123
|
record_select_options.merge! options.delete(:rs) if options[:rs]
|
|
146
124
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
rs_data = {type: 'Multiple', id: options[:id], url: url, options: record_select_options}
|
|
150
|
-
options[:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js
|
|
151
|
-
html = text_field_tag("#{name}[]", nil, options)
|
|
152
|
-
html << hidden_field_tag("#{name}[]", '', id: nil)
|
|
153
|
-
html << content_tag(:ul, '', class: 'record-select-list')
|
|
154
|
-
html << record_select_js(**rs_data) if js
|
|
155
|
-
|
|
156
|
-
html
|
|
125
|
+
rs_data = record_select_field_data('Multiple', controller, options, record_select_options, javascript: js)
|
|
126
|
+
render_record_select_multiple_field(name, options, rs_data, javascript: js)
|
|
157
127
|
end
|
|
158
128
|
|
|
159
129
|
# A helper to render RecordSelect partials
|
|
160
|
-
def render_record_select(options = {})
|
|
130
|
+
def render_record_select(options = {}) # :nodoc:
|
|
161
131
|
controller.send(:render_record_select, options) do |options|
|
|
162
132
|
render options
|
|
163
133
|
end
|
|
164
134
|
end
|
|
165
135
|
|
|
166
136
|
# Provides view access to the RecordSelect configuration
|
|
167
|
-
def record_select_config
|
|
137
|
+
def record_select_config # :nodoc:
|
|
168
138
|
controller.send :record_select_config
|
|
169
139
|
end
|
|
170
140
|
|
|
@@ -173,23 +143,78 @@ module RecordSelectHelper
|
|
|
173
143
|
end
|
|
174
144
|
|
|
175
145
|
# The id of the RecordSelect widget for the given controller.
|
|
176
|
-
def record_select_id(controller = nil)
|
|
146
|
+
def record_select_id(controller = nil) # :nodoc:
|
|
177
147
|
controller ||= params[:controller]
|
|
178
|
-
"record-select-#{controller.
|
|
148
|
+
"record-select-#{controller.tr('/', '_')}"
|
|
179
149
|
end
|
|
180
150
|
|
|
181
|
-
def record_select_search_id(controller = nil)
|
|
151
|
+
def record_select_search_id(controller = nil) # :nodoc:
|
|
182
152
|
"#{record_select_id(controller)}-search"
|
|
183
153
|
end
|
|
184
154
|
|
|
185
155
|
private
|
|
186
156
|
|
|
157
|
+
def prepare_record_select_link_options(controller, options)
|
|
158
|
+
options[:params] ||= {}
|
|
159
|
+
options[:params].merge!(controller: controller, action: :browse)
|
|
160
|
+
options[:html] ||= {}
|
|
161
|
+
options[:html][:id] ||= "rs_#{rand(9999)}"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def record_select_single_options(current, controller, options)
|
|
165
|
+
record_select_options = {id: record_select_id(controller.controller_path)}
|
|
166
|
+
record_select_options[:field_name] = options.delete(:field_name) if options[:field_name]
|
|
167
|
+
if current && !current.new_record?
|
|
168
|
+
record_select_options[:id] = current.id
|
|
169
|
+
record_select_options[:label] = label_for_field(current, controller)
|
|
170
|
+
end
|
|
171
|
+
record_select_options.merge! options.delete(:rs) if options[:rs]
|
|
172
|
+
record_select_options
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def record_select_field_data(type, controller, options, record_select_options, javascript:)
|
|
176
|
+
params = options.delete(:params)
|
|
177
|
+
options[:autocomplete] = 'off'
|
|
178
|
+
url = url_for({action: :browse, controller: controller.controller_path}.merge(params))
|
|
179
|
+
data = {type: type, id: options[:id], url: url, options: record_select_options}
|
|
180
|
+
options[:data] = data.transform_keys { |key| "rs_#{key}" } unless javascript
|
|
181
|
+
data
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def prepare_record_select_field_options(name, options)
|
|
185
|
+
options[:params] ||= {}
|
|
186
|
+
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
187
|
+
options[:class] = options[:class] ? options[:class].dup : +''
|
|
188
|
+
options[:class] << ' recordselect'
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def render_record_select_multiple_field(name, options, rs_data, javascript:)
|
|
192
|
+
html = text_field_tag("#{name}[]", nil, options)
|
|
193
|
+
html << hidden_field_tag("#{name}[]", '', id: nil)
|
|
194
|
+
html << content_tag(:ul, '', class: 'record-select-list')
|
|
195
|
+
html << record_select_js(**rs_data) if javascript
|
|
196
|
+
|
|
197
|
+
html
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def render_record_select_single_field(name, options, rs_data, clear_button_class:, javascript:)
|
|
201
|
+
clear_button = options.delete(:clear_button)
|
|
202
|
+
html = text_field_tag(name, nil, options)
|
|
203
|
+
if clear_button
|
|
204
|
+
html << button_tag('x', type: :button, class: clear_button_class, aria_label: 'Clear input',
|
|
205
|
+
title: 'Clear input')
|
|
206
|
+
end
|
|
207
|
+
html << record_select_js(**rs_data) if javascript
|
|
208
|
+
|
|
209
|
+
html
|
|
210
|
+
end
|
|
211
|
+
|
|
187
212
|
# uses renderer (defaults to record_select_config.label) to determine how the given record renders.
|
|
188
213
|
def render_record_from_config(record, renderer = record_select_config.label)
|
|
189
214
|
case renderer
|
|
190
215
|
when Symbol, String
|
|
191
216
|
# return full-html from the named partial
|
|
192
|
-
render :
|
|
217
|
+
render partial: renderer.to_s, locals: {record: record}
|
|
193
218
|
|
|
194
219
|
when Proc
|
|
195
220
|
# return an html-cleaned descriptive string
|
|
@@ -203,16 +228,17 @@ module RecordSelectHelper
|
|
|
203
228
|
# if given a controller, searches for a partial in its views path
|
|
204
229
|
def label_for_field(record, controller = self.controller)
|
|
205
230
|
renderer = controller.record_select_config.label
|
|
206
|
-
description =
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
231
|
+
description =
|
|
232
|
+
case renderer
|
|
233
|
+
when Symbol, String
|
|
234
|
+
# find the <label> element and grab its innerHTML
|
|
235
|
+
render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
|
|
236
|
+
|
|
237
|
+
when Proc
|
|
238
|
+
# just return the string
|
|
239
|
+
render_record_from_config(record, renderer)
|
|
240
|
+
end
|
|
241
|
+
description.match(%r{<label[^>]*>(.*)</label>}) { |match| match[1] } || description if description
|
|
216
242
|
end
|
|
217
243
|
|
|
218
244
|
def assert_controller_responds(controller_name)
|
|
@@ -221,6 +247,7 @@ module RecordSelectHelper
|
|
|
221
247
|
unless controller.uses_record_select?
|
|
222
248
|
raise "#{controller_name} has not been configured to use RecordSelect."
|
|
223
249
|
end
|
|
250
|
+
|
|
224
251
|
controller
|
|
225
252
|
end
|
|
226
253
|
end
|
data/lib/record_select.rb
CHANGED
|
@@ -4,10 +4,10 @@ require 'active_support/deprecation'
|
|
|
4
4
|
|
|
5
5
|
module RecordSelect
|
|
6
6
|
class << self
|
|
7
|
-
attr_accessor :allowed_columns, :excluded_columns
|
|
7
|
+
attr_accessor :allowed_columns, :excluded_columns, :like_operator
|
|
8
8
|
end
|
|
9
9
|
self.allowed_columns = []
|
|
10
|
-
self.excluded_columns =
|
|
10
|
+
self.excluded_columns = []
|
|
11
11
|
|
|
12
12
|
def self.deprecator
|
|
13
13
|
@deprecator ||= ActiveSupport::Deprecation.new('4.2.0', 'RecordSelect')
|
|
@@ -30,28 +30,35 @@ module RecordSelect
|
|
|
30
30
|
# +notify+:: a method name to invoke when a record has been selected.
|
|
31
31
|
# +order_by+:: a SQL string to order the search results
|
|
32
32
|
# +search_on+:: required searchable field or array of fields; [] disables the built-in text search
|
|
33
|
-
# +
|
|
34
|
-
#
|
|
33
|
+
# +like_operator+:: SQL search operator or callable receiving each search_on entry and the model. Overrides
|
|
34
|
+
# RecordSelect.like_operator; nil uses the adapter default (ILIKE for PostgreSQL, LIKE otherwise).
|
|
35
|
+
# +allowed_columns+:: URL parameter filter columns; nil allows all model columns. Overrides
|
|
36
|
+
# RecordSelect.allowed_columns.
|
|
37
|
+
# +excluded_columns+:: URL parameter filter columns to ignore, even if allowed. Overrides
|
|
38
|
+
# RecordSelect.excluded_columns.
|
|
35
39
|
# +full_text_search+:: a boolean for whether to use a %?% search pattern or not. default is false.
|
|
36
|
-
# +label+:: a proc that accepts a record as argument and returns an option label. default is to call
|
|
37
|
-
#
|
|
38
|
-
# +
|
|
39
|
-
#
|
|
40
|
+
# +label+:: a proc that accepts a record as argument and returns an option label. default is to call
|
|
41
|
+
# record.to_label instead.
|
|
42
|
+
# +include+:: as for ActiveRecord::Base#find. can help with search conditions or just help optimize rendering the
|
|
43
|
+
# results.
|
|
44
|
+
# +link+:: a boolean for whether wrap the text returned by label in a link or not. default is true. set to
|
|
45
|
+
# false when
|
|
46
|
+
# label returns html code which can't be inside a tag. You can use record_select_link_to_select in
|
|
47
|
+
# your proc
|
|
40
48
|
# or partial to add a link to select action
|
|
41
49
|
#
|
|
42
50
|
# You may also pass a block, which will be used as options[:notify].
|
|
43
51
|
def record_select(options = {})
|
|
44
|
-
options[:model] ||=
|
|
52
|
+
options[:model] ||= to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore
|
|
45
53
|
@record_select_config = RecordSelect::Config.new(options.delete(:model), options)
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
send :include, RecordSelect::Actions
|
|
55
|
+
send :include, RecordSelect::Conditions
|
|
48
56
|
end
|
|
49
57
|
|
|
50
58
|
def record_select_config
|
|
51
|
-
|
|
52
|
-
when defined?(@record_select_config)
|
|
59
|
+
if defined?(@record_select_config)
|
|
53
60
|
@record_select_config
|
|
54
|
-
|
|
61
|
+
elsif superclass.respond_to?(:record_select_config)
|
|
55
62
|
superclass.record_select_config
|
|
56
63
|
end
|
|
57
64
|
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.
|
|
4
|
+
version: 4.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sergio Cambra
|
|
@@ -61,13 +61,12 @@ files:
|
|
|
61
61
|
- lib/record_select/helpers/record_select_helper.rb
|
|
62
62
|
- lib/record_select/version.rb
|
|
63
63
|
- lib/recordselect.rb
|
|
64
|
-
- test/recordselect_test.rb
|
|
65
|
-
- test/test_configuration.rb
|
|
66
64
|
- vendor/assets/javascripts/jquery.visible.min.js
|
|
67
65
|
homepage: http://github.com/scambra/recordselect
|
|
68
66
|
licenses:
|
|
69
67
|
- MIT
|
|
70
|
-
metadata:
|
|
68
|
+
metadata:
|
|
69
|
+
rubygems_mfa_required: 'true'
|
|
71
70
|
rdoc_options: []
|
|
72
71
|
require_paths:
|
|
73
72
|
- lib
|
|
@@ -75,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
75
74
|
requirements:
|
|
76
75
|
- - ">="
|
|
77
76
|
- !ruby/object:Gem::Version
|
|
78
|
-
version:
|
|
77
|
+
version: '3.2'
|
|
79
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
79
|
requirements:
|
|
81
80
|
- - ">="
|
|
@@ -85,6 +84,4 @@ requirements: []
|
|
|
85
84
|
rubygems_version: 3.6.9
|
|
86
85
|
specification_version: 4
|
|
87
86
|
summary: RecordSelect widget as a replacement for massive drop down lists
|
|
88
|
-
test_files:
|
|
89
|
-
- test/recordselect_test.rb
|
|
90
|
-
- test/test_configuration.rb
|
|
87
|
+
test_files: []
|
data/test/recordselect_test.rb
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'rubygems'
|
|
4
|
-
require 'test/unit'
|
|
5
|
-
require 'active_support'
|
|
6
|
-
|
|
7
|
-
class RecordselectTest < ActiveSupport::TestCase
|
|
8
|
-
JAVASCRIPT_PATH = File.expand_path('../app/assets/javascripts/record_select.js', __dir__)
|
|
9
|
-
HELPERS_PATH = File.expand_path('../lib/record_select/helpers/record_select_helper.rb', __dir__)
|
|
10
|
-
|
|
11
|
-
def test_exposes_callback_registry_availability
|
|
12
|
-
require 'record_select'
|
|
13
|
-
|
|
14
|
-
assert RecordSelect.callback_registry?
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def test_callbacks_are_resolved_without_eval
|
|
18
|
-
source = File.read(JAVASCRIPT_PATH)
|
|
19
|
-
|
|
20
|
-
assert_includes source, 'RecordSelect.registerCallback = function(name, callback)'
|
|
21
|
-
assert_includes source, "RecordSelect.resolveCallback(this.options.onselect, 'onselect')"
|
|
22
|
-
assert_includes source, 'this.options.onselect.call(this, id, value, text, item)'
|
|
23
|
-
assert_not_includes source, 'eval('
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def test_link_data_is_preserved_when_record_select_attributes_are_added
|
|
27
|
-
source = File.read(HELPERS_PATH)
|
|
28
|
-
|
|
29
|
-
assert_includes source, '(options[:html][:data] || {}).merge'
|
|
30
|
-
assert_not_includes source, '(function(id, label)'
|
|
31
|
-
end
|
|
32
|
-
end
|
data/test/test_configuration.rb
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
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_default_policy_requires_explicit_exclusions
|
|
109
|
-
assert_equal [], @allowed_columns
|
|
110
|
-
assert_nil @excluded_columns
|
|
111
|
-
RecordSelect.allowed_columns = @allowed_columns
|
|
112
|
-
RecordSelect.excluded_columns = @excluded_columns
|
|
113
|
-
|
|
114
|
-
error = assert_raise(ArgumentError) { config }
|
|
115
|
-
assert_match(/requires explicit :excluded_columns/, error.message)
|
|
116
|
-
assert_match(/unsafe.*hidden model columns/, error.message)
|
|
117
|
-
assert_match(/allowed_columns/, error.message)
|
|
118
|
-
assert_equal [], controller(excluded_columns: []).filters
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def test_allowlist_does_not_bypass_required_exclusions
|
|
122
|
-
RecordSelect.excluded_columns = nil
|
|
123
|
-
[nil, [], [:company_id]].each do |allowed|
|
|
124
|
-
assert_raise(ArgumentError) { config(allowed_columns: allowed) }
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
def test_explicit_nil_exclusions_do_not_fall_back_to_the_global_policy
|
|
129
|
-
RecordSelect.excluded_columns = [:password_digest]
|
|
130
|
-
assert_raise(ArgumentError) { config(excluded_columns: nil) }
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def test_controller_can_supply_exclusions_when_global_policy_is_unconfigured
|
|
134
|
-
RecordSelect.excluded_columns = nil
|
|
135
|
-
assert_equal [{'company_id' => '2'}], controller(allowed_columns: [:company_id], excluded_columns: []).filters
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
def test_global_settings_are_captured_when_the_controller_is_configured
|
|
139
|
-
RecordSelect.allowed_columns = [:id, :company_id]
|
|
140
|
-
RecordSelect.excluded_columns = :id
|
|
141
|
-
instance = controller
|
|
142
|
-
RecordSelect.allowed_columns = []
|
|
143
|
-
RecordSelect.excluded_columns = [:company_id]
|
|
144
|
-
|
|
145
|
-
assert_equal ['id', 'company_id'], instance.record_select_config.allowed_columns
|
|
146
|
-
assert_equal ['id'], instance.record_select_config.excluded_columns
|
|
147
|
-
assert_equal [{'company_id' => '2'}], instance.filters
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
def test_normalized_settings_do_not_share_the_source_arrays
|
|
151
|
-
allowed = [:id, 'company_id']
|
|
152
|
-
excluded = [:id]
|
|
153
|
-
instance = controller(allowed_columns: allowed, excluded_columns: excluded)
|
|
154
|
-
allowed.clear
|
|
155
|
-
excluded << :company_id
|
|
156
|
-
|
|
157
|
-
assert_equal [{'company_id' => '2'}], instance.filters
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def test_parameter_restrictions_do_not_limit_text_search
|
|
161
|
-
instance = controller(allowed_columns: [], excluded_columns: [:name])
|
|
162
|
-
assert_equal ['(name LIKE ?)', 'alice%'], instance.text_conditions
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
end
|