orbit-basic/app/controllers/desktop/journal_pages_controller.rb

137 lines
4.0 KiB
Ruby
Raw Normal View History

class Desktop::JournalPagesController < ApplicationController
2012-11-19 04:07:24 +00:00
def index
@writing_journal = WritingJournal.where(create_user_id: current_user.id)
@level_types = JournalLevelType.all
respond_to do |format|
format.html { render :layout => false}
end
end
2012-11-19 04:07:24 +00:00
def show
end
def new
@writing_journal = WritingJournal.new
@level_types = JournalLevelType.all
@author_types = JournalAuthorType.all
@paper_types= JournalPaperType.all
respond_to do |format|
format.html { render :layout => false}
end
2012-11-20 09:20:05 +00:00
end
def edit
2012-11-19 04:07:24 +00:00
@writing_journal= WritingJournal.find(params[:id])
@level_types = JournalLevelType.all
@author_types = JournalAuthorType.all
@paper_types= JournalPaperType.all
respond_to do |format|
format.html { render :layout => false}
end
end
def create
if params[:commit].eql?"Save"
if not params[:writing_journal][:publication_date].nil?
params[:writing_journal][:publication_date] = \
Date.new *(params[:writing_journal][:publication_date].split("/").map{|s| s.to_i})
end
params[:writing_journal][:create_user_id] = current_user.id
@writing_journal = WritingJournal.new(params[:writing_journal])
if @writing_journal.save
2012-11-20 09:20:05 +00:00
# respond_to do |format|
# format.html { redirect_to desktop_journal_pages_url, :layout => false, notice: 'User was successfully created.'}
# # format.json { render json: @writing_journal, status: :created, location: @writing_journal}
# # format.js
# end
render :json => {"success"=>true,"msg"=>"Paper successfully saved!"}.to_json
else
2012-11-20 09:20:05 +00:00
render :json => {"success"=>false,"msg"=>"Saving failed!"}
end
2012-11-20 09:20:05 +00:00
end
render :json => {"success"=>true,"msg"=>"Paper successfully saved!"}.to_json
end
2012-11-19 04:07:24 +00:00
def update
if params[:commit].eql?"Save"
@writing_journal= WritingJournal.find(params[:id])
end
if not params[:writing_journal][:publication_date].nil?
params[:writing_journal][:publication_date] = \
Date.new *(params[:writing_journal][:publication_date].split("/").map{|s| s.to_i})
end
respond_to do |format|
if @writing_journal.update_attributes(params[:writing_journal])
format.html { redirect_to desktop_journal_pages_url, :layout => false, notice: 'User was successfully updated.'}
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
2012-11-19 04:07:24 +00:00
def check_file_type file
if not file.nil?
file_type = MIME::Types.type_for(file).first.to_s.split("/")[1]
2012-11-20 09:20:05 +00:00
# case file_type
# when "jpg", "jpeg"
# type = "jpg"
# when "text", "txt"
# type = "txt"
# when "pdf"
# type = "pdf"
# when "png"
# type = "png"
# else "readme"
# end
file_type = "/assets/ft-icons/#{file_type}/#{file_type}-48_32.png"
2012-11-19 04:07:24 +00:00
else
file_type = ""
end
end
2012-11-19 04:07:24 +00:00
def get_journals_json
publications = WritingJournal.where(create_user_id: current_user.id)
sort_publications= Hash.new
data = Array.new
publications.each do |publication|
if sort_publications[publication.journal_title].nil?
sort_publications[publication.journal_title] = Array.new
end
sort_publications[publication.journal_title] <<
{ title: publication.paper_title,
keywords: publication.keywords,
abstract: publication.abstract,
level: publication.journal_paper_type_id,
coauthors: publication.authors,
year: publication.year,
url_edit: edit_desktop_journal_page_path(publication),
files: publication.writing_journal_files.collect{|file|
{title: file.title, url: file.file.url, icon: check_file_type(file.file.url)}
}
}
end
sort_publications.each do |journal, papers|
data << {title: journal, papers: papers}
end
render json: JSON.pretty_generate(data)
end
end