Skip to content

Commit

Permalink
add 'Assigned to me' tab to the home page
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Oct 18, 2024
1 parent 062983e commit 3ada472
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
10 changes: 6 additions & 4 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def index
if current_user.is_a? Student
redirect_to student_view_index_url
else

case params[:which]
when 'mine'
@theses = Thesis.assigned_to_user(current_user).order('updated_at desc')
@which = params[:which]
when Thesis::UNDER_REVIEW
@theses = Thesis.under_review
@theses = Thesis.under_review.order('updated_at desc')
@which = params[:which]
when Thesis::ACCEPTED
@theses = Thesis.accepted
@theses = Thesis.accepted.order('updated_at desc')
@which = params[:which]
else
@theses = Thesis.open + Thesis.returned
@theses = Thesis.open_or_returned.order('updated_at desc')
@which = Thesis::OPEN
end
render :index
Expand Down
5 changes: 5 additions & 0 deletions app/models/thesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def certify_content_correct_present
scope :returned, -> { where('status = ? ', RETURNED) }
scope :with_embargo, -> { where('embargoed = ? ', true) }
scope :without_embargo, -> { where('embargoed = ? ', false) }
scope :open_or_returned, -> { where('status = ? OR status = ?', OPEN, RETURNED) }

def abstract=(text)
text = '' if text.nil?
Expand Down Expand Up @@ -191,4 +192,8 @@ def publish
ChagingStatusMailer.published(student.email).deliver
save(validate: false)
end

def self.assigned_to_user(user)
Thesis.where('assigned_to_id = ?', user.id)
end
end
3 changes: 3 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<li class="nav-item">
<%= active_link_to 'Accepted', root_path(which: Thesis::ACCEPTED), active: /accepted/, class:"nav-link" %>
</li>
<li class="nav-item">
<%= active_link_to 'Assigned to me', root_path(which: :mine), active: /mine/, class:"nav-link" %>
</li>
</ul>
<div id="theses">
<% if @theses.size == 0 %>
Expand Down
16 changes: 15 additions & 1 deletion test/controllers/home_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class HomeControllerTest < ActionController::TestCase
theses.each { |t| assert_equal Thesis::UNDER_REVIEW, t.status }
assert_equal Thesis::UNDER_REVIEW, which, 'Under Review'
end

should 'show thesis assigned to current user when params which=mine' do
create_list(:thesis, 1, status: Thesis::OPEN)
create_list(:thesis, 1, status: Thesis::RETURNED)
create_list(:thesis, 2, status: Thesis::UNDER_REVIEW).each { |t| t.assign_to(@user) }

get :index, params: { which: 'mine'}
theses = assigns(:theses)
which = assigns(:which)

assert_equal 2, theses.size
theses.each { |t| assert_equal Thesis::UNDER_REVIEW, t.status }
assert_equal 'mine', which
end
end

context 'as student' do
Expand All @@ -47,7 +61,7 @@ class HomeControllerTest < ActionController::TestCase
log_user_in(@student)
end

should 'redirect to student view' do
should 'redirect student user to student view' do
get :index
assert_response :redirect
assert_redirected_to student_view_index_url
Expand Down
31 changes: 31 additions & 0 deletions test/models/thesis_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,37 @@ class ThesisTest < ActiveSupport::TestCase
assert_nil thesis.assigned_to, 'No one is assigned to the thesis'
end

should 'return all thesis assigned to a user' do
user1 = create(:user)
user2 = create(:user)

unassigned_theses = create_list(:thesis, 2)

user1_theses = create_list(:thesis, 3)
user1_theses.each do |t|
t.title = "User #{user1.id} thesis #{t.id}"
t.save
t.assign_to(user1)
end

user2_theses = create_list(:thesis, 4)
user2_theses.each do |t|
t.title = "User #{user2.id} thesis #{t.id}"
t.save
t.assign_to(user2)
end

assert_equal 3, Thesis.assigned_to_user(user1).count
Thesis.assigned_to_user(user1).each do |t|
assert_equal "User #{user1.id} thesis #{t.id}", t.title
end

assert_equal 4, Thesis.assigned_to_user(user2).count
Thesis.assigned_to_user(user2).each do |t|
assert_equal "User #{user2.id} thesis #{t.id}", t.title
end
end

## READY TO PUBLISH
should 'return ready to publish theses' do
create(:thesis, status: Thesis::OPEN)
Expand Down

0 comments on commit 3ada472

Please sign in to comment.