Skip to content

Commit

Permalink
Added new 'parameterize_category' config option and updated Comfy::Ar…
Browse files Browse the repository at this point in the history
…chive::IndexController to use either current escaped Comfy::Cms::Category labels or new parameterized Category labels, plus tests. Now also results in a 404 page if the Category doesn't exist. Issue #2
  • Loading branch information
morgant committed Apr 21, 2022
1 parent ce60ce6 commit c02941b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
20 changes: 18 additions & 2 deletions app/controllers/comfy/archive/index_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def index
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)
Expand All @@ -46,4 +46,20 @@ def load_index
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 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
end
end

def find_cms_category_by_parameterized_label(parameterized)
@cms_index.categories.find { |c| c.label.parameterize == parameterized }
end

end
6 changes: 5 additions & 1 deletion lib/comfy_archive/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ class Configuration
# Number of posts per page. Default is 10
attr_accessor :posts_per_page

# Parameterize category label in paths. Default is false
attr_accessor :parameterize_category

# Configuration defaults
def initialize
@posts_per_page = 10
@posts_per_page = 10
@parameterize_category = false
end

end
Expand Down
50 changes: 43 additions & 7 deletions test/controllers/comfy/archive/index_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,61 @@ def test_get_index_for_month_archive
end

def test_get_index_with_category
assert_not ComfyArchive.config.parameterize_category
@category.categorizations.create!(categorized: @index.children.first)

get comfy_archive_pages_of_category_path(@index.url(relative: true), @category.label)

assert_response :success
assert_template :index
assert_equal @category.label, assigns(:category)
assert_equal @category, assigns(:category)
assert assigns(:archive_pages)
assert_equal 1, assigns(:archive_pages).count
assert assigns(:archive_pages).first.categories.member? @category
end

def test_get_index_with_category_invalid
get comfy_archive_pages_of_category_path(@index.url(relative: true), "invalid")
assert_response :success
assert_template :index
assert_equal "invalid", assigns(:category)
assert assigns(:archive_pages)
assert_equal 0, assigns(:archive_pages).count
assert_not ComfyArchive.config.parameterize_category
assert_exception_raised ActionController::RoutingError, "Page Not Found at: \"#{comfy_archive_pages_of_category_path(@index.url(relative: true), "invalid".parameterize).sub(/\/+/, '')}\"" do
get comfy_archive_pages_of_category_path(@index.url(relative: true), "invalid")
end
end

def test_get_index_with_parameterized_category
ComfyArchive.config.parameterize_category = true
with_routing do |set|
set.draw do
comfy_route :archive_admin
comfy_route :archive
end
assert ComfyArchive.config.parameterize_category

@category.categorizations.create!(categorized: @index.children.first)

get comfy_archive_pages_of_category_path(@index.url(relative: true), @category.label.parameterize)

assert_response :success
assert_template :index
assert_equal @category, assigns(:category)
assert assigns(:archive_pages)
assert_equal 1, assigns(:archive_pages).count
assert assigns(:archive_pages).first.categories.member? @category
end
end

def test_get_index_with_parameterized_category_invalid
ComfyArchive.config.parameterize_category = true
with_routing do |set|
set.draw do
comfy_route :archive_admin
comfy_route :archive
end
assert ComfyArchive.config.parameterize_category

assert_exception_raised ActionController::RoutingError, "Page Not Found at: \"#{comfy_archive_pages_of_category_path(@index.url(relative: true), "invalid".parameterize).sub(/^\/+/, '')}\"" do
get comfy_archive_pages_of_category_path(@index.url(relative: true), "invalid".parameterize)
end
end
end

def test_get_index_is_sorted
Expand Down
11 changes: 10 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ActiveSupport::TestCase

def reset_config
ComfyArchive.configure do |config|
config.posts_per_page = 10
config.posts_per_page = 10
config.parameterize_category = false
end
end

Expand Down Expand Up @@ -81,6 +82,14 @@ def r(method, path, options = {})
send(method, path, options)
end

# Allow drawing, calling new, and restoring routes to default state.
# See: https://stackoverflow.com/a/27083128
def with_routing(&block)
yield ComfyArchive::Application.routes
ensure
reset_config # Note: without resetting the config, the reloaded routes will still have the same dynamic routes applied
ComfyArchive::Application.routes_reloader.reload!
end
end

class Rails::Generators::TestCase
Expand Down

0 comments on commit c02941b

Please sign in to comment.