Use contracted member services and show only manageable projects in user administration (#9076)
* Reuse contracted services in memberships controller * Show only manageable projects * Show only manageable projects in select box * Reject empty values in role_ids * Add spec for when user has no manage_members permission * Also handle delete through service * Extend spec for when user has no manage_members permission * Extend Members SetAttributesSpec * Make manageable_Members an instance method and add spec * hide buttons if user is not allowed to use them * Rename manageable_projects to assignable_projects * Build a membership for the contract * Fix url for global_roles to work with update and create methods Co-authored-by: ulferts <jens.ulferts@googlemail.com>pull/9080/head
parent
4a54e9b41a
commit
e9f1781d2b
@ -1,43 +1,52 @@ |
||||
module IndividualPrincipals |
||||
module MembershipControllerMethods |
||||
def update |
||||
update_or_create(request.patch?, :notice_successful_update) |
||||
extend ActiveSupport::Concern |
||||
|
||||
included do |
||||
before_action :find_membership, only: %i[update destroy] |
||||
end |
||||
|
||||
def create |
||||
update_or_create(request.post?, :notice_successful_create) |
||||
end |
||||
membership_params = permitted_params.membership.merge(principal: @individual_principal) |
||||
call = ::Members::CreateService |
||||
.new(user: current_user) |
||||
.call(membership_params) |
||||
|
||||
def destroy |
||||
@membership = @individual_principal.memberships.find(params[:id]) |
||||
tab = redirected_to_tab(@membership) |
||||
respond_with_service_call call, message: :notice_successful_create |
||||
end |
||||
|
||||
if @membership.deletable? && request.delete? |
||||
@membership.destroy |
||||
@membership = nil |
||||
def update |
||||
call = ::Members::UpdateService |
||||
.new(model: @membership, user: current_user) |
||||
.call(permitted_params.membership) |
||||
|
||||
flash[:notice] = I18n.t(:notice_successful_delete) |
||||
respond_with_service_call call, message: :notice_successful_update |
||||
end |
||||
|
||||
redirect_to edit_polymorphic_path(@individual_principal, tab: tab) |
||||
def destroy |
||||
call = ::Members::DeleteService |
||||
.new(model: @membership, user: current_user) |
||||
.call |
||||
|
||||
respond_with_service_call call, message: :notice_successful_delete |
||||
end |
||||
|
||||
private |
||||
|
||||
def update_or_create(save_record, message) |
||||
@membership = params[:id].present? ? Member.find(params[:id]) : Member.new(principal: @individual_principal, project: nil) |
||||
|
||||
result = ::Members::EditMembershipService |
||||
.new(@membership, save: save_record, current_user: current_user) |
||||
.call(attributes: permitted_params.membership) |
||||
def find_membership |
||||
@membership = Member.visible(current_user).find(params[:id]) |
||||
rescue ActiveRecord::RecordNotFound |
||||
render_404 |
||||
end |
||||
|
||||
if result.success? |
||||
def respond_with_service_call(call, message:) |
||||
if call.success? |
||||
flash[:notice] = I18n.t(message) |
||||
else |
||||
flash[:error] = result.errors.full_messages.join("\n") |
||||
flash[:error] = call.errors.full_messages.join("\n") |
||||
end |
||||
|
||||
redirect_to edit_polymorphic_path(@individual_principal, tab: redirected_to_tab(@membership)) |
||||
redirect_to edit_polymorphic_path(@individual_principal, tab: redirected_to_tab(call.result)) |
||||
end |
||||
end |
||||
end |
||||
|
Loading…
Reference in new issue