Repository URL to install this package:
Version:
2.4.0.1 ▾
|
module Articular
module Controller
def self.included(base)
base.respond_to :html
base.respond_to :rss, only: :index
end
def index
@articles = scoped_collection
@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 = scoped_collection.tagged_with(@topic)
@title = tagged_title
render :index
end
def archive
@articles = scoped_collection.from_archive(year, month)
@title = archive_title
render :index
end
private
def scoped_collection
article_class.published.page(params[:page])
end
# 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
# 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
params[:year].to_i
end
def month
params[:month].to_i
end
def month_name
Date::MONTHNAMES[month]
end
end
end