Skip to content

Instantly share code, notes, and snippets.

@kanzie
Created May 24, 2011 14:21
Show Gist options
  • Save kanzie/988785 to your computer and use it in GitHub Desktop.
Save kanzie/988785 to your computer and use it in GitHub Desktop.
class CreatePdmRevisions < ActiveRecord::Migration
def self.up
create_table :pdm_revisions do |t|
t.column :commit_message, :string
t.column :attachment, :string
t.column :created_by, :string
t.column :created_date, :datetime
t.column :document_type, :string
t.column :pdm_document_id, :int
end
end
def self.down
drop_table :pdm_revisions
end
end
class CreatePdmDocuments < ActiveRecord::Migration
def self.up
create_table :pdm_documents do |t|
t.column :description, :text
t.column :locked_by, :int
t.column :locked_timestamp, :datetime
t.column :last_revision_by, :int
t.column :title, :string
t.column :pdm_category_id, :int
end
end
def self.down
drop_table :pdm_documents
end
end
class CreatePdmCategories < ActiveRecord::Migration
def self.up
create_table :pdm_categories do |t|
t.column :name, :string
end
PdmCategory.create (:name => 'Uncategorized')
end
def self.down
drop_table :pdm_categories
end
end
<div class="contextual">
<%= link_to l(:label_document_new),
{:project_id => @project},
:class => 'icon icon-add',
:onclick => 'Element.show("uploadDiv"); return false;' %>
</div>
<h2><%=l(:label_my)%></h2>
<div id='uploadDiv' style='display:none;'>
<%= form_tag ({:controller => 'pdm_documents', :action => 'uploadFile', :project_id => @project},:multipart => true) %>
<h3>File Upload</h3>
<p><label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile', :size => 30 %></p>
<p><label><%=l(:label_category)%></label><br />
<%= collection_select(:name, :id, @categories, :id, :name, options ={}, :class =>"category") %>
<p><label><%=l(:label_upload_document_title)%></label><br />
<%= text_field_tag('title', '', :size => 60, :id => nil)%></p>
<p><label><%=l(:label_upload_document_description)%></label><br />
<%= text_area_tag('comment', '', :size => "50x10")%></p>
<%= submit_tag l(:button_create) %>
<%= form_tag %>
<h2></h2><br />
</div>
<div id="Container">
<table id="FileTable">
<thead><tr>
<th><%= l(:label_download) %></th>
<th><%= l(:label_category) %></th>
<th><%= l(:label_last_updated) %></th>
<th><%= l(:label_name) %></th>
<th><%= l(:label_last_revision_by) %></th>
<th><%= l(:label_count_revision) %></th>
</tr></thead>
<%
puts @documents.inspect
if not @documents.nil? %>
<% @documents.each do |document| %>
<tr class="document <%= cycle("odd", "even") %>">
<td class="centerTd"><%= link_to(image_tag('document.png'), { :controller => 'pdm_revisions', :action => 'downloadLatest', :project_id => @project, :id => document.id}) %></td>
<td><%= link_to(document.pdm_category.name, { :controller => "pdm_revisions", :action => "index" , :id => document, :project_id => @project }) %></td>
<td class="centerTd"><%= link_to(document.pdm_revisions[0].created_date, { :controller => "pdm_revisions", :action => "index" , :id => document, :project_id => @project }) %></td>
<td><%= link_to(document.title, { :controller => "pdm_revisions", :action => "index" , :id => document, :project_id => @project }) %></td>
<td><%= link_to(User.find(document.last_revision_by), { :controller => "pdm_revisions", :action => "index" , :id => document, :project_id => @project }) %></td>
<td class="centerTd"><%= link_to(document.pdm_revisions.count, { :controller => "pdm_revisions", :action => "index" , :id => document, :project_id => @project }) %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
<% if @documents.empty? %>
<div id="Notification" >
<p class="Notification"><%= l(:label_no_data) %></p>
</div>
<% end %>
<% content_for :sidebar do %>
<h3><%= l(:label_statistics, '') %></h3>
<% end %>
<% content_for :header_tags do %>
<%= stylesheet_link_tag 'DocumentOverview', :plugin => 'redmine_pdm' %>
<% end %>
</body>
</html>
class PdmCategoriesController < ApplicationController
def index
#@project = Project.find(params[:project_id])
@categories = PdmCategory.find(:all)
end
def add_category
category_name = params[:name]
pdmcategory = PdmCategory.new(:name => category_name)
pdmcategory.save
redirect_to :action => 'index'
end
end
class PdmCategory < ActiveRecord::Base
has_many :pdm_documents
end
class PdmDocument < ActiveRecord::Base
has_many :pdm_revisions
belongs_to :pdm_category
end
class PdmDocumentsController < ApplicationController
before_filter :find_project, :authorize, :only => :index
cattr_accessor :storage_path
@@storage_path = ENV['RAILS_VAR'] ? File.join(ENV['RAILS_VAR'], 'files') : "#{RAILS_ROOT}/files/"
def index
@project = Project.find(params[:project_id])
@documents = PdmDocument.find(:all, :order => "id DESC", :include=>[:pdm_revisions, :pdm_category])
@categories = PdmCategory.find(:all)
end
def show
end
def download
document = PdmDocument.find(params[:id])
send_data("#{@@storage_path}/#{document.id}",:filename => "#{document.title}")
find_project
redirect_to :action => 'index', :project_id => @project
end
def uploadFile
@project = Project.find(params[:project_id])
upload = params[:upload]
name = params[:title]
if upload != nil && !name.empty?
documentName = upload['datafile'].original_filename
documentName = documentName.gsub(/[^\w\.\-]/,'_')
fileTime = Time.new.to_f
directory = "#{@@storage_path}"
fileName = (fileTime).to_s + "_" + documentName
path = File.join(directory, fileName)
File.open(path, "wb") do |f|
buffer = ""
while (buffer = upload['datafile'].read(8192))
f.write(buffer)
end
end
category_id = params[:name][:id]
#by performing hashed input like below rails automatically prevents sql-injections
pdmdocument = PdmDocument.new(:title => params[:title], :description => params[:comment], :last_revision_by => User.current.id, :pdm_category_id => category_id)
pdmdocument.save
@document = pdmdocument
pdmrevision = PdmRevision.new(:commit_message => "Initial commit", :attachment => fileName, :created_by => User.current.id, :created_date => Time.now, :pdm_document_id => pdmdocument.id)
pdmrevision.save
redirect_to :action => 'index', :project_id => @project
else
redirect_to :action => 'index', :project_id => @project
flash[:error] = l(:label_missing_param_file)
end
end
def find_project
@project = Project.find(params[:project_id])
end
end
class PdmRevision < ActiveRecord::Base
belongs_to :pdm_document
end
require 'time'
class PdmRevisionsController < ApplicationController
before_filter :find_project, :authorize
cattr_accessor :storage_path
@@storage_path = ENV['RAILS_VAR'] ? File.join(ENV['RAILS_VAR'], '/files') : "#{RAILS_ROOT}/files/" # figure out where the files-dir is
def index
@project = Project.find(params[:project_id])
@document = PdmDocument.find(params[:id])
@revisions = PdmRevision.find(:all, :order => "created_date DESC", :conditions => [ "pdm_document_id = ?", @document.id ] )
checkTimelock
end
def find_project
@project = Project.find(params[:project_id])
end
def checkTimelock
@document = PdmDocument.find(params[:id])
if(@document.locked_timestamp != nil)
#puts "@DOCUMENT: " + @document.inspect
diff = (Time.now - @document.locked_timestamp).to_i
#To minutes
diff = diff/60
#To Hours
#diff = (diff/3600).to_i
if(diff > 10)
changeLock
end
end
end
def download
@revision = PdmRevision.find(params[:id])
@document = PdmDocument.find(:first, :conditions => [ "id = ?",@revision.pdm_document_id ] )
#directory = "#{RAILS_ROOT}/files/"
revisionName = @revision.attachment
trash, newFileName = revisionName.split(/_/, 2)
FileUtils.copy(@@storage_path + revisionName, @@storage_path + newFileName)
send_data("#{@@storage_path}#{@revision.id}",:filename => newFileName, :stream => false)
#send_file (@@storage_path + newFileName, :stream => false)
File.delete(@@storage_path + newFileName)
#redirect_to :action => 'index', :project_id => @project, :id => @document
end
def downloadLatest
@project = Project.find(params[:project_id])
@document = PdmDocument.find(params[:id])
@revision = PdmRevision.find(:first, :order => "created_date DESC", :conditions => [ "pdm_document_id = ?", @document.id ] )
redirect_to :action => 'download', :project_id => @project, :id => @revision
end
def uploadNewRevision
@project = Project.find(params[:project_id])
@document = PdmDocument.find(params[:id])
upload = params[:upload]
comment = params[:comment]
if User.current.id == @document.locked_by
@document.update_attributes(:locked_by => nil, :locked_timestamp => nil)
@document.save
if upload != nil && !comment.empty?
fileTime = Time.new.to_f
documentName = upload['datafile'].original_filename
fileName = (fileTime).to_s + "_" + documentName
directory = @@storage_path
path = File.join(directory, fileName)
File.open(path, "wb") do |f|
buffer = ""
while (buffer = upload['datafile'].read(8192))
f.write(buffer)
end
end
pdmrevision = PdmRevision.new(:commit_message => params[:comment], :attachment => fileName, :created_by => User.find(User.current.id), :created_date => Time.now, :pdm_document_id => params[:id])
pdmrevision.save
redirect_to :action => 'index', :project_id => @project, :id => @document
else
@document.update_attributes(:locked_by => nil, :locked_timestamp => nil)
redirect_to :action => 'index', :project_id => @project, :id => @document
end
end
end
def changeLock
@project = Project.find(params[:project_id])
@document = PdmDocument.find(params[:id])
if @document.locked_by == nil
@document.update_attributes(:locked_by => User.current.id, :locked_timestamp => Time.now)
@document.save
redirect_to :action => 'downloadLatest', :project_id => @project, :id => @document and return
elsif User.current.id == @document.locked_by
@document.update_attributes(:locked_by => nil, :locked_timestamp => nil)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment