Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/avo/resources/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ def fields
field :priority, as: :number
field :government, as: :belongs_to
field :ministers, as: :has_many
field :lead_promises, as: :has_many

field :promises, as: :has_many
end
end
14 changes: 14 additions & 0 deletions app/avo/resources/department_promise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Avo::Resources::DepartmentPromise < Avo::BaseResource
# self.includes = []
# self.attachments = []
# self.search = {
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
# }

def fields
field :id, as: :id
field :is_lead, as: :boolean
field :department, as: :belongs_to
field :promise, as: :belongs_to
end
end
4 changes: 4 additions & 0 deletions app/avo/resources/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def fields
field :bc_priority_score, as: :number, sortable: true
field :bc_promise_direction, as: :text
field :bc_promise_rank, as: :text

field :lead_department, as: :has_one
# field :bc_promise_rank_rationale, as: :textarea
# field :bc_ranked_at, as: :date_time
field :candidate_or_government, as: :text
Expand Down Expand Up @@ -86,6 +88,8 @@ def fields
# field :policy_areas, as: :code
# field :target_groups, as: :code

field :department_promises, as: :has_many

field :evidences, as: :has_many
end
end
4 changes: 4 additions & 0 deletions app/controllers/avo/department_promises_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/3.0/controllers.html
class Avo::DepartmentPromisesController < Avo::ResourcesController
end
5 changes: 5 additions & 0 deletions app/models/department.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Department < ApplicationRecord
belongs_to :government
has_many :ministers

has_many :department_promises, dependent: :destroy
has_many :promises, through: :department_promises
has_many :lead_promises, -> { where(department_promises: { is_lead: true }) },
through: :department_promises, source: :promise
end
4 changes: 4 additions & 0 deletions app/models/department_promise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class DepartmentPromise < ApplicationRecord
belongs_to :department
belongs_to :promise
end
19 changes: 18 additions & 1 deletion app/models/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@ class Promise < ApplicationRecord
has_many :activities, through: :evidences
has_many :entries, through: :activities

has_many :department_promises, dependent: :destroy
# has_many :relevant_departments, through: :department_promises, source: :department
has_one :lead_department_promise, -> { where(is_lead: true) }, class_name: "DepartmentPromise"
has_one :lead_department, through: :lead_department_promise, source: :department

def link_department!(department, is_lead: false)
Rails.logger.info("Linking department #{department.slug} to promise #{promise_id}")
if is_lead
lead_department_promise&.update(is_lead: false)
end

dp = department_promises.find_or_initialize_by(department: department)
dp.is_lead = is_lead
dp.save!
end

def format_for_llm
{
promise_id: promise_id,
title: concise_title,
description: description
description: description,
text: text
}
end
end
11 changes: 11 additions & 0 deletions db/migrate/20250620144837_create_department_promises.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDepartmentPromises < ActiveRecord::Migration[8.0]
def change
create_table :department_promises do |t|
t.references :department, null: false, foreign_key: true
t.references :promise, null: false, foreign_key: true
t.boolean :is_lead, default: false, null: false

t.timestamps
end
end
end
81 changes: 81 additions & 0 deletions db/migrate/20250620152703_add_promise_departments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class AddPromiseDepartments < ActiveRecord::Migration[8.0]
MAPPING = {
"Minister of National Defence": "national-defence",
"Minister of Public Safety": "public-safety-canada",
"Secretary of State (Defence Procurement)": "national-defence",
"Minister of Industry": "innovation-science-and-economic-development-canada",
"Minister of Foreign Affairs": "global-affairs-canada",
"Minister of Finance": "finance-canada",
"Minister of Jobs and Families": "employment-and-social-development-canada",
"Secretary of State (Children and Youth)": "employment-and-social-development-canada",
"Minister of Indigenous Services": "indigenous-services-canada",
"Minister of Immigration, Refugees and Citizenship": "immigration-refugees-and-citizenship-canada",
"Minister of Environment and Climate Change": "environment-and-climate-change-canada",
"President of the Treasury Board": "treasury-board-of-canada-secretariat",
"Minister of Crown-Indigenous Relations": "crown-indigenous-relations-and-northern-affairs-canada",
"Minister of Transport and Internal Trade": "transport-canada",
"President of the King\u2019s Privy Council for Canada and Minister responsible for Canada-U.S. Trade, Intergovernmental Affairs and One Canadian Economy": "privy-council-office-intergovernmental-affairs-secretariat",
"Minister of Artificial Intelligence and Digital Innovation": "artificial-intelligence-and-digital-innovation",
"Minister of Energy and Natural Resources": "natural-resources-canada",
"Minister of Health": "health-canada",
"Secretary of State (Labour)": "employment-and-social-development-canada",
"Minister of Canadian Identity and Culture": "canadian-heritage",
"Minister of Justice and Attorney General of Canada": "justice-canada",
"Minister of Housing and Infrastructure": "infrastructure-canada",
"Secretary of State (Combatting Crime)": "public-safety-canada",
"Secretary of State (Small Business and Tourism)": "innovation-science-and-economic-development-canada",
"Minister of Government Transformation, Public Works and Procurement": "public-services-and-procurement-canada",
"Secretary of State (Nature)": "environment-and-climate-change-canada",
"Minister of Agriculture and Agri-Food": "agriculture-and-agri-food-canada",
"Minister of Emergency Management and Community Resilience": "emergency-preparedness-canada",
"Leader of the Government in the House of Commons": nil,
"Secretary of State (Canada Revenue Agency and Financial Institutions)": "canada-revenue-agency",
"Minister of Northern and Arctic Affairs": "crown-indigenous-relations-and-northern-affairs-canada",
"Minister of Women and Gender Equality": "women-and-gender-equality-canada",
"Secretary of State (Seniors)": "employment-and-social-development-canada",
"Secretary of State (International Development)": "global-affairs-canada",
"Minister of Fisheries": "fisheries-and-oceans-canada",
"Minister of International Trade": "global-affairs-canada",
"Minister of Veterans Affairs": "veterans-affairs-canada",
"Secretary of State (Rural Development)": "rural-economic-development",
"Minister responsible for the Atlantic Canada Opportunities Agency": "atlantic-canada-opportunities-agency",
"Minister responsible for Canada Economic Development for Quebec Regions": "canada-economic-development-for-quebec-regions",
"Minister responsible for Pacific Economic Development Canada": "federal-economic-development-agency-for-southern-ontario",
"Minister of Northern and Arctic Affairs and Minister responsible for the Canadian Northern Economic Development Agency": "crown-indigenous-relations-and-northern-affairs-canada",
"Prime Minister": "prime-minister-office"
}.freeze

def map_promise(promise)
relevant_departments = promise.relevant_departments

relevant_departments&.each do |department|
dept_slug = MAPPING[department.to_sym]
dept = Department.find_by(slug: dept_slug)

if dept.present?
promise.link_department!(dept, is_lead: false)
else
Rails.logger.warn("No department found for relevant department: #{department}")
end
end

# Do lead department last otherwise, it could be overwritten by relevant departments
dept_slug = MAPPING[promise.responsible_department_lead&.to_sym]
dept = Department.find_by(slug: dept_slug)
if dept.present?
promise.link_department!(dept, is_lead: true)
else
Rails.logger.warn("No department found for lead department: #{promise.responsible_department_lead}")
end
end

def up
Promise.find_each do |promise|
map_promise(promise)
end
end

def down
DepartmentPromise.destroy_all
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.