Skip to content

Commit

Permalink
Merge pull request #3007 from PikachuEXE/fix/numeric-blank-support
Browse files Browse the repository at this point in the history
Fix incorrectly comparing numeric columns with empty string when handling blank operator
  • Loading branch information
mshibuya committed Apr 21, 2018
2 parents c903d2e + 0294c6a commit e4c657f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def unary_operators
case @type
when :boolean
boolean_unary_operators
when :integer, :decimal, :float
numeric_unary_operators
else
generic_unary_operators
end
Expand All @@ -181,6 +183,7 @@ def boolean_unary_operators
'_not_empty' => ["(#{@column} IS NOT NULL)"],
)
end
alias_method :numeric_unary_operators, :boolean_unary_operators

def range_filter(min, max)
if min && max
Expand Down
60 changes: 60 additions & 0 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,66 @@ def build_statement(type, value, operator)
expect(build_statement(:float, ['', '', 'word2'], 'between')).to be_nil
expect(build_statement(:float, ['', 'word3', 'word4'], 'between')).to be_nil
end

it "supports '_blank' operator" do
[['_blank', ''], ['', '_blank']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NULL)"])
end
end
end

it "supports '_present' operator" do
[['_present', ''], ['', '_present']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NOT NULL)"])
end
end
end

it "supports '_null' operator" do
[['_null', ''], ['', '_null']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NULL)"])
end
end
end

it "supports '_not_null' operator" do
[['_not_null', ''], ['', '_not_null']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NOT NULL)"])
end
end
end

it "supports '_empty' operator" do
[['_empty', ''], ['', '_empty']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NULL)"])
end
end
end

it "supports '_not_empty' operator" do
[['_not_empty', ''], ['', '_not_empty']].each do |value, operator|
aggregate_failures do
expect(build_statement(:integer, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:decimal, value, operator)).to eq(["(field IS NOT NULL)"])
expect(build_statement(:float, value, operator)).to eq(["(field IS NOT NULL)"])
end
end
end
end

describe 'date type queries' do
Expand Down

0 comments on commit e4c657f

Please sign in to comment.