Skip to content

[Feat] Manage memberships #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class MembersController < ApplicationController
before_action :authenticate_user!

def destroy
find_member
@member = Member.find(params[:id])
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
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(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))
organizations = organizations.search_by_query(params[:q]) if params[:q].present?
@organizations = organizations.page(params[:page]).per(25)
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/petitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions app/views/application/menus/_user_admin_menu_items.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
<% end %>
</li>

<li role="presentation">
<%= link_to organizations_path do %>
<%= glyph :list %>
Manage memberships
<% end %>
</li>

<% current_user.members.where(manager: true).each do |m| %>
<li role="presentation">
<%= link_to m.organization do %>
Expand Down
24 changes: 24 additions & 0 deletions app/views/organizations/_organizations_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<tr>
<td><%= link_to(org.name, org) %></td>
<td><%= org.city %></td>
<td><%= org.neighborhood %></td>
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
<td><%= org.members.count %></td>
<td>
<% if current_user %>
<% petition = current_user.petitions.where(organization_id: org.id).last %>

<% 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 %>
<% end %>
</td>
</tr>
20 changes: 2 additions & 18 deletions app/views/organizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,8 @@
</tr>
</thead>
<tbody>
<% @organizations.each do |org| %>
<tr>
<td><%= link_to(org.name, org) %></td>
<td><%= org.city %></td>
<td><%= org.neighborhood %></td>
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
<td><%= org.members.count %></td>
<td>
<% 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 %>
</td>
</tr>
<% end %>
<%= render partial: 'organizations_row', collection: @user_organizations, as: :org if !params[:page] || params[:page] == '1' %>
<%= render partial: 'organizations_row', collection: @organizations, as: :org %>
</tbody>
</table>
<div class="paginate-align">
Expand Down