diff --git a/app/models/poll.rb b/app/models/poll.rb index 0289a0fc2..08e11f854 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -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 diff --git a/db/migrate/20201216132234_add_tsv_to_polls.rb b/db/migrate/20201216132234_add_tsv_to_polls.rb new file mode 100644 index 000000000..a629bf975 --- /dev/null +++ b/db/migrate/20201216132234_add_tsv_to_polls.rb @@ -0,0 +1,5 @@ +class AddTsvToPolls < ActiveRecord::Migration[5.2] + def change + add_column :polls, :tsv, :tsvector + end +end diff --git a/db/schema.rb b/db/schema.rb index 5f69e9ac4..58023c618 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/tasks/consul.rake b/lib/tasks/consul.rake index 911460c68..4f169c387 100644 --- a/lib/tasks/consul.rake +++ b/lib/tasks/consul.rake @@ -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 diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index a64643adc..6413fc976 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -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 diff --git a/spec/lib/tasks/db_spec.rb b/spec/lib/tasks/db_spec.rb index 6581ba7a5..d4a725d29 100644 --- a/spec/lib/tasks/db_spec.rb +++ b/spec/lib/tasks/db_spec.rb @@ -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 diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 0a1fc94bd..1223e53d6 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -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