class Desktop::JournalPagesController < ApplicationController #before_filter :check_for_cancel, :only => [:create, :update] 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 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 end def edit @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 params[:writing_journal][:create_user_id] = current_user.id @writing_journal = WritingJournal.new(params[:writing_journal]) if @writing_journal.save render json: {success: true, msg: "Paper successfully saved!"}.to_json else render json: {success: false, msg: @writing_journal.errors.full_messages}.to_json end end def update params[:writing_journal][:update_user_id] = current_user.id @writing_journal= WritingJournal.find(params[:id]) respond_to do |format| if @writing_journal.update_attributes(params[:writing_journal]) render json: {success: true, msg: "Paper successfully saved!"}.to_json else render json: {success: false, msg: @writing_journal.errors.full_messages}.to_json end end end def check_file_type file if not file.nil? file_type = MIME::Types.type_for(file).first.to_s.split("/")[1] # 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" else file_type = "" end end def destroy @writing_journal = WritingJournal.find(params[:id]) @writing_journal.destroy end 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, coauthors: publication.authors, year: publication.year, url_edit: edit_desktop_journal_page_path(publication), url_delete: 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 private def check_for_cancel if params[:commit] == "Cancel" end end end