class Admin::AllPageController < ApplicationController before_action :authenticate_admin! layout 'admin' def index @all_pages = AllPage.where('type_of != 3') @published_pages = PublishedPage.where('type_of = 1 OR type_of = 2').order('priority ASC') end def show end def new @all_page = AllPage.new @articles = Article.all end def create @all_page = AllPage.new(all_page_params) @all_page.updated_by = current_admin.id @all_page.updated_at = Time.now @all_page.published = false @articles = Article.all if @all_page.save if params[:publish] == '1' params[:id] = @all_page.id publish end redirect_to action: 'index' else render :new end end def edit @all_page = AllPage.find(params[:id]) @articles = Article.all end def update @all_page = AllPage.find(params[:id]) @all_page.updated_by = current_admin.id @all_page.updated_at = Time.now @all_page.published = false if @all_page.update_attributes(all_page_params) if params[:publish] == '1' params[:id] = @all_page.id publish end else @articles = Article.all @all_page.errors.full_messages.each do |msg| puts "
  • #{msg}
  • " end render :edit end end def destroy if @all_page = AllPage.find(params[:id]) @all_page.destroy end redirect_to action: 'index' end def change_priority end def publish if @all_page = AllPage.find(params[:id]) if @published_page = @all_page.published_page @published_page.name = @all_page.name @published_page.title = @all_page.title @published_page.meta_description = @all_page.meta_description @published_page.all_page_id = @all_page.id @published_page.nofollow = @all_page.nofollow @published_page.type_of = @all_page.type_of @published_page.full_text = @all_page.full_text @published_page.article_id = @all_page.article_id @published_page.updated_by = current_admin.id @published_page.updated_at = Time.now else @published_page = PublishedPage.new @published_page.name = @all_page.name @published_page.title = @all_page.title @published_page.meta_description = @all_page.meta_description @published_page.all_page_id = @all_page.id @published_page.nofollow = @all_page.nofollow @published_page.type_of = @all_page.type_of @published_page.full_text = @all_page.full_text @published_page.article_id = @all_page.article_id @published_page.priority = PublishedPage.all.size + 1; @published_page.updated_by = current_admin.id @published_page.updated_at = Time.now @published_page.created_by = current_admin.id @published_page.created_at = Time.now end if @published_page.save @all_page.published = true @all_page.save else if @published_page.errors @published_page.errors.full_messages.each do |msg| puts msg end end end end end def unpublish if @all_page = AllPage.find(params[:id]) if @all_page.published_page @all_page.published_page.destroy end @all_page.published = false @all_page.save end end private def all_page_params params.require(:all_page).permit(:name, :article_id, :title, :type_of, :full_text) end end