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

RailsApp Rails 2018年 12月 20日

events_spec の記述(destroy と show)

  1. spec/requests/kurasus/event_spec.rb を修正する.
  2. 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)

  1. app/controllers/kurasus/events_controller.rb を記述する.
  2. 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 を作成

  1. app/views/kurasus/events/show.html.haml を記述する.
  2. - content_for :title do
      - @title = "#{t '.title'}: #{@event.name} (#{@kurasu.name})"

guard の結果

  1. guard の結果は以下のようになった
  2. 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 "イベントを表示する権限がありません."

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