Skip to content

Commit

Permalink
Treat true/false as YES/NO when assigned as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jul 5, 2024
1 parent 6ac8c19 commit ab972f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/datagrid/filters/extended_boolean_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/datagrid/filters/extended_boolean_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ab972f2

Please sign in to comment.