Skip to content

Commit

Permalink
Merge pull request #594 from coopdevs/improve_reports
Browse files Browse the repository at this point in the history
Reports: more internal refactor and include new attributes
  • Loading branch information
sseerrggii authored Mar 9, 2021
2 parents 88d7262 + 16c4e44 commit 1e22e0f
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 64 deletions.
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ GEM
fabrication (2.21.1)
faker (2.16.0)
i18n (>= 1.6, < 2)
ffi (1.14.2)
ffi (1.15.0)
formtastic (4.0.0)
actionpack (>= 5.2.0)
formtastic_i18n (0.6.0)
Expand Down Expand Up @@ -199,16 +199,16 @@ GEM
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0212)
mime-types-data (3.2021.0225)
mimemagic (0.3.5)
mini_mime (1.0.2)
mini_portile2 (2.5.0)
minitest (5.14.3)
minitest (5.14.4)
net-scp (3.0.0)
net-ssh (>= 2.6.5, < 7.0.0)
net-ssh (6.1.0)
netrc (0.11.0)
nio4r (2.5.5)
nio4r (2.5.7)
nokogiri (1.11.1)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
Expand Down Expand Up @@ -279,7 +279,7 @@ GEM
ffi (~> 1.0)
rdiscount (2.2.0.2)
redis (4.2.5)
regexp_parser (2.0.3)
regexp_parser (2.1.1)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
Expand Down Expand Up @@ -307,7 +307,7 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
rubocop (1.10.0)
rubocop (1.11.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
Expand Down Expand Up @@ -390,7 +390,7 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webdrivers (4.5.0)
webdrivers (4.6.0)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$(document).on('click', 'a[data-popup]', function(event) {
event.preventDefault();

window.open($(this).attr('href'), 'popup', 'width=600,height=600');
window.open($(this).attr('href'), 'popup', 'width=800,height=600');
});

$(document).on('click', 'span.show-password', function(event) {
Expand Down
12 changes: 10 additions & 2 deletions app/decorators/member_report_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def headers
User.human_attribute_name(:username),
User.human_attribute_name(:email),
User.human_attribute_name(:phone),
User.human_attribute_name(:alt_phone)
User.human_attribute_name(:alt_phone),
User.human_attribute_name(:created_at),
User.human_attribute_name(:last_sign_in_at),
User.human_attribute_name(:locale),
Account.human_attribute_name(:balance)
]
end

Expand All @@ -28,7 +32,11 @@ def rows
member.user.username,
member.user.email_if_real,
member.user.phone,
member.user.alt_phone
member.user.alt_phone,
member.user.created_at.to_s,
member.user.last_sign_in_at.to_s,
member.user.locale,
member.account_balance.to_s
]
end
end
Expand Down
10 changes: 7 additions & 3 deletions app/decorators/post_report_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ def headers
[
"",
@type.model_name.human,
User.model_name.human
Post.human_attribute_name(:tag_list),
User.model_name.human,
Post.human_attribute_name(:created_at)
]
end

def rows
grouped_rows = []

@collection.each do |category, posts|
grouped_rows << ["", category.try(:name) || "-", ""]
grouped_rows << ["", category.try(:name) || "-", "", "", ""]

posts.each do |post|
grouped_rows << [
post.id,
post.title,
"#{post.user} (#{post.member_uid})"
post.tag_list.to_s,
"#{post.user} (#{post.member_uid})",
post.created_at.to_s
]
end
end
Expand Down
14 changes: 0 additions & 14 deletions app/services/report/csv.rb

This file was deleted.

14 changes: 12 additions & 2 deletions app/services/report/csv/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
require "csv"

module Report
module Csv
class Base
attr_accessor :decorator

def name
@decorator.name(:csv)
decorator.name(:csv)
end

def mime_type
Mime[:csv]
end

def run
Report::Csv.run(@decorator.headers, @decorator.rows)
::CSV.generate do |csv|
csv << decorator.headers

decorator.rows.each do |row|
csv << row
end
end
end
end
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)
@decorator = MemberReportDecorator.new(org, collection)
self.decorator = MemberReportDecorator.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)
@decorator = PostReportDecorator.new(org, collection, type)
self.decorator = PostReportDecorator.new(org, collection, type)
end
end
end
Expand Down
17 changes: 0 additions & 17 deletions app/services/report/pdf.rb

This file was deleted.

9 changes: 7 additions & 2 deletions app/services/report/pdf/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module Report
module Pdf
class Base
attr_accessor :decorator

def name
@decorator.name(:pdf)
decorator.name(:pdf)
end

def mime_type
Mime[:pdf]
end

def run
Report::Pdf.run(@decorator.headers, @decorator.rows)
pdf = Prawn::Document.new({ page_size: "A4", margin: 30 })
pdf.table [decorator.headers] + decorator.rows

pdf.render
end
end
end
Expand Down
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)
@decorator = MemberReportDecorator.new(org, collection)
self.decorator = MemberReportDecorator.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)
@decorator = PostReportDecorator.new(org, collection, type)
self.decorator = PostReportDecorator.new(org, collection, type)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/views/reports/post_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
<tr>
<th></th>
<th><%= @post_type.model_name.human %></th>
<th><%= Post.human_attribute_name :tag_list %></th>
<th><%= User.model_name.human %></th>
<th><%= Post.human_attribute_name :created_at %></th>
</tr>
</thead>
<tbody>
<% @posts.each do |cat, ps| %>
<tr class="info">
<td colspan="3"><h4><%= cat || '—' %></h4></td>
<td colspan="5"><h4><%= cat || '—' %></h4></td>
</tr>
<% ps.each do |post| %>
<tr>
<td><%= post.id %></td>
<td><strong><%= post.title %></strong></td>
<td><%= post.tag_list %></td>
<td><%= "#{post.user} (#{post.member_uid})" %></td>
<td><%= l(post.created_at, format: :long) %></td>
</tr>
<% end %>
<% end %>
Expand Down
20 changes: 14 additions & 6 deletions app/views/reports/user_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
<th><%= User.human_attribute_name :email %></th>
<th><%= User.human_attribute_name :phone %></th>
<th><%= User.human_attribute_name :alt_phone %></th>
<th><%= User.human_attribute_name :created_at %></th>
<th><%= User.human_attribute_name :last_sign_in_at %></th>
<th><%= User.human_attribute_name :locale %></th>
<th><%= Account.human_attribute_name :balance %></th>
</tr>
</thead>
<tbody>
<% @members.each do |u| %>
<% @members.each do |member| %>
<tr>
<th><%= u.member_uid %></th>
<td><%= u.user.username %></td>
<td><%= u.user.email_if_real %></td>
<td><%= u.user.phone %></td>
<td><%= u.user.alt_phone %></td>
<th><%= member.member_uid %></th>
<td><%= member.user.username %></td>
<td><%= member.user.email_if_real %></td>
<td><%= member.user.phone %></td>
<td><%= member.user.alt_phone %></td>
<td><%= l(member.user.created_at, format: :long) %></td>
<td><%= l(member.user.last_sign_in_at, format: :long) if member.user.last_sign_in_at.present? %></td>
<td><%= member.user.locale %></td>
<td><%= member.account_balance %></td>
</tr>
<% end %>
</tbody>
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 @@ -45,13 +45,31 @@
end

describe 'GET #post_list' do
let(:report_posts) { test_organization.posts.of_active_members.group_by(&:category) }

it 'do NOT show the inactive members' do
get :post_list, params: { type: 'offer' }

posts = assigns(:posts)[0][1]
expect(posts.size).to eq(active_organization_offers.size)
expect(posts.map(&:id)).to match_array(active_organization_offers.map(&:id))
end

it 'downloads a csv' do
get :post_list, params: { type: 'offer', format: 'csv' }

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

it 'downloads a pdf' do
get :post_list, params: { type: 'offer', format: 'pdf' }

report = Report::Pdf::Post.new(test_organization, report_posts, Offer)
expect(response.body).to eq(report.run)
expect(response.media_type).to eq("application/pdf")
end
end
end
end
12 changes: 10 additions & 2 deletions spec/decorators/member_report_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
User.human_attribute_name(:username),
User.human_attribute_name(:email),
User.human_attribute_name(:phone),
User.human_attribute_name(:alt_phone)
User.human_attribute_name(:alt_phone),
User.human_attribute_name(:created_at),
User.human_attribute_name(:last_sign_in_at),
User.human_attribute_name(:locale),
Account.human_attribute_name(:balance)
])
end

Expand All @@ -30,7 +34,11 @@
member.user.username,
member.user.email_if_real,
member.user.phone,
member.user.alt_phone
member.user.alt_phone,
member.user.created_at.to_s,
member.user.last_sign_in_at.to_s,
member.user.locale,
member.account_balance.to_s
]
])
end
Expand Down
8 changes: 5 additions & 3 deletions spec/decorators/post_report_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
expect(decorator.headers).to eq([
"",
Offer.model_name.human,
User.model_name.human
Post.human_attribute_name(:tag_list),
User.model_name.human,
Post.human_attribute_name(:created_at)
])
end

Expand All @@ -35,8 +37,8 @@
offer = org.offers.of_active_members.active.first

expect(decorator.rows).to eq([
["", category.name, ""],
[offer.id, offer.title, "#{offer.user} (#{offer.member_uid})"]
["", category.name, "", "", ""],
[offer.id, offer.title, offer.tag_list.to_s, "#{offer.user} (#{offer.member_uid})", offer.created_at.to_s]
])
end
end

0 comments on commit 1e22e0f

Please sign in to comment.