From 2f4872218e739e12c912332dec038476fff7a709 Mon Sep 17 00:00:00 2001
From: Will Bradley
Date: Sat, 12 Apr 2014 15:05:57 -0700
Subject: [PATCH] Making contracts awesome
---
app/controllers/contracts_controller.rb | 34 ++++++++++-
app/models/contract.rb | 19 +++++--
app/views/contracts/_form.html.erb | 2 +-
app/views/contracts/new.html.erb | 76 +++++++++++++++++++++++++
app/views/contracts/show.html.erb | 4 +-
config/routes.rb | 4 ++
6 files changed, 129 insertions(+), 10 deletions(-)
diff --git a/app/controllers/contracts_controller.rb b/app/controllers/contracts_controller.rb
index 350a48b..e0fd518 100644
--- a/app/controllers/contracts_controller.rb
+++ b/app/controllers/contracts_controller.rb
@@ -4,6 +4,13 @@ class ContractsController < ApplicationController
layout 'resources'
def index
+ if params[:user_id].present?
+ @contracts = Contract.where(user_id: params[:user_id])
+ end
+ respond_to do |format|
+ format.html
+ format.json { render :json => @contracts }
+ end
end
def show
@@ -16,8 +23,28 @@ class ContractsController < ApplicationController
end
def create
- Rails.logger.info "CONTRACT"
- Rails.logger.info @contract.inspect
+# if @contract.first_name.blank? && @contract.last_name.blank? && @contract.cosigner.blank? # assume autodetect of filename
+# begin
+# name_split = params[:contract][:document].original_filename.sub(".jpg","").split
+# if name_split.count == 4 # we have one name
+# @contract.first_name = name_split[0]
+# @contract.last_name = name_split[1]
+# # 2 is the hyphen
+# @contract.signed_at = Date.parse(name_split[3])
+# elsif name_split.count == 7 && name_split[2] == "by" # we have two names
+# @contract.first_name = name_split[0]
+# @contract.last_name = name_split[1]
+# # 2 is "by"
+# @contract.cosigner = "#{name_split[3]} #{name_split[4]}"
+# # 5 is the hyphen
+# @contract.signed_at = Date.parse(name_split[6])
+# else
+# Rails.logger.info "Couldn't determine name from filename array: #{name_split.inspect}"
+# end
+# rescue Exception => e
+# end
+# end
+
@contract.created_by = current_user
respond_to do |format|
if @contract.save
@@ -30,6 +57,9 @@ class ContractsController < ApplicationController
end
end
+ def find_for_user
+ end
+
def update
respond_to do |format|
if @contract.update_attributes(params[:contract])
diff --git a/app/models/contract.rb b/app/models/contract.rb
index 69fd92a..b498049 100644
--- a/app/models/contract.rb
+++ b/app/models/contract.rb
@@ -8,10 +8,17 @@ class Contract < ActiveRecord::Base
validates_presence_of :first_name, :signed_at #, :last_name
- has_attached_file :document, { :styles => { :medium => "300x300>"},
- :storage => :s3,
- :s3_credentials => { :access_key_id => ENV['S3_KEY'],
- :secret_access_key => ENV['S3_SECRET'] },
- :path => ":attachment/:id/:style.:extension",
- :bucket => ENV['S3_BUCKET'] }
+ has_attached_file :document,
+ { :styles =>
+ {
+ :medium => "300x300>",
+ :large => "900x900>"
+ },
+ :storage => :s3,
+ :s3_protocol => :https,
+ :s3_credentials => { :access_key_id => ENV['S3_KEY'],
+ :secret_access_key => ENV['S3_SECRET'] },
+ :path => ":attachment/:id/:style.:extension",
+ :bucket => ENV['S3_BUCKET']
+ }
end
diff --git a/app/views/contracts/_form.html.erb b/app/views/contracts/_form.html.erb
index ee863e1..97636ac 100644
--- a/app/views/contracts/_form.html.erb
+++ b/app/views/contracts/_form.html.erb
@@ -40,7 +40,7 @@
<%= link_to "Upload New Document", "#", class: "btn btn-default", onclick: "$('#document_upload').removeClass('hidden'); $(this).addClass('hidden')" unless @contract.document.blank? %>
">
- <%= f.file_field :document %>
+ <%= f.file_field :document, class: "form-control", style: "width: 100%; height: 100px; background-color: #f9f9f9;" %>
diff --git a/app/views/contracts/new.html.erb b/app/views/contracts/new.html.erb
index b8104b0..685eed7 100644
--- a/app/views/contracts/new.html.erb
+++ b/app/views/contracts/new.html.erb
@@ -1,4 +1,80 @@
New Contract
<%= link_to 'Back', contracts_path, class: "btn btn-default" %>
+
+
<%= render 'form' %>
+
+
+
+
Note: if you name your scans in one of the below formats, this form will try to fill itself out automatically when you select the file. (The spaces, hyphen, and jpg or pdf extensions are mandatory for this to work.) Double-check the names, users, and dates, though!!
+first last - date.[jpg|pdf]
+first last by cosigner name - date.[jpg|pdf]
+
+
+
+
diff --git a/app/views/contracts/show.html.erb b/app/views/contracts/show.html.erb
index bfd6183..4824856 100644
--- a/app/views/contracts/show.html.erb
+++ b/app/views/contracts/show.html.erb
@@ -2,6 +2,7 @@
Contract
<%= link_to 'Back', contracts_path, :class => "btn btn-default" %>
+ <%= link_to 'New', new_contract_path, :class => "btn btn-success" %>
<%= link_to 'Edit', edit_contract_path(@contract), :class => "btn btn-primary" %>
<%= link_to 'Delete', contract_path(@contract), {:confirm => 'Are you sure you want to delete this forever?', :method => :delete, :class => "btn btn-danger"} if can? :destroy, @contract %>
@@ -29,7 +30,8 @@
<% else %>
<%= link_to "Download Contract", @contract.document.url %>
-
+ <% contract_url = (@contract.document.exists?(:large) ? @contract.document.url(:large) : @contract.document.url) %>
+
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index d0bfcec..23bbe09 100755
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -20,6 +20,9 @@ Dooraccess::Application.routes.draw do
resources :certifications
resources :contracts
+ #collection do
+ #end
+ match 'contracts/find_for_user/:user_id' => 'contracts#find_for_user', as: 'find_for_user'
devise_for :users, :skip => :registrations
devise_scope :user do
@@ -42,6 +45,7 @@ Dooraccess::Application.routes.draw do
resources :users do
get 'email' => 'users#compose_email', :as => "compose_email"
post 'email' => 'users#send_email'
+ resources 'contracts', only: [:index]
end
match 'users/create' => 'users#create', :via => :post # Use POST users/create instead of POST users to avoid devise conflict