Skip to content

Commit

Permalink
Merge @jhdavids8's failing spec for #374
Browse files Browse the repository at this point in the history
and clean up schema.rb a bit.

Thanks to Jamie Davidson for the test!

This is to help anyone who wants / needs to fix #374.

- Fork Ransack to your repo

- Open the file `ransack/spec/ransack/search_spec.rb` and uncomment the
test beginning at line 220

- Set the Rails version you'd like to test in ransack's Gemfile
('4-1-stable', '4.2.1', etc.)

- In the console, run `bundle install` then `bundle exec rake spec`

- Write a fix that makes this test pass!

- Send a pull request :)
  • Loading branch information
jonatack committed Mar 25, 2015
1 parent 29bc5b8 commit 8fe8368
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions spec/blueprints/recommendations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Recommendation.blueprint do
article
person
target_person { person }
end
10 changes: 10 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ module Ransack
children_people_name_field} = 'Ernie'/
end

# Uncomment the following failing spec by Jamie Davidson / @jhdavids8
# for testing issue #374:
# https://github.com/activerecord-hackery/ransack/issues/374
#
# it 'evaluates conditions for multiple belongs_to associations to the same table contextually' do
# search = Search.new(Recommendation, person_name_eq: 'Ernie', target_person_parent_name_eq: 'Test')
# search.result.should be_an ActiveRecord::Relation
# search.result.to_sql.should == "SELECT \"recommendations\".* FROM \"recommendations\" LEFT OUTER JOIN \"people\" ON \"people\".\"id\" = \"recommendations\".\"person_id\" LEFT OUTER JOIN \"people\" \"target_people_recommendations\" ON \"target_people_recommendations\".\"id\" = \"recommendations\".\"target_person_id\" LEFT OUTER JOIN \"people\" \"parents_people\" ON \"parents_people\".\"id\" = \"target_people_recommendations\".\"parent_id\" WHERE ((\"people\".\"name\" = 'Ernie' AND \"parents_people\".\"name\" = 'Test'))"
# end

it 'evaluates compound conditions contextually' do
search = Search.new(Person, :children_name_or_name_eq => 'Ernie').result
expect(search).to be_an ActiveRecord::Relation
Expand Down
39 changes: 25 additions & 14 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'active_record'

case ENV['DB'].downcase
case ENV['DB'].try(:downcase)
when 'mysql', 'mysql2'
# To test with MySQL: `DB=mysql bundle exec rake spec`
ActiveRecord::Base.establish_connection(
Expand Down Expand Up @@ -113,6 +113,12 @@ class Article < ActiveRecord::Base
end
end

class Recommendation < ActiveRecord::Base
belongs_to :person
belongs_to :target_person, class_name: 'Person'
belongs_to :article
end

module Namespace
class Article < ::Article

Expand Down Expand Up @@ -161,33 +167,38 @@ def self.create
end

create_table :articles, :force => true do |t|
t.integer :person_id
t.string :title
t.text :subject_header
t.text :body
t.integer :person_id
t.string :title
t.text :subject_header
t.text :body
end

create_table :comments, :force => true do |t|
t.integer :article_id
t.integer :person_id
t.text :body
t.integer :article_id
t.integer :person_id
t.text :body
end

create_table :tags, :force => true do |t|
t.string :name
t.string :name
end

create_table :articles_tags, :force => true, :id => false do |t|
t.integer :article_id
t.integer :tag_id
t.integer :article_id
t.integer :tag_id
end

create_table :notes, :force => true do |t|
t.integer :notable_id
t.string :notable_type
t.string :note
t.integer :notable_id
t.string :notable_type
t.string :note
end

create_table :recommendations, :force => true do |t|
t.integer :person_id
t.integer :target_person_id
t.integer :article_id
end
end

10.times do
Expand Down

0 comments on commit 8fe8368

Please sign in to comment.