Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
articular / lib / articular / controller.rb
Size: Mime:
module Articular
  module Controller
    def self.included(base)
      base.respond_to :html
      base.respond_to :rss, only: :index
    end

    def index
      @articles = article_class.published.paginate(pagination_options)

      @title = index_title
      respond_with @articles
    end

    def show
      @article = article_class.published.friendly.find(params[:id])

      @title = show_title
    end

    def tagged
      find_topic
      @articles = article_class.published.tagged_with(@topic).paginate(pagination_options)

      @title = tagged_title
      render :index
    end

    def archive
      @articles = article_class.published
                               .from_archive(params[:year], params[:month])
                               .paginate(pagination_options)

      @title = archive_title
      render :index
    end

    private

    # Basic name of resource
    def base_title
      article_class.name.pluralize
    end

    # Title for index action
    def index_title
      base_title
    end

    # Title for show action
    def show_title
      "#{@article.title} - #{base_title}"
    end

    # Title for tagged action
    def tagged_title
      "#{base_title} posted under #{@topic.name}"
    end

    # Title for archive action
    def archive_title
      "#{base_title} posted in #{month_name} #{year}"
    end

    # Model to operate with
    def article_class
      self.class.name.gsub('Controller', '').singularize.constantize
    end

    # Options for will_paginate
    def pagination_options
      {
        per_page: 10,
        page: params[:page]
      }
    end

    # Tag retrieval for tagged action
    def find_topic
      @topic = Topical::Tag.find_by_slug(tag_context, params[:tag])
      raise ActiveRecord::RecordNotFound unless @topic
    end

    # Context for Topical lookup
    def tag_context
      :news_topics
    end

    def year
      Articular::Archiver::ArchiveDate.new(params[:year]).year
    end

    def month_name
      if params[:month]
        Date::MONTHNAMES[params[:month].to_i]
      end
    end
  end
end