From 8fe8368b07bdc0fb31ae3388f4ea8c8fa63a8efb Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 25 Mar 2015 15:36:59 +0530 Subject: [PATCH] Merge @jhdavids8's failing spec for #374 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 :) --- spec/blueprints/recommendations.rb | 5 ++++ spec/ransack/search_spec.rb | 10 ++++++++ spec/support/schema.rb | 39 +++++++++++++++++++----------- 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 spec/blueprints/recommendations.rb diff --git a/spec/blueprints/recommendations.rb b/spec/blueprints/recommendations.rb new file mode 100644 index 000000000..bcab81d4c --- /dev/null +++ b/spec/blueprints/recommendations.rb @@ -0,0 +1,5 @@ +Recommendation.blueprint do + article + person + target_person { person } +end diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 058b810cf..0463254eb 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -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 diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 0c01d0e8e..36f6adf2d 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -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( @@ -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 @@ -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