From af6bfdbb46baf35482351c7715eafb48216e78e1 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 5 Feb 2016 14:10:02 +0100 Subject: [PATCH] moves search logic to a searchable module --- app/models/concerns/searchable.rb | 19 +++++++++++++++++++ app/models/debate.rb | 13 +------------ app/models/proposal.rb | 13 +------------ 3 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 app/models/concerns/searchable.rb diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb new file mode 100644 index 000000000..4d717959e --- /dev/null +++ b/app/models/concerns/searchable.rb @@ -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 \ No newline at end of file diff --git a/app/models/debate.rb b/app/models/debate.rb index 30c13f709..c811b4925 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -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', diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 17a6e2305..5d3a693c6 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -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',