Skip to content

Commit

Permalink
Match CRUD operations and tests for good ones
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Feb 22, 2024
1 parent 1e0a5e5 commit 3c09717
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 66 deletions.
9 changes: 5 additions & 4 deletions app/controllers/admin/financial_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ class Admin::FinancialAccountsController < ApplicationController

def create
if financial_account.save
flash.notice = "Financial Account successfully created"
flash.notice = "Financial Account created"
redirect_to admin_financial_account_path(financial_account)
else
flash.alert = financial_account.errors.full_messages.join(",")
flash.alert = financial_account.errors.full_messages.to_sentence
redirect_to new_admin_financial_account_path
end
end

def update
if financial_account.update(financial_account_params)
flash.notice = "Financial Account successfully updated"
flash.notice = "Financial Account updated"
redirect_to admin_financial_account_path(financial_account)
else
flash.alert = financial_account.errors.full_messages.join(",")
flash.alert = financial_account.errors.full_messages.to_sentence
redirect_to edit_admin_financial_account_path(financial_account)
end
end

def destroy
financial_account.destroy
flash.notice = "Financial Account deleted"
redirect_to admin_financial_accounts_path
end

Expand Down
14 changes: 9 additions & 5 deletions app/controllers/admin/gift_ideas_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
class Admin::GiftIdeasController < ApplicationController
expose(:gift_idea)
expose(:gift_ideas) { GiftIdea.all.limit(10) }
expose(:gift_ideas) do
GiftIdea.order(created_at: :desc).page(params[:page])
end

def create
if gift_idea.save
flash.notice = "Gift Idea created"
redirect_to admin_gift_idea_path(gift_idea)
else
flash.alert = gift_idea.errors.full_messages
render :new
flash.alert = gift_idea.errors.full_messages.to_sentence
redirect_to new_admin_gift_idea_path
end
end

def update
if gift_idea.update(gift_idea_params)
flash.notice = "Gift Idea updated"
redirect_to admin_gift_idea_path(gift_idea)
else
flash.alert = gift_idea.errors.full_messages
render :edit
flash.alert = gift_idea.errors.full_messages.to_sentence
redirect_to edit_admin_gift_idea_path(gift_idea)
end
end

Expand Down
4 changes: 3 additions & 1 deletion app/models/gift_idea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def table_attrs
[
["Title", title],
["Website URL", website_url],
["Note", note]
["Note", note],
["Created At", created_at.to_formatted_s(:long)],
["Updated At", updated_at.to_formatted_s(:long)]
]
end
end
2 changes: 1 addition & 1 deletion app/views/admin/financial_accounts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

%p= link_to "Edit Financial Account", edit_admin_financial_account_path(financial_account)

%p= link_to "Delete Financial Account", admin_financial_account_path(action: :delete, id: financial_account.id), data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'}
%p= link_to "Delete Financial Account", admin_financial_account_path(financial_account), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }

= render partial: "attrs_table", locals: { attrs: financial_account.table_attrs }
7 changes: 4 additions & 3 deletions app/views/admin/gift_ideas/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%h1 Gift Idea #{gift_idea.id}
%p= link_to "show", admin_gift_idea_path(gift_idea)
%p= link_to "delete", admin_gift_idea_path(gift_idea), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }
%h1 Edit Gift Idea #{gift_idea.id}

%p= link_to "Show Gift Idea", admin_gift_idea_path(gift_idea)

= render "form", gift_idea: gift_idea, button_label: "update"
19 changes: 15 additions & 4 deletions app/views/admin/gift_ideas/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
%h1 Gift Ideas

%p= link_to "new gift idea", new_admin_gift_idea_path
%p= link_to "New Gift Idea", new_admin_gift_idea_path

%ul
- gift_ideas.each do |gift_idea|
%li= link_to gift_idea.title, admin_gift_idea_path(gift_idea)
%table
%thead
%tr
%th ID
%th Title
%th.text-right Created At
%tbody
- gift_ideas.each do |gift_idea|
%tr
%td= link_to gift_idea.id, admin_gift_idea_path(gift_idea)
%td= gift_idea.title
%td.text-right= gift_idea.created_at.to_formatted_s(:long)

= paginate gift_ideas
4 changes: 3 additions & 1 deletion app/views/admin/gift_ideas/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
%h1 New Gift Idea
%p= link_to "gift ideas", admin_gift_ideas_path

%p= link_to "Gift Idea List", admin_gift_ideas_path

= render "form", gift_idea: gift_idea, button_label: "create"
6 changes: 4 additions & 2 deletions app/views/admin/gift_ideas/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
%h1 Gift Idea #{gift_idea.id}

%p= link_to "gift ideas", admin_gift_ideas_path
%p= link_to "Gift Idea List", admin_gift_ideas_path

%p= link_to "edit", edit_admin_gift_idea_path(gift_idea)
%p= link_to "Edit Gift Idea", edit_admin_gift_idea_path(gift_idea)

%p= link_to "Delete Gift Idea", admin_gift_idea_path(gift_idea), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }

= render partial: "attrs_table", locals: { attrs: gift_idea.table_attrs }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin creates FinancialAccount" do
describe "Admin creates financial account" do
include_context "admin password matches"

scenario "from list page" do
Expand All @@ -14,16 +14,30 @@
scenario "create with errors" do
visit "/admin/financial_accounts/new"
click_on "create"
expect(page).to have_css ".border-pink p", text: "Name can't be blank"
expect(page).to have_css ".alert", text: "Name can't be blank"
expect(current_path).to eq new_admin_financial_account_path
end

scenario "create successfully" do
visit "/admin/financial_accounts/new"
fill_in "name", with: "Brand new account"
click_on "create"
expect(page).to have_css ".border-purple p", text: "Financial Account successfully created"
expect(FinancialAccount.count).to eq 1

expect(page).to have_css ".notice", text: "Financial Account created"

financial_account = FinancialAccount.last
expect(financial_account.name).to eq "Brand new account"
expect(current_path).to eq admin_financial_account_path(financial_account)

actual_values = page.all("tr").map do |table_row|
table_row.all("td").map(&:text)
end

expect(actual_values).to eq(
[
["Name", "Brand new account"],
["Created At", financial_account.created_at.to_formatted_s(:long)],
["Updated At", financial_account.updated_at.to_formatted_s(:long)]
]
)
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin deletes FinancialAccount" do
describe "Admin deletes financial account" do
include_context "admin password matches"

let(:financial_account) { FactoryBot.create(:financial_account) }
Expand All @@ -19,7 +19,7 @@

accept_confirm { click_on "Delete Financial Account" }

expect(page).to have_css "h1", text: "Financial Accounts"
expect(page).to have_css ".notice", text: "Financial Account deleted"

expect(FinancialAccount.count).to eq 0
expect(current_path).to eq admin_financial_accounts_path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin edits FinancialAccount" do
describe "Admin edits financial account" do
include_context "admin password matches"

scenario "from show page" do
Expand All @@ -12,24 +12,25 @@
expect(current_path).to eq edit_admin_financial_account_path(financial_account)
end

scenario "update with errors" do
scenario "edit with errors" do
financial_account = FactoryBot.create(:financial_account)
visit "/admin/financial_accounts/#{financial_account.id}/edit"
fill_in "name", with: ""
click_on "update"
expect(page).to have_css ".border-pink p", text: "Name can't be blank"
expect(page).to have_css ".alert", text: "Name can't be blank"
end

scenario "update successfully" do
scenario "edit successfully" do
financial_account = FactoryBot.create(
:financial_account,
name: "Band new account"
)
visit "/admin/financial_accounts/#{financial_account.id}/edit"
fill_in "name", with: "Brand new account"
click_on "update"
expect(page).to have_css ".border-purple p", text: "Financial Account successfully updated"
financial_account.reload
expect(financial_account.name).to eq "Brand new account"

expect(page).to have_css ".notice", text: "Financial Account updated"
expect(current_path).to eq admin_financial_account_path(financial_account)
expect(page).to have_css "td", text: "Brand new account"
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin views FinancialAccount" do
describe "Admin views financial account" do
include_context "admin password matches"

scenario "from list page" do
Expand All @@ -12,7 +12,7 @@
expect(current_path).to eq admin_financial_account_path(financial_account)
end

scenario "viewing FinancialAccount" do
scenario "viewing a record" do
financial_account = FactoryBot.create(
:financial_account,
name: "Slush fund"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin views FinancialAccount records" do
describe "Admin views financial accounts" do
include_context "admin password matches"

scenario "from dashboard" do
Expand Down
28 changes: 25 additions & 3 deletions spec/system/gift_ideas/admin_creates_gift_idea_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
require "rails_helper"

describe "Admin creates gift idea" do
describe "Admin creates GiftIdea" do
include_context "admin password matches"

scenario "create gift idea" do
scenario "from list page" do
visit "/admin/gift_ideas"
click_on "New Gift Idea"
expect(page).to have_css "h1", text: "New Gift Idea"
expect(page).to have_css "a", text: "Gift Idea List"
expect(current_path).to eq new_admin_gift_idea_path
end

scenario "create with errors" do
visit "/admin/gift_ideas/new"
click_on "create"
expect(page).to have_css ".alert", text: "Title can't be blank and Website url can't be blank"
expect(current_path).to eq new_admin_gift_idea_path
end

scenario "create successfully" do
visit "/admin/gift_ideas/new"
fill_in "title", with: "New Mario Game"
fill_in "website", with: "https://www.nintendo.com/new-mario-game"
fill_in "note", with: "Please get me the actual physical game, thanks!"
click_on "create"

expect(page).to have_css ".notice", text: "Gift Idea created"

gift_idea = GiftIdea.last
expect(current_path).to eq admin_gift_idea_path(gift_idea)

actual_values = page.all("tr").map do |table_row|
table_row.all("td").map(&:text)
end
Expand All @@ -18,7 +38,9 @@
[
["Title", "New Mario Game"],
["Website URL", "https://www.nintendo.com/new-mario-game"],
["Note", "Please get me the actual physical game, thanks!"]
["Note", "Please get me the actual physical game, thanks!"],
["Created At", gift_idea.created_at.to_formatted_s(:long)],
["Updated At", gift_idea.updated_at.to_formatted_s(:long)]
]
)
end
Expand Down
25 changes: 19 additions & 6 deletions spec/system/gift_ideas/admin_deletes_gift_idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
describe "Admin deletes gift idea" do
include_context "admin password matches"

scenario "delete gift idea" do
gift_idea = FactoryBot.create(:gift_idea)
visit "/admin/gift_ideas/#{gift_idea.id}/edit"
click_on "delete"
accept_alert
let(:gift_idea) { FactoryBot.create(:gift_idea) }

expect(page).to have_content "Gift Idea deleted"
scenario "cancels delete" do
visit "/admin/gift_ideas/#{gift_idea.id}"

dismiss_confirm { click_on "Delete Gift Idea" }

expect(GiftIdea.count).to eq 1
expect(current_path).to eq admin_gift_idea_path(gift_idea)
end

scenario "confirms delete" do
visit "/admin/gift_ideas/#{gift_idea.id}"

accept_confirm { click_on "Delete Gift Idea" }

expect(page).to have_css ".notice", text: "Gift Idea deleted"

expect(GiftIdea.count).to eq 0
expect(current_path).to eq admin_gift_ideas_path
end
end
36 changes: 36 additions & 0 deletions spec/system/gift_ideas/admin_edits_gift_idea_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "rails_helper"

describe "Admin edits gift idea" do
include_context "admin password matches"

scenario "from show page" do
gift_idea = FactoryBot.create(:gift_idea)
visit "/admin/gift_ideas/#{gift_idea.id}"
click_on "Edit Gift Idea"
expect(page).to have_css "h1", text: "Edit Gift Idea #{gift_idea.id}"
expect(page).to have_css "a", text: "Show Gift Idea"
expect(current_path).to eq edit_admin_gift_idea_path(gift_idea)
end

scenario "edit with errors" do
gift_idea = FactoryBot.create(:gift_idea)
visit "/admin/gift_ideas/#{gift_idea.id}/edit"
fill_in "title", with: ""
click_on "update"
expect(page).to have_css ".alert", text: "Title can't be blank"
end

scenario "edit successfully" do
gift_idea = FactoryBot.create(
:gift_idea,
title: "Mew Nario Game"
)
visit "/admin/gift_ideas/#{gift_idea.id}/edit"
fill_in "title", with: "New Mario Game"
click_on "update"

expect(page).to have_css ".notice", text: "Gift Idea updated"
expect(current_path).to eq admin_gift_idea_path(gift_idea)
expect(page).to have_css "td", text: "New Mario Game"
end
end
14 changes: 0 additions & 14 deletions spec/system/gift_ideas/admin_updates_gift_idea_spec.rb

This file was deleted.

Loading

0 comments on commit 3c09717

Please sign in to comment.