Skip to content

Commit

Permalink
Merge pull request #7031 from fjordllc/feature/assign-admin-as-host-i…
Browse files Browse the repository at this point in the history
…f-none

定期イベントの主催者が退会と休会をした際、自動的にkomagataさんが主催者になるよう修正
  • Loading branch information
komagata authored Dec 26, 2023
2 parents 0899c44 + a62971b commit 143030c
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/hibernation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def create
destroy_subscription!
notify_to_chat
notify_to_mentors_and_admins
current_user.delete_and_assign_new_organizer
logout
redirect_to hibernation_path
else
Expand Down
1 change: 1 addition & 0 deletions app/controllers/retirement_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def create
current_user.retired_on = Date.current
if current_user.save(context: :retirement)
user = current_user
current_user.delete_and_assign_new_organizer
Newspaper.publish(:retirement_create, user)
begin
UserMailer.retire(user).deliver_now
Expand Down
6 changes: 6 additions & 0 deletions app/models/organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ class Organizer < ApplicationRecord
belongs_to :regular_event

validates :user_id, uniqueness: { scope: :regular_event_id }

def delete_and_assign_new
event = regular_event
delete
event.assign_admin_as_organizer_if_none
end
end
7 changes: 7 additions & 0 deletions app/models/regular_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ def remove_event(events_arr, id)
end
end

def assign_admin_as_organizer_if_none
return if organizers.exists?

admin_user = User.find_by(login_name: User::DEFAULT_REGULAR_EVENT_ORGANIZER)
Organizer.new(user: admin_user, regular_event: self).save if admin_user
end

private

def end_at_be_greater_than_start_at
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User < ApplicationRecord
'adviser' => :advisers,
'trainee' => :trainees
}.freeze
DEFAULT_REGULAR_EVENT_ORGANIZER = 'komagata'

enum job: {
student: 0,
Expand Down Expand Up @@ -755,6 +756,10 @@ def become_watcher!(watchable)
watches.find_or_create_by!(watchable:)
end

def delete_and_assign_new_organizer
organizers.each(&:delete_and_assign_new)
end

private

def password_required?
Expand Down
18 changes: 18 additions & 0 deletions app/views/hibernation/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ hr.a-border
| こちら
| のページの「分報 URL」欄に分報チャンネルの URL を登録してください。

- if current_user.regular_events.any?
.form-item
label.a-form-label
| 定期イベント
.important-message
.important-message__body
.a-short-text.is-sm
p
|ご自身が主催者である定期イベントがあります。
br
| 休会をお考えの場合、イベントの進行に影響がないよう、任意で他の参加者に主催を引き継ぐことを推奨します。
| 休会手続きを完了する前に、以下のリンク先でイベント設定変更を行なってください。
ul
- current_user.regular_events.each do |event|
li
= link_to edit_regular_event_path(event), target: '_blank', rel: 'noopener' do
| 定期イベント「#{event.title}」の設定変更

.form-item
label.a-form-label.is-required
| 休会についての注意を読みましたか?
Expand Down
17 changes: 17 additions & 0 deletions app/views/retirement/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ hr.a-border
= render 'errors', object: current_user
= form_with model: current_user, local: true, url: retirement_path, method: :post, class: 'form' do |f|
.form__items
- if current_user.regular_events.any?
.form-item
.important-message
.important-message__body
.a-short-text.is-sm
p
|ご自身が主催者である定期イベントがあります。
br
| 退会をお考えの場合、イベントの進行に影響がないよう、任意で他の参加者に主催を引き継ぐことを推奨します。
| 退会手続きを完了する前に、
| 以下のリンク先でイベント設定変更を行なってください。
ul
- current_user.regular_events.each do |event|
li
= link_to edit_regular_event_path(event), target: '_blank', rel: 'noopener' do
| 定期イベント「#{event.title}」の設定変更

.form-item
= f.label :retire_reasons, '退会の理由を教えてください(複数選択可)', class: 'a-form-label'
.checkboxes
Expand Down
16 changes: 16 additions & 0 deletions test/models/organizer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'test_helper'

class OrganizerTest < ActiveSupport::TestCase
test '#delete_and_assign_new' do
organizer = organizers(:organizer12)
event = organizer.regular_event

assert_changes -> { Organizer.where(regular_event: event, user: organizer.user).exists? }, from: true, to: false do
organizer.delete_and_assign_new
end

assert Organizer.where(regular_event: event).exists?
end
end
17 changes: 17 additions & 0 deletions test/models/regular_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,21 @@ class RegularEventTest < ActiveSupport::TestCase
assert regular_events1.include?(regular_event2)
assert regular_events2.include?(regular_event3)
end

test '#assign_admin_as_organizer_if_none' do
regular_event = RegularEvent.new(
title: '主催者のいないイベント',
description: '主催者のいないイベント',
finished: false,
hold_national_holiday: false,
start_at: Time.zone.local(2020, 1, 1, 21, 0, 0),
end_at: Time.zone.local(2020, 1, 1, 22, 0, 0),
user: users(:kimura),
category: 0,
published_at: '2023-08-01 00:00:00'
)
regular_event.save(validate: false)
regular_event.assign_admin_as_organizer_if_none
assert_equal User.find_by(login_name: User::DEFAULT_REGULAR_EVENT_ORGANIZER), regular_event.organizers.first
end
end
8 changes: 8 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,12 @@ class UserTest < ActiveSupport::TestCase
assert_equal User.students_and_trainees, User.users_role(not_scope_name, allowed_targets: allowed_targets, default_target: 'student_and_trainee')
assert_empty User.users_role(not_scope_name, allowed_targets: allowed_targets)
end

test '#delete_and_assign_new_organizer' do
user = users(:hajime)

assert_changes -> { Organizer.where(user: user).exists? }, from: true, to: false do
user.delete_and_assign_new_organizer
end
end
end
36 changes: 36 additions & 0 deletions test/system/hibernation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,40 @@ class HibernationTest < ApplicationSystemTestCase
page.driver.browser.switch_to.alert.accept
assert_text '復帰予定日を入力してください'
end

test 'hibernate with event organizer' do
visit_with_auth new_hibernation_path, 'hajime'
within('form[name=hibernation]') do
fill_in(
'hibernation[scheduled_return_on]',
with: (Date.current + 30)
)
fill_in('hibernation[reason]', with: 'test')
end
find('.check-box-to-read').click
click_on '休会する'
page.driver.browser.switch_to.alert.accept
assert_text '休会処理が完了しました'

regular_event = regular_events(:regular_event4)
visit_with_auth "regular_events/#{regular_event.id}", 'kimura'
assert_no_selector '.is-hajime'

visit_with_auth new_hibernation_path, 'kimura'
within('form[name=hibernation]') do
fill_in(
'hibernation[scheduled_return_on]',
with: (Date.current + 30)
)
fill_in('hibernation[reason]', with: 'test')
end
find('.check-box-to-read').click
click_on '休会する'
page.driver.browser.switch_to.alert.accept
assert_text '休会処理が完了しました'

visit_with_auth "regular_events/#{regular_event.id}", 'komagata'
assert_no_selector '.is-kimura'
assert_selector '.is-komagata'
end
end
2 changes: 1 addition & 1 deletion test/system/regular_events_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class RegularEventsTest < ApplicationSystemTestCase
visit_with_auth regular_events_path, 'kimura'
assert_selector '.card-list-item', count: 25
visit regular_events_path(page: 2)
assert_selector '.card-list-item', count: 7
assert_selector '.card-list-item', count: 8
end

test 'create a regular event for all students and trainees' do
Expand Down
22 changes: 22 additions & 0 deletions test/system/retirement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,26 @@ class RetirementTest < ApplicationSystemTestCase
visit '/retirement'
assert_equal 'FJORD BOOT CAMP(フィヨルドブートキャンプ)', title
end

test 'retire with event organizer' do
visit_with_auth new_retirement_path, 'hajime'
find('label', text: 'とても良い').click
click_on '退会する'
page.driver.browser.switch_to.alert.accept
assert_text '退会処理が完了しました'

regular_event = regular_events(:regular_event4)
visit_with_auth "regular_events/#{regular_event.id}", 'kimura'
assert_no_selector '.is-hajime'

visit_with_auth new_retirement_path, 'kimura'
find('label', text: 'とても良い').click
click_on '退会する'
page.driver.browser.switch_to.alert.accept
assert_text '退会処理が完了しました'

visit_with_auth "regular_events/#{regular_event.id}", 'komagata'
assert_no_selector '.is-kimura'
assert_selector '.is-komagata'
end
end

0 comments on commit 143030c

Please sign in to comment.