Class: Gluttonberg::Search
- Inherits:
-
Object
- Object
- Gluttonberg::Search
- Defined in:
- app/models/gluttonberg/search.rb
Overview
this class do text based searching. It supports like query for mysql and native postgresql text based searching for postgresql
Class Method Summary collapse
- .find(query, opts = {}) ⇒ Object
- .find_in_mysql(query, page_num, per_page, models, published_only) ⇒ Object
- .find_in_postgresql(query, page_num, per_page, models, published_only) ⇒ Object
- .index_data_using_texticle ⇒ Object
- .load_block_classes ⇒ Object
Class Method Details
.find(query, opts = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/models/gluttonberg/search.rb', line 7 def self.find(query, opts = {} ) query= query.gsub(/[\\\!\*″′‟‛„‚”“”˝\(\)\;\:\.\@\&\=\+\$\,\/?\%\#\[\]]/, '') query = query.gsub(/'/, "\\\\'") query = query.gsub(/"/, "\\\"") published_only = opts[:published_only].blank? ? true : opts[:published_only] per_page = Gluttonberg::Setting.get_setting("number_of_per_page_items") || opts[:per_page] # if sources are provided then only look in sources models. # It is only required when user want to search in specified models. models = self.find_models(opts[:sources]) case Gluttonberg.dbms_name when "mysql" find_in_mysql(query, opts[:page] , per_page , models , published_only) when "postgresql" find_in_postgresql(query, opts[:page] , per_page, models , published_only) end end |
.find_in_mysql(query, page_num, per_page, models, published_only) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/gluttonberg/search.rb', line 26 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/gluttonberg/search.rb', line 48 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 |
.index_data_using_texticle ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/gluttonberg/search.rb', line 70 def self.index_data_using_texticle if Gluttonberg.dbms_name == "postgresql" Rails.configuration.search_models.each do |model , columns| begin model = eval(model) model.index do columns.each do |column| send(column) end end rescue end end end end |
.load_block_classes ⇒ Object
64 65 66 67 68 |
# File 'app/models/gluttonberg/search.rb', line 64 def self.load_block_classes Gluttonberg::Content::Block.classes.uniq.each do |klass| Gluttonberg.const_get klass.name.demodulize end end |