Skip to content

Commit

Permalink
πŸžπŸ”¨ Journal: Show error messages when Entry cannot be saved (#1630)
Browse files Browse the repository at this point in the history
- https://github.com/zinc-collective/convene/issues/1566

Womp womp; apparently I had forgotten to do the `unprocessable_entity`,
which meant that `turbo` was failing miserably instead of replacing the
form when creating or updating a journal entry.

Well; now it's fixed!
  • Loading branch information
zspencer authored Jul 6, 2023
1 parent 9b9d38d commit 14d6ce1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/furniture/journal/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def create
if entry.save
redirect_to [space, room]
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand All @@ -17,7 +17,7 @@ def update
if entry.update(entry_params)
redirect_to entry.location
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand Down
1 change: 1 addition & 0 deletions app/furniture/journal/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Entry < ApplicationRecord
scope :recent, -> { order("published_at DESC NULLS FIRST") }

attribute :headline, :string
validates :headline, presence: true
attribute :body, :string
validates :body, presence: true

Expand Down
41 changes: 31 additions & 10 deletions spec/furniture/journal/entries_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,50 @@
let(:journal_entry_attributes) { attributes_for(:journal_entry) }
let(:created_journal_entry) { journal.entries.first }

specify { expect { perform_request }.to change(journal.entries, :count).by(1) }

specify {
perform_request
expect { perform_request }.to change(journal.entries, :count).by(1)
expect(response).to redirect_to(polymorphic_path(room.location))
expect(created_journal_entry.headline).to eql(journal_entry_attributes[:headline])
}

specify {
perform_request
expect(created_journal_entry.body).to eql(journal_entry_attributes[:body])
}

context "when the journal entry cannot be created" do
let(:journal_entry_attributes) { {headline: ""} }

specify {
expect { perform_request }.not_to change(journal.entries, :count)

expect(response).to be_unprocessable
}
end
end

describe "#update" do
subject(:perform_request) do
put polymorphic_path(entry.location), params: {entry: {published_at: published_at}}
put polymorphic_path(entry.location), params: {entry: entry_params}
entry.reload
response
end

let(:published_at) { 1.day.ago.beginning_of_day }
let(:entry_params) { {published_at: 1.day.ago.beginning_of_day} }
let(:entry) { create(:journal_entry, journal: journal) }

before { sign_in(space, member) }

specify { expect { perform_request }.to change { entry.reload.published_at }.from(nil).to(published_at.to_time) }
specify do
expect { perform_request }.to change(entry, :published_at).from(nil).to(entry_params[:published_at].to_time).and(change(entry, :updated_at))

expect(response).to redirect_to(entry.location)
end

context "when the update cannot be saved" do
let(:entry_params) { {headline: ""} }

specify do
expect { perform_request }.not_to change(entry, :updated_at)

expect(response).to be_unprocessable
end
end
end
end
9 changes: 7 additions & 2 deletions spec/furniture/journal/entry_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require "rails_helper"

RSpec.describe Journal::Entry do
subject(:entry) { build(:journal_entry, body: body) }
RSpec.describe Journal::Entry, type: :model do
subject(:entry) { build(:journal_entry) }

it { is_expected.to validate_presence_of(:headline) }
it { is_expected.to validate_presence_of(:body) }

describe "#to_html" do
subject(:to_html) { entry.to_html }

let(:entry) { build(:journal_entry, body: body) }

context "when #body is 'https://www.google.com @[email protected]'" do
let(:body) { "https://www.google.com @[email protected]" }

Expand Down

0 comments on commit 14d6ce1

Please sign in to comment.