Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(weekly topup): create on the account creation form #96

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def show
end

def new
@account = Account.new
@account.automatic_topup_configs.build(from_account_id: current_user.account.id)
end

def edit
Expand All @@ -21,12 +23,16 @@ def update
end

def create
Account.create!(parent: current_user.account, **ps.slice(:name))
Account.create!(parent: current_user.account, **account_params)
redirect_to my_account_path
end

private

def account_params
params.require(:account).permit(:name, automatic_topup_configs_attributes: %i[from_account_id amount])
end

helper_method memoize def account
Account.visible_for(current_user).find(ps.fetch(:id))
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Account < ApplicationRecord
has_one :user

has_many :automatic_topup_configs, class_name: 'AccountAutomaticTopupConfig', foreign_key: :to_account_id
accepts_nested_attributes_for :automatic_topup_configs, reject_if: ->(attributes) { attributes['amount'].blank? }

has_many :objectives

belongs_to :parent, class_name: 'Account', optional: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/account_automatic_topup_config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AccountAutomaticTopupConfig < ApplicationRecord
belongs_to :from_account, class_name: 'Account', foreign_key: :from_account_id
belongs_to :to_account, class_name: 'Account', foreign_key: :to_account_id
belongs_to :to_account, class_name: 'Account', foreign_key: :to_account_id, optional: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it's optional?

Copy link
Contributor Author

@andreybakanovsky andreybakanovsky Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an attempt to resolve the validation issue.

end
14 changes: 9 additions & 5 deletions app/views/accounts/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
.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 model: @account do |f|
.field
= f.text_field :name, autofocus: true, class: "input", placeholder: "Name"
br
br
div.buttons.is-flex.is-justify-content-flex-end
.field
= f.fields_for :automatic_topup_configs do |auto_topup_form|
= auto_topup_form.text_field :amount, class: "input", placeholder: "Weekly amount (optional)"
= auto_topup_form.hidden_field :from_account_id
.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"
= f.submit 'Save', class: "button is-primary is-fullwidth"
18 changes: 12 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,13 @@
end

describe '#create' do
subject { post :create, params: { parent: account, name: account.name } }
let(:valid_params) do
{ account: { parent: account, name: account.name,
automatic_topup_configs_attributes: {
'0': {amount: 10, from_account_id: user.account.id }
}}}
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 +41,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 +53,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