Permalink
Browse files

Refactor searcher to define named_scopes on initialization

  • Loading branch information...
1 parent f1807ee commit 752a8a56c38416591310cd78153fd2f74515cab8 @radar committed Oct 4, 2010
Showing with 22 additions and 40 deletions.
  1. +11 −19 lib/searcher.rb
  2. +7 −20 lib/searcher/class_methods.rb
  3. BIN searcher.sqlite3
  4. +4 −1 spec/spec_helper.rb
View
@@ -11,23 +11,15 @@ def self.classes
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
+ Searcher.classes.each do |klass|
+ table = Table(klass.table_name)
+ klass.searcher[:externals].each do |name, config|
+ association = klass.reflect_on_association(config[:from])
+ association_table = Table(association.klass.table_name)
+ if [:has_and_belongs_to_many, :belongs_to].include?(association.macro)
+ scope = lambda { |q, field| klass.joins(config[:from]).where(association_table[field].eq(q)) }
+ klass.scope "by_#{name}", scope
+ end
+ end
+ end
end
@@ -3,35 +3,22 @@
module Searcher
module ClassMethods
def searcher(&block)
- Searcher.classes << self
+ Searcher.classes << self unless Searcher.classes.include?(self)
@config ||= Searcher::Config.new.instance_exec(&block)
end
def search(query)
klass = self
- table = Table(klass.table_name)
-
+
result = query.split(" ").inject(klass) do |k, piece|
if piece.include?(":")
- external, q = piece.split(":")
- external = @config[:externals][external.to_sym]
- next unless external
- send("by_#{external[:field]}", q)
-
-
- # 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
+ name, q = piece.split(":")
+ external = @config[:externals][name.to_sym]
+ next unless external
+ send("by_#{name}", q, external[:field])
else
- k.where(table[@config[:default]].matches_any("%#{piece}%"))
+ k.where(klass.arel_table[@config[:default]].matches_any("%#{piece}%"))
end
end
View
Binary file not shown.
View
@@ -13,4 +13,7 @@
load here + '/fixtures/schema.rb'
# Load the models
-require here + '/fixtures/models'
+require here + '/fixtures/models'
+
+# Call the after_initialize hook defined in lib/searcher.rb
+ActiveSupport.run_load_hooks(:after_initialize)

0 comments on commit 752a8a5

Please sign in to comment.