Sha256: 13712a0d9091ec92a9131c0a2ac5dc909c04a22b666f04302ded4c8d8dc16049

Contents?: true

Size: 1.84 KB

Versions: 11

Compression:

Stored size: 1.84 KB

Contents

require_dependency "pages_cms/application_controller"

module PagesCms
  class Admin::ArticlesController < ApplicationController
    before_action :logged_in_admin
    before_action :selected_account

    def index
      return_articles
      if params[:edit].present? && Article.exists?(params[:edit])
        @article = Article.find(params[:edit])
        @edit = true
      elsif params[:new] == 'true'
        @article = Article.new
        @new = true
      else
        @article = false
      end
    end

    def create
      return_articles
      @article = current_account.articles.build(article_params)
      if @article.save
        flash[:success] = 'Successfully updated'
        redirect_to admin_articles_path
      else
        @new = true
        flash[:danger] = "Failed: #{@article.errors.full_messages.to_sentence}"
        render :index
      end
    end

    def update
      return_articles
      @article = Article.find(params[:id])
      if @article.update(article_params)
        @edit = true
        flash[:success] = 'Successfully updated'
        render :index
      else
        @edit = true
        flash[:danger] = "Failed: #{@article.errors.full_messages.to_sentence}"
        render :index
      end
    end

    def destroy
      Article.find(params[:id]).destroy
      flash[:success] = 'Destroyed article'
      redirect_to admin_articles_path
    end

    private
      def return_articles
        @articles = current_account.articles.all
        filter_params(params).each do |search, result|
          @articles = @articles.public_send(search, result) if result.present?
        end
      end

      def article_params
        params.require(:article).permit(
          :title, :tags, :content, :image_id, :draft, :archived, :content_md
        )
      end

      def filter_params(params)
        params.slice(:search, :status)
      end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pages_cms-2.4.2 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.3.2 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.3.1 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.3.0 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.2.3 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.2.2 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.2.1 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.1.1 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.1.0 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.0.1 app/controllers/pages_cms/admin/articles_controller.rb
pages_cms-2.0.0 app/controllers/pages_cms/admin/articles_controller.rb