Commit 4280df894b6bac6bed15b60c29ba270e13e31c8d
Committed by
Antonio Terceiro
1 parent
998a91fa
Exists in
master
and in
22 other branches
class ActsAsSearchable transformed into a module
(ActionItem1519)
Showing
1 changed file
with
35 additions
and
28 deletions
Show diff stats
lib/acts_as_searchable.rb
| 1 | -class << ActiveRecord::Base | ||
| 2 | - | ||
| 3 | - def acts_as_searchable(options = {}) | ||
| 4 | - acts_as_ferret({ :remote => true }.merge(options)) | ||
| 5 | - def find_by_contents(query, ferret_options = {}, db_options = {}) | ||
| 6 | - pg_options = {} | ||
| 7 | - if ferret_options[:page] | ||
| 8 | - pg_options[:page] = ferret_options.delete(:page) | ||
| 9 | - end | ||
| 10 | - if ferret_options[:per_page] | ||
| 11 | - pg_options[:per_page] = ferret_options.delete(:per_page) | ||
| 12 | - end | 1 | +module ActsAsSearchable |
| 2 | + | ||
| 3 | + module ClassMethods | ||
| 4 | + def acts_as_searchable(options = {}) | ||
| 5 | + acts_as_ferret({ :remote => true }.merge(options)) | ||
| 6 | + extend FindByContents | ||
| 7 | + end | ||
| 13 | 8 | ||
| 14 | - ferret_options[:limit] = :all | ||
| 15 | - | ||
| 16 | - # FIXME this is a HORRIBLE HACK | ||
| 17 | - ids = find_ids_with_ferret(query, ferret_options)[1][0..8000].map{|r|r[:id].to_i} | 9 | + module FindByContents |
| 10 | + def find_by_contents(query, ferret_options = {}, db_options = {}) | ||
| 11 | + pg_options = {} | ||
| 12 | + if ferret_options[:page] | ||
| 13 | + pg_options[:page] = ferret_options.delete(:page) | ||
| 14 | + end | ||
| 15 | + if ferret_options[:per_page] | ||
| 16 | + pg_options[:per_page] = ferret_options.delete(:per_page) | ||
| 17 | + end | ||
| 18 | 18 | ||
| 19 | - if ids.empty? | ||
| 20 | - ids << -1 | ||
| 21 | - end | 19 | + ferret_options[:limit] = :all |
| 22 | 20 | ||
| 23 | - if db_options[:conditions] | ||
| 24 | - db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" | ||
| 25 | - else | ||
| 26 | - db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" | ||
| 27 | - end | 21 | + # FIXME this is a HORRIBLE HACK |
| 22 | + ids = find_ids_with_ferret(query, ferret_options)[1][0..8000].map{|r|r[:id].to_i} | ||
| 28 | 23 | ||
| 29 | - pg_options[:page] ||= 1 | ||
| 30 | - result = find(:all, db_options) | ||
| 31 | - result.paginate(pg_options) | 24 | + if ids.empty? |
| 25 | + ids << -1 | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + if db_options[:conditions] | ||
| 29 | + db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" | ||
| 30 | + else | ||
| 31 | + db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" | ||
| 32 | + end | ||
| 33 | + | ||
| 34 | + pg_options[:page] ||= 1 | ||
| 35 | + result = find(:all, db_options) | ||
| 36 | + result.paginate(pg_options) | ||
| 37 | + end | ||
| 32 | end | 38 | end |
| 33 | end | 39 | end |
| 34 | - | ||
| 35 | end | 40 | end |
| 41 | + | ||
| 42 | +ActiveRecord::Base.send(:extend, ActsAsSearchable::ClassMethods) |