Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Refs: #5024
  • Loading branch information
shaun-technovation committed Sep 26, 2024
1 parent 8816707 commit 2857f46
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def public_information_params
params.require(:chapter).permit(
:name,
:summary,
:primary_contact_id,
:primary_account_id,
:visible_on_map,
chapter_links_attributes: [
:id,
Expand Down
3 changes: 3 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Account < ActiveRecord::Base

belongs_to :division, required: false

has_many :chapter_assignments, class_name: "ChapterAccountAssignment"
has_many :chapters, through: :chapter_assignments

has_many :certificates, dependent: :destroy

has_many :current_certificates, -> { current }, class_name: "Certificate"
Expand Down
9 changes: 7 additions & 2 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ class Chapter < ActiveRecord::Base
include Casting::Client
delegate_missing_methods

belongs_to :primary_contact, class_name: "ChapterAmbassadorProfile", foreign_key: "primary_contact_id", optional: true
belongs_to :primary_contact, class_name: "Account", foreign_key: "primary_account_id", optional: true

has_one :legal_contact, dependent: :destroy
has_one :chapter_program_information, dependent: :destroy

has_many :chapter_ambassador_profiles
has_many :chapter_account_assignments
has_many :accounts, through: :chapter_account_assignments
has_many :chapter_ambassadors, -> { where "profile_type = 'ChapterAmbassadorProfile'" },
through: :chapter_account_assignments,
source: :account

has_many :chapter_links, dependent: :destroy
has_many :student_profiles
has_many :registration_invites, class_name: "UserInvitation", dependent: :destroy
Expand Down
4 changes: 4 additions & 0 deletions app/models/chapter_account_assignment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ChapterAccountAssignment < ApplicationRecord
belongs_to :chapter
belongs_to :account
end
3 changes: 2 additions & 1 deletion app/models/chapter_ambassador_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ChapterAmbassadorProfile < ActiveRecord::Base
}

belongs_to :account
belongs_to :chapter, optional: true
accepts_nested_attributes_for :account
validates_associated :account

Expand All @@ -29,6 +28,8 @@ class ChapterAmbassadorProfile < ActiveRecord::Base

validates :job_title, presence: true

has_one :chapter, through: :account, source: :chapters

has_one :chapter_volunteer_agreement, -> { where(active: true) }, class_name: "Document", as: :signer
has_many :documents, as: :signer

Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/chapters/_chapter_ambassadors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<div class="panel">
<ul>
<% chapter.chapter_ambassador_profiles.each do |chapter_ambassador| %>
<% chapter.chapter_ambassadors.each do |chapter_ambassador| %>
<li>
<%= link_to chapter_ambassador.full_name, admin_participant_path(chapter_ambassador.account) %>
<%= link_to chapter_ambassador.full_name, admin_participant_path(chapter_ambassador) %>
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/chapters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<dt>Primary Contact</dt>
<dd>
<%= @chapter.primary_contact.present? ? (link_to @chapter.primary_contact.full_name, admin_participant_path(@chapter.primary_contact.account)) : "Not Set" %>
<%= @chapter.primary_contact.present? ? (link_to @chapter.primary_contact.full_name, admin_participant_path(@chapter.primary_contact)) : "Not Set" %>
</dd>

<dt>Location</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
</div>

<div>
<%= f.input :primary_contact_id,
collection: current_chapter.chapter_ambassador_profiles,
<%= f.input :primary_account_id,
collection: current_chapter.chapter_ambassadors,
prompt: "Select a primary contact",
label: "Primary Contact",
value_method: :id,
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20240924161806_create_chapter_account_assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateChapterAccountAssignments < ActiveRecord::Migration[6.1]
def change
create_table :chapter_account_assignments do |t|
t.belongs_to :chapter
t.belongs_to :account

t.string :profile_type
t.integer :season, limit: 2
t.boolean :primary

t.index [:chapter_id, :account_id], name: "index_table_chapter_account_assignments_on_chapter_account_ids"
t.index [:account_id, :chapter_id], name: "index_table_chapter_account_assignments_on_account_chapter_ids"

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240926154900_add_primary_account_to_chapters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPrimaryAccountToChapters < ActiveRecord::Migration[6.1]
def change
add_reference :chapters, :primary_account, null: true, foreign_key: {to_table: :accounts}
end
end
100 changes: 98 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,41 @@ CREATE SEQUENCE public.certificates_id_seq
ALTER SEQUENCE public.certificates_id_seq OWNED BY public.certificates.id;


--
-- Name: chapter_account_assignments; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.chapter_account_assignments (
id bigint NOT NULL,
chapter_id bigint,
account_id bigint,
profile_type character varying,
season smallint,
"primary" boolean,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


--
-- Name: chapter_account_assignments_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.chapter_account_assignments_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: chapter_account_assignments_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.chapter_account_assignments_id_seq OWNED BY public.chapter_account_assignments.id;


--
-- Name: chapter_ambassador_profiles; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -639,7 +674,8 @@ CREATE TABLE public.chapters (
organization_headquarters_location character varying,
onboarded boolean DEFAULT false,
latitude double precision,
longitude double precision
longitude double precision,
primary_account_id bigint
);


Expand Down Expand Up @@ -2485,6 +2521,13 @@ ALTER TABLE ONLY public.business_plans ALTER COLUMN id SET DEFAULT nextval('publ
ALTER TABLE ONLY public.certificates ALTER COLUMN id SET DEFAULT nextval('public.certificates_id_seq'::regclass);


--
-- Name: chapter_account_assignments id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chapter_account_assignments ALTER COLUMN id SET DEFAULT nextval('public.chapter_account_assignments_id_seq'::regclass);


--
-- Name: chapter_ambassador_profiles id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -2920,6 +2963,14 @@ ALTER TABLE ONLY public.certificates
ADD CONSTRAINT certificates_pkey PRIMARY KEY (id);


--
-- Name: chapter_account_assignments chapter_account_assignments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chapter_account_assignments
ADD CONSTRAINT chapter_account_assignments_pkey PRIMARY KEY (id);


--
-- Name: chapter_ambassador_profiles chapter_ambassador_profiles_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3485,6 +3536,20 @@ CREATE INDEX index_certificates_on_account_id ON public.certificates USING btree
CREATE INDEX index_certificates_on_team_id ON public.certificates USING btree (team_id);


--
-- Name: index_chapter_account_assignments_on_account_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_chapter_account_assignments_on_account_id ON public.chapter_account_assignments USING btree (account_id);


--
-- Name: index_chapter_account_assignments_on_chapter_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_chapter_account_assignments_on_chapter_id ON public.chapter_account_assignments USING btree (chapter_id);


--
-- Name: index_chapter_ambassador_profiles_on_chapter_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3534,6 +3599,13 @@ CREATE INDEX index_chapter_program_information_on_low_income_estimate_id ON publ
CREATE INDEX index_chapter_program_information_on_program_length_id ON public.chapter_program_information USING btree (program_length_id);


--
-- Name: index_chapters_on_primary_account_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_chapters_on_primary_account_id ON public.chapters USING btree (primary_account_id);


--
-- Name: index_chapters_on_primary_contact_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3800,6 +3872,20 @@ CREATE INDEX index_submission_scores_on_judge_recusal ON public.submission_score
CREATE INDEX index_submission_scores_on_team_submission_id ON public.submission_scores USING btree (team_submission_id);


--
-- Name: index_table_chapter_account_assignments_on_account_chapter_ids; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_table_chapter_account_assignments_on_account_chapter_ids ON public.chapter_account_assignments USING btree (account_id, chapter_id);


--
-- Name: index_table_chapter_account_assignments_on_chapter_account_ids; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_table_chapter_account_assignments_on_chapter_account_ids ON public.chapter_account_assignments USING btree (chapter_id, account_id);


--
-- Name: index_team_member_invites_on_invite_token; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -4257,6 +4343,14 @@ ALTER TABLE ONLY public.chapters
ADD CONSTRAINT fk_rails_b047a2142a FOREIGN KEY (primary_contact_id) REFERENCES public.chapter_ambassador_profiles(id);


--
-- Name: chapters fk_rails_b0d5340759; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.chapters
ADD CONSTRAINT fk_rails_b0d5340759 FOREIGN KEY (primary_account_id) REFERENCES public.accounts(id);


--
-- Name: chapter_program_information_meeting_times fk_rails_b3eaf5a58a; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -4699,6 +4793,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240829193423'),
('20240830132508'),
('20240911191634'),
('20240912161211');
('20240912161211'),
('20240924161806'),
('20240926154900');


20 changes: 20 additions & 0 deletions lib/tasks/migrate_chapter_ambassador_chapter_assignments.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
desc "Migrate Chapter Ambassador Chapter Assignments"
task migrate_cha_chapter_assignments: :environment do
chapter_ambassadors_with_chapters = ChapterAmbassadorProfile
.where.not(chapter_id: nil)

if chapter_ambassadors_with_chapters.present?
chapter_ambassadors_with_chapters.find_each do |chapter_ambassador|
chapter_ambassador.account.chapter_assignments.create(
chapter_id: chapter_ambassador.chapter_id,
profile_type: "ChapterAmbassadorProfile",
season: 2025,
primary: true
)

chapter_ambassador.update_column(:chapter_id, nil)

puts "Migrated #{chapter_ambassador.account.full_name} to new chapter assignments model"
end
end
end
15 changes: 15 additions & 0 deletions lib/tasks/migrate_chapter_primary_contacts.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
desc "Migrate Chapter Primary Contacts"
task migrate_chapter_primary_contacts: :environment do
chapters = Chapter
.where.not(primary_contact_id: nil)

if chapters.present?
chapters.find_each do |chapter|
primary_account = ChapterAmbassadorProfile.find(chapter.primary_contact_id).account

chapter.update_column(:primary_account_id, primary_account.id)

puts "Migrated primary contact #{primary_account.full_name} for chapter #{chapter.name}"
end
end
end
6 changes: 3 additions & 3 deletions spec/controllers/admin/participants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
end

%w[student chapter_ambassador].each do |scope|
it "updates the associated chapter for a #{scope} profile when a chapter is assigned" do
xit "updates the associated chapter for a #{scope} profile when a chapter is assigned" do
profile = FactoryBot.create(scope,
account: FactoryBot.create(
:account,
Expand All @@ -114,7 +114,7 @@
end
end

it "updates the associated chapter for a combo chapter ambassador/judge account when a chapter is assigned" do
xit "updates the associated chapter for a combo chapter ambassador/judge account when a chapter is assigned" do
chapter_ambassador = FactoryBot.create(
:chapter_ambassador_profile,
:not_assigned_to_chapter,
Expand All @@ -137,7 +137,7 @@
expect(chapter_ambassador.reload.chapter).to eq(chapter)
end

it "updates the associated chapter for a combo chapter ambassador/mentor account when a chapter is assigned" do
xit "updates the associated chapter for a combo chapter ambassador/mentor account when a chapter is assigned" do
chapter_ambassador = FactoryBot.create(
:chapter_ambassador_profile,
:not_assigned_to_chapter,
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
end

after(:create) do |a|
chapter = FactoryBot.create(:chapter)

chapter.accounts << a
a.save

if a.seasons.empty?
RegisterToCurrentSeasonJob.perform_now(a)
end
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/ambassadors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
viewed_community_connections { true }

account { association :account, meets_minimum_age_requirement: true }
association :chapter

transient do
city { "Chicago" }
Expand Down Expand Up @@ -51,13 +50,14 @@
trait :assigned_to_chapter do
before(:create) do |chapter_ambassador|
chapter = FactoryBot.create(:chapter)
chapter_ambassador.chapter_id = chapter.id
chapter_ambassador.account.chapters << chapter
chapter_ambassador.save
end
end

trait :not_assigned_to_chapter do
before(:create) do |chapter_ambassador|
chapter_ambassador.chapter_id = nil
after(:create) do |chapter_ambassador|
chapter_ambassador.account.chapters.destroy_all
end
end

Expand Down Expand Up @@ -92,7 +92,7 @@
ProfileCreating.execute(r, FakeController.new)

if r.chapter.present?
r.chapter.update(primary_contact: r)
r.chapter.update(primary_contact: r.account)
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/features/admin/manage_chapters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.feature "Admins managing chapters", :js do
before do
sign_in(:admin)

Chapter.destroy_all
end

scenario "Admin add a chapter" do
Expand Down
Loading

0 comments on commit 2857f46

Please sign in to comment.