Skip to content

Commit 721115e

Browse files
committed
adds petition button on organizations/show page, plus bug fix and security fix
1 parent 12affd6 commit 721115e

File tree

6 files changed

+86
-22
lines changed

6 files changed

+86
-22
lines changed

app/controllers/petitions_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class PetitionsController < ApplicationController
33

44
def create
55
petition = Petition.new petition_params
6+
petition.status = "pending"
67

78
if petition.save
89
OrganizationNotifier.new_petition(petition).deliver_now
@@ -13,7 +14,7 @@ def create
1314
flash[:error] = t('errors.internal_server_error.description')
1415
end
1516

16-
redirect_to organizations_path
17+
redirect_back fallback_location: organization_path(petition.organization)
1718
end
1819

1920
def update
@@ -38,6 +39,6 @@ def manage
3839
private
3940

4041
def petition_params
41-
params.permit(%i[organization_id user_id status])
42+
params.permit(%i[organization_id user_id])
4243
end
4344
end

app/views/organizations/_organizations_row.html.erb

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,6 @@
55
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
66
<td><%= org.members.count %></td>
77
<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 t('users.user_rows.delete_membership'),
13-
member,
14-
method: :delete,
15-
data: { confirm: t('users.user_rows.sure_delete', organization_name: org.name) },
16-
class: 'btn btn-danger' %>
17-
<% elsif petition && !current_user.was_member?(petition) %>
18-
<span class="badge"><%= petition.status %></span>
19-
<% else %>
20-
<%= link_to t('petitions.apply'),
21-
petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'),
22-
method: :post,
23-
class: 'btn btn-default' %>
24-
<% end %>
25-
<% end %>
8+
<%= render "organizations/petition_button", organization: org %>
269
</td>
2710
</tr>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<% if current_user %>
2+
<% petition = current_user.petitions.where(organization_id: organization.id).last %>
3+
4+
<% if member = Member.where(user: current_user, organization: organization).first %>
5+
<%= link_to t('users.user_rows.delete_membership'),
6+
member,
7+
method: :delete,
8+
data: { confirm: t('users.user_rows.sure_delete', organization_name: organization.name) },
9+
class: 'btn btn-danger' %>
10+
<% elsif petition && !current_user.was_member?(petition) %>
11+
<span class="badge"><%= petition.status %></span>
12+
<% else %>
13+
<%= link_to t('petitions.apply'),
14+
petitions_path(user_id: current_user.id, organization_id: organization.id),
15+
method: :post,
16+
class: 'btn btn-default' %>
17+
<% end %>
18+
<% end %>

app/views/organizations/show.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
</div>
8585
<div class="col-sm-5">
8686
<ul class="nav nav-pills pull-right">
87-
<% if admin? %>
87+
<% if current_user&.manages?(@organization) %>
8888
<li>
8989
<%= link_to edit_organization_path(@organization) do %>
9090
<%= glyph :pencil %>
@@ -101,6 +101,7 @@
101101
</li>
102102
<% end %>
103103
</ul>
104+
<%= render "organizations/petition_button", organization: @organization %>
104105
</div>
105106
</div>
106107

spec/controllers/petitions_controller_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
before { login(user) }
88

99
it 'creates the petition' do
10+
request.env['HTTP_REFERER'] = organizations_path
11+
1012
expect do
1113
post :create, params: { user_id: user.id, organization_id: organization.id }
1214
end.to change(Petition, :count).by(1)
15+
expect(response).to redirect_to(organizations_path)
1316
end
1417
end
1518

@@ -35,7 +38,7 @@
3538

3639
describe 'GET #manage' do
3740
before do
38-
allow(controller).to receive(:current_organization) { organization }
41+
allow(controller).to receive(:current_organization) { organization }
3942
login(admin.user)
4043
end
4144
let!(:petition) { Petition.create(user: user, organization: organization, status: 'pending') }

spec/views/organizations/show.html.erb_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,63 @@
5959
it 'displays the organization page' do
6060
expect(rendered).to match(organization.name)
6161
end
62+
63+
it 'displays link to delete the member' do
64+
expect(rendered).to have_link(
65+
t('users.user_rows.delete_membership'),
66+
href: member_path(member)
67+
)
68+
end
69+
end
70+
71+
context 'with a logged user (but not organization member)' do
72+
let(:user) { Fabricate(:user) }
73+
74+
before do
75+
allow(view).to receive(:current_user).and_return(user)
76+
77+
assign :movements, Movement.page
78+
render template: 'organizations/show'
79+
end
80+
81+
it 'displays link to create petition' do
82+
expect(rendered).to have_link(
83+
t('petitions.apply'),
84+
href: petitions_path(user_id: user.id, organization_id: organization.id)
85+
)
86+
end
87+
end
88+
89+
context 'with a logged admin' do
90+
let(:admin) { Fabricate(:member, organization: organization, manager: true) }
91+
let(:user) { admin.user }
92+
93+
before do
94+
allow(view).to receive(:current_user).and_return(user)
95+
96+
assign :movements, Movement.page
97+
render template: 'organizations/show'
98+
end
99+
100+
it 'has link to edit organization' do
101+
expect(rendered).to have_link(t('global.edit'), href: edit_organization_path(organization))
102+
end
103+
end
104+
105+
context 'with a logged admin from other organization' do
106+
let(:other_organization) { Fabricate(:organization) }
107+
let(:admin) { Fabricate(:member, organization: other_organization, manager: true) }
108+
let(:user) { admin.user }
109+
110+
before do
111+
allow(view).to receive(:current_user).and_return(user)
112+
113+
assign :movements, Movement.page
114+
render template: 'organizations/show'
115+
end
116+
117+
it 'does not have link to edit organization' do
118+
expect(rendered).to_not have_link(t('global.edit'), href: edit_organization_path(organization))
119+
end
62120
end
63121
end

0 commit comments

Comments
 (0)