60 lines
1.5 KiB
Ruby
60 lines
1.5 KiB
Ruby
class CollectionsController < ApplicationController
|
|
before_action :set_collection, except: %i[ new create ]
|
|
|
|
include FilterScoped
|
|
|
|
def new
|
|
@collection = Collection.new
|
|
end
|
|
|
|
def create
|
|
@collection = Collection.create! collection_params.with_defaults(all_access: true)
|
|
redirect_to cards_path(collection_ids: [ @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
|