Only ransackable_attributes are valid for search. Fix spec and add one

to test search valid attributes. Remove unused formatter from schema.rb.
This commit is contained in:
Alejandro Babio 2013-10-29 20:16:49 -03:00
parent 47d7a95eac
commit 852c3db6c5
3 changed files with 11 additions and 5 deletions

View File

@ -19,7 +19,8 @@ module Ransack
end
def valid?
bound? && attr
bound? && attr &&
context.klassify(parent).ransackable_attributes.include?(attr_name)
end
def type
@ -50,4 +51,4 @@ module Ransack
end
end
end
end

View File

@ -59,9 +59,14 @@ module Ransack
s.result.to_sql.should_not match /ORDER BY "people"."name" \|\| "only_search" \|\| "people"."name" ASC/
end
it 'allows search by "only_search" field' do
s = Person.search(:only_search_eq => 'htimS cirA')
s.result.to_sql.should match /WHERE "people"."name" \|\| "only_search" \|\| "people"."name" = 'htimS cirA'/
end
it "can't be searched by 'only_sort'" do
s = Person.search(:only_sort_eq => 'htimS cirA')
s.result.to_sql.should_not match /'htimS cirA'/
s.result.to_sql.should_not match /WHERE "people"."name" \|\| "only_sort" \|\| "people"."name" = 'htimS cirA'/
end
end

View File

@ -29,11 +29,11 @@ class Person < ActiveRecord::Base
Arel::Nodes::InfixOperation.new('||', parent.table[:name], parent.table[:name])
end
ransacker :only_search, :formatter => proc {|v| "only_search#{v}"} do |parent|
ransacker :only_search do |parent|
Arel::Nodes::InfixOperation.new('|| "only_search" ||', parent.table[:name], parent.table[:name])
end
ransacker :only_sort, :formatter => proc {|v| "only_sort#{v}"} do |parent|
ransacker :only_sort do |parent|
Arel::Nodes::InfixOperation.new('|| "only_sort" ||', parent.table[:name], parent.table[:name])
end