From 2f06411f753c2effa4926c5936f8f2edd4944c49 Mon Sep 17 00:00:00 2001 From: franpb14 Date: Sat, 26 Nov 2022 12:39:10 +0100 Subject: [PATCH 1/3] Users can delete their memberships --- app/controllers/organizations_controller.rb | 7 ++++- .../menus/_user_admin_menu_items.html.erb | 7 +++++ .../organizations/_organizations_row.html.erb | 26 +++++++++++++++++++ app/views/organizations/index.html.erb | 20 ++------------ 4 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 app/views/organizations/_organizations_row.html.erb diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 42eae556c..a27f806ad 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -2,7 +2,12 @@ class OrganizationsController < ApplicationController before_action :load_resource, only: [:show, :edit, :update, :set_current] def index - organizations = Organization.all + if current_user + user_organizations = Organization.left_outer_joins(:petitions).where(id: current_user.organizations.pluck(:id)) + @user_organizations = user_organizations.where(petitions: { user_id: current_user.id }).distinct + end + + organizations = Organization.where.not(id: @user_organizations&.pluck(:id)) organizations = organizations.search_by_query(params[:q]) if params[:q].present? @organizations = organizations.page(params[:page]).per(25) end diff --git a/app/views/application/menus/_user_admin_menu_items.html.erb b/app/views/application/menus/_user_admin_menu_items.html.erb index b145ff05d..f2d10c9b0 100644 --- a/app/views/application/menus/_user_admin_menu_items.html.erb +++ b/app/views/application/menus/_user_admin_menu_items.html.erb @@ -21,6 +21,13 @@ <% end %> +
  • + <%= link_to organizations_path do %> + <%= glyph :list %> + Manage memberships + <% end %> +
  • + <% current_user.members.where(manager: true).each do |m| %>
  • <%= link_to m.organization do %> diff --git a/app/views/organizations/_organizations_row.html.erb b/app/views/organizations/_organizations_row.html.erb new file mode 100644 index 000000000..fb492c29f --- /dev/null +++ b/app/views/organizations/_organizations_row.html.erb @@ -0,0 +1,26 @@ +<% organizations.each do |org| %> + + <%= link_to(org.name, org) %> + <%= org.city %> + <%= org.neighborhood %> + <%= link_to(org.web, org.web) if org.web.present? %> + <%= org.members.count %> + + <% if current_user %> + <% if petition = current_user.petitions.where(organization_id: org.id).last %> + <% if petition.status == 'accepted' && member = Member.where(user: current_user, organization: org).first %> + <%= link_to 'Delete membership', + member_path(member), + method: :delete, + data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" }, + class: 'btn btn-danger' %> + <% else %> + <%= petition.status %> + <% end %> + <% else %> + <%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %> + <% end %> + <% end %> + + +<% end %> diff --git a/app/views/organizations/index.html.erb b/app/views/organizations/index.html.erb index 0e4a29d6b..25aca3792 100644 --- a/app/views/organizations/index.html.erb +++ b/app/views/organizations/index.html.erb @@ -31,24 +31,8 @@ - <% @organizations.each do |org| %> - - <%= link_to(org.name, org) %> - <%= org.city %> - <%= org.neighborhood %> - <%= link_to(org.web, org.web) if org.web.present? %> - <%= org.members.count %> - - <% if current_user %> - <% if petition = current_user.petitions.where(organization_id: org.id).last %> - <%= petition.status %> - <% else %> - <%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %> - <% end %> - <% end %> - - - <% end %> + <%= render 'organizations_row', organizations: @user_organizations if !params[:page] || params[:page] == '1' %> + <%= render 'organizations_row', organizations: @organizations %>
    From eb528a7ee6d440624695d8c9be35f9ebacfedb5f Mon Sep 17 00:00:00 2001 From: franpb14 Date: Sun, 4 Dec 2022 22:26:50 +0100 Subject: [PATCH 2/3] fixes & refactor and link to apply restored after left and org --- app/controllers/members_controller.rb | 2 +- app/controllers/organizations_controller.rb | 4 +- app/controllers/petitions_controller.rb | 6 +-- app/models/user.rb | 4 ++ .../organizations/_organizations_row.html.erb | 45 +++++++++---------- app/views/organizations/index.html.erb | 4 +- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index d84b2ff49..59dd18e9d 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -6,7 +6,7 @@ def destroy toggle_active_posts @member.destroy - redirect_to manage_users_path + redirect_to request.referer.include?(organizations_path) ? organizations_path : manage_users_path end def toggle_manager diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index a27f806ad..01389658d 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -3,8 +3,8 @@ class OrganizationsController < ApplicationController def index if current_user - user_organizations = Organization.left_outer_joins(:petitions).where(id: current_user.organizations.pluck(:id)) - @user_organizations = user_organizations.where(petitions: { user_id: current_user.id }).distinct + user_organizations = Organization.left_outer_joins(:petitions).where(petitions: { user_id: current_user.id }) + @user_organizations = user_organizations.or(Organization.where(id: current_user.organizations.pluck(:id))).distinct end organizations = Organization.where.not(id: @user_organizations&.pluck(:id)) diff --git a/app/controllers/petitions_controller.rb b/app/controllers/petitions_controller.rb index c19ccea57..4ddb81cdd 100644 --- a/app/controllers/petitions_controller.rb +++ b/app/controllers/petitions_controller.rb @@ -10,11 +10,11 @@ def create redirect_to organizations_path end - + def update petition = Petition.find params[:id] status = params[:status] - + if petition.update(status: status) User.find(params[:user_id]).add_to_organization(current_organization) if status == 'accepted' flash[:notice] = "Application #{status}" @@ -24,7 +24,7 @@ def update redirect_to manage_petitions_path end - + def manage @status = params[:status] || 'pending' @users = User.joins(:petitions).where(petitions: { organization_id: current_organization.id, status: @status }).page(params[:page]).per(20) diff --git a/app/models/user.rb b/app/models/user.rb index d14700328..fd1ba0409 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -124,4 +124,8 @@ def has_valid_email? def email_if_real has_valid_email? ? email : "" end + + def was_member?(petition) + petition.status == 'accepted' && Member.where(organization: petition.organization, user: self).none? + end end diff --git a/app/views/organizations/_organizations_row.html.erb b/app/views/organizations/_organizations_row.html.erb index fb492c29f..a7afaa22d 100644 --- a/app/views/organizations/_organizations_row.html.erb +++ b/app/views/organizations/_organizations_row.html.erb @@ -1,26 +1,25 @@ -<% organizations.each do |org| %> - - <%= link_to(org.name, org) %> - <%= org.city %> - <%= org.neighborhood %> - <%= link_to(org.web, org.web) if org.web.present? %> - <%= org.members.count %> - - <% if current_user %> - <% if petition = current_user.petitions.where(organization_id: org.id).last %> - <% if petition.status == 'accepted' && member = Member.where(user: current_user, organization: org).first %> - <%= link_to 'Delete membership', - member_path(member), - method: :delete, - data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" }, - class: 'btn btn-danger' %> - <% else %> - <%= petition.status %> - <% end %> + + <%= link_to(org.name, org) %> + <%= org.city %> + <%= org.neighborhood %> + <%= link_to(org.web, org.web) if org.web.present? %> + <%= org.members.count %> + + <% if current_user %> + <% petition = current_user.petitions.where(organization_id: org.id).last %> + <% if petition && !current_user.was_member?(petition) %> + <% if petition.status == 'accepted' && member = Member.where(user: current_user, organization: org).first %> + <%= link_to 'Delete membership', + member_path(member), + method: :delete, + data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" }, + class: 'btn btn-danger' %> <% else %> - <%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %> + <%= petition.status %> <% end %> + <% else %> + <%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %> <% end %> - - -<% end %> + <% end %> + + diff --git a/app/views/organizations/index.html.erb b/app/views/organizations/index.html.erb index 25aca3792..64082bf56 100644 --- a/app/views/organizations/index.html.erb +++ b/app/views/organizations/index.html.erb @@ -31,8 +31,8 @@ - <%= render 'organizations_row', organizations: @user_organizations if !params[:page] || params[:page] == '1' %> - <%= render 'organizations_row', organizations: @organizations %> + <%= render partial: 'organizations_row', collection: @user_organizations, as: :org if !params[:page] || params[:page] == '1' %> + <%= render partial: 'organizations_row', collection: @organizations, as: :org %>
    From fab24dd7da14309d9f75afb98d0e4587d37568e6 Mon Sep 17 00:00:00 2001 From: franpb14 Date: Mon, 5 Dec 2022 21:05:24 +0100 Subject: [PATCH 3/3] bug member find fixed and without petitions can delete too --- app/controllers/members_controller.rb | 2 +- .../organizations/_organizations_row.html.erb | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index 59dd18e9d..ad585bb44 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -2,7 +2,7 @@ class MembersController < ApplicationController before_action :authenticate_user! def destroy - find_member + @member = Member.find(params[:id]) toggle_active_posts @member.destroy diff --git a/app/views/organizations/_organizations_row.html.erb b/app/views/organizations/_organizations_row.html.erb index a7afaa22d..232eb2317 100644 --- a/app/views/organizations/_organizations_row.html.erb +++ b/app/views/organizations/_organizations_row.html.erb @@ -7,16 +7,15 @@ <% if current_user %> <% petition = current_user.petitions.where(organization_id: org.id).last %> - <% if petition && !current_user.was_member?(petition) %> - <% if petition.status == 'accepted' && member = Member.where(user: current_user, organization: org).first %> - <%= link_to 'Delete membership', - member_path(member), - method: :delete, - data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" }, - class: 'btn btn-danger' %> - <% else %> - <%= petition.status %> - <% end %> + + <% if member = Member.where(user: current_user, organization: org).first %> + <%= link_to 'Delete membership', + member, + method: :delete, + data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" }, + class: 'btn btn-danger' %> + <% elsif petition && !current_user.was_member?(petition) %> + <%= petition.status %> <% else %> <%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %> <% end %>