From fbc326efa3ddbc08c63f0b9de2f82de331f3af01 Mon Sep 17 00:00:00 2001 From: Anna Anks Nowak Date: Sat, 24 Oct 2020 16:23:16 +0200 Subject: [PATCH 1/3] Create court date model #805 --- app/controllers/casa_cases_controller.rb | 2 +- app/models/casa_case.rb | 1 + app/models/court_date.rb | 24 +++++++++++++++++++ .../20201024113046_create_court_dates.rb | 10 ++++++++ db/schema.rb | 11 ++++++++- 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 app/models/court_date.rb create mode 100644 db/migrate/20201024113046_create_court_dates.rb diff --git a/app/controllers/casa_cases_controller.rb b/app/controllers/casa_cases_controller.rb index c000060566..55ca2a8f5f 100644 --- a/app/controllers/casa_cases_controller.rb +++ b/app/controllers/casa_cases_controller.rb @@ -83,7 +83,7 @@ def casa_case_params :case_number, :transition_aged_youth, :birth_month_year_youth, - :court_date, + court_date_attributes: [ :date ], :court_report_due_date, :hearing_type_id ) diff --git a/app/models/casa_case.rb b/app/models/casa_case.rb index 4ba427a861..1b43157256 100644 --- a/app/models/casa_case.rb +++ b/app/models/casa_case.rb @@ -4,6 +4,7 @@ class CasaCase < ApplicationRecord has_many :case_assignments, dependent: :destroy has_many(:volunteers, through: :case_assignments, source: :volunteer, class_name: "User") has_many :case_contacts, dependent: :destroy + has_many :court_dates, dependent: :destroy validates :case_number, uniqueness: {case_sensitive: false}, presence: true belongs_to :hearing_type, optional: true belongs_to :casa_org diff --git a/app/models/court_date.rb b/app/models/court_date.rb new file mode 100644 index 0000000000..fee82ec156 --- /dev/null +++ b/app/models/court_date.rb @@ -0,0 +1,24 @@ +class CourtDate < ApplicationRecord + belongs_to :casa_case + + scope :passed, -> { where("date < ?", Time.now)} +end + +# == Schema Information +# +# Table name: court_dates +# +# id :bigint not null, primary key +# date :datetime not null +# created_at :datetime not null +# updated_at :datetime not null +# casa_case_id :bigint not null +# +# Indexes +# +# index_court_dates_on_casa_case_id (casa_case_id) +# +# Foreign Keys +# +# fk_rails_... (casa_case_id => casa_cases.id) +# diff --git a/db/migrate/20201024113046_create_court_dates.rb b/db/migrate/20201024113046_create_court_dates.rb new file mode 100644 index 0000000000..62335923b8 --- /dev/null +++ b/db/migrate/20201024113046_create_court_dates.rb @@ -0,0 +1,10 @@ +class CreateCourtDates < ActiveRecord::Migration[6.0] + def change + create_table :court_dates do |t| + t.datetime :date, null: false + t.references :casa_case, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c6b989e823..a645f958de 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_10_20_095451) do +ActiveRecord::Schema.define(version: 2020_10_24_121156) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -146,6 +146,14 @@ t.index ["contact_type_group_id"], name: "index_contact_types_on_contact_type_group_id" end + create_table "court_dates", force: :cascade do |t| + t.datetime "date", null: false + t.bigint "casa_case_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["casa_case_id"], name: "index_court_dates_on_casa_case_id" + end + create_table "hearing_types", force: :cascade do |t| t.bigint "casa_org_id", null: false t.string "name", null: false @@ -213,6 +221,7 @@ add_foreign_key "case_assignments", "users", column: "volunteer_id" add_foreign_key "case_contacts", "casa_cases" add_foreign_key "case_contacts", "users", column: "creator_id" + add_foreign_key "court_dates", "casa_cases" add_foreign_key "supervisor_volunteers", "users", column: "supervisor_id" add_foreign_key "supervisor_volunteers", "users", column: "volunteer_id" add_foreign_key "users", "casa_orgs" From 9a0cdd7daa99424b605dc59de09af947bb1b207a Mon Sep 17 00:00:00 2001 From: Anna Anks Nowak Date: Sat, 24 Oct 2020 16:23:45 +0200 Subject: [PATCH 2/3] Create court dates based on existing casa cases court date column #805 --- ...e_instance_from_casa_case_court_date_column.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb diff --git a/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb b/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb new file mode 100644 index 0000000000..aac6563ae6 --- /dev/null +++ b/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb @@ -0,0 +1,15 @@ +class CreateCourtDateInstanceFromCasaCaseCourtDateColumn < ActiveRecord::Migration[6.0] + def up + CasaCase.all.each do |cc| + if cc.court_date + CourtDate.create!(date: cc.court_date, casa_case_id: cc.id) + end + end + end + + def down + CourtDate.all.each do |cd| + cd.destroy! + end + end +end From b2fe23ffe1e609039162d7b92debe33eb3ff8778 Mon Sep 17 00:00:00 2001 From: Anna Anks Nowak Date: Sun, 25 Oct 2020 12:51:25 +0100 Subject: [PATCH 3/3] Show past court dates on edit casa case page #805 --- app/controllers/casa_cases_controller.rb | 2 +- app/models/casa_case.rb | 2 +- .../{court_date.rb => past_court_date.rb} | 8 +++----- app/views/casa_cases/edit.html.erb | 15 +++++++++++++++ ... 20201024113046_create_past_court_dates.rb} | 4 ++-- ...nstance_from_casa_case_court_date_column.rb | 15 --------------- db/schema.rb | 18 +++++++++--------- lib/tasks/scheduler.rake | 5 ++++- 8 files changed, 35 insertions(+), 34 deletions(-) rename app/models/{court_date.rb => past_court_date.rb} (68%) rename db/migrate/{20201024113046_create_court_dates.rb => 20201024113046_create_past_court_dates.rb} (59%) delete mode 100644 db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb diff --git a/app/controllers/casa_cases_controller.rb b/app/controllers/casa_cases_controller.rb index 55ca2a8f5f..c000060566 100644 --- a/app/controllers/casa_cases_controller.rb +++ b/app/controllers/casa_cases_controller.rb @@ -83,7 +83,7 @@ def casa_case_params :case_number, :transition_aged_youth, :birth_month_year_youth, - court_date_attributes: [ :date ], + :court_date, :court_report_due_date, :hearing_type_id ) diff --git a/app/models/casa_case.rb b/app/models/casa_case.rb index 1b43157256..b7cf2d39b5 100644 --- a/app/models/casa_case.rb +++ b/app/models/casa_case.rb @@ -4,7 +4,7 @@ class CasaCase < ApplicationRecord has_many :case_assignments, dependent: :destroy has_many(:volunteers, through: :case_assignments, source: :volunteer, class_name: "User") has_many :case_contacts, dependent: :destroy - has_many :court_dates, dependent: :destroy + has_many :past_court_dates, dependent: :destroy validates :case_number, uniqueness: {case_sensitive: false}, presence: true belongs_to :hearing_type, optional: true belongs_to :casa_org diff --git a/app/models/court_date.rb b/app/models/past_court_date.rb similarity index 68% rename from app/models/court_date.rb rename to app/models/past_court_date.rb index fee82ec156..bb81b2283b 100644 --- a/app/models/court_date.rb +++ b/app/models/past_court_date.rb @@ -1,12 +1,10 @@ -class CourtDate < ApplicationRecord +class PastCourtDate < ApplicationRecord belongs_to :casa_case - - scope :passed, -> { where("date < ?", Time.now)} end # == Schema Information # -# Table name: court_dates +# Table name: past_court_dates # # id :bigint not null, primary key # date :datetime not null @@ -16,7 +14,7 @@ class CourtDate < ApplicationRecord # # Indexes # -# index_court_dates_on_casa_case_id (casa_case_id) +# index_past_court_dates_on_casa_case_id (casa_case_id) # # Foreign Keys # diff --git a/app/views/casa_cases/edit.html.erb b/app/views/casa_cases/edit.html.erb index bd91379129..0ff7684b23 100644 --- a/app/views/casa_cases/edit.html.erb +++ b/app/views/casa_cases/edit.html.erb @@ -3,6 +3,21 @@ <%= render 'form', casa_case: @casa_case %> +
+
+
Past court dates:
+ <% if @casa_case.past_court_dates.size > 0 %> +
    + <% @casa_case.past_court_dates.each do |pcd| %> +

    <%= pcd.date.strftime("%B %e, %Y") %>

    + <% end %> +
+ <% else %> + No past court dates + <% end %> +
+
+ <% if Pundit.policy(current_user, @casa_case).assign_volunteers? %> <%= render "volunteer_assignment" %> <% end %> diff --git a/db/migrate/20201024113046_create_court_dates.rb b/db/migrate/20201024113046_create_past_court_dates.rb similarity index 59% rename from db/migrate/20201024113046_create_court_dates.rb rename to db/migrate/20201024113046_create_past_court_dates.rb index 62335923b8..ed31c96ec8 100644 --- a/db/migrate/20201024113046_create_court_dates.rb +++ b/db/migrate/20201024113046_create_past_court_dates.rb @@ -1,6 +1,6 @@ -class CreateCourtDates < ActiveRecord::Migration[6.0] +class CreatePastCourtDates < ActiveRecord::Migration[6.0] def change - create_table :court_dates do |t| + create_table :past_court_dates do |t| t.datetime :date, null: false t.references :casa_case, null: false, foreign_key: true diff --git a/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb b/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb deleted file mode 100644 index aac6563ae6..0000000000 --- a/db/migrate/20201024121156_create_court_date_instance_from_casa_case_court_date_column.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateCourtDateInstanceFromCasaCaseCourtDateColumn < ActiveRecord::Migration[6.0] - def up - CasaCase.all.each do |cc| - if cc.court_date - CourtDate.create!(date: cc.court_date, casa_case_id: cc.id) - end - end - end - - def down - CourtDate.all.each do |cd| - cd.destroy! - end - end -end diff --git a/db/schema.rb b/db/schema.rb index a645f958de..1047a1bc81 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -146,14 +146,6 @@ t.index ["contact_type_group_id"], name: "index_contact_types_on_contact_type_group_id" end - create_table "court_dates", force: :cascade do |t| - t.datetime "date", null: false - t.bigint "casa_case_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["casa_case_id"], name: "index_court_dates_on_casa_case_id" - end - create_table "hearing_types", force: :cascade do |t| t.bigint "casa_org_id", null: false t.string "name", null: false @@ -161,6 +153,14 @@ t.index ["casa_org_id"], name: "index_hearing_types_on_casa_org_id" end + create_table "past_court_dates", force: :cascade do |t| + t.datetime "date", null: false + t.bigint "casa_case_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["casa_case_id"], name: "index_past_court_dates_on_casa_case_id" + end + create_table "supervisor_volunteers", force: :cascade do |t| t.bigint "supervisor_id", null: false t.bigint "volunteer_id", null: false @@ -221,7 +221,7 @@ add_foreign_key "case_assignments", "users", column: "volunteer_id" add_foreign_key "case_contacts", "casa_cases" add_foreign_key "case_contacts", "users", column: "creator_id" - add_foreign_key "court_dates", "casa_cases" + add_foreign_key "past_court_dates", "casa_cases" add_foreign_key "supervisor_volunteers", "users", column: "supervisor_id" add_foreign_key "supervisor_volunteers", "users", column: "volunteer_id" add_foreign_key "users", "casa_orgs" diff --git a/lib/tasks/scheduler.rake b/lib/tasks/scheduler.rake index a2e5bfa9af..7f74ea7b50 100644 --- a/lib/tasks/scheduler.rake +++ b/lib/tasks/scheduler.rake @@ -8,6 +8,9 @@ end desc "Clear court dates and report information when date has passed, run by heroku scheduler" task clear_passed_dates: :environment do puts "Checking case due dates..." - CasaCase.due_date_passed.each { |key| key.clear_court_dates } + CasaCase.due_date_passed.each do |cc| + PastCourtDate.create!(date: cc.court_date, casa_case_id: cc.id) + cc.clear_court_dates + end puts "done." end