Skip to content

Commit

Permalink
feat(weekly topup): create on the account creation form (#96)
Browse files Browse the repository at this point in the history
* feat(weekly topup): create on the account creation form

* feat(weekly topup): update tests

* feat(weekly topup): fix tests

* Revert "feat(weekly topup): update tests"

This reverts commit d88ebda.

* feat(weekly topup): fix

* feat(weekly topup): fix style

* feat(weekly topup): update the solution
  • Loading branch information
andreybakanovsky authored Sep 13, 2023
1 parent a2600e1 commit efdc3ca
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
1 change: 0 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ Style/ClassAndModuleChildren:
Style/EmptyMethod:
Exclude:
- 'app/controllers/account_automatic_topup_configs_controller.rb'
- 'app/controllers/accounts_controller.rb'
- 'app/controllers/home_controller.rb'
- 'app/controllers/objectives_controller.rb'
- 'app/controllers/spends_controller.rb'
Expand Down
20 changes: 13 additions & 7 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
class AccountsController < ApplicationController
before_action :authenticate_user!

def show
end
def show; end

def new
end
def new; end

def edit
end
def edit; end

def update
if account.update(ps[:account])
Expand All @@ -21,7 +18,16 @@ def update
end

def create
Account.create!(parent: current_user.account, **ps.slice(:name))
ActiveRecord::Base.transaction do
account = Account.create!(parent: current_user.account, **ps.slice(:name))

automatic_topup_amount = ps[:automatic_topup].fetch(:amount)
if automatic_topup_amount.present?
AccountAutomaticTopupConfig.create!(from_account: current_user.account,
to_account: account,
amount: automatic_topup_amount)
end
end
redirect_to my_account_path
end

Expand Down
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Account < ApplicationRecord
has_one :user

has_many :automatic_topup_configs, class_name: 'AccountAutomaticTopupConfig', foreign_key: :to_account_id

has_many :objectives

belongs_to :parent, class_name: 'Account', optional: true
Expand Down
15 changes: 9 additions & 6 deletions app/views/accounts/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
.columns.is-centered
.column.is-one-third
h2.title.is-4.has-text-centered New account
= form_with url: accounts_path do |f|
= form_with url: accounts_path do |form|
.field
= f.text_field :name, autofocus: true, class: "input", placeholder: "Name"
br
br
div.buttons.is-flex.is-justify-content-flex-end
= form.text_field :name, autofocus: true, class: "input", placeholder: "Name"
.field
= form.fields_for :automatic_topup do |auto_topup_form|
= auto_topup_form.text_field :amount, class: "input", placeholder: "Weekly amount (optional)"
.notification.is-light
| This amount will be automatically added to the account every week. You will receive an email notification about that
.buttons.is-flex.is-justify-content-flex-end
= link_to 'Back', my_account_path, class: 'button is-light'
= f.submit 'Save', class: "button is-primary is-fullwidth"
= form.submit 'Save', class: "button is-primary is-fullwidth"
16 changes: 10 additions & 6 deletions spec/controllers/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:user) { create(:user, account: account) }

before { sign_in user }

describe '#show' do
subject(:show) { get :show, params: {id: account } }

Expand All @@ -25,7 +25,11 @@
end

describe '#create' do
subject { post :create, params: { parent: account, name: account.name } }
let(:valid_params) do
{ name: account.name, automatic_topup: {amount: 10 }}
end

subject { post :create, params: valid_params}

it { is_expected.to redirect_to(my_account_path) }
it { is_expected.to have_http_status(302) }
Expand All @@ -35,7 +39,7 @@

describe '#edit' do
subject(:edit) { get :edit, params: { id: account }}

it { is_expected.to have_http_status(:success) }
it { is_expected.to render_template(:edit) }
it { is_expected.to_not render_template(:new) }
Expand All @@ -47,9 +51,9 @@

subject(:bad_update) { patch :update, params: { id: user.account, account: { name: nil }} }
subject(:update) { patch :update, params: { id: user.account, account: { name: name }} }
it { is_expected.to have_http_status(:redirect) }
it { expect(bad_update).not_to be_redirect }

it { is_expected.to have_http_status(:redirect) }
it { expect(bad_update).not_to be_redirect }
it 'name changed' do
expect(Account.where(id: account.id).name).not_to eq(old_name)
end
Expand Down

0 comments on commit efdc3ca

Please sign in to comment.