publication/app/controllers/admin/publications_controller.rb

75 lines
1.9 KiB
Ruby
Raw Permalink Normal View History

2023-06-04 14:41:53 +00:00
class Admin::PublicationsController < OrbitAdminController
2023-07-21 02:10:22 +00:00
include Admin::PublicationsHelper
before_action :categories, only: [:index, :new, :edit]
2018-10-04 02:47:23 +00:00
def index
2023-07-21 02:10:22 +00:00
@table_fields = [:title, 'publications_.chapter', :pub_date, :category]
2018-10-04 02:47:23 +00:00
@filter_fields = filter_fields(@categories, @tags)
2023-06-04 14:41:53 +00:00
@publications = Publication.order_by(sort)
2018-10-04 02:47:23 +00:00
.with_categories(filters("category"))
.with_status(filters("status"))
2023-06-04 14:41:53 +00:00
@publications = search_data(@publications, [:title]).page(params[:page]).per(10)
2018-10-04 02:47:23 +00:00
render partial: "index" if request.xhr?
end
def new
2023-06-04 14:41:53 +00:00
@publication = Publication.new
2018-10-04 02:47:23 +00:00
end
def create
2023-06-23 10:25:35 +00:00
publication = Publication.new(publication_params)
publication.save
2023-06-04 14:41:53 +00:00
redirect_to admin_publications_path
2018-10-04 02:47:23 +00:00
end
def edit
2023-06-04 14:41:53 +00:00
@publication = Publication.find(params[:id])
2018-10-04 02:47:23 +00:00
end
def update
2023-06-04 14:41:53 +00:00
publication = Publication.find(params[:id])
publication.update_attributes(publication_params)
redirect_to admin_publications_path
2018-10-04 02:47:23 +00:00
end
def show
2023-06-04 14:41:53 +00:00
@publication = Publication.find(params[:id])
@chapters = @publication.chapters.order_by(sort).page(params[:page]).per(10)
2023-06-23 10:25:35 +00:00
@table_fields = [:title, :tags, :author, :page, 'chapter.keywords']
2018-10-04 02:47:23 +00:00
end
def destroy
2023-06-04 14:41:53 +00:00
publication = Publication.find(params[:id])
publication.destroy
redirect_to admin_publications_path
2018-10-04 02:47:23 +00:00
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
2023-06-04 14:41:53 +00:00
def publication_params
2023-06-23 10:25:35 +00:00
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
2018-10-04 02:47:23 +00:00
end
def categories
@categories = @module_app.categories
@tags = @module_app.tags
end
end