Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement belongs_to lookup.

Fucking christ that was so much easier than has_and_belongs_to_many
  • Loading branch information...
commit bbe63e849594f44fcfaea9915a5321068abc8f49 1 parent 3ee4f81
@radar authored
View
12 lib/searcher/class_methods.rb
@@ -14,11 +14,13 @@ def search(query)
if piece.include?(":")
external, q = piece.split(":")
external = @config[:externals][external.to_sym]
+ next unless external
# Find the type of association we're dealing with
association = reflect_on_association(external[:from])
association_table = Table(association.klass.table_name)
case association.macro
+
when :has_and_belongs_to_many
join_table = Table(association.options[:join_table])
@@ -31,6 +33,16 @@ def search(query)
klass.find_by_sql(table.join(join_table).on(jtl.eq(jtr)).
join(association_table).on(other_primary_key.eq(association_foreign_key)).
where(association_table[external[:field]].eq(q)).to_sql)
+
+ when :belongs_to
+ foreign_key = table[association.primary_key_name]
+ other_primary_key = association_table[association.klass.primary_key]
+
+ sql = table.join(association_table).on(foreign_key.eq(other_primary_key)).
+ where(association_table[external[:field]].eq(q)).to_sql
+
+ klass.find_by_sql(table.join(association_table).on(foreign_key.eq(other_primary_key)).
+ where(association_table[external[:field]].eq(q)).to_sql)
end
else
View
BIN  searcher.sqlite3
Binary file not shown
View
12 spec/fixtures/models.rb
@@ -1,10 +1,11 @@
class Ticket < ActiveRecord::Base
has_and_belongs_to_many :tags
- belongs_to :tag
+ belongs_to :state
search_config do
default :description
external :tag, :from => :tags, :field => "name"
+ external :state, :from => :state, :field => "name"
end
end
@@ -16,10 +17,13 @@ class State < ActiveRecord::Base
has_many :tickets
end
+#############
+### SEEDS ###
+#############
-## seed data:
-
-# Ticket with a description, no tag
+# Ticket with a description, tag and state.
ticket = Ticket.create(:description => "Hello world! You are awesome.")
ticket.tags << Tag.create(:name => "bug")
+ticket.state = State.create(:name => "Open")
+ticket.save!
View
3  spec/fixtures/schema.rb
@@ -3,6 +3,7 @@
create_table :tickets, :force => true do |t|
t.string :description
+ t.integer :state_id
t.timestamps
end
@@ -14,7 +15,7 @@
t.string :name
end
- create_table :state, :force => true do |t|
+ create_table :states, :force => true do |t|
t.string :name
end
View
14 spec/searcher_spec.rb
@@ -7,15 +7,23 @@
context "default field" do
subject { Ticket.search("Hello") }
- it "first result" do
+ it "finds a ticket" do
first_result.description.should eql("Hello world! You are awesome.")
end
end
- context "label search" do
+ context "habtm label search" do
subject { Ticket.search("tag:bug") }
- it "has_and_belongs_to_many" do
+ it "finds a ticket" do
first_result.description.should eql("Hello world! You are awesome.")
end
end
+
+ context "belongs_to label search" do
+ subject { Ticket.search("state:Open") }
+ it "finds a ticket" do
+ first_result.description.should eql("Hello world! You are awesome.")
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.