Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore ActiveModel::RangeError in Ransack conditions #1340

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/ransack/adapters/active_record/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Nodes
class Condition

def arel_predicate
attributes.map { |attribute|
predicate = attributes.map { |attribute|
association = attribute.parent
if negative? && attribute.associated_collection?
query = context.build_correlated_subquery(association)
Expand All @@ -19,6 +19,17 @@ def arel_predicate
format_predicate(attribute)
end
}.reduce(combinator_method)

if replace_right_node?(predicate)
# Replace right node object to plain integer value in order to avoid
# ActiveModel::RangeError from Arel::Node::Casted.
# The error can be ignored here because RDBMSs accept large numbers
# in condition clauses.
plain_value = predicate.right.value
predicate.right = plain_value
end

predicate
end

private
Expand Down Expand Up @@ -56,6 +67,17 @@ def format_values_for(predicate)
end
end

def replace_right_node?(predicate)
return false unless predicate.is_a?(Arel::Nodes::Binary)

arel_node = predicate.right
return false unless arel_node.is_a?(Arel::Nodes::Casted)

relation, name = arel_node.attribute.values
attribute_type = relation.type_for_attribute(name).type
attribute_type == :integer && arel_node.value.is_a?(Integer)
end

end
end
end
35 changes: 35 additions & 0 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ module Ransack
@s.awesome_eq = nil
expect(@s.result.to_sql).not_to match /WHERE/
end

it 'generates a = condition with a huge integer value' do
val = 123456789012345678901
@s.salary_eq = val
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} = #{val}/
end
end

describe 'lteq' do
Expand All @@ -56,6 +63,13 @@ module Ransack
@s.salary_lteq = nil
expect(@s.result.to_sql).not_to match /WHERE/
end

it 'generates a <= condition with a huge integer value' do
val = 123456789012345678901
@s.salary_lteq = val
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} <= #{val}/
end
end

describe 'lt' do
Expand All @@ -77,6 +91,13 @@ module Ransack
@s.salary_lt = nil
expect(@s.result.to_sql).not_to match /WHERE/
end

it 'generates a = condition with a huge integer value' do
val = 123456789012345678901
@s.salary_lt = val
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} < #{val}/
end
end

describe 'gteq' do
Expand All @@ -98,6 +119,13 @@ module Ransack
@s.salary_gteq = nil
expect(@s.result.to_sql).not_to match /WHERE/
end

it 'generates a >= condition with a huge integer value' do
val = 123456789012345678901
@s.salary_gteq = val
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} >= #{val}/
end
end

describe 'gt' do
Expand All @@ -119,6 +147,13 @@ module Ransack
@s.salary_gt = nil
expect(@s.result.to_sql).not_to match /WHERE/
end

it 'generates a > condition with a huge integer value' do
val = 123456789012345678901
@s.salary_gt = val
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} > #{val}/
end
end

describe 'cont' do
Expand Down