📘 hkob-astro-notion-blog

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

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

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

ここもほぼ同内容である.

event_spec の記述(edit と update)

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

describe 'GET #edit' do
  context 'owned object' do
    subject { -> { get edit_kurasu_event_path(kurasu, object) } }
    it_behaves_like :response_status_check, 200
    it_behaves_like :response_body_includes, '2300'
  end

  context 'not owned object' do
    subject { -> { get edit_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 'not owned kurasu' do
    let(:return_path) { kurasus_path }
    subject { -> { get edit_kurasu_event_path(not_mine.kurasu, not_mine) } }
    it_behaves_like :response_status_check, 302
    it_behaves_like :redirect_to
    it_behaves_like :flash_alert_message, 'イベントを編集する権限がありません.'
  end
end

describe 'PATCH #update' do
  before { @attrs = object.attributes }

  context 'owned object' do
    subject { -> { patch kurasu_event_path(kurasu, object), params: {event: @attrs} } }

    context '正しいパラメータに対して' do
      before { @attrs['name'] = 'new name' }
      it_behaves_like :response_status_check, 302
      it_behaves_like :change_object_value_by_update, Event, :name, 'new name'
      it_behaves_like :redirect_to
      it_behaves_like :flash_notice_message, 'イベントを更新しました.'
    end

    context '不正なパラメータに対して' do
      before { @attrs['name'] = nil }
      it_behaves_like :response_status_check, 200
      it_behaves_like :not_change_object_value_by_update, Event, :name
      it_behaves_like :flash_alert_message, 'イベントが更新できません.'
    end
  end

  context 'not owned object' do
    subject { -> { put 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 'not owned kurasu' do
    let(:return_path) { kurasus_path }
    subject { -> { put kurasu_event_path(not_mine.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
end

event_controller の記述(edit と update)

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

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

def update
  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?
  if @event.update_attributes(event_params)
    redirect_to kurasu_events_path(@kurasu, edit_mode: true), notice: notice_message(Event)
  else
    flash.now[:alert] = alert_message(Event)
    render action: :edit
  end
end

edit の view を作成

app/views/kurasus/events/edit.html.haml を記述する.kurasus/edit と同様に _form の呼び出しのみである.

- content_for :title do
  - @title = "#{t '.title'}: #{@event.name}"
= render partial: 'form', locals: {event: @event, kurasu: @kurasu}

guard の結果

guard の結果は以下のようになった

GET #edit
  owned object
    behaves like response_status_check
      response should be 200
    behaves like response_body_includes
      response body includes 2300
  not owned object
    behaves like response_status_check
      response should be 302
    behaves like redirect_to
      should redirect to "/kurasus/2500/events?edit_mode=true"
    behaves like flash_alert_message
      should eq "イベントを編集する権限がありません."
  not 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 "イベントを編集する権限がありません."
PATCH #update
  owned object
    正しいパラメータに対して
      behaves like response_status_check
        response should be 302
      behaves like change_object_value_by_update
        should change `klass.find(object.id).send(key)` from "始業" to "new name"
      behaves like redirect_to
        should redirect to "/kurasus/2512/events?edit_mode=true"
      behaves like flash_notice_message
        should eq "イベントを更新しました."
    不正なパラメータに対して
      behaves like response_status_check
        response should be 200
      behaves like not_change_object_value_by_update
        should not change `klass.find(object.id).send(key)`
      behaves like flash_alert_message
        should eq "イベントが更新できません."
  not owned object
    behaves like response_status_check
      response should be 302
    behaves like redirect_to
      should redirect to "/kurasus/2519/events?edit_mode=true"
    behaves like flash_alert_message
      should eq "イベントが更新できません."
  not 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 "イベントが更新できません."

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