moves search logic to a searchable module

This commit is contained in:
rgarcia
2016-02-05 14:10:02 +01:00
parent 8bd5985118
commit af6bfdbb46
3 changed files with 21 additions and 24 deletions

View File

@@ -0,0 +1,19 @@
module Searchable
extend ActiveSupport::Concern
included do
include PgSearch
include SearchCache
pg_search_scope :pg_search, {
against: :ignored, # not used since using a tsvector_column
using: {
tsearch: { tsvector_column: 'tsv', dictionary: "spanish", prefix: true }
},
ignoring: :accents,
ranked_by: '(:tsearch)',
order_within_rank: "#{self.table_name}.cached_votes_up DESC"
}
end
end

View File

@@ -5,8 +5,7 @@ class Debate < ActiveRecord::Base
include Conflictable
include Measurable
include Sanitizable
include PgSearch
include SearchCache
include Searchable
include Filterable
apply_simple_captcha
@@ -41,16 +40,6 @@ class Debate < ActiveRecord::Base
# Ahoy setup
visitable # Ahoy will automatically assign visit_id on create
pg_search_scope :pg_search, {
against: :ignored, # not used since the using: option has a tsvector_column
using: {
tsearch: { dictionary: "spanish", tsvector_column: 'tsv', prefix: true }
},
ignoring: :accents,
ranked_by: '(:tsearch)',
order_within_rank: "debates.cached_votes_up DESC"
}
def searchable_values
{ title => 'A',
author.username => 'B',

View File

@@ -4,8 +4,7 @@ class Proposal < ActiveRecord::Base
include Conflictable
include Measurable
include Sanitizable
include PgSearch
include SearchCache
include Searchable
include Filterable
apply_simple_captcha
@@ -44,16 +43,6 @@ class Proposal < ActiveRecord::Base
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
pg_search_scope :pg_search, {
against: :ignored, # not used since the using: option has a tsvector_column
using: {
tsearch: { dictionary: "spanish", tsvector_column: 'tsv', prefix: true }
},
ignoring: :accents,
ranked_by: '(:tsearch)',
order_within_rank: "proposals.cached_votes_up DESC"
}
def searchable_values
{ title => 'A',
question => 'B',