Skip to content

Commit

Permalink
find_practiceというメソッド定義をやめた
Browse files Browse the repository at this point in the history
・2箇所の重複しかないため、DRYとしての意味が薄いため
・不要なメソッドを定義することで可読性が低くなってしまっていたため
  • Loading branch information
masyuko0222 committed Jul 16, 2024
1 parent 30188e2 commit 82833b3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Mentor::Practices::SubmissionAnswerController < ApplicationController
before_action :set_submission_answer, only: %i[edit update]

def new
@practice = find_practice
@practice = Practice.find(params[:practice_id])
@submission_answer = @practice.build_submission_answer
end

Expand All @@ -14,7 +14,7 @@ def edit
end

def create
@practice = find_practice
@practice = Practice.find(params[:practice_id])
@submission_answer = @practice.build_submission_answer(submission_answer_params)
if @submission_answer.save
redirect_to practice_submission_answer_url, notice: '模範解答を作成しました。'
Expand All @@ -38,10 +38,6 @@ def set_submission_answer
@submission_answer = SubmissionAnswer.find_by(practice_id: params[:practice_id])
end

def find_practice
Practice.find(params[:practice_id])
end

def submission_answer_params
params.require(:submission_answer).permit(:description)
end
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/practices/submission_answer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ class Practices::SubmissionAnswerController < ApplicationController
before_action :check_permission!, only: %i[show]

def show
@practice = find_practice
@practice = Practice.find(params[:practice_id])
@submission_answer = @practice.submission_answer
end

private

def check_permission!
practice = find_practice
practice = Practice.find(params[:practice_id])
submission_answer = practice.submission_answer || SubmissionAnswer.new(practice:)
return if policy(submission_answer).show?

redirect_to practice_path(practice), alert: 'プラクティスを修了するまで模範解答は見れません。'
end

def find_practice
Practice.find(params[:practice_id])
end
end

0 comments on commit 82833b3

Please sign in to comment.