Files
fizzy/app/controllers/collections_controller.rb
T
2025-09-28 18:43:23 +02:00

64 lines
1.6 KiB
Ruby

class CollectionsController < ApplicationController
before_action :set_collection, except: %i[ new create ]
include FilterScoped
def new
@collection = Collection.new
end
def show
set_page_and_extract_portion_from @collection.cards.awaiting_triage.reverse_chronologically
end
def create
@collection = Collection.create! collection_params.with_defaults(all_access: true)
redirect_to collection_path(@collection)
end
def edit
selected_user_ids = @collection.users.pluck :id
@selected_users, @unselected_users = User.active.alphabetically.partition { |user| selected_user_ids.include? user.id }
end
def update
@collection.update! collection_params
@collection.accesses.revise granted: grantees, revoked: revokees if grantees_changed?
if @collection.accessible_to?(Current.user)
redirect_to edit_collection_path(@collection), notice: "Saved"
else
redirect_to root_path, notice: "Saved (you were removed from the collection)"
end
end
def destroy
@collection.destroy
redirect_to root_path
end
private
def set_collection
@collection = Current.user.collections.find params[:id]
end
def collection_params
params.expect(collection: [ :name, :all_access, :auto_postpone_period, :public_description ])
end
def grantees
User.active.where id: grantee_ids
end
def revokees
@collection.users.where.not id: grantee_ids
end
def grantee_ids
params.fetch :user_ids, []
end
def grantees_changed?
params.key?(:user_ids)
end
end