From 903086ab97db64476ce9ec652087d8974befc60a Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Thu, 11 Oct 2018 23:26:12 +0200 Subject: [PATCH] Add import route for bank_transactions --- .../finance/bank_accounts_controller.rb | 14 ++++++++++++++ app/models/bank_account.rb | 3 +++ .../finance/bank_transactions/index.html.haml | 3 +++ config/locales/en.yml | 3 +++ config/routes.rb | 4 ++++ 5 files changed, 27 insertions(+) diff --git a/app/controllers/finance/bank_accounts_controller.rb b/app/controllers/finance/bank_accounts_controller.rb index 8430143d..943fe400 100644 --- a/app/controllers/finance/bank_accounts_controller.rb +++ b/app/controllers/finance/bank_accounts_controller.rb @@ -5,4 +5,18 @@ class Finance::BankAccountsController < Finance::BaseController redirect_to finance_bank_account_transactions_url(@bank_accounts.first) if @bank_accounts.count == 1 end + def import + @bank_account = BankAccount.find(params[:id]) + import_method = @bank_account.find_import_method + if import_method + count = import_method.call(@bank_account) + redirect_to finance_bank_account_transactions_url(@bank_account), notice: t('finance.bank_accounts.controller.import.notice', count: count) + else + # @todo add import for csv files as fallback + redirect_to finance_bank_account_transactions_url(@bank_account), alert: t('finance.bank_accounts.controller.import.no_import_method') + end + rescue => error + redirect_to finance_bank_account_transactions_url(@bank_account), alert: t('errors.general_msg', msg: error.message) + end + end diff --git a/app/models/bank_account.rb b/app/models/bank_account.rb index 4642afd3..8249e9ca 100644 --- a/app/models/bank_account.rb +++ b/app/models/bank_account.rb @@ -9,4 +9,7 @@ class BankAccount < ActiveRecord::Base validates_format_of :iban, :with => /\A[A-Z]{2}[0-9]{2}[0-9A-Z]{,30}\z/ validates_numericality_of :balance, :message => I18n.t('bank_account.model.invalid_balance') + # @return [Function] Method wich can be called to import transaction from a bank or nil if unsupported + def find_import_method + end end diff --git a/app/views/finance/bank_transactions/index.html.haml b/app/views/finance/bank_transactions/index.html.haml index a6ad44ed..845cecd8 100644 --- a/app/views/finance/bank_transactions/index.html.haml +++ b/app/views/finance/bank_transactions/index.html.haml @@ -1,5 +1,8 @@ - title t('.title', name: @bank_account.name, balance: number_to_currency(@bank_account.balance)) +- content_for :actionbar do + = link_to t('.import_transaction'), import_finance_bank_account_path(@bank_account), class: 'btn btn-primary' + .well.well-small = form_tag finance_bank_account_transactions_path, :method => :get, :remote => true, 'data-submit-onchange' => true, class: 'form-search' do diff --git a/config/locales/en.yml b/config/locales/en.yml index dee1237c..492a9592 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -905,6 +905,9 @@ en: with_extra_charge: 'with extra charge:' without_extra_charge: 'without extra charge:' bank_accounts: + controller: + import: + notice: '%{count} new transactions have been imported' index: account_statement: Account statement title: Bank Accounts diff --git a/config/routes.rb b/config/routes.rb index c9dfa78e..700e88cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -192,6 +192,10 @@ Foodsoft::Application.routes.draw do post 'transactions/create_collection' => 'financial_transactions#create_collection', as: 'create_transaction_collection' resources :bank_accounts, only: [:index] do + member do + get :import + end + resources :bank_transactions, as: :transactions end