debshots/app/models/package.rb

26 lines
808 B
Ruby

class Package < ActiveRecord::Base
has_many :screenshots, :inverse_of=>:package
default_scope {
order('name ASC')
}
# Use PostgreSQL's full-text search capability for the user search
def self.text_search(query)
if query.present?
# Add a rank column to each result
rank = <<-RANK
ts_rank(to_tsvector(packages.name), plainto_tsquery(#{sanitize(query)}))
+
ts_rank(to_tsvector(packages.description), plainto_tsquery(#{sanitize(query)}))
RANK
# Do the full-text search and return results by rank
where("to_tsvector('english', packages.name) @@ plainto_tsquery(:q) or
to_tsvector('english', packages.description) @@ plainto_tsquery(:q)
", q: query).order("#{rank} desc")
else
all
end
end
end