Permalink
Browse files

Move code into after_initialize block so that we can define scopes ra…

…ther than doing it on the fly.
  • Loading branch information...
1 parent 34f3b37 commit f1807ee7cb94133d7590593ddc0f5eccd80952dc @radar committed Oct 4, 2010
Showing with 38 additions and 28 deletions.
  1. +26 −1 lib/searcher.rb
  2. +12 −27 lib/searcher/class_methods.rb
View
@@ -2,7 +2,32 @@
require 'searcher/class_methods'
module Searcher
- # Your code goes here...
+ mattr_accessor :classes
+ def self.classes
+ @classes ||= []
+ end
end
ActiveRecord::Base.extend(Searcher::ClassMethods)
+
+ActiveSupport.on_load(:after_initialize) do
+ p Searcher.classes
+ # @externals].each do |name, config|
+ # association = reflect_on_association(config[:from])
+ # association_table = Table(association.klass.table_name)
+ # case association.macro
+ # when :has_and_belongs_to_many
+ # join_table = Table(association.options[:join_table])
+ #
+ # jtl = table[klass.primary_key]
+ # jtr = join_table[association.primary_key_name]
+ #
+ # other_primary_key = association_table[association.klass.primary_key]
+ # association_foreign_key = join_table[association.association_foreign_key]
+ #
+ # self.send(:scope, "by_#{config[:field]}", lambda { |q| 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)) })
+ # end
+ # end
+end
@@ -3,6 +3,7 @@
module Searcher
module ClassMethods
def searcher(&block)
+ Searcher.classes << self
@config ||= Searcher::Config.new.instance_exec(&block)
end
@@ -15,35 +16,19 @@ def search(query)
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])
-
- jtl = table[klass.primary_key]
- jtr = join_table[association.primary_key_name]
-
- other_primary_key = association_table[association.klass.primary_key]
- association_foreign_key = join_table[association.association_foreign_key]
-
- 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]
+ send("by_#{external[:field]}", q)
- 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
+ # 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
k.where(table[@config[:default]].matches_any("%#{piece}%"))

0 comments on commit f1807ee

Please sign in to comment.