Skip to content

Commit 7c8aa24

Browse files
authored
[Feat] Manage memberships (#665)
1 parent d91a32b commit 7c8aa24

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

app/controllers/members_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class MembersController < ApplicationController
22
before_action :authenticate_user!
33

44
def destroy
5-
find_member
5+
@member = Member.find(params[:id])
66
toggle_active_posts
77
@member.destroy
88

9-
redirect_to manage_users_path
9+
redirect_to request.referer.include?(organizations_path) ? organizations_path : manage_users_path
1010
end
1111

1212
def toggle_manager

app/controllers/organizations_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ class OrganizationsController < ApplicationController
22
before_action :load_resource, only: [:show, :edit, :update, :set_current]
33

44
def index
5-
organizations = Organization.all
5+
if current_user
6+
user_organizations = Organization.left_outer_joins(:petitions).where(petitions: { user_id: current_user.id })
7+
@user_organizations = user_organizations.or(Organization.where(id: current_user.organizations.pluck(:id))).distinct
8+
end
9+
10+
organizations = Organization.where.not(id: @user_organizations&.pluck(:id))
611
organizations = organizations.search_by_query(params[:q]) if params[:q].present?
712
@organizations = organizations.page(params[:page]).per(25)
813
end

app/controllers/petitions_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def create
1010

1111
redirect_to organizations_path
1212
end
13-
13+
1414
def update
1515
petition = Petition.find params[:id]
1616
status = params[:status]
17-
17+
1818
if petition.update(status: status)
1919
User.find(params[:user_id]).add_to_organization(current_organization) if status == 'accepted'
2020
flash[:notice] = "Application #{status}"
@@ -24,7 +24,7 @@ def update
2424

2525
redirect_to manage_petitions_path
2626
end
27-
27+
2828
def manage
2929
@status = params[:status] || 'pending'
3030
@users = User.joins(:petitions).where(petitions: { organization_id: current_organization.id, status: @status }).page(params[:page]).per(20)

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ def has_valid_email?
124124
def email_if_real
125125
has_valid_email? ? email : ""
126126
end
127+
128+
def was_member?(petition)
129+
petition.status == 'accepted' && Member.where(organization: petition.organization, user: self).none?
130+
end
127131
end

app/views/application/menus/_user_admin_menu_items.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
<% end %>
2222
</li>
2323

24+
<li role="presentation">
25+
<%= link_to organizations_path do %>
26+
<%= glyph :list %>
27+
Manage memberships
28+
<% end %>
29+
</li>
30+
2431
<% current_user.members.where(manager: true).each do |m| %>
2532
<li role="presentation">
2633
<%= link_to m.organization do %>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<tr>
2+
<td><%= link_to(org.name, org) %></td>
3+
<td><%= org.city %></td>
4+
<td><%= org.neighborhood %></td>
5+
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
6+
<td><%= org.members.count %></td>
7+
<td>
8+
<% if current_user %>
9+
<% petition = current_user.petitions.where(organization_id: org.id).last %>
10+
11+
<% if member = Member.where(user: current_user, organization: org).first %>
12+
<%= link_to 'Delete membership',
13+
member,
14+
method: :delete,
15+
data: { confirm: "Are you sure you want to delete your membership from #{org.name}?" },
16+
class: 'btn btn-danger' %>
17+
<% elsif petition && !current_user.was_member?(petition) %>
18+
<%= petition.status %>
19+
<% else %>
20+
<%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %>
21+
<% end %>
22+
<% end %>
23+
</td>
24+
</tr>

app/views/organizations/index.html.erb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,8 @@
3131
</tr>
3232
</thead>
3333
<tbody>
34-
<% @organizations.each do |org| %>
35-
<tr>
36-
<td><%= link_to(org.name, org) %></td>
37-
<td><%= org.city %></td>
38-
<td><%= org.neighborhood %></td>
39-
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
40-
<td><%= org.members.count %></td>
41-
<td>
42-
<% if current_user %>
43-
<% if petition = current_user.petitions.where(organization_id: org.id).last %>
44-
<%= petition.status %>
45-
<% else %>
46-
<%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %>
47-
<% end %>
48-
<% end %>
49-
</td>
50-
</tr>
51-
<% end %>
34+
<%= render partial: 'organizations_row', collection: @user_organizations, as: :org if !params[:page] || params[:page] == '1' %>
35+
<%= render partial: 'organizations_row', collection: @organizations, as: :org %>
5236
</tbody>
5337
</table>
5438
<div class="paginate-align">

0 commit comments

Comments
 (0)