Skip to content

Commit

Permalink
Added file upload functionality to Tickets, Clients, and Devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Hoyt authored and Jonathan Hoyt committed Dec 12, 2008
1 parent 7a2b11d commit ef57ef6
Show file tree
Hide file tree
Showing 24 changed files with 294 additions and 165 deletions.
87 changes: 0 additions & 87 deletions app/controllers/attachments_controller.rb

This file was deleted.

88 changes: 88 additions & 0 deletions app/controllers/things_controller.rb
@@ -0,0 +1,88 @@
class ThingsController < ApplicationController
before_filter :login_required
layout nil

def index
@things = Thing.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @things }
end
end

# GET /things/1
# GET /things/1.xml
def show
@thing = Thing.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @thing }
end
end

# GET /things/new
# GET /things/new.xml
def new
@thing = Thing.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @thing }
end
end

# GET /things/1/edit
def edit
@thing = Thing.find(params[:id])
end

# POST /things
# POST /things.xml
def create
@thing = Thing.new(params[:thing])
@thing.attached_id = params[:thing][:attached_id]
@thing.attached_type = params[:thing][:attached_type]

respond_to do |format|
if @thing.save
flash[:notice] = 'Thing was successfully created.'
format.html { redirect_to :back }
format.xml { render :xml => @thing, :status => :created, :location => @thing }
else
format.html { redirect_to :back }
format.xml { render :xml => @thing.errors, :status => :unprocessable_entity }
end
end
end

# PUT /things/1
# PUT /things/1.xml
def update
@thing = Thing.find(params[:id])

respond_to do |format|
if @thing.update_attributes(params[:thing])
flash[:notice] = 'Thing was successfully updated.'
format.html { redirect_to(@thing) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @thing.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /things/1
# DELETE /things/1.xml
def destroy
@thing = Thing.find(params[:id])
@thing.destroy

respond_to do |format|
format.html { redirect_to(things_url) }
format.xml { head :ok }
end
end
end
2 changes: 0 additions & 2 deletions app/helpers/attachments_helper.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/helpers/things_helper.rb
@@ -0,0 +1,2 @@
module ThingsHelper
end
2 changes: 0 additions & 2 deletions app/models/attachment.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/models/client.rb
Expand Up @@ -8,6 +8,7 @@ class Client < ActiveRecord::Base

has_many :devices, :dependent => :destroy
has_many :tickets
has_many :things, :as => :attached, :dependent => :destroy

has_many :phones, :dependent => :destroy
has_many :emails, :dependent => :destroy
Expand Down
1 change: 1 addition & 0 deletions app/models/device.rb
Expand Up @@ -3,6 +3,7 @@ class Device < ActiveRecord::Base
belongs_to :device_type
has_and_belongs_to_many :tickets
has_many :checklists, :dependent => :destroy
has_many :things, :as => :attached, :dependent => :destroy

validates_uniqueness_of :service_tag
validates_presence_of :client_id, :device_type_id
Expand Down
15 changes: 15 additions & 0 deletions app/models/thing.rb
@@ -0,0 +1,15 @@
class Thing < ActiveRecord::Base
belongs_to :attached, :polymorphic => true

has_attached_file :attachment, :whiny_thumbnails => false

validates_attachment_presence :attachment
validates_attachment_size :attachment, :less_than => 200.megabytes, :message => "File is too large."

before_create :assign_name

def assign_name
self.name.blank? ? self.name = self.attachment_file_name : true
end

end
1 change: 1 addition & 0 deletions app/models/ticket.rb
Expand Up @@ -4,6 +4,7 @@ class Ticket < ActiveRecord::Base
has_many :ticket_entries
has_and_belongs_to_many :devices
has_many :checklists
has_many :things, :as => :attached, :dependent => :destroy

validates_presence_of :client_id
validates_presence_of :user_id
Expand Down
16 changes: 0 additions & 16 deletions app/views/attachments/edit.html.erb

This file was deleted.

20 changes: 0 additions & 20 deletions app/views/attachments/index.html.erb

This file was deleted.

15 changes: 0 additions & 15 deletions app/views/attachments/new.html.erb

This file was deleted.

8 changes: 0 additions & 8 deletions app/views/attachments/show.html.erb

This file was deleted.

45 changes: 45 additions & 0 deletions app/views/clients/_things.html.erb
@@ -0,0 +1,45 @@
<div id="files">
<ul class="toolbar">
<li><a href="javascript:void(0)" class="upload_file">Upload File</a></li>
</ul>
<div id="upload_file_form" class="hide toolbox">
<h3>Upload File</h3>
<% form_for @client.things.new, :html => { :multipart => true } do |f| %>
<table class="m">
<tr><td>Name:</td><td><%= f.text_field :name %></td></tr>
<tr><td>File:</td><td><%= f.file_field :attachment %></td></tr>
<tr><td>Description:</td><td><%= f.text_area :description, :style => "height:50px" %></td></tr>
</table>
<%= f.hidden_field :attached_id, :value => @client.id %>
<%= f.hidden_field :attached_type, :value => "Client" %>
<%= f.submit %>
<% end %>
<br />
</div>
<h3>Files Attached to this Ticket</h3>
<table class="itu">
<tr><th>Name</th><th>Description</th><th>Size</th><th>Action</th></tr>
<% @client.things.each do |thing| %>
<tr>
<td><%= link_to thing.name, thing.attachment.url %></td>
<td><%= link_to thing.description, thing.attachment.url %></td>
<td><%= link_to thing.attachment_file_size, thing.attachment.url %></td>
<td>
<div class="delete_file">
<%= link_to_remote image_tag("/images/icons/delete.png"), :url => thing_url(thing), :method => :delete %>
</div>
</td>
</tr>
<% end %>
</table>
</div>

<script>
// setup device tab buttons
$("a.upload_file").bind("click", function(){
$("#upload_file_form").slideDown(1000);
});
$("div.delete_file a").click(function(){
$(this).parent().parent().parent().slideUp("slow");
});
</script>
2 changes: 2 additions & 0 deletions app/views/clients/show.html.erb
Expand Up @@ -8,13 +8,15 @@
<li class="ui-tabs-nav-item"><a href="#notes" title="notes">Notes</a></li>
<li class="ui-tabs-nav-item"><a href="#tickets" title="tickets">Tickets</a></li>
<li class="ui-tabs-nav-item"><a href="#devices" title="devices">Devices</a></li>
<li class="ui-tabs-nav-item"><a href="#files" title="files">Files</a></li>
<% if not_a_user(@client) %><li class="ui-tabs-nav-item"><a href="/clients/<%= @client.id %>/users/new" title="#newaccount">Invite</a></li><% end %>
<% if my_account(@client) %><li class="ui-tabs-nav-item"><a href="/users/<%= current_user.id %>" title="#myaccount">My Account</a></li><% end %>
</ul>
<%= render :partial => 'notes', :locals => {:client => @client} %>
<%= render :partial => 'details', :locals => {:client => @client} %>
<%= render :partial => 'tickets', :locals => {:client => @client} %>
<%= render :partial => 'devices', :locals => {:client => @client} %>
<%= render :partial => 'things', :locals => {:client => @client} %>
</div>

<script>
Expand Down
45 changes: 45 additions & 0 deletions app/views/devices/_things.html.erb
@@ -0,0 +1,45 @@
<div id="files">
<ul class="toolbar">
<li><a href="javascript:void(0)" class="upload_file">Upload File</a></li>
</ul>
<div id="upload_file_form" class="hide toolbox">
<h3>Upload File</h3>
<% form_for @device.things.new, :html => { :multipart => true } do |f| %>
<table class="m">
<tr><td>Name:</td><td><%= f.text_field :name %></td></tr>
<tr><td>File:</td><td><%= f.file_field :attachment %></td></tr>
<tr><td>Description:</td><td><%= f.text_area :description, :style => "height:50px" %></td></tr>
</table>
<%= f.hidden_field :attached_id, :value => @device.id %>
<%= f.hidden_field :attached_type, :value => "Device" %>
<%= f.submit %>
<% end %>
<br />
</div>
<h3>Files Attached to this Ticket</h3>
<table class="itu">
<tr><th>Name</th><th>Description</th><th>Size</th><th>Action</th></tr>
<% @device.things.each do |thing| %>
<tr>
<td><%= link_to thing.name, thing.attachment.url %></td>
<td><%= link_to thing.description, thing.attachment.url %></td>
<td><%= link_to thing.attachment_file_size, thing.attachment.url %></td>
<td>
<div class="delete_file">
<%= link_to_remote image_tag("/images/icons/delete.png"), :url => thing_url(thing), :method => :delete %>
</div>
</td>
</tr>
<% end %>
</table>
</div>

<script>
// setup device tab buttons
$("a.upload_file").bind("click", function(){
$("#upload_file_form").slideDown(1000);
});
$("div.delete_file a").click(function(){
$(this).parent().parent().parent().slideUp("slow");
});
</script>

0 comments on commit ef57ef6

Please sign in to comment.