Skip to content

Instantly share code, notes, and snippets.

@asiniy
Created October 22, 2017 16:08
Show Gist options
  • Save asiniy/420bd2075f0879a9b3a62788ce15313c to your computer and use it in GitHub Desktop.
Save asiniy/420bd2075f0879a9b3a62788ce15313c to your computer and use it in GitHub Desktop.
Rails sources
class DownloadController < ActionController::API
def bill
user_id = DownloadToken.verify(params[:token])
return head :unprocessable_entity if user_id.nil?
user = User.find_by(id: user_id)
return head :not_found if user.nil?
member = Member.find_by(id: params[:id])
return head :not_found if member.nil?
policy = Member::Policy.new(user, member)
return head :forbidden unless policy.bill?
bill = PDF::Bill.generate(member.id, header: true)
filename_prefix = user.id == member.user_id ? 'Balance' : "#{member.user.first_name} #{member.user.last_name}".parameterize
filename = "#{filename_prefix}.#{Time.zone.now.strftime('%F')}.pdf"
send_data bill.render, filename: filename, type: 'application/pdf', disposition: 'inline'
end
def invoices
user_id = DownloadToken.verify(params[:token])
return head :unprocessable_entity if user_id.nil?
user = User.find_by(id: user_id)
return head :not_found if user.nil?
organization = Organization.find_by(id: params[:id])
return head :not_found if organization.nil?
policy = Organization::Policy.new(user, organization)
return head :forbidden unless policy.invoices?
members = organization.members.select { |m| m.balance > 0 }
due_on = params[:due_date]&.match(/^\d{4}-\d{2}-\d{2}$/) ? Date.parse(params[:due_date]) : nil
pdfs = members.map { |member| PDF::Bill.generate(member.id, printable: true, parent_co: true, default_due_on: due_on).render }
megaPDF = PDF::Merger.(pdfs)
filename_prefix = "#{organization.name} #{organization.secondary}.pdf".downcase.gsub(' ', '-')
filename = "#{filename_prefix}.#{Time.zone.now.strftime('%F')}.pdf"
send_data megaPDF, filename: filename, type: 'application/pdf', disposition: 'inline'
end
def csv
send_data JSON.parse(params[:csv]),
filename: params[:filename],
disposition: 'attachment'
end
def transactions
user_id = DownloadToken.verify(params[:token])
return head :unprocessable_entity if user_id.nil?
user = User.find_by(id: user_id)
return head :not_found if user.nil?
op = Transaction::Operation::Search.({ 'current_user' => user }, params.to_unsafe_hash)
return head :unprocessable_entity if op['validation_errors'] != {}
return head :forbidden if op['context_errors'] != {}
return head :unauthorized unless op.authorization.success?
if op['meta'][:exceeds_maximum_amount_of_results]
send_data(
transactions_html_response(Transaction::Operation::Search::MAXIMUM_AMOUNT_OF_RESULTS),
type: 'text/html; charset=utf-8; header=present',
filename: 'index.html',
disposition: 'inline'
)
else
csv = []
csv << ['Created at', 'Member', 'Description', 'Amount']
op['collection'].each do |t|
created_at = t.created_at.in_time_zone('America/New_York').strftime('%B %-d, %Y %-l:%M%p').gsub(/[AP]M/, &:downcase)
amount = t.is_a?(Payment) ? -t.amount : t.amount
csv << ["\"#{created_at}\"", "\"#{t.member.user.first_name} #{t.member.user.last_name}\"", "\"#{t.description}\"", '%0.2f' % amount]
end
send_data(
csv.map{ |s| s.join(",") }.join("\n"),
type: 'text/csv; charset=utf-8; header=present',
filename: 'transactions.csv',
disposition: 'attachment'
)
end
end
private
def transactions_html_response(records_count)
<<-MSG.squish
<html>
<head>
<title>You're requesting too much transactions</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<style type="text/css">
body {
background: #f4f4f4;
}
div {
margin: 50px auto;
text-align: center;
font-size: 20px;
line-height: 1.5;
color: #3a5266;
}
a.back {
padding: 10px 20px;
background: #fff;
border: 1px solid #ddd;
font-size: 16px;
color: #333;
border-radius: 5px;
margin-top: 20px;
text-decoration: none;
}
</style>
</head>
<body>
<div>
Due to the amount of data you're downloading (over #{records_count} records),<br />
please contact <a href="mailto:[email protected]">[email protected]</a> to export your data
<br /><br />
<a href="#" onclick="window.history.back()" class="back">Go back</a>
</div>
</body>
</html>
MSG
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment