Skip to content

Commit

Permalink
Tweaks and specs...
Browse files Browse the repository at this point in the history
  • Loading branch information
burkematthew committed Sep 5, 2022
1 parent 4446ace commit 8c82b1c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 11 deletions.
3 changes: 0 additions & 3 deletions app/controllers/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def new

def create
@budget = Budget.new(budget_params)
debugger
respond_to do |format|
if @budget.save
format.html { redirect_to budgets_path, notice: "Budget was successfully added." }
Expand Down Expand Up @@ -51,8 +50,6 @@ def destroy
else
render :show, notice: "Failed to delete Budget."
end
rescue ActiveRecord::RecordNotFound
redirect_to budgets_path, status: :not_found
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/budgets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<%= render(Forms::HeaderComponent.new(text: "Edit #{@budget.description} Budget for #{budget.month.titleize}")) %>
<%= render(Forms::HeaderComponent.new(text: "Edit #{@budget.description} Budget for #{@budget.month.titleize}")) %>

<%= render partial: "form", budget: @budget %>
</div>
2 changes: 1 addition & 1 deletion app/views/budgets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<tbody>
<% @budgets.each do |budget| %>
<tr class="whitespace-nowrap">
<td class="px-6 py-4 text-sm text-gray-500"><%= budget.month %></td>
<td class="px-6 py-4 text-sm text-gray-500"><%= budget.month.humanize %></td>
<td class="px-6 py-4 text-sm text-gray-500"><%= budget.cash_flow_type %></td>
<td class="px-6 py-4 text-sm text-gray-500"><%= budget.description %></td>
<td class="px-6 py-4 text-sm text-gray-500"><%= budget.amount %></td>
Expand Down
10 changes: 6 additions & 4 deletions app/views/budgets/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<%= render(Forms::ShowComponent.new(
header: @member.full_name,
header: "#{@budget.description} Budget for #{@budget.month.titleize}",
fields: [
{ label: "Birthdate", data: @member.birthdate&.strftime("%B %d, %Y") },
{ label: "Created", data: @member.created_at.to_date&.strftime("%B %d, %Y") },
{ label: "Updated", data: @member.updated_at.to_date&.strftime("%B %d, %Y") }
{ label: "Cash Flow Type", data: @budget.cash_flow_type },
{ label: "Budget Category", data: @budget.category.description },
{ label: "Amount", data: @budget.amount },
{ label: "Created", data: @budget.created_at.to_date&.strftime("%B %d, %Y") },
{ label: "Updated", data: @budget.updated_at.to_date&.strftime("%B %d, %Y") }
]
))
%>
5 changes: 3 additions & 2 deletions spec/factories/budgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
FactoryBot.define do
factory :budget do
month { Budget::BUDGET_MONTHS.first }
description { "Landing Paycheck" }
description { "Paycheck" }
cash_flow_type { :income }
association :budget_category, factory: :budget_category
amount { 1.0 }
association :category, factory: :budget_category
end
end
22 changes: 22 additions & 0 deletions spec/helpers/budgets_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "rails_helper"

# Specs in this file have access to a helper object that includes
# the MembersHelper. For example:
#
# describe MembersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe BudgetsHelper, type: :helper do
describe "#budget_month_options_for_select" do
it "builds an array with 12 elements" do
expect(helper.budget_month_options_for_select.is_a?(Array)).to be_truthy
expect(helper.budget_month_options_for_select.size).to eq(12)
end
end
end
141 changes: 141 additions & 0 deletions spec/requests/budgets_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe BudgetsController, type: :request do
let(:budget_category) { FactoryBot.create(:budget_category) }
let(:budget) { FactoryBot.create(:budget, category: budget_category) }

describe "GET /budgets" do
it "returns HTTP success" do
get budgets_path
expect(response).to have_http_status(:success)
end
end

describe "GET /budget/:id" do
context "when the ID exists" do
it "returns HTTP success" do
get budget_path(budget)
expect(response).to have_http_status(:success)
end
end

context "when the ID doesn't exist" do
it "returns HTTP missing" do
get budget_path(id: budget.id + 10)
expect(response).to have_http_status(:missing)
end
end
end

describe "GET /budget/new" do
it "returns HTTP success" do
get new_budget_path
expect(response).to have_http_status(:success)
end
end

describe "POST /budget" do
let(:good_params) do
{
budget: {
month: Budget::BUDGET_MONTHS.first,
description: "Paycheck",
amount: 1.0,
cash_flow_type: :income,
budget_category_id: budget_category.id
}
}
end

let(:bad_params) do
{
budget: {
month: Budget::BUDGET_MONTHS.first,
description: "Paycheck",
amount: 1.0,
cash_flow_type: :income
}
}
end

context "when the record doesn't already exist" do
it "returns HTTP redirect" do
post budgets_path, params: good_params
expect(response).to have_http_status(:redirect)
end
end

context "when the record is missing required fields" do
it "returns HTTP unprocessible entity" do
post budgets_path, params: bad_params
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "PUT /budgets" do
let(:good_params) do
{
budget: {
month: Budget::BUDGET_MONTHS.first,
description: "Paycheck",
amount: 2.0,
cash_flow_type: :income,
budget_category_id: budget_category.id
}
}
end

let(:missing_params) do
{
id: budget.id,
budget: {
month: Budget::BUDGET_MONTHS.first,
description: nil,
amount: 1.0,
cash_flow_type: :income
}
}
end

context "when the record already exist" do
it "returns HTTP redirect" do
put budget_path(id: budget.id), params: good_params
expect(response).to have_http_status(:redirect)
expect(budget.reload.amount).to eq(2.0)
end
end

context "when the record doesn't already exist" do
it "returns HTTP missing" do
put budget_path(id: budget.id + 1), params: good_params
expect(response).to have_http_status(:missing)
end
end

context "when the record is missing required fields" do
it "returns HTTP unprocessable entity" do
put budget_path(id: budget.id), params: missing_params
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "DELETE /budget/:id" do
context "when the ID exists" do
it "returns HTTP redirect" do
delete budget_path(budget)
expect(response).to have_http_status(:redirect)
end
end

context "when the ID doesn't exist" do
it "returns HTTP missing" do
delete budget_path(id: budget.id + 10)
expect(response).to have_http_status(:missing)
end
end
end
end

0 comments on commit 8c82b1c

Please sign in to comment.