Skip to content

Commit e06a27d

Browse files
Fix enum support (#1559)
The casting of condition values is incorrect for enums. The value is a number but enum type under the hood (DB) is an integer, so the string values get casted to zero. Apparently this casting is not actually needed (except for converting dates) because ActiveRecord handles this just fine internally. So I removed it and it seems to fix enums and does not make any specs fail. Co-authored-by: Peko <[email protected]>
1 parent e7d4996 commit e06a27d

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

lib/ransack/nodes/condition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def validated_values
226226
end
227227

228228
def casted_values_for_attribute(attr)
229-
validated_values.map { |v| v.cast(predicate.type || attr.type) }
229+
validated_values.map(&:cast_array)
230230
end
231231

232232
def formatted_values_for_attribute(attr)

lib/ransack/nodes/value.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def cast(type)
4343
end
4444
end
4545

46+
def cast_array
47+
if value.is_a?(Array)
48+
cast_to_date(value)
49+
else
50+
value
51+
end
52+
end
53+
4654
def cast_to_date(val)
4755
if val.respond_to?(:to_date)
4856
val.to_date rescue nil

spec/ransack/search_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ module Ransack
342342
let(:people_name_field) {
343343
"#{quote_table_name("people")}.#{quote_column_name("name")}"
344344
}
345+
let(:people_temperament_field) {
346+
"#{quote_table_name("people")}.#{quote_column_name("temperament")}"
347+
}
345348
let(:children_people_name_field) {
346349
"#{quote_table_name("children_people")}.#{quote_column_name("name")}"
347350
}
@@ -356,6 +359,36 @@ module Ransack
356359
children_people_name_field} = 'Ernie'/
357360
end
358361

362+
context 'when evaluating enums' do
363+
before do
364+
Person.take.update_attribute(:temperament, 'choleric')
365+
end
366+
367+
it 'evaluates enum key correctly' do
368+
s = Search.new(Person, temperament_eq: 'choleric')
369+
370+
expect(s.result.to_sql).not_to match /#{
371+
people_temperament_field} = 0/
372+
373+
expect(s.result.to_sql).to match /#{
374+
people_temperament_field} = #{Person.temperaments[:choleric]}/
375+
376+
expect(s.result).not_to be_empty
377+
end
378+
379+
it 'evaluates enum value correctly' do
380+
s = Search.new(Person, temperament_eq: Person.temperaments[:choleric])
381+
382+
expect(s.result.to_sql).not_to match /#{
383+
people_temperament_field} = 0/
384+
385+
expect(s.result.to_sql).to match /#{
386+
people_temperament_field} = #{Person.temperaments[:choleric]}/
387+
388+
expect(s.result).not_to be_empty
389+
end
390+
end
391+
359392
it 'use appropriate table alias' do
360393
s = Search.new(Person, {
361394
name_eq: "person_name_query",

spec/support/schema.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Person < ApplicationRecord
8181
scope :sort_by_reverse_name_asc, lambda { order(Arel.sql("REVERSE(name) ASC")) }
8282
scope :sort_by_reverse_name_desc, lambda { order("REVERSE(name) DESC") }
8383

84+
enum :temperament, { sanguine: 1, choleric: 2, melancholic: 3, phlegmatic: 4 }
85+
8486
alias_attribute :full_name, :name
8587

8688
ransack_alias :term, :name_or_email
@@ -278,6 +280,7 @@ def self.create
278280
t.string :new_start
279281
t.string :stop_end
280282
t.integer :salary
283+
t.integer :temperament
281284
t.date :life_start
282285
t.boolean :awesome, default: false
283286
t.boolean :terms_and_conditions, default: false

0 commit comments

Comments
 (0)