diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index f1a2e12c1..abf8875a7 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -5,6 +5,7 @@ class Legislation::Process < ApplicationRecord include Imageable include Documentable include SDG::Relatable + include Searchable acts_as_paranoid column: :hidden_at acts_as_taggable_on :customs @@ -123,6 +124,22 @@ class Legislation::Process < ApplicationRecord end end + def searchable_translations_definitions + { + title => "A", + summary => "C", + description => "D" + } + end + + def searchable_values + searchable_globalized_values + end + + def self.search(terms) + pg_search(terms) + end + private def valid_date_ranges diff --git a/db/migrate/20201216132642_add_tsv_to_legislation_processes.rb b/db/migrate/20201216132642_add_tsv_to_legislation_processes.rb new file mode 100644 index 000000000..fdeb8fdd5 --- /dev/null +++ b/db/migrate/20201216132642_add_tsv_to_legislation_processes.rb @@ -0,0 +1,5 @@ +class AddTsvToLegislationProcesses < ActiveRecord::Migration[5.2] + def change + add_column :legislation_processes, :tsv, :tsvector + end +end diff --git a/db/schema.rb b/db/schema.rb index 58023c618..6a67f083f 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_12_16_132234) do +ActiveRecord::Schema.define(version: 2020_12_16_132642) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -747,6 +747,7 @@ ActiveRecord::Schema.define(version: 2020_12_16_132234) do t.boolean "homepage_enabled", default: false t.text "background_color" t.text "font_color" + t.tsvector "tsv" t.index ["allegations_end_date"], name: "index_legislation_processes_on_allegations_end_date" t.index ["allegations_start_date"], name: "index_legislation_processes_on_allegations_start_date" t.index ["debate_end_date"], name: "index_legislation_processes_on_debate_end_date" diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 6413fc976..622f9e9c5 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -11,8 +11,9 @@ namespace :db do load(Rails.root.join("db", "sdg.rb")) end - desc "Calculates the TSV column for all polls" + desc "Calculates the TSV column for all polls and legislation processes" task calculate_tsv: :environment do Poll.find_each(&:calculate_tsvector) + Legislation::Process.find_each(&:calculate_tsvector) end end diff --git a/spec/lib/tasks/db_spec.rb b/spec/lib/tasks/db_spec.rb index d4a725d29..b556b41b4 100644 --- a/spec/lib/tasks/db_spec.rb +++ b/spec/lib/tasks/db_spec.rb @@ -50,4 +50,15 @@ describe "rake db:calculate_tsv" do expect(poll.reload.tsv).not_to be nil end + + it "calculates the tsvector for legislation processes" do + process = create(:legislation_process) + process.update_column(:tsv, nil) + + expect(process.reload.tsv).to be nil + + run_rake_task + + expect(process.reload.tsv).not_to be nil + end end diff --git a/spec/models/legislation/process_spec.rb b/spec/models/legislation/process_spec.rb index 0ea89c715..5302497a0 100644 --- a/spec/models/legislation/process_spec.rb +++ b/spec/models/legislation/process_spec.rb @@ -229,4 +229,35 @@ describe Legislation::Process do end end end + + describe ".search" do + let!(:traffic) do + create(:legislation_process, + title: "Traffic regulation", + summary: "Lane structure", + description: "From top to bottom") + end + + let!(:animal_farm) do + create(:legislation_process, + title: "Hierarchy structure", + summary: "Pigs at the top", + description: "Napoleon in charge of the traffic") + end + + it "returns only matching polls" do + expect(Legislation::Process.search("lane")).to eq [traffic] + expect(Legislation::Process.search("pigs")).to eq [animal_farm] + expect(Legislation::Process.search("nothing here")).to be_empty + end + + it "gives more weight to name" do + expect(Legislation::Process.search("traffic")).to eq [traffic, animal_farm] + expect(Legislation::Process.search("structure")).to eq [animal_farm, traffic] + end + + it "gives more weight to summary than description" do + expect(Legislation::Process.search("top")).to eq [animal_farm, traffic] + end + end end