Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Certificates PDF with GEM PRAWN #43

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.bundle
.rvmrc
db/*.sqlite3
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ gem 'rdiscount'
gem 'yaml_db'
gem 'draper'

gem 'prawn'
gem "pdf-inspector", :group => [:test, :development]

group :development do
gem 'capistrano'
gem 'debugger'
Expand All @@ -21,6 +24,7 @@ end
gem 'pg', :group => :production
gem 'sqlite3', :group => [:test, :development]


group :test do
gem 'rspec-rails', '~> 2.4'
gem 'capybara'
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: http://rubygems.org/
specs:
Ascii85 (1.0.2)
actionmailer (3.1.0)
actionpack (= 3.1.0)
mail (~> 2.3.0)
Expand Down Expand Up @@ -30,6 +31,7 @@ GEM
activesupport (= 3.1.0)
activesupport (3.1.0)
multi_json (~> 1.0)
afm (0.2.0)
arel (2.2.1)
bcrypt-ruby (3.0.0)
builder (3.0.0)
Expand Down Expand Up @@ -72,6 +74,7 @@ GEM
thor (~> 0.14.6)
guard-rspec (0.4.3)
guard (>= 0.4.0)
hashery (2.1.0)
hashie (1.2.0)
heroku (2.35.0)
heroku-api (~> 0.3.7)
Expand Down Expand Up @@ -127,8 +130,19 @@ GEM
omniauth-oauth2 (1.1.1)
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
pdf-inspector (1.0.2)
pdf-reader (>= 0.9.0)
pdf-reader (1.3.2)
Ascii85 (~> 1.0.0)
afm (~> 0.2.0)
hashery (~> 2.0)
ruby-rc4
ttfunk
pg (0.14.0)
polyglot (0.3.2)
prawn (0.12.0)
pdf-reader (>= 0.9.0)
ttfunk (~> 1.0.2)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
Expand Down Expand Up @@ -176,6 +190,7 @@ GEM
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.6.0)
ruby-rc4 (0.1.5)
rubyzip (0.9.4)
selenium-webdriver (2.7.0)
childprocess (>= 0.2.1)
Expand All @@ -193,6 +208,7 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
ttfunk (1.0.3)
tzinfo (0.3.29)
xpath (0.1.4)
nokogiri (~> 1.3)
Expand All @@ -218,7 +234,9 @@ DEPENDENCIES
omniauth (~> 1.0)
omniauth-facebook
omniauth-github
pdf-inspector
pg
prawn
pry
rails
rdiscount
Expand Down
47 changes: 47 additions & 0 deletions app/controllers/certificates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CertificatesController < ApplicationController
respond_to :html
filter_resource_access

def index
@certificates = Certificate.joins(:event).where("user_id = ? and confirmed = ? and events.event_date <= ?", current_user.id, true, Date.current)
@breadcrumb = "Meus Certificados"
end

def create
@certificate = Certificate.where("user_id = ? and event_id = ? and go = ?", current_user.id, params[:certificate][:event_id], true)
unless @certificate.present?
@certificate = Certificate.create(params[:certificate].merge(:go => true))
redirect_to(event_path(@certificate.event))
else
flash[:error] = "Sorry."
redirect_to events_url
end
end

def update
@certificate = Certificate.find(params[:id])
if @certificate.update_attributes(:confirmed => true)
flash[:notice] = 'Certificate was successfully updated.'
respond_with @certificate.event
else
flash[:error] = "Sorry, certificate not found."
redirect_to events_url
end
end

def show
@certificate = Certificate.find(params[:id])
if @certificate
respond_to do |format|
format.pdf do
send_data @certificate.generate.render,
filename: "certificate_#{@certificate.event.to_param}_#{@certificate.user.identities.first.name}.pdf",
type: "application/pdf"
end
end
else
flash[:error] = "Sorry, certificate not found."
redirect_to root_url
end
end
end
19 changes: 19 additions & 0 deletions app/models/certificate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Certificate < ActiveRecord::Base

belongs_to :user
belongs_to :event
validates :user_id, :presence => true
validates :event_id, :presence => true
before_save :generate_token

scope :going, where(:go => true)
scope :confirmed, where(:confirmed => true)

include GeneratePdf

private

def generate_token
self.token = Digest::MD5.hexdigest("#{self.user.identities.first.name}-#{self.user.email}-#{self.event.name}-#{self.event.event_date}")
end
end
14 changes: 13 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ class Event < ActiveRecord::Base
before_save :slugify
validates :name, presence: true, uniqueness: true
has_many :lectures, dependent: :destroy, order: 'relative_votes desc, negative_vote asc'
has_many :certificates, :dependent => :destroy

def to_param
slug
end

def user_go?(user_id)
certificates.going.where(:user_id => user_id).any?
end

def going
certificates.going
end

def confirmed
certificates.confirmed
end

private
def slugify
self.slug = name.parameterize.to_s
end

end

30 changes: 30 additions & 0 deletions app/models/generate_pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: UTF-8
module GeneratePdf
PDF_OPTIONS = {
:page_size => "A4",
:page_layout => :landscape,
:background => "public/images/background.png",
:margin => [40, 75]
}

def generate
attested = self
Prawn::Document.new(PDF_OPTIONS) do
move_down 50
text "Certificado de Participação", :size => 36, :style => :bold, :align => :center
move_down 30
text "Certificamos que", :size => 20, :align => :center
move_down 15
text "<b>#{attested.user.identities.first.name}</b>", :inline_format => true, :size => 30, :align => :center
move_down 15
text "participou do <b>#{attested.event.name}</b>, realizado no(a) #{attested.event.place.upcase} em #{I18n.l(attested.event.event_date, :format => :long)} às #{attested.event.hour}, perfazendo uma carga horária total de <b>4 horas</b>.",
:inline_format => true, :size => 20, :align => :center
move_down 30
text "Teresina(PI), #{I18n.l(Date.current, :format => :long)}.", :size => 20, :align => :center
move_down 50
text "Realização", :size => 16, :style => :bold, :align => :center
move_down 10
image "#{Rails.root}/public/images/logo.png", :position => :center
end
end
end
10 changes: 10 additions & 0 deletions app/views/certificates/_confirmed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul id="confirmed" class="certificate">
<% @event.confirmed.each do |certificate| %>
<li>
<div class="speaker">
<%= image_tag certificate.user.identities.first.image, :alt => certificate.user.identities.first.name, :class => "user_image photo" if certificate.user.identities.first.image %>
<%= label_tag nil, certificate.user.identities.first.name, :class => "user_name" %>
</div>
</li>
<% end %>
</ul>
9 changes: 9 additions & 0 deletions app/views/certificates/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="blank">
<%= form_tag("/certificados", {:id => "certificate-form"}) do %>
<%= hidden_field :certificate, :user_id, :value => current_user.id %>
<%= hidden_field :certificate, :event_id, :value => @event.id %>
<div class="actions">
<%= button_tag "Eu Vou !", :class => "btn" %>
</div>
<% end %>
</div>
11 changes: 11 additions & 0 deletions app/views/certificates/_going.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ul id="going" class="certificate">
<% @event.going.each do |certificate| %>
<li>
<div class="speaker">
<%= image_tag certificate.user.identities.first.image, :alt => certificate.user.identities.first.name, :class => "user_image photo" if certificate.user.identities.first.image %>
<%= label_tag nil, certificate.user.identities.first.name, :class => "user_name" %>
<%= link_to "Confirmar Presença", certificate_path(certificate), :method => :put, :class => "user_name" if permitted_to?(:update, :certificates) && !certificate.confirmed? %>
</div>
</li>
<% end %>
</ul>
21 changes: 21 additions & 0 deletions app/views/certificates/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1 class="hat title">Meus Certificados</h1>

<table>
<thead>
<th>Evento</th>
<th>Data do evento</th>
<th>Ação</th>
</thead>

<tbody>
<% @certificates.each do |certificate| %>
<tr>
<td>
<%= link_to certificate.event.name, event_path(certificate.event) %>
</td>
<td><%=l certificate.event.event_date %></td>
<td><%= link_to image_tag("icn_pdf.gif", :title => "Gerar PDF"), certificate_path(certificate, :format => "pdf"), {:target => "_new"} %></td>
</tr>
<% end %>
</tbody>
</table>
72 changes: 26 additions & 46 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
<%= form_for(@event) do |f| %>
<% if @event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2>

<ul>
<% @event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :event_date %><br />
<%= f.date_select :event_date %>
</div>
<div class="field">
<%= f.label :hour %><br />
<%= f.text_field :hour %>
</div>
<div class="field">
<%= f.label :place %><br />
<%= f.text_field :place %>
</div>
<div class="field">
<%= f.label :place_url, "URL da localização" %><br />
<%= f.text_field :place_url %>
</div>
<div class="field">
<%= f.label :enable_lectures, "Permitir palestras?" %><br />
<%= f.check_box :enable_lectures %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

<div id="formulario">
<%= form_for(@event) do |f| %>
<% if @event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% @event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :name, :placeholder => "Nome:" %>
<%= f.text_area :description, :placeholder => "Descrição:" %>
<%= f.label :event_date %><br />
<%= f.date_select :event_date %><br />
<%= f.text_field :hour, :placeholder => "Horário:" %>
<%= f.text_field :place, :placeholder => "Local:" %>
<%= f.text_field :place_url, :placeholder => "URL da localização:" %>
<%= f.label :enable_lectures, "Permitir palestras?" %>
<%= f.check_box :enable_lectures %>
<div class="actions">
<%= button_tag "Enviar"%>
<div>
<% end %>
</div>
8 changes: 3 additions & 5 deletions app/views/events/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<h1>Editing event</h1>

<h1 class="hat title">Alterando Evento</h1>
<%= render 'form' %>

<%= link_to 'Show', @event %> |
<%= link_to 'Back', events_path %>
<%= link_to 'Visualizar', @event, :class => "btn" %>
<%= link_to 'Voltar', events_path, :class => "btn" %>
6 changes: 2 additions & 4 deletions app/views/events/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<h1>New event</h1>

<h1 class="hat title">Novo Evento</h1>
<%= render 'form' %>

<%= link_to 'Back', events_path %>
<%= link_to 'Cancelar', events_path, :class => 'btn' %>
Loading