Pack up functionality done
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
class GangsController < ApplicationController
|
||||
include ActionController::MimeResponds
|
||||
before_action :set_gang, only: [:update, :backup]
|
||||
|
||||
def index
|
||||
json_response Gang.all
|
||||
rescue StandardError
|
||||
@@ -18,19 +21,40 @@ class GangsController < ApplicationController
|
||||
end
|
||||
|
||||
def update
|
||||
if params[:id]
|
||||
gang = Gang.find(params[:id])
|
||||
gang.update!(gang_params)
|
||||
@gang.update!(gang_params)
|
||||
json_response onboarded: true
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
rescue StandardError
|
||||
rescue StandardError => err
|
||||
Rails.logger.error(err)
|
||||
error_response :bad_request
|
||||
end
|
||||
|
||||
def backup
|
||||
csv_string = CSV.generate do |csv|
|
||||
csv << [:homie_name, :money_amount, :work_amount, :description, :date, :deleted]
|
||||
@gang.money_moves.each do |money_move|
|
||||
csv << money_move.to_csv_row
|
||||
end
|
||||
|
||||
@gang.works.each do |work|
|
||||
csv << work.to_csv_row
|
||||
end
|
||||
end
|
||||
send_data csv_string
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_gang
|
||||
if params[:gang_id] || params[:id]
|
||||
@gang = Gang.find(params[:id] || params[:gang_id])
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
rescue StandardError => err
|
||||
Rails.logger.error(err)
|
||||
error_response :bad_request
|
||||
end
|
||||
|
||||
def gang_params
|
||||
params.require(:gang).permit :name,
|
||||
:about,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
class Gang < ApplicationRecord
|
||||
has_many :homies
|
||||
end
|
||||
has_many :homies, class_name: "Homie"
|
||||
has_many :money_moves, through: :homies
|
||||
has_many :works, through: :homies
|
||||
end
|
||||
|
||||
@@ -25,6 +25,17 @@ class MoneyMove < ApplicationRecord
|
||||
settle_record.save
|
||||
end
|
||||
|
||||
def to_csv_row
|
||||
[homie.name,
|
||||
amount,
|
||||
0,
|
||||
description,
|
||||
created_at.strftime("%Y-%m-%d"),
|
||||
deleted_at.present?.to_s].map do |element|
|
||||
element.to_s
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
private
|
||||
|
||||
@@ -65,5 +76,6 @@ class MoneyMove < ApplicationRecord
|
||||
description: new_desc
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
class Work < ApplicationRecord
|
||||
belongs_to :homie
|
||||
end
|
||||
|
||||
def to_csv_row
|
||||
[homie.name,
|
||||
0,
|
||||
amount,
|
||||
description,
|
||||
created_at.strftime("%Y-%m-%d"),
|
||||
false].map do |element|
|
||||
element.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user