From ab972f2ae66ea29c08c62a4a7afba12fbfca845c Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Fri, 5 Jul 2024 13:22:16 +0200 Subject: [PATCH] Treat true/false as YES/NO when assigned as strings --- CHANGELOG.md | 1 + lib/datagrid/filters/extended_boolean_filter.rb | 9 ++++++--- spec/datagrid/filters/extended_boolean_filter_spec.rb | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45969844..d64ef705 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## master +* Treat true/false as YES/NO when assigned as strings for xboolean filter * Support infinite ranges for date, datetime and integer filters ## 1.8.1 diff --git a/lib/datagrid/filters/extended_boolean_filter.rb b/lib/datagrid/filters/extended_boolean_filter.rb index 20d25ed6..cdad91ac 100644 --- a/lib/datagrid/filters/extended_boolean_filter.rb +++ b/lib/datagrid/filters/extended_boolean_filter.rb @@ -3,6 +3,8 @@ class Datagrid::Filters::ExtendedBooleanFilter < Datagrid::Filters::EnumFilter YES = "YES" NO = "NO" + TRUTH_VALUES = [true, 'true', "y", "yes"] + FALSE_VALUES = [false, 'false', "n", "no"] def initialize(report, attribute, options = {}, &block) options[:select] = -> { boolean_select } @@ -15,10 +17,11 @@ def execute(value, scope, grid_object) end def parse(value) - case - when value == true + value = value.downcase if value.is_a?(String) + case value + when *TRUTH_VALUES YES - when value == false + when *FALSE_VALUES NO when value.blank? nil diff --git a/spec/datagrid/filters/extended_boolean_filter_spec.rb b/spec/datagrid/filters/extended_boolean_filter_spec.rb index 6956f96a..22e36e53 100644 --- a/spec/datagrid/filters/extended_boolean_filter_spec.rb +++ b/spec/datagrid/filters/extended_boolean_filter_spec.rb @@ -41,6 +41,10 @@ expect(grid.disabled).to eq("YES") grid.disabled = false expect(grid.disabled).to eq("NO") + grid.disabled = "true" + expect(grid.disabled).to eq("YES") + grid.disabled = "false" + expect(grid.disabled).to eq("NO") end end