Skip to content

Commit 617693d

Browse files
authored
Merge pull request #602 from coopdevs/develop
v3.9.0
2 parents 93dd021 + 92c8cf9 commit 617693d

28 files changed

+197
-66
lines changed

app/controllers/reports_controller.rb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@ def user_list
88
includes(:user).
99
order("members.member_uid")
1010

11-
respond_to do |format|
12-
format.html
13-
format.csv do
14-
report = Report::Csv::Member.new(current_organization, @members)
15-
send_data report.run, filename: report.name, type: report.mime_type
16-
end
17-
format.pdf do
18-
report = Report::Pdf::Member.new(current_organization, @members)
19-
send_data report.run, filename: report.name, type: report.mime_type
20-
end
21-
end
11+
report_responder('Member', current_organization, @members)
2212
end
2313

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

25+
report_responder('Post', current_organization, @posts, @post_type)
26+
end
27+
28+
def transfer_list
29+
@transfers = current_organization.all_transfers_with_accounts
30+
31+
report_responder('Transfer', current_organization, @transfers)
32+
end
33+
34+
private
35+
36+
def report_responder(report_class, *args)
3537
respond_to do |format|
3638
format.html
37-
format.csv do
38-
report = Report::Csv::Post.new(current_organization, @posts, @post_type)
39-
send_data report.run, filename: report.name, type: report.mime_type
40-
end
41-
format.pdf do
42-
report = Report::Pdf::Post.new(current_organization, @posts, @post_type)
43-
send_data report.run, filename: report.name, type: report.mime_type
44-
end
39+
format.csv { download_report("Report::Csv::#{report_class}", *args) }
40+
format.pdf { download_report("Report::Pdf::#{report_class}", *args) }
4541
end
4642
end
43+
44+
def download_report(report_class, *args)
45+
report = report_class.constantize.new(*args)
46+
send_data report.run, filename: report.name, type: report.mime_type
47+
end
4748
end

app/controllers/statistics_controller.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ def type_swaps
8383
end
8484

8585
def all_transfers
86-
@transfers = current_organization.all_transfers.
87-
includes(movements: {account: :accountable}).
88-
order("transfers.created_at DESC").
89-
distinct.
86+
@transfers = current_organization.
87+
all_transfers_with_accounts.
9088
page(params[:page]).
9189
per(20)
9290
end

app/decorators/member_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def avatar_img(size=32)
2828
end
2929

3030
def account_balance
31-
view.seconds_to_hm(object.account.try(:balance) || 0)
31+
view.seconds_to_hm(object.account.try(:balance))
3232
end
3333

3434
def toggle_manager_member_path

app/decorators/member_report_decorator.rb renamed to app/decorators/report/member_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class MemberReportDecorator
1+
class Report::MemberDecorator
22
def initialize(org, collection)
33
@org = org
44
@collection = collection

app/decorators/post_report_decorator.rb renamed to app/decorators/report/post_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class PostReportDecorator
1+
class Report::PostDecorator
22
def initialize(org, collection, type)
33
@org = org
44
@collection = collection
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Report::TransferDecorator
2+
include Rails.application.routes.url_helpers
3+
include ActionView::Helpers
4+
include ApplicationHelper, TransfersHelper
5+
6+
def initialize(org, collection)
7+
@org = org
8+
@collection = collection
9+
end
10+
11+
def name(extension)
12+
"#{@org.name}_"\
13+
"#{Transfer.model_name.human(count: :many)}_"\
14+
"#{Date.today}."\
15+
"#{extension}"
16+
end
17+
18+
def headers
19+
[
20+
I18n.t('statistics.all_transfers.date'),
21+
I18n.t('statistics.all_transfers.from'),
22+
I18n.t('statistics.all_transfers.to'),
23+
I18n.t('statistics.all_transfers.post'),
24+
I18n.t('statistics.all_transfers.quantity')
25+
]
26+
end
27+
28+
def rows
29+
@collection.map do |transfer|
30+
[
31+
transfer.created_at.to_s,
32+
accounts_from_movements(transfer),
33+
transfer.post.to_s,
34+
seconds_to_hm(transfer.movements.first.amount.abs, 0)
35+
].flatten
36+
end
37+
end
38+
end

app/helpers/application_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module ApplicationHelper
2-
32
TEXT_SUCCESS = 'text-success'.freeze
43
TEXT_DANGER = 'text-danger'.freeze
54

@@ -24,14 +23,15 @@ def mdash
2423
raw "—"
2524
end
2625

27-
def seconds_to_hm(seconds)
26+
def seconds_to_hm(seconds, default = mdash)
2827
sign = seconds <=> 0
28+
2929
if sign.try :nonzero?
3030
minutes, _seconds = seconds.abs.divmod(60)
3131
hours, minutes = minutes.divmod(60)
32-
raw format("%s%d:%02d", ("-" if sign < 0), hours, minutes)
32+
format("%s%d:%02d", ("-" if sign < 0), hours, minutes)
3333
else
34-
mdash
34+
default
3535
end
3636
end
3737

app/helpers/transfers_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,20 @@ def accountable_path(accountable)
66
user_path(accountable.user)
77
end
88
end
9+
10+
def accounts_from_movements(transfer, with_links: false)
11+
transfer.movements.sort_by(&:amount).map do |movement|
12+
account = movement.account
13+
14+
if account.accountable.blank?
15+
I18n.t('.deleted_user')
16+
elsif account.accountable_type == 'Organization'
17+
link_to_if(with_links, account, organization_path(account.accountable))
18+
elsif account.accountable.active
19+
link_to_if(with_links, account.accountable.display_name_with_uid, user_path(account.accountable.user))
20+
else
21+
I18n.t('.inactive_user')
22+
end
23+
end
24+
end
925
end

app/models/organization.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class Organization < ApplicationRecord
1616
before_validation :ensure_url
1717
after_create :create_account
1818

19+
def all_transfers_with_accounts
20+
all_transfers.
21+
includes(movements: { account: :accountable }).
22+
order("transfers.created_at DESC").
23+
distinct
24+
end
25+
1926
def to_s
2027
"#{name}"
2128
end

app/services/report/csv/member.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Report
22
module Csv
33
class Member < Base
44
def initialize(org, collection)
5-
self.decorator = MemberReportDecorator.new(org, collection)
5+
self.decorator = Report::MemberDecorator.new(org, collection)
66
end
77
end
88
end

0 commit comments

Comments
 (0)