DRY up the public controllers by introducing a shared base class

This commit is contained in:
David Heinemeier Hansson
2025-11-03 15:56:25 +01:00
parent 393e5fd3d4
commit 65d04f681a
9 changed files with 30 additions and 73 deletions
@@ -1,13 +0,0 @@
module PublicCardScoped
extend ActiveSupport::Concern
included do
before_action :set_collection_and_card
end
private
def set_collection_and_card
@collection = Collection.find_by_published_key(params[:collection_id])
@card = @collection.cards.find(params[:id])
end
end
@@ -1,12 +0,0 @@
module PublicCollectionScoped
extend ActiveSupport::Concern
included do
before_action :set_collection
end
private
def set_collection
@collection = Collection.find_by_published_key(params[:collection_id] || params[:id])
end
end
+19
View File
@@ -0,0 +1,19 @@
class Public::BaseController < ApplicationController
allow_unauthenticated_access
allow_unauthorized_access
before_action :set_collection_and_card
before_action :set_public_cache_expiration
layout "public"
private
def set_collection_and_card
@collection = Collection.find_by_published_key(params[:collection_id] || params[:id])
@card = @collection.cards.find(params[:id]) if params[:collection_id] && params[:id]
end
def set_public_cache_expiration
expires_in 30.seconds, public: true
end
end
+1 -8
View File
@@ -1,11 +1,4 @@
class Public::CardsController < ApplicationController
include CachedPublicly, PublicCardScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::CardsController < Public::BaseController
def show
end
end
@@ -1,11 +1,4 @@
class Public::Collections::Columns::ClosedsController < ApplicationController
include CachedPublicly, PublicCollectionScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::Collections::Columns::ClosedsController < Public::BaseController
def show
set_page_and_extract_portion_from @collection.cards.closed.recently_closed_first
end
@@ -1,11 +1,4 @@
class Public::Collections::Columns::NotNowsController < ApplicationController
include CachedPublicly, PublicCollectionScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::Collections::Columns::NotNowsController < Public::BaseController
def show
set_page_and_extract_portion_from @collection.cards.postponed.reverse_chronologically.with_golden_first
end
@@ -1,11 +1,4 @@
class Public::Collections::Columns::StreamsController < ApplicationController
include CachedPublicly, PublicCollectionScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::Collections::Columns::StreamsController < Public::BaseController
def show
set_page_and_extract_portion_from @collection.cards.awaiting_triage.latest.with_golden_first
end
@@ -1,11 +1,4 @@
class Public::Collections::ColumnsController < ApplicationController
include CachedPublicly, PublicCollectionScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::Collections::ColumnsController < Public::BaseController
before_action :set_column, only: :show
def show
@@ -13,6 +6,11 @@ class Public::Collections::ColumnsController < ApplicationController
end
private
# Unlike the other public controllers, this is using params[:id] to fetch the column
def set_collection_and_card
@collection = Collection.find_by_published_key(params[:collection_id])
end
def set_column
@column = @collection.columns.find(params[:id])
end
@@ -1,11 +1,4 @@
class Public::CollectionsController < ApplicationController
include CachedPublicly, PublicCollectionScoped
allow_unauthenticated_access only: :show
allow_unauthorized_access only: :show
layout "public"
class Public::CollectionsController < Public::BaseController
def show
set_page_and_extract_portion_from @collection.cards.awaiting_triage.latest.with_golden_first
end