app/controllers/comfy/archive/index_controller.rb in comfy_archive-0.2.0 vs app/controllers/comfy/archive/index_controller.rb in comfy_archive-0.3.0

- old
+ new

@@ -20,12 +20,12 @@ if params[:month] @month = params[:month] scope = scope.for_month(@cms_index.datetime_fragment, @month) end elsif params[:category] - @category = CGI.unescape(params[:category]) - scope = scope.for_category(@category).distinct(false) + load_category + scope = scope.for_category(@category.label).distinct(false) end @archive_pages = comfy_paginate(scope, per_page: ComfyArchive.config.posts_per_page) if @cms_index.force_render_page show @@ -42,8 +42,28 @@ if @cms_page unless @cms_index = Comfy::Archive::Index.find_by(page: @cms_page) @cms_index = Comfy::Archive::Index.find_by(page: @cms_page.parent) end end + end + + def load_category + @category = ComfyArchive.config.parameterize_category ? find_cms_category_by_parameterized_label(params[:category]) : Comfy::Cms::Category.find_by(label: CGI.unescape(params[:category])) + unless @category + if ComfyArchive.config.strict_categories + if find_cms_page_by_full_path("/404") + render_page(:not_found) + else + message = "Page Not Found at: \"#{params[:cms_path]}/category/#{params[:category]}\"" + raise ActionController::RoutingError, message + end + else + @category = Comfy::Cms::Category.new(label: ComfyArchive.config.parameterize_category ? params[:category].gsub('-', ' ').titleize : CGI.unescape(params[:category])) + end + end + end + + def find_cms_category_by_parameterized_label(parameterized) + @cms_index.categories.find { |c| c.label.parameterize == parameterized } end end