diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9900dc20..b53e04a4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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' diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index c01d6f9e..9ce853a3 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -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]) @@ -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 diff --git a/app/models/account.rb b/app/models/account.rb index 868dc139..ade2474d 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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 diff --git a/app/views/accounts/new.html.slim b/app/views/accounts/new.html.slim index da01b682..b81b798c 100644 --- a/app/views/accounts/new.html.slim +++ b/app/views/accounts/new.html.slim @@ -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" \ No newline at end of file + = form.submit 'Save', class: "button is-primary is-fullwidth" diff --git a/spec/controllers/accounts_spec.rb b/spec/controllers/accounts_spec.rb index d2f1b305..3947cf7c 100644 --- a/spec/controllers/accounts_spec.rb +++ b/spec/controllers/accounts_spec.rb @@ -7,7 +7,7 @@ let(:user) { create(:user, account: account) } before { sign_in user } - + describe '#show' do subject(:show) { get :show, params: {id: account } } @@ -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) } @@ -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) } @@ -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