Files
fizzy/app/controllers/cards_controller.rb
T
Jorge Manrubia 375b16b26e Introduce columns and column entity
To refactor some messy code used to render the cards index screen
2025-09-18 11:41:28 +02:00

63 lines
1.4 KiB
Ruby

require "ostruct"
class CardsController < ApplicationController
include CardsHelper, FilterScoped
before_action :set_collection, only: %i[ create ]
before_action :set_card, only: %i[ show edit update destroy ]
enable_collection_filtering only: :index
PAGE_SIZE = 25
def index
@columns = Cards::Columns.new(user_filtering: @user_filtering, page_size: PAGE_SIZE)
fresh_when etag: @columns
end
def create
card = @collection.cards.create!
redirect_to card
end
def show
fresh_when etag: @card.cache_invalidation_parts.for_perma
end
def edit
end
def update
@card.update! card_params
if @card.published?
render_card_replacement
else
redirect_to @card
end
end
def destroy
@card.destroy!
redirect_to cards_path(collection_ids: [ @card.collection ]), notice: ("Card deleted" unless @card.creating?)
end
private
def set_collection
@collection = Current.user.collections.find(params[:collection_id])
end
def set_card
@card = Current.user.accessible_cards.find params[:id]
end
def card_params
params.expect(card: [ :status, :title, :description, :image, tag_ids: [] ])
end
def render_card_replacement
render turbo_stream: turbo_stream.replace([ @card, :card_container ], partial: "cards/container", locals: { card: @card.reload })
end
end