From 722fc71f170c450226813d2d470b75c491113369 Mon Sep 17 00:00:00 2001 From: Bilal Date: Fri, 9 Oct 2020 16:12:44 +0300 Subject: [PATCH] implement soft delete for money moves --- app/controllers/money_moves_controller.rb | 8 ++++++++ app/models/homie.rb | 2 +- config/routes.rb | 2 +- .../20201009114126_add_deleted_at_to_money_moves.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20201009114126_add_deleted_at_to_money_moves.rb diff --git a/app/controllers/money_moves_controller.rb b/app/controllers/money_moves_controller.rb index 2d7a83d..31a8d0e 100644 --- a/app/controllers/money_moves_controller.rb +++ b/app/controllers/money_moves_controller.rb @@ -22,6 +22,14 @@ class MoneyMovesController < ApplicationController error_response(:bad_request) end + def destroy + money_move = MoneyMove.find(params[:id]) + money_move.update!(deleted_at: DateTime.now) + index + rescue StandardError + error_response :bad_request + end + def money_move_params params.require(:money_move).permit( :description, diff --git a/app/models/homie.rb b/app/models/homie.rb index 9ad4538..eb491cf 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -7,7 +7,7 @@ class Homie < ApplicationRecord def self.info(importance, gang) # TODO: This can be improved - cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) + cash_totals = Homie.all.joins(:money_moves).where(money_moves: { deleted_at: nil}).group(:id).order(:id).sum(:amount) work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount) Homie.where(['importance > ? and gang_id = ?', importance, gang]).map do |homie| diff --git a/config/routes.rb b/config/routes.rb index 06d8614..4a079e0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,10 +8,10 @@ Rails.application.routes.draw do end member do post 'settle' + resources :money_moves end end end - resources :money_moves resources :work resources :chips, only: %i[index create destroy] resources :chip_values, only: %i[create update destroy] diff --git a/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb b/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb new file mode 100644 index 0000000..993fc5f --- /dev/null +++ b/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb @@ -0,0 +1,5 @@ +class AddDeletedAtToMoneyMoves < ActiveRecord::Migration[5.2] + def change + add_column :money_moves, :deleted_at, :timestamp + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 80897d6..a38451d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_10_07_213411) do +ActiveRecord::Schema.define(version: 2020_10_09_114126) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -63,6 +63,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_213411) do t.integer "homie_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "deleted_at" end create_table "work", force: :cascade do |t|