Add documents (merges foodcoops/foodsoft#353)
This commit is contained in:
parent
33c9b23e2f
commit
8bb58c5519
18 changed files with 310 additions and 0 deletions
63
plugins/documents/app/controllers/documents_controller.rb
Normal file
63
plugins/documents/app/controllers/documents_controller.rb
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
require 'filemagic'
|
||||
|
||||
class DocumentsController < ApplicationController
|
||||
before_filter -> { require_plugin_enabled FoodsoftDocuments }
|
||||
|
||||
def index
|
||||
if params["sort"]
|
||||
sort = case params["sort"]
|
||||
when "name" then "name"
|
||||
when "created_at" then "created_at"
|
||||
when "name_reverse" then "name DESC"
|
||||
when "created_at_reverse" then "created_at DESC"
|
||||
end
|
||||
else
|
||||
sort = "name"
|
||||
end
|
||||
|
||||
@documents = Document.page(params[:page]).per(@per_page).order(sort)
|
||||
end
|
||||
|
||||
def new
|
||||
@document = Document.new
|
||||
end
|
||||
|
||||
def create
|
||||
@document = Document.new
|
||||
@document.data = params[:document][:data].read
|
||||
@document.mime = FileMagic.new(FileMagic::MAGIC_MIME).buffer(@document.data)
|
||||
if params[:document][:name] == ''
|
||||
name = params[:document][:data].original_filename
|
||||
name = File.basename(name)
|
||||
@document.name = name.gsub(/[^\w\.\-]/, '_')
|
||||
else
|
||||
@document.name = params[:document][:name]
|
||||
end
|
||||
@document.created_by = current_user
|
||||
@document.save!
|
||||
redirect_to documents_path, notice: I18n.t('documents.create.notice')
|
||||
rescue => error
|
||||
redirect_to documents_path, alert: t('documents.create.error', error: error.message)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@document = Document.find(params[:id])
|
||||
if @document.created_by == current_user or current_user.role_admin?
|
||||
@document.destroy
|
||||
redirect_to documents_path, notice: t('documents.destroy.notice')
|
||||
else
|
||||
redirect_to documents_path, alert: t('documents.destroy.no_right')
|
||||
end
|
||||
rescue => error
|
||||
redirect_to documents_path, alert: t('documents.destroy.error', error: error.message)
|
||||
end
|
||||
|
||||
def show
|
||||
@document = Document.find(params[:id])
|
||||
filename = @document.name
|
||||
unless filename.include? '.'
|
||||
filename += '.' + MIME::Types[@document.mime].first.preferred_extension
|
||||
end
|
||||
send_data(@document.data, :filename => filename, :type => @document.mime)
|
||||
end
|
||||
end
|
||||
5
plugins/documents/app/models/document.rb
Normal file
5
plugins/documents/app/models/document.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Document < ActiveRecord::Base
|
||||
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
|
||||
|
||||
validates_presence_of :data
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
/ insert_before ':root:first-child'
|
||||
= config_input form, :use_documents, as: :boolean
|
||||
20
plugins/documents/app/views/documents/_documents.html.haml
Normal file
20
plugins/documents/app/views/documents/_documents.html.haml
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
- if Document.count > 20
|
||||
= items_per_page
|
||||
= pagination_links_remote @documents
|
||||
%table.table.table-striped
|
||||
%thead
|
||||
%tr
|
||||
%th= sort_link_helper heading_helper(Document, :name), "name", :per_page => @per_page
|
||||
%th= sort_link_helper heading_helper(Document, :created_at), "created_at", :per_page => @per_page
|
||||
%th= heading_helper(Document, :created_by)
|
||||
%th
|
||||
%tbody
|
||||
- for document in @documents
|
||||
%tr
|
||||
%td= link_to document.name, document
|
||||
%td= format_time(document.created_at)
|
||||
%td= h show_user(document.created_by)
|
||||
%td
|
||||
- if document.created_by == current_user or current_user.role_admin?
|
||||
= link_to t('ui.delete'), document, :method => :delete, :data => {:confirm => t('admin.confirm', name: document.name)},
|
||||
class: 'btn btn-mini btn-danger'
|
||||
7
plugins/documents/app/views/documents/index.html.haml
Normal file
7
plugins/documents/app/views/documents/index.html.haml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
- title t('.title')
|
||||
|
||||
- content_for :actionbar do
|
||||
= link_to t('.new'), new_document_path, class: 'btn btn-primary'
|
||||
|
||||
#documentsTable
|
||||
= render :partial => "documents"
|
||||
1
plugins/documents/app/views/documents/index.js.haml
Normal file
1
plugins/documents/app/views/documents/index.js.haml
Normal file
|
|
@ -0,0 +1 @@
|
|||
$('#documentsTable').html('#{escape_javascript(render("documents"))}');
|
||||
8
plugins/documents/app/views/documents/new.html.haml
Normal file
8
plugins/documents/app/views/documents/new.html.haml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
- title t('.title')
|
||||
|
||||
= simple_form_for(@document) do |f|
|
||||
= f.input :name, as: :string
|
||||
= f.input :data, as: :file
|
||||
.form-actions
|
||||
= f.submit class: 'btn'
|
||||
= link_to t('ui.or_cancel'), :back
|
||||
Loading…
Add table
Add a link
Reference in a new issue