Skip to content

Commit

Permalink
Add test coverage for :joins through a has_many association
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Hutchins & Peter Jaros committed Dec 13, 2010
1 parent 4e85a55 commit 463a9c8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/pg_search/scope_options.rb
Expand Up @@ -42,8 +42,8 @@ def joins
if @config.joins
foreign_againsts = @config.columns.select(&:foreign?)
selects = foreign_againsts.map{|column| "string_agg(#{column.full_name}, ' ') AS #{column.alias}" }.join(', ')
arel = model.joins(@config.joins).select("#{primary_key}, #{selects}").group(primary_key)
"LEFT OUTER JOIN (#{arel.to_sql}) pg_search_agg ON pg_search_agg.id = #{primary_key}"
relation = model.joins(@config.joins).select("#{primary_key}, #{selects}").group(primary_key)
"LEFT OUTER JOIN (#{relation.to_sql}) pg_search_agg ON pg_search_agg.id = #{primary_key}"
end
end

Expand Down
51 changes: 48 additions & 3 deletions spec/pg_search_spec.rb
Expand Up @@ -321,11 +321,58 @@
it "returns rows that match the query in either its own columns or the columns of the associated model" do
associated = associated_model.create!(:title => 'abcdef')
included = [
model_with_belongs_to.create!(:title => 'abcdef', :another_model => associated),
model_with_belongs_to.create!(:title => 'ghijkl', :another_model => associated),
model_with_belongs_to.create!(:title => 'abcdef')
]
excluded = model_with_belongs_to.create!(:title => 'mnopqr',
:another_model => associated_model.create!(:title => 'stuvwx'))

results = model_with_belongs_to.with_associated('abcdef')
results.should =~ included
results.should_not include(excluded)
end
end

context "through a has_many association" do
with_model :associated_model_with_has_many do
table do |t|
t.string 'title'
t.belongs_to 'model_with_has_many'
end
end

with_model :model_with_has_many do
table do |t|
t.string 'title'
end

model do
include PgSearch
has_many :other_models, :class_name => 'AssociatedModelWithHasMany', :foreign_key => 'model_with_has_many_id'

pg_search_scope :with_associated, :against => [:title, :"#{AssociatedModelWithHasMany.table_name}.title"], :joins => :other_models
end
end

it "returns rows that match the query in either its own columns or the columns of the associated model" do
included = [
model_with_has_many.create!(:title => 'abcdef', :other_models => [
associated_model_with_has_many.create!(:title => 'foo'),
associated_model_with_has_many.create!(:title => 'bar')
]),
model_with_has_many.create!(:title => 'ghijkl', :other_models => [
associated_model_with_has_many.create!(:title => 'foo bar'),
associated_model_with_has_many.create!(:title => 'mnopqr')
]),
model_with_has_many.create!(:title => 'foo bar')
]
excluded = model_with_has_many.create!(:title => 'stuvwx', :other_models => [
associated_model_with_has_many.create!(:title => 'abcdef')
])

results = model_with_has_many.with_associated('foo bar')
results.should =~ included
results.should_not include(excluded)
end
end
end
Expand Down Expand Up @@ -383,8 +430,6 @@
end
end



it "returns rows that match the query and that are prefixed by the query" do
included = model_with_pg_search.create!(:title => 'prefix')
excluded = model_with_pg_search.create!(:title => 'postfix')
Expand Down

0 comments on commit 463a9c8

Please sign in to comment.