Skip to content

Commit

Permalink
Merge pull request #602 from coopdevs/develop
Browse files Browse the repository at this point in the history
v3.9.0
  • Loading branch information
sseerrggii authored Mar 15, 2021
2 parents 93dd021 + 92c8cf9 commit 617693d
Show file tree
Hide file tree
Showing 28 changed files with 197 additions and 66 deletions.
39 changes: 20 additions & 19 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ def user_list
includes(:user).
order("members.member_uid")

respond_to do |format|
format.html
format.csv do
report = Report::Csv::Member.new(current_organization, @members)
send_data report.run, filename: report.name, type: report.mime_type
end
format.pdf do
report = Report::Pdf::Member.new(current_organization, @members)
send_data report.run, filename: report.name, type: report.mime_type
end
end
report_responder('Member', current_organization, @members)
end

def post_list
Expand All @@ -32,16 +22,27 @@ def post_list
to_a.
sort_by { |category, _| category.try(:name).to_s }

report_responder('Post', current_organization, @posts, @post_type)
end

def transfer_list
@transfers = current_organization.all_transfers_with_accounts

report_responder('Transfer', current_organization, @transfers)
end

private

def report_responder(report_class, *args)
respond_to do |format|
format.html
format.csv do
report = Report::Csv::Post.new(current_organization, @posts, @post_type)
send_data report.run, filename: report.name, type: report.mime_type
end
format.pdf do
report = Report::Pdf::Post.new(current_organization, @posts, @post_type)
send_data report.run, filename: report.name, type: report.mime_type
end
format.csv { download_report("Report::Csv::#{report_class}", *args) }
format.pdf { download_report("Report::Pdf::#{report_class}", *args) }
end
end

def download_report(report_class, *args)
report = report_class.constantize.new(*args)
send_data report.run, filename: report.name, type: report.mime_type
end
end
6 changes: 2 additions & 4 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ def type_swaps
end

def all_transfers
@transfers = current_organization.all_transfers.
includes(movements: {account: :accountable}).
order("transfers.created_at DESC").
distinct.
@transfers = current_organization.
all_transfers_with_accounts.
page(params[:page]).
per(20)
end
Expand Down
2 changes: 1 addition & 1 deletion app/decorators/member_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def avatar_img(size=32)
end

def account_balance
view.seconds_to_hm(object.account.try(:balance) || 0)
view.seconds_to_hm(object.account.try(:balance))
end

def toggle_manager_member_path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MemberReportDecorator
class Report::MemberDecorator
def initialize(org, collection)
@org = org
@collection = collection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PostReportDecorator
class Report::PostDecorator
def initialize(org, collection, type)
@org = org
@collection = collection
Expand Down
38 changes: 38 additions & 0 deletions app/decorators/report/transfer_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Report::TransferDecorator
include Rails.application.routes.url_helpers
include ActionView::Helpers
include ApplicationHelper, TransfersHelper

def initialize(org, collection)
@org = org
@collection = collection
end

def name(extension)
"#{@org.name}_"\
"#{Transfer.model_name.human(count: :many)}_"\
"#{Date.today}."\
"#{extension}"
end

def headers
[
I18n.t('statistics.all_transfers.date'),
I18n.t('statistics.all_transfers.from'),
I18n.t('statistics.all_transfers.to'),
I18n.t('statistics.all_transfers.post'),
I18n.t('statistics.all_transfers.quantity')
]
end

def rows
@collection.map do |transfer|
[
transfer.created_at.to_s,
accounts_from_movements(transfer),
transfer.post.to_s,
seconds_to_hm(transfer.movements.first.amount.abs, 0)
].flatten
end
end
end
8 changes: 4 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module ApplicationHelper

TEXT_SUCCESS = 'text-success'.freeze
TEXT_DANGER = 'text-danger'.freeze

Expand All @@ -24,14 +23,15 @@ def mdash
raw "—"
end

def seconds_to_hm(seconds)
def seconds_to_hm(seconds, default = mdash)
sign = seconds <=> 0

if sign.try :nonzero?
minutes, _seconds = seconds.abs.divmod(60)
hours, minutes = minutes.divmod(60)
raw format("%s%d:%02d", ("-" if sign < 0), hours, minutes)
format("%s%d:%02d", ("-" if sign < 0), hours, minutes)
else
mdash
default
end
end

Expand Down
16 changes: 16 additions & 0 deletions app/helpers/transfers_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ def accountable_path(accountable)
user_path(accountable.user)
end
end

def accounts_from_movements(transfer, with_links: false)
transfer.movements.sort_by(&:amount).map do |movement|
account = movement.account

if account.accountable.blank?
I18n.t('.deleted_user')
elsif account.accountable_type == 'Organization'
link_to_if(with_links, account, organization_path(account.accountable))
elsif account.accountable.active
link_to_if(with_links, account.accountable.display_name_with_uid, user_path(account.accountable.user))
else
I18n.t('.inactive_user')
end
end
end
end
7 changes: 7 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class Organization < ApplicationRecord
before_validation :ensure_url
after_create :create_account

def all_transfers_with_accounts
all_transfers.
includes(movements: { account: :accountable }).
order("transfers.created_at DESC").
distinct
end

def to_s
"#{name}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/report/csv/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Report
module Csv
class Member < Base
def initialize(org, collection)
self.decorator = MemberReportDecorator.new(org, collection)
self.decorator = Report::MemberDecorator.new(org, collection)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/report/csv/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Report
module Csv
class Post < Base
def initialize(org, collection, type)
self.decorator = PostReportDecorator.new(org, collection, type)
self.decorator = Report::PostDecorator.new(org, collection, type)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/report/csv/transfer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Report
module Csv
class Transfer < Base
def initialize(org, collection)
self.decorator = Report::TransferDecorator.new(org, collection)
end
end
end
end
2 changes: 1 addition & 1 deletion app/services/report/pdf/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Report
module Pdf
class Member < Base
def initialize(org, collection)
self.decorator = MemberReportDecorator.new(org, collection)
self.decorator = Report::MemberDecorator.new(org, collection)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/report/pdf/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Report
module Pdf
class Post < Base
def initialize(org, collection, type)
self.decorator = PostReportDecorator.new(org, collection, type)
self.decorator = Report::PostDecorator.new(org, collection, type)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/report/pdf/transfer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Report
module Pdf
class Transfer < Base
def initialize(org, collection)
self.decorator = Report::TransferDecorator.new(org, collection)
end
end
end
end
12 changes: 6 additions & 6 deletions app/views/application/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@
<%= Inquiry.model_name.human(count: :many) %>
<% end %>
</li>
<li class="<%= "active" if current_page?(alpha_grouped_index_tags_path(post_type: "offer")) %>">
<%= link_to alpha_grouped_index_tags_path(post_type: "offer") do %>
<%= glyph :tags %>
<%= t ".tags" %>
<% end %>
</li>
<% if current_user.manages? current_organization %>
<li class="<%= "active" if current_page?(alpha_grouped_index_tags_path(post_type: "offer")) %>">
<%= link_to alpha_grouped_index_tags_path(post_type: "offer") do %>
<%= glyph :tags %>
<%= t ".tags" %>
<% end %>
</li>
<%= render 'application/menus/organization_reports_menu' %>
<%= render 'application/menus/organization_statistics_menu' %>
<%= render 'application/menus/organization_listings_menu' %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
<%= Inquiry.model_name.human(count: :many) %>
<% end %>
</li>
<li>
<%= link_to transfer_list_report_path, data: { popup: true } do %>
<%= glyph :list_alt %>
<%= Transfer.model_name.human(count: :many) %>
<% end %>
</li>
</ul>
</li>
2 changes: 1 addition & 1 deletion app/views/organizations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<strong>
<%= t 'global.balance' %>
</strong>
<%= seconds_to_hm(@organization.account.try(:balance) || '—') %>
<%= seconds_to_hm(@organization.account.try(:balance)) %>
</p>
</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions app/views/reports/transfer_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="row">
<table class="table table-condensed table-bordered table-striped">
<thead>
<tr>
<th><%= t('statistics.all_transfers.date') %></th>
<th><%= t('statistics.all_transfers.from') %></th>
<th><%= t('statistics.all_transfers.to') %></th>
<th><%= t('statistics.all_transfers.post') %></th>
<th><%= t('statistics.all_transfers.quantity') %></th>
</tr>
</thead>
<tbody>
<% @transfers.each do |transfer| %>
<tr>
<td><%= l(transfer.created_at, format: :long) %></td>
<% accounts_from_movements(transfer).each do |account| %>
<td><%= account %></td>
<% end %>
<td><%= transfer.post %></td>
<td><%= seconds_to_hm(transfer.movements.first.amount.abs) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
20 changes: 2 additions & 18 deletions app/views/statistics/all_transfers.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,8 @@
<td>
<%= l transfer.created_at, format: :long %>
</td>
<% transfer.movements.sort_by(&:amount).each do |mv| %>
<td>
<% mv.account.tap do |account| %>
<% if account.accountable.present? %>
<% if account.accountable_type == 'Organization' %>
<%= link_to account,
organization_path(account.accountable) %>
<% elsif account.accountable.active %>
<%= link_to account.accountable.display_name_with_uid,
user_path(account.accountable.user) %>
<% else %>
<%= t '.inactive_user' %>
<% end %>
<% else %>
<%= t '.deleted_user' %>
<% end %>
<% end %>
</td>
<% accounts_from_movements(transfer, with_links: true).each do |account| %>
<td><%= account %></td>
<% end %>
<td>
<%= link_to_if transfer.try(:post).try(:active?),
Expand Down
2 changes: 1 addition & 1 deletion app/views/statistics/type_swaps.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<tr>
<td><%= number_to_percentage offer.last * 100, precision: 2 %></td>
<td><%= "#{offer[0]} -> #{offer[1]}" %></td>
<td><%= seconds_to_hm offer[2] || '—' %></td>
<td><%= seconds_to_hm offer[2] %></td>
<td><%= offer[3] %></td>
</tr>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
<br/>
<strong class="lead <%= green_red(@member.account&.balance) %>">
<%= t(".balance") %>
<%= seconds_to_hm(@member.account.try(:balance) || mdash) %>
<%= seconds_to_hm(@member.account.try(:balance)) %>
</strong>
</p>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
get "user_list"
get "offer_list" => :post_list, type: "offer"
get "inquiry_list" => :post_list, type: "inquiry"
get "transfer_list"
end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/reports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,23 @@
expect(response.media_type).to eq("application/pdf")
end
end

describe 'GET #transfer_list' do
it 'downloads a csv' do
get :transfer_list, params: { format: 'csv' }

report = Report::Csv::Transfer.new(test_organization, test_organization.all_transfers)
expect(response.body).to eq(report.run)
expect(response.media_type).to eq("text/csv")
end

it 'downloads a pdf' do
get :transfer_list, params: { format: 'pdf' }

report = Report::Pdf::Transfer.new(test_organization, test_organization.all_transfers)
expect(response.body).to eq(report.run)
expect(response.media_type).to eq("application/pdf")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
RSpec.describe MemberReportDecorator do
RSpec.describe Report::MemberDecorator do
let (:member) { Fabricate(:member) }
let (:org) { member.organization }
let (:decorator) do
MemberReportDecorator.new(org, org.members)
Report::MemberDecorator.new(org, org.members)
end

it "#name" do
Expand Down
Loading

0 comments on commit 617693d

Please sign in to comment.