Skip to content

Instantly share code, notes, and snippets.

@claeusdev
Forked from query-string/admin_posts_controller.rb
Last active February 11, 2022 02:22
Show Gist options
  • Save claeusdev/51a9058b7024d4911942fa162ef8d373 to your computer and use it in GitHub Desktop.
Save claeusdev/51a9058b7024d4911942fa162ef8d373 to your computer and use it in GitHub Desktop.
class Admin::AdminPostsController < InheritedResources::Base
layout 'admin'
before_filter :require_login
before_action :set_post
before_action :set_branches
before_action :set_sections
load_and_authorize_resource
def index
@posts = Post.order("created_at DESC")
@posts = @posts.where('user_id = ?', current_user.id) unless current_user.role == 'admin'
@posts = @posts.paginate(:page => params[:page], :per_page => 20)
end
def show
@menu_post_create = true
@date = @post.created_at
@blocks = @post.blocks.order(:degree)
@sections = @post.branch.sections.order(:degree)
end
def refresh
@posts = Post.order("id DESC")
render :layout => false
end
def new
@post = Post.new
@menu_post_create = true
end
def create
@post = Post.new(params[:post].merge(user_id: current_user.id))
@menu_post_create = true
if @post.save
expire_page controller: 'static', action: 'index' if @post.status == 'published'
redirect_to admin_post_path(@post)
else
render 'new'
end
end
def update
respond_to do |format|
if @post.update_attributes(params[:post].merge(:created_at => "#{params[:date_year]}-#{params[:date_month]}-#{params[:date_day]} #{params[:date_hour]}:#{params[:date_minute]}:00"))
if params[:text]
params[:text].each do |itm|
Blocktext.find(itm[0]).update_attributes( :text => itm[1] )
end
end
format.html { redirect_to @post }
format.js
else
format.html { redirect_to :back, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.comment_threads.destroy_all
@post.destroy
respond_to do |format|
format.html { redirect_to admin_posts_path, :flash => { :success => "#{t 'articles.controllers.destroy.flash.success', :name => @post.name}"} }
format.js
end
end
def upload
if @post.update_attributes( :photo => params[:file] )
render :json => { 'success' => true, 'url' => "#{@post.photo.url(:middle)}#{@post.photo.updated_at}" }
else
render :json => { 'error' => @post.errors }
end
end
def remove
@post.update_attributes( :photo => nil )
render :text => nil
end
def flush
params[:items].each do |itm|
Post.find(itm).destroy
end
render :text => 'success'
end
def search
@posts = Post.where('name LIKE ?', '%'+params[:request]+'%')
@posts = @posts.where('user_id = ?', current_user.id) unless current_user.role == 'admin'
if @posts.size < 1
render :text => 0
else
render :layout => false
end
end
private
def posts_categories
post_categories = []
@posts.each do |post|
if get_categories.include?(post.category)
post_categories << post.category
end
end
post_categories
end
def set_branches
@branches = Branch.order(:degree)
@branches = @branches.delete_if{|branch| !current_user.access_branches.include?(branch.id)} if current_user.role == 'editor'
end
def set_sections
@sections = @branches.first.sections.order(:degree)
end
def set_post
@post = Post.find(params[:id])
end
def selector
@posts = Post.order("id DESC")
@posts = @posts.where('user_id = ?', current_user) unless current_user.role == 'admin'
@posts = @posts.where("status = ?", params[:status]) if params[:status] != 'all'
@posts = @posts.where("branch_id = ?", params[:branch]) if params[:branch] != 'all'
render :layout => false
end
def get_categories
Category.visible.all
end
def post_params
params.require(:post).permit(:created_at, :status, :branch, :user_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment