debshots/app/models/package.rb

54 lines
1.9 KiB
Ruby

class Package < ActiveRecord::Base
# PostgreSQL-based full-text search:
# https://github.com/Casecommons/pg_search
include PgSearch
# TODO: Make search weighted on users' rating
pg_search_scope :general_search,
:against => [:name, :description, :long_description],
:using => {
:tsearch => {:dictionary => "english"}
}
# I am using "destroy_all" here so that when a package gets destroys the
# callbacks for all dependent screenshots are executed - thus removing the
# screenshot files from disk.
has_many :screenshots, :inverse_of=>:package, :dependent => :destroy
default_scope {
order('name ASC')
}
# Return a query of all packages that have screenshots
def self.with_screenshots
# Query for all packages who's ID appears in a screenshot's "package_id" field
subselect = Screenshot.select(:package_id)
where(id: subselect )
end
# Return a query of all packages that have screenshots
def self.without_screenshots
# Query for all packages who's ID does not appear in a screenshot's "package_id" field
subselect = Screenshot.select(:package_id)
where.not(id: subselect)
end
# Use PostgreSQL's full-text search capability for the user search
# TODO: check if pgSearch does this job for us
#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