Making contracts awesome

This commit is contained in:
Will Bradley 2014-04-12 15:05:57 -07:00
parent daeb7713e0
commit 2f4872218e
6 changed files with 129 additions and 10 deletions

View File

@ -4,6 +4,13 @@ class ContractsController < ApplicationController
layout 'resources' layout 'resources'
def index 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 end
def show def show
@ -16,8 +23,28 @@ class ContractsController < ApplicationController
end end
def create def create
Rails.logger.info "CONTRACT" # if @contract.first_name.blank? && @contract.last_name.blank? && @contract.cosigner.blank? # assume autodetect of filename
Rails.logger.info @contract.inspect # 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 @contract.created_by = current_user
respond_to do |format| respond_to do |format|
if @contract.save if @contract.save
@ -30,6 +57,9 @@ class ContractsController < ApplicationController
end end
end end
def find_for_user
end
def update def update
respond_to do |format| respond_to do |format|
if @contract.update_attributes(params[:contract]) if @contract.update_attributes(params[:contract])

View File

@ -8,10 +8,17 @@ class Contract < ActiveRecord::Base
validates_presence_of :first_name, :signed_at #, :last_name validates_presence_of :first_name, :signed_at #, :last_name
has_attached_file :document, { :styles => { :medium => "300x300>"}, has_attached_file :document,
:storage => :s3, { :styles =>
:s3_credentials => { :access_key_id => ENV['S3_KEY'], {
:secret_access_key => ENV['S3_SECRET'] }, :medium => "300x300>",
:path => ":attachment/:id/:style.:extension", :large => "900x900>"
:bucket => ENV['S3_BUCKET'] } },
: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 end

View File

@ -40,7 +40,7 @@
<p> <p>
<%= link_to "Upload New Document", "#", class: "btn btn-default", onclick: "$('#document_upload').removeClass('hidden'); $(this).addClass('hidden')" unless @contract.document.blank? %> <%= link_to "Upload New Document", "#", class: "btn btn-default", onclick: "$('#document_upload').removeClass('hidden'); $(this).addClass('hidden')" unless @contract.document.blank? %>
<div id="document_upload" class="<%= "hidden" unless @contract.document.blank? %>"> <div id="document_upload" class="<%= "hidden" unless @contract.document.blank? %>">
<%= f.file_field :document %> <%= f.file_field :document, class: "form-control", style: "width: 100%; height: 100px; background-color: #f9f9f9;" %>
</div> </div>
</p> </p>
</div> </div>

View File

@ -1,4 +1,80 @@
<h1>New Contract <h1>New Contract
<%= link_to 'Back', contracts_path, class: "btn btn-default" %> <%= link_to 'Back', contracts_path, class: "btn btn-default" %>
</h1> </h1>
<div class="col-xs-12">
<%= render 'form' %> <%= render 'form' %>
</div>
<div class="col-xs-12">
<div class="alert alert-info">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.) <em>Double-check the names, users, and dates, though!!</em><br/>
<strong>first last - date.[jpg|pdf]</strong><br/>
<strong>first last by cosigner name - date.[jpg|pdf]</strong><br/>
</div>
</div>
<script type="text/javascript">
function capitalizeIfIsntCapitalized(str){
if(str[0].toLowerCase() == str[0] ){
return str[0].toUpperCase() + str.slice(1);
}
else{
return str;
}
}
$(function() {
$("#contract_document").change(function (){
doc = document.getElementById("contract_document")
if(doc.files && doc.files.length > 0) {
name_split = doc.files[0].name.replace(".jpg","").replace(".pdf","").split(" ");
$("#contract_user_alert").remove(); // clear any existing alerts
$("#contract_user_spinner").remove(); // clear spinner
if(name_split.length == 4){ // we have one name
$("#contract_first_name").val(capitalizeIfIsntCapitalized(name_split[0]));
$("#contract_last_name").val(capitalizeIfIsntCapitalized(name_split[1]));
// 2 is the hyphen
signed_at = new Date(name_split[3]);
$("#contract_signed_at_1i").val(signed_at.getUTCFullYear()).change();
$("#contract_signed_at_2i").val(signed_at.getUTCMonth()+1).change();
$("#contract_signed_at_3i").val(signed_at.getUTCDate()).change();
}
else if(name_split.length == 7 && name_split[2] == "by"){ // we have two names
$("#contract_first_name").val(capitalizeIfIsntCapitalized(name_split[0]));
$("#contract_last_name").val(capitalizeIfIsntCapitalized(name_split[1]));
// 2 is "by"
$("#contract_cosigner").val(capitalizeIfIsntCapitalized(name_split[3])+" "+capitalizeIfIsntCapitalized(name_split[4]));
// 5 is the hyphen
signed_at = new Date(name_split[6]);
$("#contract_signed_at_1i").val(signed_at.getUTCFullYear());
$("#contract_signed_at_2i").val(signed_at.getUTCMonth()+1);
$("#contract_signed_at_3i").val(signed_at.getUTCDate());
}
// Try and select the relevant user if exists
if( $("#contract_first_name").val().length > 0 && $("#contract_last_name").val().length > 0 ) {
user_id = $('#contract_user_id option').filter(function(){
return $(this).text() == $("#contract_first_name").val() + " " + $("#contract_last_name").val();
}).prop("selected", "true").val();
// Start a spinner before AJAX request
$("#contract_user_id").after("<i id='contract_user_spinner' class='icon-spin icon-refresh'></i>");
// If we found a user, check how many contracts that user already has
$.get("/users/"+user_id+"/contracts.json",function(data){
$("#contract_user_spinner").remove(); // clear spinner
if(data.length > 0){
other_contract_links = $.map(data,function(item){
output = "<a href='/contracts/"+item["id"]+"'>"+item["id"]+"</a> &nbsp;&nbsp; ";
return output;
});
$("#contract_user_id").after("<span id='contract_user_alert' class='label label-danger'>Warning: this user already has "+data.length+" other contract(s). Check the existing one(s) to avoid duplication: "+other_contract_links+"</span>");
}
});
}
}
});
});
</script>

View File

@ -2,6 +2,7 @@
<h1 class="col-md-8"> <h1 class="col-md-8">
Contract Contract
<%= link_to 'Back', contracts_path, :class => "btn btn-default" %> <%= 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 '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 %> <%= 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 %>
</h1> </h1>
@ -29,7 +30,8 @@
<% else %> <% else %>
<p><%= link_to "Download Contract", @contract.document.url %> <p><%= link_to "Download Contract", @contract.document.url %>
<div class="col-xs-12"> <div class="col-xs-12">
<iframe src="<%= @contract.document.url %>" width="100%" height="600"></iframe> <% contract_url = (@contract.document.exists?(:large) ? @contract.document.url(:large) : @contract.document.url) %>
<iframe src="<%= contract_url %>" width="100%" height="600"></iframe>
</div> </div>
</p> </p>
<% end %> <% end %>

View File

@ -20,6 +20,9 @@ Dooraccess::Application.routes.draw do
resources :certifications resources :certifications
resources :contracts 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_for :users, :skip => :registrations
devise_scope :user do devise_scope :user do
@ -42,6 +45,7 @@ Dooraccess::Application.routes.draw do
resources :users do resources :users do
get 'email' => 'users#compose_email', :as => "compose_email" get 'email' => 'users#compose_email', :as => "compose_email"
post 'email' => 'users#send_email' post 'email' => 'users#send_email'
resources 'contracts', only: [:index]
end end
match 'users/create' => 'users#create', :via => :post # Use POST users/create instead of POST users to avoid devise conflict match 'users/create' => 'users#create', :via => :post # Use POST users/create instead of POST users to avoid devise conflict