Skip to content

Commit

Permalink
refactored method names & converted slot_available? method to instanc…
Browse files Browse the repository at this point in the history
…e method
  • Loading branch information
root authored and root committed Apr 26, 2024
1 parent e45cbfe commit 7a7c993
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/models/sign_up_sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.signup_team(assignment_id, user_id, topic_id = nil)
# Confirm the signup topic if a topic ID is provided
@signup_topic = SignUpTopic.find_by(id: topic_id)
unless @signup_topic.nil?
confirmation_status = @signup_topic.signup_team_for_chosen_topic(team_id)
confirmation_status = @signup_topic.sign_team_up(team_id)
end
# Log the signup topic save status
ExpertizaLogger.info "The signup topic save status:#{confirmation_status} for assignment #{assignment_id} by #{user_id}"
Expand Down
26 changes: 14 additions & 12 deletions app/models/sign_up_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def self.find_waitlisted_topics_for_team(assignment_id, team_id)
SignedUpTeam.find_by_sql(['SELECT u.id FROM sign_up_topics t, signed_up_teams u WHERE t.id = u.topic_id and u.is_waitlisted = true and t.assignment_id = ? and u.team_id = ?', assignment_id.to_s, team_id.to_s])
end

def self.slot_available?(topic_id)
def slot_available?
topic_id = self.id
# Retrieve the SignUpTopic record based on the given topic_id
topic = SignUpTopic.find(topic_id)

Expand Down Expand Up @@ -164,11 +165,11 @@ def reassign_topic(team_id)
end
end
# Delete the entry for the team that was either previously assigned or waiting.
SignedUpTeam.drop_off_signup_record(topic_id, team_id)
SignedUpTeam.drop_signup_record(topic_id, team_id)
end

# Method to handle the process when a user signs up
def signup_team_for_chosen_topic(team_id)
def sign_team_up(team_id)
topic_id = self.id
team = Team.find(team_id)
# Fetch all topics for the user within the team for the assignment
Expand All @@ -178,33 +179,34 @@ def signup_team_for_chosen_topic(team_id)
return false unless user_signup.first&.is_waitlisted == true
end
# Create a new SignedUpTeam instance with the provided topic and team details
sign_up = SignedUpTeam.new(topic_id: topic_id, team_id: team_id)
if SignUpTopic.slot_available?(topic_id)
signup = SignedUpTeam.new(topic_id: topic_id, team_id: team_id)
@signed_topic = SignUpTopic.find_by(id: topic_id)
if @signed_topic.slot_available?
# Assign the topic to the team if a slot is available and drop off the team from all waitlists
SignUpTopic.assign_topic_to_team(sign_up, topic_id)
SignUpTopic.assign_topic_to_team(signup, topic_id)
# Once assigned, drop all the waitlisted topics for this team
result = SignedUpTeam.drop_off_waitlists(team_id)
else
# Save the team as waitlisted if no slots are available
result = SignUpTopic.save_waitlist_entry(sign_up, team_id)
result = SignUpTopic.save_waitlist_entry(signup, team_id)
end
result
end

# Method to assign a topic to the team and update the waitlist status
def self.assign_topic_to_team(sign_up, topic_id)
def self.assign_topic_to_team(signup, topic_id)
# Set the team's waitlist status to false as they are assigned a topic
sign_up.update(is_waitlisted: false)
signup.update(is_waitlisted: false)
# Update the topic_id in the signed_up_teams table for the user
signed_up_team = SignedUpTeam.find_by(topic_id: topic_id)
signed_up_team.update(topic_id: topic_id) if signed_up_team
end

# Method to save the user as waitlisted if no slots are available
def self.save_waitlist_entry(sign_up, team_id)
sign_up.is_waitlisted = true
def self.save_waitlist_entry(signup, team_id)
signup.is_waitlisted = true
# Save the user's waitlist status
result = sign_up.save
result = signup.save
# Log the creation of the sign-up sheet for the waitlisted user
ExpertizaLogger.info(LoggerMessage.new('SignUpSheet', '', "Sign up sheet created for waitlisted with teamId #{team_id}"))
result
Expand Down
2 changes: 1 addition & 1 deletion app/models/signed_up_team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def self.topic_id_by_team_id(team_id)
end

# Remove a specific signed_up_teams record for a given topic and team.
def self.drop_off_signup_record(topic_id,team_id)
def self.drop_signup_record(topic_id,team_id)
# Fetching record for a given topic and team.
signup_record = SignedUpTeam.find_by(topic_id: topic_id, team_id: team_id)
# If the signup_record in not nil destroy it.
Expand Down
2 changes: 1 addition & 1 deletion app/views/sign_up_sheet/signup_as_instructor.html.erb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_tag(:controller => "sign_up_sheet", :action => "signup_as_instructor_action",:topic_id => params[:topic_id], :assignment_id => params[:assignment_id]) do %>
<%= label_tag(:username, "Sign Up User:") %>
<%= label_tag(:username, "Sign up this user’s team:") %>
<%= text_field_tag(:username) %>
<%= hidden_field_tag "topic_id", params[:topic_id] %>
<%= hidden_field_tag "assignment_id", params[:assignment_id] %>
Expand Down
18 changes: 9 additions & 9 deletions spec/models/sign_up_topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
end
end

describe '#signup_team_for_chosen_topic' do
describe '#sign_team_up' do
let(:team) { create(:team) }
let(:topic) { SignUpTopic.new(id: 1, max_choosers: 3) }

Expand All @@ -157,9 +157,9 @@
end

it 'signs up the team for the chosen topic' do
allow(topic).to receive(:signup_team_for_chosen_topic).and_return(true)
allow(topic).to receive(:sign_team_up).and_return(true)
allow(SignUpTopic).to receive(:slot_available?).and_return(true)
expect(topic.signup_team_for_chosen_topic(team.id)).to eq(true)
expect(topic.sign_team_up(team.id)).to eq(true)
end
end

Expand All @@ -169,8 +169,8 @@
end

it 'does not create a new signup entry' do
allow(topic).to receive(:signup_team_for_chosen_topic).and_return(false)
expect(topic.signup_team_for_chosen_topic(team.id)).to eq(false)
allow(topic).to receive(:sign_team_up).and_return(false)
expect(topic.sign_team_up(team.id)).to eq(false)
end
end

Expand All @@ -180,8 +180,8 @@
end

it 'creates a new waitlisted signup entry' do
allow(topic).to receive(:signup_team_for_chosen_topic).and_return(true)
expect(topic.signup_team_for_chosen_topic(team.id)).to eq(true)
allow(topic).to receive(:sign_team_up).and_return(true)
expect(topic.sign_team_up(team.id)).to eq(true)
end
end
end
Expand Down Expand Up @@ -224,7 +224,7 @@
expect(longest_waiting_team).to receive(:is_waitlisted=).with(false)
expect(longest_waiting_team).to receive(:save).once
expect(SignedUpTeam).to receive(:drop_off_waitlists).with(longest_waiting_team.team_id).once
expect(SignedUpTeam).to receive(:drop_off_signup_record).with(topic_id, team_id).once
expect(SignedUpTeam).to receive(:drop_signup_record).with(topic_id, team_id).once

your_class_instance.reassign_topic(team_id) # Pass team_id as an argument
end
Expand All @@ -242,7 +242,7 @@
expect(longest_waiting_team).not to receive(:save)
expect(SignedUpTeam).not_to receive(:drop_off_waitlists)

expect(SignedUpTeam).to receive(:drop_off_signup_record).with(topic_id, team_id).once
expect(SignedUpTeam).to receive(:drop_signup_record).with(topic_id, team_id).once

your_class_instance.reassign_topic(team_id) # Pass team_id as an argument
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/signed_up_team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
let(:team) { create(:assignment_team, id: 1, name: 'team 1', users: [user, user2]) }
let(:user) { create(:student) }
let(:user2) { create(:student, name: 'qwertyui', id: 5) }
describe '.drop_off_signup_record' do
describe '.drop_signup_record' do
context 'when the signup record exists' do
it 'deletes the signup record' do
puts "Before: SignedUpTeam count: #{SignedUpTeam.count}"
expect {
SignedUpTeam.drop_off_signup_record(topic.id, team.id)
SignedUpTeam.drop_signup_record(topic.id, team.id)
}.to change { SignedUpTeam.count }.by(-1)
puts "After: SignedUpTeam count: #{SignedUpTeam.count}"
end
Expand All @@ -20,7 +20,7 @@
it 'does not raise an error' do
puts "Before: SignedUpTeam count: #{SignedUpTeam.count}"
expect {
SignedUpTeam.drop_off_signup_record(999, 999) # Assuming 999 is not a valid topic_id and team_id
SignedUpTeam.drop_signup_record(999, 999) # Assuming 999 is not a valid topic_id and team_id
}.not_to raise_error
puts "After: SignedUpTeam count: #{SignedUpTeam.count}"
end
Expand Down

0 comments on commit 7a7c993

Please sign in to comment.