Add search method to legislation processes

This way we'll be able to search processes in the SDG Management
section.
This commit is contained in:
Javi Martín
2020-12-16 14:36:23 +01:00
parent 852014e478
commit 948a8b2904
6 changed files with 68 additions and 2 deletions

View File

@@ -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

View File

@@ -0,0 +1,5 @@
class AddTsvToLegislationProcesses < ActiveRecord::Migration[5.2]
def change
add_column :legislation_processes, :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_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"

View File

@@ -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

View File

@@ -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

View File

@@ -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