Class: Gluttonberg::Search
- Inherits:
-
Object
- Object
- Gluttonberg::Search
- Defined in:
- app/models/gluttonberg/search.rb
Class Method Summary collapse
-
.find(query, opts = {}) ⇒ Object
if search engine is provided the use its custom methods otherwise use texticle for postgresql and like queries for mysql opts = { :sources => [], :published_only => true, :per_page => 20, :page => 1 }.
- .find_in_mysql(query, page_num, per_page, models, published_only) ⇒ Object
- .find_in_postgresql(query, page_num, per_page, models, published_only) ⇒ Object
Class Method Details
.find(query, opts = {}) ⇒ Object
if search engine is provided the use its custom methods otherwise use texticle for postgresql and like queries for mysql opts =
:sources => [],
:published_only => true,
:per_page => 20,
:page => 1
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/gluttonberg/search.rb', line 32 def self.find(query, opts = {} ) query= query.gsub(/[\\\!\*″′‟‛„‚”“”˝\(\)\;\:\.\@\&\=\+\$\,\/?\%\#\[\]]/, '') query = query.gsub(/'/, "\\\\'") query = query.gsub(/"/, "\\\"") models = {} sources = opts[:sources] published_only = opts[:published_only].blank? ? true : opts[:published_only] per_page = opts[:per_page].blank? ? Gluttonberg::Setting.get_setting("number_of_per_page_items") : opts[:per_page] page_num = opts[:page] # if sources are provided then only look in sources models. It is only required when user want to search in specified models. if sources.blank? models = Rails.configuration.search_models else sources.each do |src| models[src] = Rails.configuration.search_models[src] end end case self.dbms_name when "mysql" find_in_mysql(query, page_num , per_page , models , published_only) when "postgresql" find_in_postgresql(query, page_num , per_page, models , published_only) end end |
.find_in_mysql(query, page_num, per_page, models, published_only) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/gluttonberg/search.rb', line 59 def self.find_in_mysql(query, page_num , per_page , models , published_only) results = [] prepared_query = "'%#{query}%'" models.each do |model , columns| conditions = "" columns.each do |col| conditions << " OR " unless conditions.blank? conditions << " #{col} LIKE #{prepared_query}" end model = eval(model) #convert class name from sting to a constant if published_only == true && model.respond_to?(:published) results << model.published.find(:all , :conditions => conditions ) else results << model.find(:all , :conditions => conditions ) end end results = results.flatten results.uniq! replace_contents_with_page(results).paginate(:per_page => per_page , :page => page_num ) end |
.find_in_postgresql(query, page_num, per_page, models, published_only) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/gluttonberg/search.rb', line 81 def self.find_in_postgresql(query, page_num , per_page , models , published_only) results = [] models.each do |model , columns| model = eval(model) #convert class name from sting to a constant if published_only == true && model.respond_to?(:published) results << model.published.search(query) else results << model.search(query) end end results = results.flatten results.uniq! replace_contents_with_page(results).paginate(:per_page => per_page , :page => page_num ) end |