From 39ef1321aaf1a5ced69def879cffd11ee28c4bdc Mon Sep 17 00:00:00 2001 From: Adam Rice Date: Mon, 25 Jul 2016 13:43:10 +1000 Subject: [PATCH] Fix #1099: presence filtering on boolean columns Filtering a boolean field on "Is present" or "Is blank" produces SQL that includes checking against an empty string. Since a boolean field can only be true, false, or null, this causes errors in some databases. This commit removes the empty string search for boolean field types. --- lib/rails_admin/adapters/active_record.rb | 44 ++++++++++++++++------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/rails_admin/adapters/active_record.rb b/lib/rails_admin/adapters/active_record.rb index 999fe65ee3..77b6885c07 100644 --- a/lib/rails_admin/adapters/active_record.rb +++ b/lib/rails_admin/adapters/active_record.rb @@ -152,22 +152,40 @@ def build_statement(column, type, value, operator) end class StatementBuilder < RailsAdmin::AbstractModel::StatementBuilder - protected + protected - def unary_operators - { - '_blank' => ["(#{@column} IS NULL OR #{@column} = '')"], - '_present' => ["(#{@column} IS NOT NULL AND #{@column} != '')"], - '_null' => ["(#{@column} IS NULL)"], - '_not_null' => ["(#{@column} IS NOT NULL)"], - '_empty' => ["(#{@column} = '')"], - '_not_empty' => ["(#{@column} != '')"], - } - end + def unary_operators + case @type + when :boolean + boolean_unary_operators + else + generic_unary_operators + end + end - private + private + + def generic_unary_operators + { + '_blank' => ["(#{@column} IS NULL OR #{@column} = '')"], + '_present' => ["(#{@column} IS NOT NULL AND #{@column} != '')"], + '_null' => ["(#{@column} IS NULL)"], + '_not_null' => ["(#{@column} IS NOT NULL)"], + '_empty' => ["(#{@column} = '')"], + '_not_empty' => ["(#{@column} != '')"] + } + end + + def boolean_unary_operators + generic_unary_operators.merge( + { + '_blank' => ["(#{@column} IS NULL)"], + '_present' => ["(#{@column} IS NOT NULL)"] + } + ) + end - def range_filter(min, max) + def range_filter(min, max) if min && max ["(#{@column} BETWEEN ? AND ?)", min, max] elsif min