publication/app/controllers/admin/publications_controller.rb

75 lines
1.9 KiB
Ruby

class Admin::PublicationsController < OrbitAdminController
include Admin::PublicationsHelper
before_action :categories, only: [:index, :new, :edit]
def index
@table_fields = [:title, 'publications_.chapter', :pub_date, :category]
@filter_fields = filter_fields(@categories, @tags)
@publications = Publication.order_by(sort)
.with_categories(filters("category"))
.with_status(filters("status"))
@publications = search_data(@publications, [:title]).page(params[:page]).per(10)
render partial: "index" if request.xhr?
end
def new
@publication = Publication.new
end
def create
publication = Publication.new(publication_params)
publication.save
redirect_to admin_publications_path
end
def edit
@publication = Publication.find(params[:id])
end
def update
publication = Publication.find(params[:id])
publication.update_attributes(publication_params)
redirect_to admin_publications_path
end
def show
@publication = Publication.find(params[:id])
@chapters = @publication.chapters.order_by(sort).page(params[:page]).per(10)
@table_fields = [:title, :tags, :author, :page, 'chapter.keywords']
end
def destroy
publication = Publication.find(params[:id])
publication.destroy
redirect_to admin_publications_path
end
def save_chapter_order
ids = params[:ids]
ids.each_with_index do |id,index|
chapter = Chapter.find(id) rescue nil
if !chapter.nil?
chapter.sort_number = index
chapter.save
end
end
render json: {success: true}.to_json
end
private
def publication_params
tmp = params.require(:publication).permit!
if tmp['full_files_attributes']['0']['file'].nil? && !tmp['full_files_attributes']['0']['_destroy']
tmp.delete('full_files_attributes')
end
tmp
end
def categories
@categories = @module_app.categories
@tags = @module_app.tags
end
end