📘 hkob-astro-notion-blog

これまではてなブログにて情報発信をしていましたが、令和5年3月22日より、こちらでの情報発信を始めました。2019年以前の古い記事は過去の Middleman 時代のものなので、情報が古いです。記録のためだけに残しています。

events コントローラの作成(4) - 不定期刊 Rails App を作る(33)

💡
この記事は Middleman 時代に書いた古いものです。記録のため、astro-notion-blog に移行していますが、あまり参考にしないでください。
events_spec の記述(destroy と show)

spec/requests/kurasus/event_spec.rb を修正する.

describe 'DELETE #destroy' do
  context 'owned object' do
    subject { -> { delete kurasu_event_path(kurasu, object) } }
    it_behaves_like :response_status_check, 302
    it_behaves_like :decrement_object_by_destroy, Event
    it_behaves_like :redirect_to
    it_behaves_like :flash_notice_message, 'イベントを削除しました.'
  end

  context 'now owned object' do
    subject { -> { delete kurasu_event_path(kurasu, not_mine), params: {event: @attrs} } }
    it_behaves_like :response_status_check, 302
    it_behaves_like :redirect_to
    it_behaves_like :flash_alert_message, 'イベントを削除する権限がありません.'
  end

  context 'now owned kurasu' do
    subject { -> { delete kurasu_event_path(not_mine.kurasu, not_mine), params: {event: @attrs} } }
    let(:return_path) { kurasus_path }
    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_event_path(kurasu, object) } }
    it_behaves_like :response_status_check, 200
    it_behaves_like :response_body_includes, %w[イベント表示: 始業]
  end

  context 'now owned object' do
    subject { -> { get kurasu_event_path(kurasu, not_mine) } }
    it_behaves_like :response_status_check, 302
    it_behaves_like :redirect_to
    it_behaves_like :flash_alert_message, 'イベントを表示する権限がありません.'
  end

  context 'now owned kurasu' do
    subject { -> { get kurasu_event_path(not_mine.kurasu, not_mine) } }
    let(:return_path) { kurasus_path }
    it_behaves_like :response_status_check, 302
    it_behaves_like :redirect_to
    it_behaves_like :flash_alert_message, 'イベントを表示する権限がありません.'
  end
end

events_controller の記述(edit と update)

app/controllers/kurasus/events_controller.rb を記述する.

def destroy
  redirect_to(kurasus_path, alert: alert_message(Event)) and return if @kurasu.nil?
  redirect_to(kurasu_events_path(@kurasu, edit_mode: true), alert: alert_message(Event)) and return if @event.nil?
  @event.destroy
  redirect_to kurasu_events_path(@kurasu, edit_mode: true), notice: notice_message(Event)
end

def show
  redirect_to(kurasus_path, alert: alert_message(Event)) and return if @kurasu.nil?
  redirect_to(kurasu_events_path(@kurasu, edit_mode: true), alert: alert_message(Event)) and return if @event.nil?
end

show の view を作成

app/views/kurasus/events/show.html.haml を記述する.

- content_for :title do
  - @title = "#{t '.title'}: #{@event.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 `Event.count` by -1
    behaves like redirect_to
      should redirect to "/kurasus/2901/events?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/2905/events?edit_mode=true"
    behaves like flash_alert_message
      should eq "イベントを削除する権限がありません."
  now owned kurasu
    behaves like response_status_check
      response should be 302
    behaves like redirect_to
      should redirect to "/kurasus"
    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 ["イベント表示:", "始業"]
  now owned object
    behaves like response_status_check
      response should be 302
    behaves like redirect_to
      should redirect to "/kurasus/2919/events?edit_mode=true"
    behaves like flash_alert_message
      should eq "イベントを表示する権限がありません."
  now owned kurasu
    behaves like response_status_check
      response should be 302
    behaves like redirect_to
      should redirect to "/kurasus"
    behaves like flash_alert_message
      should eq "イベントを表示する権限がありません."

長くなったので今日はここまで