diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 3e38a8c..6835618 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -180,10 +180,15 @@ module Ransack def strip_predicate_and_index(str) string = str.split(/\(/).first + string = strip_before_type_cast(string) Predicate.detect_and_strip_from_string!(string) string end + def strip_before_type_cast(str) + str.split("_before_type_cast").first + end + end end end \ No newline at end of file diff --git a/spec/blueprints/notes.rb b/spec/blueprints/notes.rb index f143114..f6702ca 100644 --- a/spec/blueprints/notes.rb +++ b/spec/blueprints/notes.rb @@ -1,3 +1,5 @@ Note.blueprint do note + notable_type { "Article" } + notable_id end \ No newline at end of file diff --git a/spec/ransack/helpers/form_builder_spec.rb b/spec/ransack/helpers/form_builder_spec.rb index b00cd8e..7b8fc41 100644 --- a/spec/ransack/helpers/form_builder_spec.rb +++ b/spec/ransack/helpers/form_builder_spec.rb @@ -7,6 +7,7 @@ module Ransack router = ActionDispatch::Routing::RouteSet.new router.draw do resources :people + resources :notes get ':controller(/:action(/:id(.:format)))' end @@ -132,6 +133,22 @@ module Ransack end end end + + context 'fields used in polymorphic relations as search attributes in form' do + before do + @controller.view_context.search_form_for Note.search do |f| + @f = f + end + end + it 'accepts poly_id field' do + html = @f.text_field(:notable_id_eq) + html.should match /id=\"q_notable_id_eq\"/ + end + it 'accepts poly_type field' do + html = @f.text_field(:notable_type_eq) + html.should match /id=\"q_notable_type_eq\"/ + end + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 77d9173..b9dd2ec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,6 +20,7 @@ Sham.define do only_admin { Faker::Lorem.words(3).join(' ') } only_search { Faker::Lorem.words(3).join(' ') } only_sort { Faker::Lorem.words(3).join(' ') } + notable_id { |id| id } end RSpec.configure do |config|