Skip to content

Commit

Permalink
add pdf exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Apr 17, 2024
1 parent fed27ab commit 82c30cd
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/models/decidim/anonymous_codes/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def self.for(organization)
end

def expired?
expires_at.present? && expires_at < Time.current
expires? && expires_at < Time.current
end

def expires?
expires_at.present?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<tr>
<td><%= token[:token] %></td>
<td><a href="<%= token[:resource_url] %>"><%= token[:resource_url] %></a></td>
<td><%= t("booleans.#{token[:available]}") %></td>
<td><%= t("booleans.#{token[:used]}") %></td>
<td><%= token[:usage_count] %></td>
</tr>
25 changes: 25 additions & 0 deletions app/views/decidim/anonymous_codes/admin/export/tokens_pdf.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="tokens">
<div class="header">
<h1><%= t(".tokens_for", group: translated_attribute(code_group.title)) %></h1>
<% if collection.count > 1 %>
<h2><%= t(".totals", total: collection.count) %></h2>
<h2><%= t(".expires", expires: t("booleans.#{code_group.expires?}")) %></h2>
<%= content_tag "h3", t(".expires_at", expires_at: l(code_group.expires_at, format: :decidim_short)) if code_group.expires? %>
<% end %>
</div>

<table>
<thead>
<tr>
<th class='token'><%= t("token", scope: "decidim.anonymous_codes.tokens_serializer") %></th>
<th><%= t("resource_url", scope: "decidim.anonymous_codes.tokens_serializer") %></th>
<th><%= t("available", scope: "decidim.anonymous_codes.tokens_serializer") %></th>
<th><%= t("used", scope: "decidim.anonymous_codes.tokens_serializer") %></th>
<th><%= t("usage_count", scope: "decidim.anonymous_codes.tokens_serializer") %></th>
</tr>
</thead>
<tbody>
<%= render partial: "decidim/anonymous_codes/admin/export/token_tr", collection: collection, locals: { code_group: code_group }, as: :token %>
</tbody>
</table>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="<%= I18n.locale %>" class="no-js">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width">
<title><%= @title %></title>
<style type="text/css">
.tokens .header h1 {
margin: 0;
padding: 25px;
background: rgb(59, 69, 87);
color: white;
border-radius: 4px 4px 0 0;
}
.tokens table th,tokens table td {
text-align: left;
}
.tokens table a {
font-size: 0.9em;
}
.tokens table .token {
font-weight: bold;
}
</style>
</head>
<body>
<%= yield %>
</body>
</html>
16 changes: 16 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ en:
max_uses_exceeded: The code has already been used the maximum number of
times.
decidim:
admin:
exports:
formats:
AnonymousTokensPDF: PDF
anonymous_codes:
admin:
actions:
Expand Down Expand Up @@ -51,6 +55,12 @@ en:
back: Back to groups
new_codes_button: Generate new codes
title: Codes for group %{group}
export:
tokens_pdf:
expires: Expires? %{expires}
expires_at: 'Expires at: %{expires_at}'
tokens_for: Tokens for "%{group}"
totals: "%{total} total tokens"
exports:
all_tokens: All tokens
menu_title: Access Codes
Expand All @@ -75,4 +85,10 @@ en:
label: Code
submit: Continue
title: Form restricted
tokens_serializer:
available: Available?
resource_url: Resource URL
token: Code
usage_count: Num. of uses
used: Used?
used_code: The introduced code has already been used. Please try again.
7 changes: 6 additions & 1 deletion lib/decidim/anonymous_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module AnonymousCodes
end

config_accessor :export_formats do
ENV.fetch("ANONYMOUS_CODES_EXPORT_FORMATS", "CSV JSON Excel").split
ENV.fetch("ANONYMOUS_CODES_EXPORT_FORMATS", "CSV JSON Excel AnonymousTokensPDF").split
end

def self.token_generator(length = nil)
Expand All @@ -31,4 +31,9 @@ def self.token_generator(length = nil)
end
end
end

module Exporters
autoload :AnonymousTokensPDF, "decidim/exporters/anonymous_tokens_pdf"
autoload :AnonymousTokensPDFControllerHelper, "decidim/exporters/anonymous_tokens_pdf_controller_helper"
end
end
31 changes: 31 additions & 0 deletions lib/decidim/exporters/anonymous_tokens_pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "wicked_pdf"

module Decidim
module Exporters
# Inherits from abstract PDF exporter. This class is used to set
# the parameters used to create a PDF when exporting Survey Answers.
#
class AnonymousTokensPDF < PDF
def controller
@controller ||= AnonymousTokensPDFControllerHelper.new
end

def template
"decidim/anonymous_codes/admin/export/tokens_pdf"
end

def layout
"decidim/anonymous_codes/admin/export/pdf"
end

def locals
{
code_group: collection&.first&.group,
collection: collection.map { |token| Decidim::AnonymousCodes::TokenSerializer.new(token).serialize }
}
end
end
end
end
12 changes: 12 additions & 0 deletions lib/decidim/exporters/anonymous_tokens_pdf_controller_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Decidim
module Exporters
# rubocop: disable Rails/ApplicationController
# A dummy controller to render views while exporting questionnaires
class AnonymousTokensPDFControllerHelper < ActionController::Base
# rubocop: enable Rails/ApplicationController
helper Decidim::TranslationsHelper
end
end
end

0 comments on commit 82c30cd

Please sign in to comment.