Skip to content

Commit

Permalink
Fix railsadminteam#1099: presence filtering on boolean columns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Adam Rice authored and Adam Rice committed Jul 29, 2016
1 parent eefa8ac commit 39ef132
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39ef132

Please sign in to comment.