Pack up functionality done

This commit is contained in:
Senad Uka
2020-10-21 15:11:20 +02:00
parent 25cfc6610c
commit 6f655dfe2b
11 changed files with 95 additions and 13 deletions

View File

@@ -43,3 +43,6 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "csv", "~> 3.1"

View File

@@ -49,6 +49,7 @@ GEM
byebug (11.0.1)
concurrent-ruby (1.1.5)
crass (1.0.4)
csv (3.1.7)
erubi (1.8.0)
ffi (1.11.1)
globalid (0.4.2)
@@ -134,6 +135,7 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
csv (~> 3.1)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 3.11)
@@ -146,4 +148,4 @@ RUBY VERSION
ruby 2.6.3p62
BUNDLED WITH
1.17.2
1.17.3

View File

@@ -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,

View File

@@ -1,3 +1,5 @@
class Gang < ApplicationRecord
has_many :homies
has_many :homies, class_name: "Homie"
has_many :money_moves, through: :homies
has_many :works, through: :homies
end

View File

@@ -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

View File

@@ -1,3 +1,14 @@
class Work < ApplicationRecord
belongs_to :homie
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

View File

@@ -5,6 +5,7 @@
"proxy": "http://localhost:3001",
"dependencies": {
"axios": "^0.19.0",
"js-file-download": "^0.4.12",
"materialize-css": "^1.0.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",

View File

@@ -5,6 +5,7 @@ import {errorToast} from "../common/errorHelpers";
import {withRouter} from 'react-router-dom';
import {GANGS} from "../RouteNames";
import M from 'materialize-css';
import fileDownload from 'js-file-download';
const GangDetails = (props) => {
const gang = props.gang;
@@ -13,6 +14,17 @@ const GangDetails = (props) => {
const disableSubmit = () => gangName.length === 0;
const getBackup = async () => {
try {
const response = await axios.get(`/api/gangs/${gang.id}/backup`);
const timestamp = Date.now();
const safeGangName = str.replace(/blue/g, "red");
fileDownload(response.data, `fil.csv`, 'text/csv')
} catch (e) {
console.log("EE ", e);
}
}
const updateGang = async () => {
const updatedGang = {
name: gangName
@@ -47,6 +59,15 @@ const GangDetails = (props) => {
Make it done
</Button>
</div>
<h1> Pack up </h1>
<div>
<p>
<Button onClick={getBackup}> Pack up </Button>
</p>
<p>
</p>
</div>
</div>
)
}

View File

@@ -5555,6 +5555,11 @@ jest@24.7.1:
import-local "^2.0.0"
jest-cli "^24.7.1"
js-file-download@^0.4.12:
version "0.4.12"
resolved "https://registry.yarnpkg.com/js-file-download/-/js-file-download-0.4.12.tgz#10c70ef362559a5b23cdbdc3bd6f399c3d91d821"
integrity sha512-rML+NkoD08p5Dllpjo0ffy4jRHeY6Zsapvr/W86N7E0yuzAO6qa5X9+xog6zQNlH102J7IXljNY2FtS6Lj3ucg==
js-levenshtein@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"

View File

@@ -11,6 +11,7 @@ Rails.application.routes.draw do
resources :money_moves
end
end
get 'backup'
end
resources :work
resources :chips, only: %i[index create destroy]