diff --git a/app/controllers/bank_accounts_controller.rb b/app/controllers/bank_accounts_controller.rb new file mode 100644 index 0000000..4bbfd1c --- /dev/null +++ b/app/controllers/bank_accounts_controller.rb @@ -0,0 +1,83 @@ +class BankAccountsController < ApplicationController + before_action :set_owner + before_action :set_bank_account, only: [:edit, :update, :destroy] + + def new + @bank_account = BankAccount.new + end + + def create + @bank_account = @owner.bank_accounts.build(bank_account_params) + if @bank_account.save + redirect_to owner_path(@owner), notice: 'Bank account was successfully created.' + else + render :new + end + end + + def index + @bank_accounts = @owner.bank_accounts.order(start_date: :desc) + end + + def edit + end + + def update + @bank_account = BankAccount.find(params[:id]) + if @bank_account.update(bank_account_params) + redirect_to owner_path(@owner), notice: 'Bank account was successfully updated.' + else + render :edit + end + end + + def destroy + @bank_account.destroy + redirect_to polymorphic_path([@owner]), notice: 'Bank account was successfully deleted.' + end + + + private + + def set_owner + @owner = if params[:participant_id] + Participant.find(params[:participant_id]) + elsif params[:worker_id] + Worker.find(params[:worker_id]) + elsif params[:vendor_id] + Vendor.find(params[:vendor_id]) + end + end + + + def find_owner + params.each do |name, value| + if name =~ /(.+)_id$/ + return $1.classify.constantize.find(value) + end + end + nil + end + + def set_bank_account + @bank_account = BankAccount.find(params[:id]) + end + + def bank_account_params + params.require(:bank_account).permit(:institution_name, :account_type, :routing_number, :account_number, :start_date, :end_date) + end + + def owner_path(owner) + case owner + when Participant + participant_path(owner) + when Worker + worker_path(owner) + when Vendor + vendor_path(owner) + else + root_path # Default path if owner type is unknown + end + end +end + diff --git a/app/helpers/bank_accounts_helper.rb b/app/helpers/bank_accounts_helper.rb new file mode 100644 index 0000000..1c6af26 --- /dev/null +++ b/app/helpers/bank_accounts_helper.rb @@ -0,0 +1,7 @@ +module BankAccountsHelper + def hide_account_number(account_number) + # Example implementation that hides all but the last 4 digits + "**** **** **** #{account_number.last(4)}" + end + end + \ No newline at end of file diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 24ca01a..aef3a42 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -3,4 +3,7 @@ import "@hotwired/turbo-rails" import "controllers" import Rails from '@rails/ujs'; -Rails.start(); \ No newline at end of file +Rails.start(); + + + \ No newline at end of file diff --git a/app/models/bank_account.rb b/app/models/bank_account.rb new file mode 100644 index 0000000..e11dc8a --- /dev/null +++ b/app/models/bank_account.rb @@ -0,0 +1,3 @@ +class BankAccount < ApplicationRecord + belongs_to :owner, polymorphic: true +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 1bfece2..5e75557 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -8,6 +8,7 @@ class Participant < ApplicationRecord has_many :employer_records has_many :employers, through: :employer_records has_and_belongs_to_many :programs + has_many :bank_accounts, as: :owner # Validations validates :first_name, :last_name, :ssn, presence: true diff --git a/app/models/vendor.rb b/app/models/vendor.rb index 398ce6a..d8d072f 100644 --- a/app/models/vendor.rb +++ b/app/models/vendor.rb @@ -3,6 +3,7 @@ class Vendor < ApplicationRecord has_and_belongs_to_many :employers has_many :service_contracts has_many :participants, through: :service_contracts + has_many :bank_accounts, as: :owner # Validations validates :tin, uniqueness: true, allow_blank: true, presence: true diff --git a/app/models/worker.rb b/app/models/worker.rb index d861cbb..9fd1a12 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -18,4 +18,7 @@ class Worker < ApplicationRecord def full_name "#{first_name} #{last_name}" end + + has_many :bank_accounts, as: :owner + end diff --git a/app/views/bank_accounts/_form.html.erb b/app/views/bank_accounts/_form.html.erb new file mode 100644 index 0000000..e9a08fe --- /dev/null +++ b/app/views/bank_accounts/_form.html.erb @@ -0,0 +1,59 @@ +<%= form_with(model: [@owner, @bank_account], local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %> + <% if @bank_account.errors.any? %> +
+

<%= pluralize(@bank_account.errors.count, "error") %> prohibited this bank account from being saved:

+ +
+ <% end %> + +
+ <%= form.label :institution_name, 'Institution Name', class: 'form-label' %> + <%= form.text_field :institution_name, class: 'form-control', placeholder: 'Institution Name' %> +
+ +
+
+ <%= form.radio_button :account_type, 'Checking', class: 'form-check-input' %> + <%= form.label :account_type_checking, 'Checking', class: 'form-check-label' %> + <%= form.radio_button :account_type, 'Savings', class: 'form-check-input' %> + <%= form.label :account_type_savings, 'Savings', class: 'form-check-label' %> +
+ +
+ <%= form.label :routing_number, 'Routing Number', class: 'form-label' %> + <%= form.text_field :routing_number, class: 'form-control', placeholder: 'Routing Number' %> +
+ +
+ <%= form.label :routing_number_confirmation, 'Re-enter Routing Number', class: 'form-label' %> + <%= form.text_field :routing_number_confirmation, class: 'form-control', placeholder: 'Re-enter Routing Number' %> +
+ +
+ <%= form.label :account_number, 'Account Number', class: 'form-label' %> + <%= form.text_field :account_number, class: 'form-control', placeholder: 'Account Number' %> +
+ +
+ <%= form.label :account_number_confirmation, 'Re-enter Account Number', class: 'form-label' %> + <%= form.text_field :account_number_confirmation, class: 'form-control', placeholder: 'Re-enter Account Number' %> +
+ +
+ <%= form.label :start_date, 'Start Date', class: 'form-label' %> + <%= form.date_field :start_date, class: 'form-control' %> +
+ +
+ <%= form.label :end_date, 'End Date', class: 'form-label' %> + <%= form.date_field :end_date, class: 'form-control' %> +
+ +
+ <%= form.submit 'Submit', class: 'btn btn-dark' %> +
+<% end %> diff --git a/app/views/bank_accounts/create.html.erb b/app/views/bank_accounts/create.html.erb new file mode 100644 index 0000000..e1c2dd2 --- /dev/null +++ b/app/views/bank_accounts/create.html.erb @@ -0,0 +1,2 @@ +

BankAccounts#create

+

Find me in app/views/bank_accounts/create.html.erb

diff --git a/app/views/bank_accounts/edit.html.erb b/app/views/bank_accounts/edit.html.erb new file mode 100644 index 0000000..55d49a9 --- /dev/null +++ b/app/views/bank_accounts/edit.html.erb @@ -0,0 +1,13 @@ +
+
+
+

Edit Bank Account

+ + <%= render 'form', owner: @owner, bank_account: @bank_account %> + +
+ <%= link_to 'Back to Participant', polymorphic_path(@owner), class: "btn btn-secondary" %> +
+
+
+
diff --git a/app/views/bank_accounts/index.html.erb b/app/views/bank_accounts/index.html.erb new file mode 100644 index 0000000..97e9cfb --- /dev/null +++ b/app/views/bank_accounts/index.html.erb @@ -0,0 +1,44 @@ +
+

Bank Information

+ + + + + + + + + + + + + + <% @bank_accounts.each do |account| %> + + + + + + + + + + <% end %> + +
Institution NameTypeRouting NumberAccount NumberStart DateEnd DateActions
<%= account.institution_name %><%= account.account_type.capitalize %><%= account.routing_number %><%= hide_account_number(account.account_number) %><%= account.start_date.strftime('%B %d, %Y') if account.start_date %><%= account.end_date.strftime('%B %d, %Y') if account.end_date %> + <%= link_to edit_polymorphic_path([@owner, account]), class: 'btn btn-info btn-sm' do %> + + <% end %> + <%= link_to polymorphic_path([@owner, account]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-sm' do %> + + <% end %> +
+ + <%= link_to 'Add New Bank Account', new_polymorphic_path([@owner, BankAccount.new]), class: 'btn btn-dark' %> +
+ +
+ <%= link_to 'Back', polymorphic_path(@owner), class: 'btn btn-secondary mt-3' %> +
+ + diff --git a/app/views/bank_accounts/new.html.erb b/app/views/bank_accounts/new.html.erb new file mode 100644 index 0000000..3d33728 --- /dev/null +++ b/app/views/bank_accounts/new.html.erb @@ -0,0 +1,13 @@ +
+
+
+

New Bank Account

+ + <%= render 'form', owner: @owner, bank_account: @bank_account %> + +
+ <%= link_to 'Back to Participant', polymorphic_path(@owner), class: "btn btn-secondary" %> +
+
+
+
diff --git a/app/views/bank_accounts/update.html.erb b/app/views/bank_accounts/update.html.erb new file mode 100644 index 0000000..567bc83 --- /dev/null +++ b/app/views/bank_accounts/update.html.erb @@ -0,0 +1,2 @@ +

BankAccounts#update

+

Find me in app/views/bank_accounts/update.html.erb

diff --git a/app/views/participants/show.html.erb b/app/views/participants/show.html.erb index 20dece5..9788caa 100644 --- a/app/views/participants/show.html.erb +++ b/app/views/participants/show.html.erb @@ -1,3 +1,15 @@ + + + +
diff --git a/config/routes.rb b/config/routes.rb index 3335097..c118e59 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,10 +10,10 @@ Rails.application.routes.draw do end resources :participants do + resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] collection do get 'search' # Define search on the collection, not on a member end - member do post 'link_worker' # Other member routes post 'link_vendor' #Add this line to link a vendor @@ -22,6 +22,7 @@ Rails.application.routes.draw do resources :workers do + resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] # Added :index action member do post 'link_participant' post 'link_employer' @@ -31,10 +32,12 @@ Rails.application.routes.draw do resources :vendors do + resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] # Added :index action collection do get 'search' end end + resources :employments, only: [:edit, :update, :destroy] diff --git a/db/migrate/20240212212110_create_bank_accounts.rb b/db/migrate/20240212212110_create_bank_accounts.rb new file mode 100644 index 0000000..bca8793 --- /dev/null +++ b/db/migrate/20240212212110_create_bank_accounts.rb @@ -0,0 +1,15 @@ +class CreateBankAccounts < ActiveRecord::Migration[7.1] + def change + create_table :bank_accounts do |t| + t.string :institution_name + t.string :account_type + t.string :routing_number + t.string :account_number + t.date :start_date + t.date :end_date + t.references :owner, polymorphic: true, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 15459d3..1a8e209 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_06_215805) do +ActiveRecord::Schema[7.1].define(version: 2024_02_12_212110) do + create_table "bank_accounts", force: :cascade do |t| + t.string "institution_name" + t.string "account_type" + t.string "routing_number" + t.string "account_number" + t.date "start_date" + t.date "end_date" + t.string "owner_type", null: false + t.integer "owner_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["owner_type", "owner_id"], name: "index_bank_accounts_on_owner" + end + create_table "employer_records", force: :cascade do |t| t.integer "participant_id", null: false t.integer "employer_id", null: false diff --git a/test/controllers/bank_accounts_controller_test.rb b/test/controllers/bank_accounts_controller_test.rb new file mode 100644 index 0000000..678b663 --- /dev/null +++ b/test/controllers/bank_accounts_controller_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class BankAccountsControllerTest < ActionDispatch::IntegrationTest + test "should get new" do + get bank_accounts_new_url + assert_response :success + end + + test "should get create" do + get bank_accounts_create_url + assert_response :success + end + + test "should get edit" do + get bank_accounts_edit_url + assert_response :success + end + + test "should get update" do + get bank_accounts_update_url + assert_response :success + end +end diff --git a/test/fixtures/bank_accounts.yml b/test/fixtures/bank_accounts.yml new file mode 100644 index 0000000..7b147ee --- /dev/null +++ b/test/fixtures/bank_accounts.yml @@ -0,0 +1,21 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + institution_name: MyString + account_type: MyString + routing_number: MyString + account_number: MyString + start_date: 2024-02-12 + end_date: 2024-02-12 + owner: one + owner_type: Owner + +two: + institution_name: MyString + account_type: MyString + routing_number: MyString + account_number: MyString + start_date: 2024-02-12 + end_date: 2024-02-12 + owner: two + owner_type: Owner diff --git a/test/models/bank_account_test.rb b/test/models/bank_account_test.rb new file mode 100644 index 0000000..eba23cd --- /dev/null +++ b/test/models/bank_account_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class BankAccountTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end