Sha256: 1fa0b5a0e3d5a839208195fc3c7808c0af204f8c2dbd186f6b75253fb599858d

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Blogelator
  class PostsController < ApplicationController
    before_action :set_tag, :set_posts, only: [:index]
    before_action :set_post, only: [:show]
    PER_PAGE = 5

    def index
    end

    def show
      @title = @post.title
    end

  private

    def all_posts
      Blogelator::Post.page(params[:page]).per(PER_PAGE)
    end

    def posts
      try(:current_admin_user) ? all_posts : published_posts
    end

    def published_posts
      Blogelator::Post.published.page(params[:page]).per(PER_PAGE)
    end

    def posts_with_tag
      if try(:current_admin_user)
        all_posts.joins(:tags).where("blogelator_tags.id = ?", @tag.id)
      else
        published_posts.joins(:tags).where("blogelator_tags.id = ?", @tag.id)
      end
    end

    def set_post
      if try(:current_admin_user)
        @post = Blogelator::Post.find_by(slug: params[:id])
      else
        @post = Blogelator::Post.published.find_by(slug: params[:id])
      end
      fail ActionController::RoutingError.new("Not Found") unless @post
    end

    def set_posts
      @posts = params[:tag_id] ? posts_with_tag : posts
    end

    def set_tag
      return unless params[:tag_id]
      @tag = Blogelator::Tag.find_by(slug: params[:tag_id])
      fail ActionController::RoutingError.new("Not Found") unless @tag
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
blogelator-1.0.3 app/controllers/blogelator/posts_controller.rb
blogelator-1.0.2 app/controllers/blogelator/posts_controller.rb
blogelator-1.0.1 app/controllers/blogelator/posts_controller.rb
blogelator-1.0.0 app/controllers/blogelator/posts_controller.rb