def create
uploaded_file = params[:batch_payouts_csv]
unless valid_file_type?(uploaded_file)
render json: {"error_message" => I18n.t("merchant.messages.errors.invalid_file_type")}, status: 400
return
end
uploaded_csv_file = Batch::CsvFile.new(uploaded_file.path)
unless uploaded_csv_file.valid_size?
render json: {"error_message" => I18n.t("merchant.messages.errors.exceeding_file_size")}, status: 400
return
end
filename_with_timestamp = "#{DateTime.now.strftime("%Q")}_#{uploaded_file.original_filename}"
merchant_employee = @current_user
currency = Currency.find(batch_payout_params[:currency_id])
batch_payout = BatchPayout.create(merchant_employee: merchant_employee, currency: currency)
unless request.xhr?
response = {filename: filename_with_timestamp, status: "success", location: merchants_batch_payouts_path(batch_payout)}
render text: response.to_json, status: 201 \
return
end
render json: {"filename" => filename_with_timestamp}, location: merchants_batch_payouts_path(batch_payout), status: 201
end
======================================================= I welcome VSRE emails. Learn more at http://vsre.info/ =======================================================
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class BatchPayoutController < ApplicationControllerdef createCreatingBatchPayout.new(self).call(@current_user, params)endprivatedef determined_invalid_file_type
render json: {"error_message" => I18n.t("merchant.messages.errors.invalid_file_type")}, status: 400
enddef determined_invalid_file_size
render json: {"error_message" => I18n.t("merchant.messages.errors.exceeding_file_size")}, status: 400
enddef batch_payout_created_successfully(batch_payout, file)if request.xhr?render json: {"filename" => file.filename}, location: merchants_batch_payouts_path(batch_payout), status: 201elseresponse = {filename: file.filename, status: "success", location: merchants_batch_payouts_path(batch_payout)}
render text: response.to_json, status: 201
endendendclass TimeStampedFile < SimpleDelegatordef filename"#{DateTime.now.strftime("%Q")}_#{original_filename}"endendclass UploadedFile < SimpleDelegatordef valid_type?# add logic hereendendclass CreatingBatchPayoutdef initialize(listener)@listener = listenerenddef call(merchant_employee, params)uploaded_file = TimeStampedFile.new(UploadedFile.new(params[:batch_payouts_csv]))@listener.determined_invalid_file_type and return unless uploaded_file.valid_type?uploaded_csv_file = Batch::CsvFile.new(uploaded_file.path)@listener.determined_invalid_file_size and return unless uploaded_csv_file.valid_size?currency = Currency.find(params[:batch_payout][:currency_id])batch_payout = BatchPayout.create!(merchant_employee: merchant_employee, currency: currency)@listener.batch_payout_created_successfully(batch_payout, uploaded_file)endend