Skip to content

Commit

Permalink
Hide present / blank filter options for required fields
Browse files Browse the repository at this point in the history
For cases where a field or association is required (via validations, model config, or otherwise) and is filterable, the associated filter dropdown includes 'present' and 'blank' options that are unnecessary as the field/association is always expected to be present.
  • Loading branch information
codealchemy committed Feb 20, 2021
1 parent b514455 commit 0033327
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Lint/UnneededSplatExpansion:
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 1081
Exclude:
- 'spec/integration/actions/index_spec.rb'

# Offense count: 7
Naming/AccessorMethodName:
Expand Down
54 changes: 38 additions & 16 deletions app/assets/javascripts/rails_admin/ra.filter-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var field_value = options['value'];
var field_operator = options['operator'];
var select_options = options['select_options'];
var required = options['required'];
var index = options['index'];
var value_name = 'f[' + field_name + '][' + index + '][v]';
var operator_name = 'f[' + field_name + '][' + index + '][o]';
Expand All @@ -24,9 +25,13 @@
.append('<option value="_discard">...</option>')
.append($('<option value="true"></option>').prop('selected', field_value == "true").text(RailsAdmin.I18n.t("true")))
.append($('<option value="false"></option>').prop('selected', field_value == "false").text(RailsAdmin.I18n.t("false")))
.append('<option disabled="disabled">---------</option>')
.append($('<option value="_present"></option>').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")))
.append($('<option value="_blank"></option>').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank")));
if (!required) {
control.append([
'<option disabled="disabled">---------</option>',
$('<option value="_present"></option>').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")),
$('<option value="_blank"></option>').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank"))
])
}
break;
case 'date':
additional_control =
Expand Down Expand Up @@ -56,9 +61,13 @@
.append($('<option value="yesterday"></option>').prop('selected', field_operator == "yesterday").text(RailsAdmin.I18n.t("yesterday")))
.append($('<option value="this_week"></option>').prop('selected', field_operator == "this_week").text(RailsAdmin.I18n.t("this_week")))
.append($('<option value="last_week"></option>').prop('selected', field_operator == "last_week").text(RailsAdmin.I18n.t("last_week")))
.append('<option disabled="disabled">---------</option>')
.append($('<option value="_not_null"></option>').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")))
.append($('<option value="_null"></option>').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank")));
if (!required) {
control.append([
'<option disabled="disabled">---------</option>',
$('<option value="_not_null"></option>').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")),
$('<option value="_null"></option>').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank"))
])
}
additional_control = additional_control ||
$('<input size="25" class="datetime additional-fieldset default input-sm form-control" type="text" />')
.css('display', (!field_operator || field_operator == "default") ? 'inline-block' : 'none')
Expand All @@ -84,10 +93,14 @@
.prop('name', multiple_values ? undefined : value_name)
.data('name', value_name)
.append('<option value="_discard">...</option>')
.append($('<option value="_present"></option>').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")))
.append($('<option value="_blank"></option>').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank")))
.append('<option disabled="disabled">---------</option>')
.append(select_options)
if (!required) {
control.append([
$('<option value="_present"></option>').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")),
$('<option value="_blank"></option>').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank")),
'<option disabled="disabled">---------</option>'
])
}
control.append(select_options)
.add(
$('<select multiple="multiple" class="select-multiple input-sm form-control"></select>')
.css('display', multiple_values ? 'inline-block' : 'none')
Expand All @@ -111,9 +124,13 @@
.append($('<option data-additional-fieldset="additional-fieldset" value="is"></option>').prop('selected', field_operator == "is").text(RailsAdmin.I18n.t("is_exactly")))
.append($('<option data-additional-fieldset="additional-fieldset" value="starts_with"></option>').prop('selected', field_operator == "starts_with").text(RailsAdmin.I18n.t("starts_with")))
.append($('<option data-additional-fieldset="additional-fieldset" value="ends_with"></option>').prop('selected', field_operator == "ends_with").text(RailsAdmin.I18n.t("ends_with")))
.append('<option disabled="disabled">---------</option>')
.append($('<option value="_present"></option>').prop('selected', field_operator == "_present").text(RailsAdmin.I18n.t("is_present")))
.append($('<option value="_blank"></option>').prop('selected', field_operator == "_blank").text(RailsAdmin.I18n.t("is_blank")));
if (!required) {
control.append([
'<option disabled="disabled">---------</option>',
$('<option value="_present"></option>').prop('selected', field_operator == "_present").text(RailsAdmin.I18n.t("is_present")),
$('<option value="_blank"></option>').prop('selected', field_operator == "_blank").text(RailsAdmin.I18n.t("is_blank"))
])
}
additional_control = $('<input class="additional-fieldset input-sm form-control" type="text" />')
.css('display', field_operator == "_present" || field_operator == "_blank" ? 'none' : 'inline-block')
.prop('name', value_name)
Expand All @@ -126,9 +143,13 @@
.prop('name', operator_name)
.append($('<option data-additional-fieldset="default" value="default"></option>').prop('selected', field_operator == "default").text(RailsAdmin.I18n.t("number")))
.append($('<option data-additional-fieldset="between" value="between"></option>').prop('selected', field_operator == "between").text(RailsAdmin.I18n.t("between_and_")))
.append('<option disabled="disabled">---------</option>')
.append($('<option value="_not_null"></option>').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")))
.append($('<option value="_null"></option>').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank")));
if (!required) {
control.append([
'<option disabled="disabled">---------</option>',
$('<option value="_not_null"></option>').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")),
$('<option value="_null"></option>').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank"))
])
}
additional_control =
$('<input class="additional-fieldset default input-sm form-control" type="text" />')
.css('display', (!field_operator || field_operator == "default") ? 'inline-block' : 'none')
Expand Down Expand Up @@ -193,6 +214,7 @@
value: $(this).data('field-value'),
operator: $(this).data('field-operator'),
select_options: $(this).data('field-options'),
required: $(this).data('field-required'),
index: $.now().toString().slice(6,11),
datetimepicker_format: $(this).data('field-datetimepicker-format')
});
Expand Down
1 change: 1 addition & 0 deletions app/helpers/rails_admin/main_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def ordered_filter_options
options[:value] = filter_hash['v']
options[:label] = field.label
options[:operator] = filter_hash['o']
options[:required] = field.required
options
end if ordered_filters
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- else
- ''
%li
%a{href: '#', :"data-field-label" => field.label, :"data-field-name" => field.name, :"data-field-options" => field_options.html_safe, :"data-field-type" => field.type, :"data-field-value" => "", :"data-field-datetimepicker-format" => (field.try(:parser) && field.parser.to_momentjs)}= capitalize_first_letter(field.label)
%a{href: '#', :"data-field-label" => field.label, :"data-field-name" => field.name, :"data-field-options" => field_options.html_safe, :"data-field-required" => field.required.to_s, :"data-field-type" => field.type, :"data-field-value" => "", :"data-field-datetimepicker-format" => (field.try(:parser) && field.parser.to_momentjs)}= capitalize_first_letter(field.label)
%style
- properties.select{ |p| p.column_width.present? }.each do |property|
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/actions/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@
@comment = FactoryBot.create(:comment, commentable: @players[2])
end

it 'hides redundant filter options for required fields', js: true do
RailsAdmin.config Player do
list do
field :name do
required true
end
field :team
end
end

visit index_path(model_name: 'player', f: {name: {'1' => {v: ''}}, team: {'2' => {v: ''}}})

within(:select, name: 'f[name][1][o]') do
expect(page.all('option').map(&:value)).to_not include('_present', '_blank')
end

within(:select, name: 'f[team][2][o]') do
expect(page.all('option').map(&:value)).to include('_present', '_blank')
end
end

it 'allows to query on any attribute' do
RailsAdmin.config Player do
list do
Expand Down Expand Up @@ -294,6 +315,7 @@
type: 'string',
value: '',
operator: nil,
required: true,
},
{
index: 2,
Expand All @@ -302,6 +324,7 @@
type: 'belongs_to_association',
value: '',
operator: nil,
required: false,
},
]
end
Expand Down

0 comments on commit 0033327

Please sign in to comment.