Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions app/controllers/api/checks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ def index

def create
if checkable.checks.empty?
@check = Check.create!(
user: current_user,
checkable:
)
ActiveSupport::Notifications.instrument('check.create', check: @check)
head :created
begin
Check.transaction do
@check = Check.create!(user: current_user, checkable:)
ActiveSupport::Notifications.instrument('check.create', check: @check)
end
head :created
rescue StandardError => e
Rails.logger.error("[API::ChecksController#create] チェック作成でエラー: #{e.message}")
render json: { message: 'エラーが発生しました。' }, status: :internal_server_error
end
else
render json: { message: "この#{checkable.class.model_name.human}は確認済です。" }, status: :unprocessable_entity
end
end

def destroy
@check = Check.find(params[:id]).destroy
ActiveSupport::Notifications.instrument('check.cancel', check: @check)

Check.transaction do
@check = Check.find(params[:id]).destroy!
ActiveSupport::Notifications.instrument('check.cancel', check: @check)
end
head :no_content
rescue StandardError => e
Rails.logger.error("[API::ChecksController#destroy] チェック削除でエラー: #{e.message}")
render json: { message: 'エラーが発生しました。' }, status: :internal_server_error
end

private
Expand Down
30 changes: 18 additions & 12 deletions app/controllers/checks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def create

@check = @checkable.checks.build(user: current_user)

if @check.save
Newspaper.publish(:check_create, { check: @check })
if @checkable.is_a?(Product)
@checkable.change_learning_status(:complete)
redirect_back(fallback_location: @checkable, notice: '提出物を合格にしました。')
else
redirect_back(fallback_location: @checkable, notice: '日報を確認済みにしました。')
begin
Check.transaction do
@check.save!
ActiveSupport::Notifications.instrument('check.create', check: @check)
end
else
notice = @checkable.is_a?(Product) ? '提出物を合格にしました。' : '日報を確認済みにしました。'
redirect_back(fallback_location: @checkable, notice:)
rescue StandardError => e
Rails.logger.error("[ChecksController#create] チェック作成でエラー: #{e.message}")
redirect_back(fallback_location: @checkable, alert: 'エラーが発生しました。')
end
end
Expand All @@ -31,10 +31,16 @@ def destroy
@check = Check.find(params[:id])
@checkable = @check.checkable

@check.destroy
Newspaper.publish(:check_cancel, { check: @check })
@checkable.change_learning_status(:submitted) if @checkable.is_a?(Product)
redirect_back(fallback_location: @checkable)
begin
Check.transaction do
@check.destroy!
ActiveSupport::Notifications.instrument('check.cancel', check: @check)
end
redirect_back(fallback_location: @checkable)
rescue StandardError => e
Rails.logger.error("[ChecksController#destroy] チェック削除でエラー: #{e.message}")
redirect_back(fallback_location: @checkable, alert: 'エラーが発生しました。')
end
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/models/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class Check < ApplicationRecord
belongs_to :user
belongs_to :checkable, polymorphic: true
after_create CheckCallbacks.new
after_destroy CheckCallbacks.new
after_create_commit -> { CheckCallbacks.new.after_create(self) }
after_destroy_commit -> { CheckCallbacks.new.after_destroy(self) }
alias sender user

validates :user_id, uniqueness: { scope: %i[checkable_id checkable_type] }
Expand Down
2 changes: 1 addition & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def change_learning_status(status)
user_id: user.id,
practice_id: practice.id
)
learning.update(status:)
learning.update!(status:)
end

# nilの場合あり
Expand Down