From 8832ba071bc741a2ba80ba3928b4906f50835df0 Mon Sep 17 00:00:00 2001 From: Bilal Date: Wed, 7 Oct 2020 22:47:19 +0300 Subject: [PATCH] add homie to the gang when homie is created --- app/controllers/gangs_controller.rb | 9 ++++++++- app/controllers/homies_controller.rb | 3 ++- app/models/gang.rb | 2 +- app/models/homie.rb | 2 ++ config/routes.rb | 2 +- db/migrate/20201007182916_add_name_to_gangs.rb | 5 +++++ .../20201007183139_add_gang_relation_to_homies.rb | 5 +++++ ...3411_add_initial_gang_and_connect_to_homies.rb | 15 +++++++++++++++ db/schema.rb | 5 ++++- 9 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20201007182916_add_name_to_gangs.rb create mode 100644 db/migrate/20201007183139_add_gang_relation_to_homies.rb create mode 100644 db/migrate/20201007213411_add_initial_gang_and_connect_to_homies.rb diff --git a/app/controllers/gangs_controller.rb b/app/controllers/gangs_controller.rb index 958b243..82204a4 100644 --- a/app/controllers/gangs_controller.rb +++ b/app/controllers/gangs_controller.rb @@ -1,4 +1,10 @@ class GangsController < ApplicationController + def index + json_response Gang.all + rescue StandardError + error_response :bad_request + end + def show if Gang.count.zero? Gang.create.save @@ -19,7 +25,8 @@ class GangsController < ApplicationController private def gang_params - params.require(:gang).permit :chip_name, + params.require(:gang).permit :name, + :chip_name, :chip_code, :chip_symbol, :chip_scale, diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 4d2da8a..66ad232 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -46,7 +46,8 @@ class HomiesController < ApplicationController params.require(:homie).permit( :name, :importance, - :about + :about, + :gang_id ) end end diff --git a/app/models/gang.rb b/app/models/gang.rb index e2d3d62..00db4a6 100644 --- a/app/models/gang.rb +++ b/app/models/gang.rb @@ -1,3 +1,3 @@ class Gang < ApplicationRecord - + has_many :homies end \ No newline at end of file diff --git a/app/models/homie.rb b/app/models/homie.rb index 65e5271..c4e8e1b 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -2,6 +2,8 @@ class Homie < ApplicationRecord has_many :money_moves has_many :work + belongs_to :gang + def self.info(importance) cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount) diff --git a/config/routes.rb b/config/routes.rb index fa4ec00..ea639b5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do constraints format: :json do scope :api do - resources :gangs, only: [:show, :update] + resources :gangs, only: [:index, :show, :update] resources :money_moves resources :work resources :chips, only: %i[index create destroy] diff --git a/db/migrate/20201007182916_add_name_to_gangs.rb b/db/migrate/20201007182916_add_name_to_gangs.rb new file mode 100644 index 0000000..26c258e --- /dev/null +++ b/db/migrate/20201007182916_add_name_to_gangs.rb @@ -0,0 +1,5 @@ +class AddNameToGangs < ActiveRecord::Migration[5.2] + def change + add_column :gangs, :name, :text + end +end \ No newline at end of file diff --git a/db/migrate/20201007183139_add_gang_relation_to_homies.rb b/db/migrate/20201007183139_add_gang_relation_to_homies.rb new file mode 100644 index 0000000..b1808a4 --- /dev/null +++ b/db/migrate/20201007183139_add_gang_relation_to_homies.rb @@ -0,0 +1,5 @@ +class AddGangRelationToHomies < ActiveRecord::Migration[5.2] + def change + add_reference :homies, :gang + end +end \ No newline at end of file diff --git a/db/migrate/20201007213411_add_initial_gang_and_connect_to_homies.rb b/db/migrate/20201007213411_add_initial_gang_and_connect_to_homies.rb new file mode 100644 index 0000000..5ca4667 --- /dev/null +++ b/db/migrate/20201007213411_add_initial_gang_and_connect_to_homies.rb @@ -0,0 +1,15 @@ +class AddInitialGangAndConnectToHomies < ActiveRecord::Migration[5.2] + def up + # execute "INSERT INTO gangs SELECT 'name' WHERE NOT EXISTS (SELECT * FROM gangs)" + if Gang.count.zero? + Gang.create.save + end + gang = Gang.first + + Homie.all.update(gang: gang) + end + + def down + Homie.all.update(gang: nil) + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 28977bc..dc50711 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_180902) do +ActiveRecord::Schema.define(version: 2020_10_07_213411) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_180902) do t.integer "chip_scale" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.text "name" end create_table "homies", force: :cascade do |t| @@ -51,6 +52,8 @@ ActiveRecord::Schema.define(version: 2020_10_07_180902) do t.text "about" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "gang_id" + t.index ["gang_id"], name: "index_homies_on_gang_id" end create_table "money_moves", force: :cascade do |t|