💡
この記事は Middleman 時代に書いた古いものです。記録のため、astro-notion-blog に移行していますが、あまり参考にしないでください。
gakusei_spec の記述(destroy と show)
spec/requests/kurasus/gakusei_spec.rb を修正する.
describe 'DELETE #destroy' do
context 'owned object' do
subject { -> { delete kurasu_gakusei_path(kurasu, object) } }
it_behaves_like :response_status_check, 302
it_behaves_like :decrement_object_by_destroy, Gakusei
it_behaves_like :redirect_to
it_behaves_like :flash_notice_message, '学生を削除しました.'
end
context 'now owned object' do
subject { -> { delete kurasu_gakusei_path(kurasu, not_mine) } }
it_behaves_like :response_status_check, 302
it_behaves_like :redirect_to
it_behaves_like :flash_alert_message, '学生を削除する権限がありません.'
end
end
describe 'GET #show' do
context 'owned object' do
subject { -> { get kurasu_gakusei_path(kurasu, object) } }
it_behaves_like :response_status_check, 200
it_behaves_like :response_body_includes, %w[学生表示: foo]
end
context 'now owned object' do
subject { -> { get kurasu_gakusei_path(kurasu, not_mine) } }
it_behaves_like :response_status_check, 302
it_behaves_like :redirect_to
it_behaves_like :flash_alert_message, '学生を表示する権限がありません.'
end
end
gakusei_controller の記述(edit と update)
app/controllers/kurasus/gakuseis_controller.rb を記述する.
def destroy
redirect_to(kurasu_gakuseis_path(@kurasu, edit_mode: true), alert: alert_message(Gakusei)) and return if @gakusei.nil?
@gakusei.destroy
redirect_to kurasu_gakuseis_path(@kurasu, edit_mode: true), notice: notice_message(Gakusei)
end
def show
redirect_to(kurasu_gakuseis_path(@kurasu, edit_mode: true), alert: alert_message(Gakusei)) and return if @gakusei.nil?
end
show の view を作成
app/views/kurasus/gakuseis/show.html.haml を記述する.
- content_for :title do
- @title = "#{t '.title'}: #{@gakusei.name} (#{@kurasu.name})"
guard の結果
guard の結果は以下のようになった
DELETE #destroy
owned object
behaves like response_status_check
response should be 302
behaves like decrement_object_by_destroy
should change `Gakusei.count` by -1
behaves like redirect_to
should redirect to "/kurasus/3492/gakuseis?edit_mode=true"
behaves like flash_notice_message
should eq "学生を削除しました."
now owned object
behaves like response_status_check
response should be 302
behaves like redirect_to
should redirect to "/kurasus/3496/gakuseis?edit_mode=true"
behaves like flash_alert_message
should eq "学生を削除する権限がありません."
GET #show
owned object
behaves like response_status_check
response should be 200
behaves like response_body_includes
response body includes ["学生表示:", "foo"]
now owned object
behaves like response_status_check
response should be 302
behaves like redirect_to
should redirect to "/kurasus/3504/gakuseis?edit_mode=true"
behaves like flash_alert_message
should eq "学生を表示する権限がありません."
長くなったので今日はここまで