Add search method to polls

So far the method does not take questions nor answers into account.

This way we'll be able to search polls in the SDG Management section.
This commit is contained in:
Javi Martín
2020-12-16 14:24:24 +01:00
parent 00910b82a3
commit 852014e478
7 changed files with 75 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ class Poll < ApplicationRecord
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
include Notifiable
include Searchable
include Sluggable
include StatsVersionable
include Reportable
@@ -175,4 +176,20 @@ class Poll < ApplicationRecord
def budget_poll?
budget.present?
end
def searchable_translations_definitions
{
name => "A",
summary => "C",
description => "D"
}
end
def searchable_values
searchable_globalized_values
end
def self.search(terms)
pg_search(terms)
end
end

View File

@@ -0,0 +1,5 @@
class AddTsvToPolls < ActiveRecord::Migration[5.2]
def change
add_column :polls, :tsv, :tsvector
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_11_24_145559) do
ActiveRecord::Schema.define(version: 2020_12_16_132234) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@@ -1166,6 +1166,7 @@ ActiveRecord::Schema.define(version: 2020_11_24_145559) do
t.integer "budget_id"
t.string "related_type"
t.integer "related_id"
t.tsvector "tsv"
t.index ["budget_id"], name: "index_polls_on_budget_id", unique: true
t.index ["related_type", "related_id"], name: "index_polls_on_related_type_and_related_id"
t.index ["starts_at", "ends_at"], name: "index_polls_on_starts_at_and_ends_at"

View File

@@ -6,6 +6,7 @@ namespace :consul do
desc "Runs tasks needed to upgrade from 1.2.0 to 1.3.0"
task "execute_release_1.3.0_tasks": [
"db:load_sdg"
"db:load_sdg",
"db:calculate_tsv"
]
end

View File

@@ -10,4 +10,9 @@ namespace :db do
ApplicationLogger.new.info "Adding Sustainable Development Goals content"
load(Rails.root.join("db", "sdg.rb"))
end
desc "Calculates the TSV column for all polls"
task calculate_tsv: :environment do
Poll.find_each(&:calculate_tsvector)
end
end

View File

@@ -32,3 +32,22 @@ describe "rake db:load_sdg" do
expect(SDG::Target.last.id).to eq target_id
end
end
describe "rake db:calculate_tsv" do
before { Rake::Task["db:calculate_tsv"].reenable }
let :run_rake_task do
Rake.application.invoke_task("db:calculate_tsv")
end
it "calculates the tsvector for polls" do
poll = create(:poll)
poll.update_column(:tsv, nil)
expect(poll.reload.tsv).to be nil
run_rake_task
expect(poll.reload.tsv).not_to be nil
end
end

View File

@@ -433,4 +433,29 @@ describe Poll do
expect(poll.recounts_confirmed?).to be true
end
end
describe ".search" do
let!(:square) do
create(:poll, name: "Square reform", summary: "Next to the park", description: "Give it more space")
end
let!(:park) do
create(:poll, name: "New park", summary: "Green spaces", description: "Next to the square")
end
it "returns only matching polls" do
expect(Poll.search("reform")).to eq [square]
expect(Poll.search("green")).to eq [park]
expect(Poll.search("nothing here")).to be_empty
end
it "gives more weight to name" do
expect(Poll.search("square")).to eq [square, park]
expect(Poll.search("park")).to eq [park, square]
end
it "gives more weight to summary than description" do
expect(Poll.search("space")).to eq [park, square]
end
end
end