From 51403dc6310d8efef446afaaa2e78fdd1f27ccd3 Mon Sep 17 00:00:00 2001 From: Andrew Cockerham Date: Sun, 1 Sep 2013 16:01:35 -0500 Subject: [PATCH] Added certificate pdf upload and view --- app/assets/javascripts/certificates.js.coffee | 3 + app/assets/stylesheets/certificates.css.scss | 3 + app/assets/stylesheets/scaffolds.css.scss | 69 ++++++++++++++ app/controllers/certificates_controller.rb | 89 +++++++++++++++++++ app/helpers/certificates_helper.rb | 2 + app/models/certificate.rb | 11 +++ app/models/user.rb | 2 + app/views/certificates/_form.html.erb | 19 ++++ app/views/certificates/edit.html.erb | 6 ++ app/views/certificates/index.html.erb | 27 ++++++ app/views/certificates/new.html.erb | 5 ++ app/views/certificates/show.html.erb | 35 ++++++++ app/views/layouts/_header.html.erb | 1 + config/routes.rb | 5 ++ .../20130825215632_create_certificates.rb | 12 +++ ...30825220821_add_user_id_to_certificates.rb | 6 ++ ...achment_certificate_pdf_to_certificates.rb | 11 +++ db/schema.rb | 18 +++- test/fixtures/certificates.yml | 11 +++ .../certificates_controller_test.rb | 49 ++++++++++ test/unit/certificate_test.rb | 7 ++ test/unit/helpers/certificates_helper_test.rb | 4 + 22 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/certificates.js.coffee create mode 100644 app/assets/stylesheets/certificates.css.scss create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/controllers/certificates_controller.rb create mode 100644 app/helpers/certificates_helper.rb create mode 100644 app/models/certificate.rb create mode 100644 app/views/certificates/_form.html.erb create mode 100644 app/views/certificates/edit.html.erb create mode 100644 app/views/certificates/index.html.erb create mode 100644 app/views/certificates/new.html.erb create mode 100644 app/views/certificates/show.html.erb create mode 100644 db/migrate/20130825215632_create_certificates.rb create mode 100644 db/migrate/20130825220821_add_user_id_to_certificates.rb create mode 100644 db/migrate/20130826114257_add_attachment_certificate_pdf_to_certificates.rb create mode 100644 test/fixtures/certificates.yml create mode 100644 test/functional/certificates_controller_test.rb create mode 100644 test/unit/certificate_test.rb create mode 100644 test/unit/helpers/certificates_helper_test.rb diff --git a/app/assets/javascripts/certificates.js.coffee b/app/assets/javascripts/certificates.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/certificates.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/certificates.css.scss b/app/assets/stylesheets/certificates.css.scss new file mode 100644 index 0000000..f349731 --- /dev/null +++ b/app/assets/stylesheets/certificates.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Certificates controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/app/controllers/certificates_controller.rb b/app/controllers/certificates_controller.rb new file mode 100644 index 0000000..6d93b22 --- /dev/null +++ b/app/controllers/certificates_controller.rb @@ -0,0 +1,89 @@ +class CertificatesController < ApplicationController + # GET /certificates + # GET /certificates.json + def index + @certificates = Certificate.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @certificates } + end + end + + # GET /certificates/1 + # GET /certificates/1.json + def show + @certificate = Certificate.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @certificate } + end + end + + # GET /certificates/new + # GET /certificates/new.json + def new + @certificate = Certificate.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @certificate } + end + end + + # GET /certificates/1/edit + def edit + @certificate = Certificate.find(params[:id]) + end + + # POST /certificates + # POST /certificates.json + def create + @certificate = Certificate.new(params[:certificate]) + + respond_to do |format| + if @certificate.save + format.html { redirect_to @certificate, notice: 'Certificate was successfully created.' } + format.json { render json: @certificate, status: :created, location: @certificate } + else + format.html { render action: "new" } + format.json { render json: @certificate.errors, status: :unprocessable_entity } + end + end + end + + # PUT /certificates/1 + # PUT /certificates/1.json + def update + @certificate = Certificate.find(params[:id]) + + respond_to do |format| + if @certificate.update_attributes(params[:certificate]) + format.html { redirect_to @certificate, notice: 'Certificate was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @certificate.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /certificates/1 + # DELETE /certificates/1.json + def destroy + @certificate = Certificate.find(params[:id]) + @certificate.destroy + + respond_to do |format| + format.html { redirect_to certificates_url } + format.json { head :no_content } + end + end + + # def pdf + # pdf_filename = File.join(Rails.root, "/system/certificates/certificate_pdfs/000/000/007/thumb/19035122-V2W-LFCZ_(1).pdf") + # send_file(pdf_filename, :filename => "19035122-V2W-LFCZ_(1).pdf", :disposition => 'inline', :type => "application/pdf") + + #end +end diff --git a/app/helpers/certificates_helper.rb b/app/helpers/certificates_helper.rb new file mode 100644 index 0000000..1bc2041 --- /dev/null +++ b/app/helpers/certificates_helper.rb @@ -0,0 +1,2 @@ +module CertificatesHelper +end diff --git a/app/models/certificate.rb b/app/models/certificate.rb new file mode 100644 index 0000000..befa76a --- /dev/null +++ b/app/models/certificate.rb @@ -0,0 +1,11 @@ +class Certificate < ActiveRecord::Base + attr_accessible :date_completed, :institution, :name, :certificate_pdf, :user_id + + validates :institution, presence: true + validates :user_id, presence: true + validates_attachment :certificate_pdf, presence: true, + content_type: { content_type: "application/pdf" }, + size: { less_than: 10.megabytes } + belongs_to :user + has_attached_file :certificate_pdf #, :styles => { :thumb => ["50x50#", :png] } +end diff --git a/app/models/user.rb b/app/models/user.rb index 6e2d58f..e9d6e8d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,8 @@ class User < ActiveRecord::Base has_many :job_applications, :dependent => :destroy has_many :job_experiences, :dependent => :destroy + has_many :certificates + # Nested Attributes accepts_nested_attributes_for :job_experiences # :allow_destroy => true, diff --git a/app/views/certificates/_form.html.erb b/app/views/certificates/_form.html.erb new file mode 100644 index 0000000..1795c28 --- /dev/null +++ b/app/views/certificates/_form.html.erb @@ -0,0 +1,19 @@ +<%= simple_form_for(@certificate) do |f| %> + <%= f.error_notification %> + <%= f.full_error :certificate_pdf_content_type, class: "alert alert-error" %> + <%= f.full_error :certificate_pdf_file_size, class: "alert alert-error" %> + +
+ <%= f.input :name %> + <%= f.input :institution %> + <%= f.input :date_completed %> + <%= f.input :certificate_pdf, label: "Upload a pdf of your certificate" %> +
+
+ <%= f.hidden_field :user_id, value: current_user.id %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/certificates/edit.html.erb b/app/views/certificates/edit.html.erb new file mode 100644 index 0000000..fafa6f1 --- /dev/null +++ b/app/views/certificates/edit.html.erb @@ -0,0 +1,6 @@ +

Editing certificate

+ +<%= render 'form' %> + +<%= link_to 'Show', @certificate %> | +<%= link_to 'Back', certificates_path %> diff --git a/app/views/certificates/index.html.erb b/app/views/certificates/index.html.erb new file mode 100644 index 0000000..67a551e --- /dev/null +++ b/app/views/certificates/index.html.erb @@ -0,0 +1,27 @@ +

Listing certificates

+ + + + + + + + + + + +<% @certificates.each do |certificate| %> + + + + + + + + +<% end %> +
NameInstitutionDate completed
<%= certificate.name %><%= certificate.institution %><%= certificate.date_completed %><%= link_to 'Show', certificate %><%= link_to 'Edit', edit_certificate_path(certificate) %><%= link_to 'Destroy', certificate, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Certificate', new_certificate_path %> diff --git a/app/views/certificates/new.html.erb b/app/views/certificates/new.html.erb new file mode 100644 index 0000000..fa4f8e7 --- /dev/null +++ b/app/views/certificates/new.html.erb @@ -0,0 +1,5 @@ +

New certificate

+ +<%= render 'form' %> + +<%= link_to 'Back', certificates_path %> diff --git a/app/views/certificates/show.html.erb b/app/views/certificates/show.html.erb new file mode 100644 index 0000000..7c1df15 --- /dev/null +++ b/app/views/certificates/show.html.erb @@ -0,0 +1,35 @@ +

<%= notice %>

+ +
+
+
+ + width="500" height="375" type="application/pdf"> + + + <%= link_to "View Certificate" , @certificate.certificate_pdf.url %> + +

+ Name: +

+ +

+ Institution: + <%= @certificate.institution %> +

+ +

+ Date completed: + <%= @certificate.date_completed %> +

+ +<%= link_to 'Edit', edit_certificate_path(@certificate) %> | +<%= link_to 'Back', certificates_path %> + +
+
+
diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4416cc1..6cca125 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -34,6 +34,7 @@