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 24, 2024
1 parent 64d39c9 commit f1d3be9
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 5 deletions.
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
7 changes: 6 additions & 1 deletion app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ class Chapter < ActiveRecord::Base
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
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
81 changes: 80 additions & 1 deletion 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 @@ -2485,6 +2520,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 +2962,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 +3535,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 @@ -3800,6 +3864,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 @@ -4699,6 +4777,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240829193423'),
('20240830132508'),
('20240911191634'),
('20240912161211');
('20240912161211'),
('20240924161806');


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

0 comments on commit f1d3be9

Please sign in to comment.