app/controllers/articles_controller.rb in adva-0.1.4 vs app/controllers/articles_controller.rb in adva-0.2.0
- old
+ new
@@ -1,79 +1,57 @@
class ArticlesController < BaseController
- before_action :set_section
- before_action :adjust_action
- before_action :set_category, :only => :index
- before_action :set_tags, :only => :index
- before_action :set_articles, :only => :index
- before_action :set_article, :only => :show
-
- authenticates_anonymous_user
-
def index
- @article = @articles.first
+ @article = articles.first
if !@article
raise ActiveRecord::RecordNotFound
- elsif @article.is_a?(Link)
- redirect_to @article.body
else
show
end
end
def show
- if stale?(etag: [@article, @section, @site], last_modified: [@article, @section, @site].map(&:updated_at).compact.max, public: true)
- render template: "#{@section.type.tableize}/articles/show"
+ @article ||= section.articles.find_by_permalink!(params[:permalink])
+ if @article.draft?
+ raise ActiveRecord::RecordNotFound unless current_user.admin?
end
- end
+ return redirect_to @article.body if @article.is_a?(Link)
- protected
-
- def current_resource
- @section.try(:single_article_mode) ? @section : @article || @section
+ if stale?([*@article.cells, @article, section, site], public: true)
+ render template: "#{section.type.tableize}/articles/show"
end
+ end
- # adjusts the action from :index to :show when the current section is in single-article mode ...
- def adjust_action
- if params[:action] == 'index' and @section.try(:single_article_mode)
- # ... but only if there is one published article
- unless @section.contents.blank? || (@section.contents.first.draft? && current_user.admin?)
- @action_name = @_params[:action] = request.parameters['action'] = 'show'
- end
- end
- end
+ private
- def set_article
- @article = if params[:permalink]
- @section.contents.includes(:author).find_by_permalink!(params[:permalink])
- elsif @section.try(:single_article_mode)
- @section.contents.first
- end
- end
-
- def set_articles
- scope = @category ? @category.all_contents : @section.contents
- scope = scope.tagged(@tags) if @tags.present?
+ helper_method def articles
+ @articles ||= begin
+ scope = category ? category.all_contents : section.contents
+ scope = scope.tagged(tags) if tags.any?
scope = scope.published
- @articles = scope.paginate(page: current_page).limit(@section.contents_per_page)
+ scope.paginate(page: current_page).limit(section.contents_per_page.to_i)
end
+ end
- def set_category
- if params[:category_id]
- @category = @section.categories.find(params[:category_id])
- raise ActiveRecord::RecordNotFound unless @category
- end
+ helper_method def category
+ if !defined?(@category)
+ @category = params[:category_id] ? section.categories.find(params[:category_id]) : nil
end
+ @category
+ end
- def set_tags
- if params[:tags]
+ 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
- def guard_view_permissions
- if @article && @article.draft?
- raise ActiveRecord::RecordNotFound unless current_user.admin?
- end
- end
+ helper_method def current_resource
+ section.try(:single_article_mode) ? section : @article || section
+ end
end