diff --git a/.gitignore b/.gitignore index 206bc04..163fc3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store .bundle .rvmrc db/*.sqlite3 diff --git a/Gemfile b/Gemfile index 6aede02..dac794b 100644 --- a/Gemfile +++ b/Gemfile @@ -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' @@ -21,6 +24,7 @@ end gem 'pg', :group => :production gem 'sqlite3', :group => [:test, :development] + group :test do gem 'rspec-rails', '~> 2.4' gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 0fee6f9..612b6f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -218,7 +234,9 @@ DEPENDENCIES omniauth (~> 1.0) omniauth-facebook omniauth-github + pdf-inspector pg + prawn pry rails rdiscount diff --git a/app/controllers/certificates_controller.rb b/app/controllers/certificates_controller.rb new file mode 100644 index 0000000..3805d11 --- /dev/null +++ b/app/controllers/certificates_controller.rb @@ -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 diff --git a/app/models/certificate.rb b/app/models/certificate.rb new file mode 100644 index 0000000..e5c5196 --- /dev/null +++ b/app/models/certificate.rb @@ -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 diff --git a/app/models/event.rb b/app/models/event.rb index 42a2256..a906c25 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/models/generate_pdf.rb b/app/models/generate_pdf.rb new file mode 100644 index 0000000..4ba6088 --- /dev/null +++ b/app/models/generate_pdf.rb @@ -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 "#{attested.user.identities.first.name}", :inline_format => true, :size => 30, :align => :center + move_down 15 + text "participou do #{attested.event.name}, 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 4 horas.", + :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 \ No newline at end of file diff --git a/app/views/certificates/_confirmed.html.erb b/app/views/certificates/_confirmed.html.erb new file mode 100644 index 0000000..4255a99 --- /dev/null +++ b/app/views/certificates/_confirmed.html.erb @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/app/views/certificates/_form.html.erb b/app/views/certificates/_form.html.erb new file mode 100644 index 0000000..4467877 --- /dev/null +++ b/app/views/certificates/_form.html.erb @@ -0,0 +1,9 @@ +
+ <%= form_tag("/certificados", {:id => "certificate-form"}) do %> + <%= hidden_field :certificate, :user_id, :value => current_user.id %> + <%= hidden_field :certificate, :event_id, :value => @event.id %> +
+ <%= button_tag "Eu Vou !", :class => "btn" %> +
+ <% end %> +
\ No newline at end of file diff --git a/app/views/certificates/_going.html.erb b/app/views/certificates/_going.html.erb new file mode 100644 index 0000000..305a922 --- /dev/null +++ b/app/views/certificates/_going.html.erb @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/app/views/certificates/index.html.erb b/app/views/certificates/index.html.erb new file mode 100644 index 0000000..a8f36be --- /dev/null +++ b/app/views/certificates/index.html.erb @@ -0,0 +1,21 @@ +

Meus Certificados

+ + + + + + + + + + <% @certificates.each do |certificate| %> + + + + + + <% end %> + +
EventoData do eventoAção
+ <%= link_to certificate.event.name, event_path(certificate.event) %> + <%=l certificate.event.event_date %><%= link_to image_tag("icn_pdf.gif", :title => "Gerar PDF"), certificate_path(certificate, :format => "pdf"), {:target => "_new"} %>
\ No newline at end of file diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb index 61f7336..7d82de9 100644 --- a/app/views/events/_form.html.erb +++ b/app/views/events/_form.html.erb @@ -1,46 +1,26 @@ -<%= form_for(@event) do |f| %> - <% if @event.errors.any? %> -
-

<%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:

- - -
- <% end %> - -
- <%= f.label :name %>
- <%= f.text_field :name %> -
-
- <%= f.label :description %>
- <%= f.text_area :description %> -
-
- <%= f.label :event_date %>
- <%= f.date_select :event_date %> -
-
- <%= f.label :hour %>
- <%= f.text_field :hour %> -
-
- <%= f.label :place %>
- <%= f.text_field :place %> -
-
- <%= f.label :place_url, "URL da localização" %>
- <%= f.text_field :place_url %> -
-
- <%= f.label :enable_lectures, "Permitir palestras?" %>
- <%= f.check_box :enable_lectures %> -
-
- <%= f.submit %> -
-<% end %> - +
+ <%= form_for(@event) do |f| %> + <% if @event.errors.any? %> +
+

<%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:

+ +
+ <% end %> + <%= f.text_field :name, :placeholder => "Nome:" %> + <%= f.text_area :description, :placeholder => "Descrição:" %> + <%= f.label :event_date %>
+ <%= f.date_select :event_date %>
+ <%= 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 %> +
+ <%= button_tag "Enviar"%> +
+ <% end %> +
diff --git a/app/views/events/edit.html.erb b/app/views/events/edit.html.erb index d7d4525..972bbc9 100644 --- a/app/views/events/edit.html.erb +++ b/app/views/events/edit.html.erb @@ -1,6 +1,4 @@ -

Editing event

- +

Alterando Evento

<%= render 'form' %> - -<%= link_to 'Show', @event %> | -<%= link_to 'Back', events_path %> +<%= link_to 'Visualizar', @event, :class => "btn" %> +<%= link_to 'Voltar', events_path, :class => "btn" %> diff --git a/app/views/events/new.html.erb b/app/views/events/new.html.erb index 6119978..cf7f802 100644 --- a/app/views/events/new.html.erb +++ b/app/views/events/new.html.erb @@ -1,5 +1,3 @@ -

New event

- +

Novo Evento

<%= render 'form' %> - -<%= link_to 'Back', events_path %> +<%= link_to 'Cancelar', events_path, :class => 'btn' %> diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index bc5dccc..abb2a90 100644 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -1,19 +1,38 @@

<%= notice %>

<%= markdown @event.description %> -

No dia <%=l @event.event_date %>, às <%= @event.hour %>, <%= @event.place %>

+

+ No dia <%=l @event.event_date %>, às <%= @event.hour %>, <%= @event.place %> +

+ +<% if current_user && Date.current <= @event.event_date %> + <% unless @event.user_go?(current_user.id) %> + <%= render "certificates/form"%> + <% end %> +<% end %> + + +<% if ((current_user && current_user.admin? && @event.going.present?) || (@event.going.present? && Date.current <= @event.event_date)) %> +

Quem vai

+ <%= render "certificates/going", :object => @event %> +<% end %> + +<% if @event.confirmed.present? && @event.event_date < Date.current %> +

Quem foi

+ <%= render "certificates/confirmed", :object => @event %> +<% end %> <% if @event.enable_lectures - if @event.lectures.present? + if @event.lectures.present? %> -

Palestras

- -<% end %> +

Palestras

+ + <% end %> -<% permitted_to? :create, :lectures do %> -

Add a Lecture:

-<%= render "lectures/form" %> -<% end %> + <% permitted_to? :create, :lectures do %> +

Adicionar Palestras:

+ <%= render "lectures/form" %> + <% end %> <% end %>
diff --git a/app/views/layouts/_profile.html.erb b/app/views/layouts/_profile.html.erb index 6e1d094..0e86939 100644 --- a/app/views/layouts/_profile.html.erb +++ b/app/views/layouts/_profile.html.erb @@ -3,7 +3,7 @@ <% if current_user %>
<% if current_user_provider.image -%><%= current_user_provider.name %><% end -%> - +
<% else %> <%= link_to "Conectar com Facebook", "/auth/facebook", :class => "login_facebook" %> diff --git a/app/views/lectures/_form.html.erb b/app/views/lectures/_form.html.erb index e49620b..551512d 100644 --- a/app/views/lectures/_form.html.erb +++ b/app/views/lectures/_form.html.erb @@ -1,15 +1,12 @@ -<%= form_for([@event, @event.lectures.build]) do |f| %> -
- <%= f.label Lecture.human_attribute_name(:name) %>
- <%= f.text_field :name %> -
-
- <%= f.label Lecture.human_attribute_name(:description) %>
- <%= f.text_area :description %> - <%= f.hidden_field :user_id, :value => current_user.id %> -
-
- <%= f.submit %> -
-<% end %> +
+ <%= form_for([@event, @event.lectures.build]) do |f| %> + <%= f.text_field :name, :placeholder => "Nome:" %> + <%= f.text_area :description, :placeholder => "Descrição:" %> + <%= f.hidden_field :user_id, :value => current_user.id %> +
+ <%= button_tag "Enviar"%> +
+ <% end %> +
+ diff --git a/app/views/lectures/_lecture.html.erb b/app/views/lectures/_lecture.html.erb index 52494fb..5d2cec8 100644 --- a/app/views/lectures/_lecture.html.erb +++ b/app/views/lectures/_lecture.html.erb @@ -7,7 +7,7 @@
- <% if lecture.user.identities.first.image %><%= image_tag lecture.user.identities.first.image, :alt => lecture.user.identities.first.name, :class => "user_image photo" %><% end %> + <%= image_tag lecture.user.identities.first.image, :alt => lecture.user.identities.first.name, :class => "user_image photo" if lecture.user.identities.first.image %> <%= link_to lecture.user.identities.first.name, lecture, :class => "user_name" %>
<%= render :partial => 'layouts/vote_lecture', :locals => { :lecture => lecture, :can_login => false } %> diff --git a/config/authorization_rules.rb b/config/authorization_rules.rb index ed8b009..cca42c7 100644 --- a/config/authorization_rules.rb +++ b/config/authorization_rules.rb @@ -7,6 +7,7 @@ if_attribute :roles => { :title => is { "admin" } } if_attribute :roles => { :title => is { "member" } } end + has_permission_on [:certificates], :to => [:index, :create, :show] end role :member do @@ -14,6 +15,7 @@ has_permission_on [:users], :to => [:index, :show] has_permission_on [:events], :to => [:show] has_permission_on [:lectures], :to => [:new, :create, :edit, :update, :destroy, :index ] + has_permission_on [:certificates], :to => [:index, :create, :show] end role :admin do @@ -22,6 +24,7 @@ has_permission_on [:events], :to => [:new, :create, :edit, :update, :destroy, :index, :show, :vote_increment] has_permission_on [:posts], :to => [:new, :create, :edit, :update, :destroy, :index, :show, :vote_increment] has_permission_on [:welcome], :to => [:index] + has_permission_on [:certificates], :to => [:index, :create, :update, :show] end end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index 72aca7e..29813b2 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -3,3 +3,4 @@ # Add new mime types for use in respond_to blocks: # Mime::Type.register "text/richtext", :rtf # Mime::Type.register_alias "text/html", :iphone +Mime::Type.register "application/pdf", :pdf diff --git a/config/routes.rb b/config/routes.rb index 148fb23..436bfb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,5 +19,7 @@ root :to => "welcome#index" match '/:controller(/:action(/:id))' + + resources :certificates, :path => "certificados", :only => [:index, :create, :update, :show] end diff --git a/db/migrate/20130329185256_create_certificates.rb b/db/migrate/20130329185256_create_certificates.rb new file mode 100644 index 0000000..e7c13fb --- /dev/null +++ b/db/migrate/20130329185256_create_certificates.rb @@ -0,0 +1,13 @@ +class CreateCertificates < ActiveRecord::Migration + def change + create_table :certificates do |t| + t.boolean :go, :default => false + t.boolean :confirmed, :default => false + t.string :token + t.references :event + t.references :user + + t.timestamps + end + end +end diff --git a/db/test.sqlite3-journal b/db/test.sqlite3-journal new file mode 100644 index 0000000..dd29c30 Binary files /dev/null and b/db/test.sqlite3-journal differ diff --git a/public/images/background.png b/public/images/background.png new file mode 100755 index 0000000..3c406cf Binary files /dev/null and b/public/images/background.png differ diff --git a/public/images/icn_pdf.gif b/public/images/icn_pdf.gif new file mode 100755 index 0000000..28b2bf4 Binary files /dev/null and b/public/images/icn_pdf.gif differ diff --git a/public/images/logo.png b/public/images/logo.png new file mode 100644 index 0000000..7918c11 Binary files /dev/null and b/public/images/logo.png differ diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 3fd1a82..0a09b35 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -313,6 +313,12 @@ tr:last-child td.last { resize: none; } +form select { + padding: 0 7px 0 7px; + margin-bottom: 10px; + height: 28px; +} + #main #formulario form textarea{ background: url(/images/fundo-textarea.png) top left no-repeat; height: 165px; @@ -413,3 +419,10 @@ tr:last-child td.last { #btn_contact { position:fixed; top: 263px; right:0; height:152px; width:23px; z-index:1000; } #btn_contact a#contato{background:url(/images/btn_contato.png) center no-repeat; height:230px; width:23px; display:block;} #btn_contact a#contato:hover{background:url(/images/btn_contato_hover.png) center no-repeat;} + +.actions button {float: left !important;} +.blank {overflow: hidden;} +.certificate li {list-style: none;} +.btn {-moz-border-bottom-colors: none;-moz-border-left-colors: none;-moz-border-right-colors: none;-moz-border-top-colors: none;background-color: #F5F5F5; background-image: linear-gradient(to bottom, #FFFFFF, #E6E6E6);background-repeat: repeat-x;border-color: #BBBBBB #BBBBBB #A2A2A2;border-image: none;border-radius: 4px 4px 4px 4px;border-style: solid;border-width: 1px;box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);color: #333333;cursor: pointer;display: inline-block;font-size: 14px;line-height: 20px;margin: 5px 3px 0 0;padding: 4px 14px;text-align: center;text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);vertical-align: middle;} + + diff --git a/spec/controllers/certificates_controller_spec.rb b/spec/controllers/certificates_controller_spec.rb new file mode 100644 index 0000000..03d96a2 --- /dev/null +++ b/spec/controllers/certificates_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe CertificatesController do + +end diff --git a/spec/models/certificate_spec.rb b/spec/models/certificate_spec.rb new file mode 100644 index 0000000..4e86376 --- /dev/null +++ b/spec/models/certificate_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe Certificate do + it "generations attested" do + @user = User.make!(:identities => [Identity.make!]) + @attested = Certificate.create(:user => @user, :event => Event.make!) + array_texts = PDF::Inspector::Text.analyze(@attested.generate.render).strings + array_texts.include?(@attested.user.identities.first.name).should be_true + end +end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 333836c..0d7b678 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -1,9 +1,13 @@ require 'spec_helper' describe Event do + before do + @eventy = Event.create(:name => "Event with name", :event_date => Date.current) + @user = User.make!(:identities => [Identity.make!]) + end + it "should create slug" do - eventy = Event.create(:name => "Event with name") - eventy.slug.should == "event-with-name" + @eventy.slug.should == "event-with-name" end it "should order lectures from balance" do @@ -13,4 +17,24 @@ eventy = Event.make!(:lectures => [lecture1, lecture2, lecture3]) Event.find(eventy.id).lectures.should == [lecture3, lecture1, lecture2] end + + it "should user go to the event" do + @certificate = Certificate.create(:event => @eventy, :user => @user, :go => true) + @eventy.user_go?(@user.id).should be_true + end + + it "should user not go to the event" do + @certificate = Certificate.create(:event => @eventy, :user => @user, :go => false) + @eventy.user_go?(@user.id).should be_false + end + + it "should have users will go to the event" do + @certificate = Certificate.create(:event => @eventy, :user => @user, :go => true) + @eventy.going.present?.should be_true + end + + it "should have users that was to event" do + @certificate = Certificate.create(:event => @eventy, :user => @user, :confirmed => true) + @eventy.confirmed.present?.should be_true + end end diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb index 955a3cd..30bd162 100644 --- a/spec/support/blueprints.rb +++ b/spec/support/blueprints.rb @@ -74,3 +74,8 @@ image { "http://profile.ak.fbcdn.net/hprofile-ak-sn#{sn}/260670_1681442949_224808880_q.jpg" } link { "https://www.facebook.com/rogerio.medeiros.{sn}" } end + +Certificate.blueprint do + user { User.make! } + event { Event.make!(:interaje) } +end