Sha256: 532c0ec1b8089df9bd3b86aa9a585d4d70793e4d4550cbdf1f9954a145c74ca1

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

class ArticlesController < BaseController
  def index
    @article = articles.first
    if !@article
      raise ActiveRecord::RecordNotFound
    else
      show
    end
  end

  def show
    @article ||= section.articles.find_by_permalink!(params[:permalink])
    if @article.draft?
      raise ActiveRecord::RecordNotFound unless current_user.admin?
    end
    return redirect_to @article.body if @article.is_a?(Link)

    if stale?([*@article.cells, @article, section, site], public: true)
      render template: "#{section.type.tableize}/articles/show"
    end
  end

  private

  helper_method def articles
    @articles ||= begin
      scope = category ? category.all_contents : section.contents
      scope = scope.tagged(tags) if tags.any?
      scope = scope.published
      scope.paginate(page: current_page).limit(section.contents_per_page.to_i)
    end
  end

  helper_method def category
    if !defined?(@category)
      @category = params[:category_id] ? section.categories.find(params[:category_id]) : nil
    end
    @category
  end

  helper_method def tags
    if !defined?(@tags)
      @tags = if params[:tags]
        names = params[:tags].split('+')
        @tags = Tag.where(name: names).pluck(:name)
        raise ActiveRecord::RecordNotFound unless @tags.size == names.size
      else
        []
      end
    end
    @tags
  end

  helper_method def current_resource
    section.try(:single_article_mode) ? section : @article || section
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adva-0.2.0 app/controllers/articles_controller.rb