Skip to content

Commit c4fb4aa

Browse files
authored
Merge pull request #384 from coopdevs/feature/set-current-org-on-post-show
Automatically switch to post's organization on post show
2 parents 1af054d + f1f8b80 commit c4fb4aa

File tree

10 files changed

+262
-81
lines changed

10 files changed

+262
-81
lines changed

app/controllers/application_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ApplicationController < ActionController::Base
1717
end
1818

1919
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
20+
rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
2021

2122
helper_method :current_organization, :admin?, :superadmin?
2223

@@ -118,4 +119,8 @@ def user_not_authorized
118119
flash[:error] = "You are not authorized to perform this action."
119120
redirect_to(request.referrer || root_path)
120121
end
122+
123+
def resource_not_found
124+
render 'errors/not_found', status: 404
125+
end
121126
end

app/controllers/posts_controller.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def edit
5757
instance_variable_set("@#{resource}", post)
5858
end
5959

60+
# GET /offers/:id
61+
# GET /inquiries/:id
62+
#
6063
def show
61-
scope = if current_user.present?
62-
current_organization.posts.active.of_active_members
63-
else
64-
model.all.active.of_active_members
65-
end
66-
post = scope.find params[:id]
64+
post = Post.active.of_active_members.find(params[:id])
65+
update_current_organization!(post.organization)
66+
6767
instance_variable_set("@#{resource}", post)
6868
end
6969

@@ -115,4 +115,20 @@ def post_params
115115
set_user_id(p)
116116
end
117117
end
118+
119+
# TODO: remove this horrible hack ASAP
120+
#
121+
# This hack set the current organization to the post's
122+
# organization, both in session and controller instance variable.
123+
#
124+
# Before changing the current organization it's important to check that
125+
# the current_user is an active member of the organization.
126+
#
127+
# @param organization [Organization]
128+
def update_current_organization!(organization)
129+
return unless current_user && current_user.active?(organization)
130+
131+
session[:current_organization_id] = organization.id
132+
@current_organization = organization
133+
end
118134
end

app/policies/application_policy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class ApplicationPolicy
22
attr_reader :member, :user, :organization, :record
33

4+
# TODO: Investigate how to just pass current_user here.
5+
# Probably this will be solved by scoping the resources
6+
# under `/organization`.
7+
#
48
def initialize(member, record)
59
@member = member
610
@user = member.user if member

app/views/inquiries/show.html.erb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
<p class="actions text-right">
2-
<% if admin? || @inquiry.user == current_user %>
3-
<%= link_to edit_inquiry_path(@inquiry), class: "btn btn-warning" do %>
4-
<%= glyph :pencil %>
5-
<%= t "global.edit" %>
1+
<% if @inquiry.organization == current_organization %>
2+
<p class="actions text-right">
3+
<% if admin? or @inquiry.user == current_user %>
4+
<%= render 'shared/post_actions', post: @inquiry %>
65
<% end %>
7-
<%= link_to @inquiry,
8-
data: { method: :delete, confirm: "sure?" },
9-
class: "btn btn-danger" do %>
10-
<%= glyph :trash %>
11-
<%= t "global.delete" %>
12-
<% end %>
13-
<% end %>
14-
</p>
6+
</p>
7+
<% end %>
158
<%= render "shared/post", post: @inquiry %>

app/views/offers/show.html.erb

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
<p class="actions text-right">
2-
<% if admin? or @offer.user == current_user %>
3-
<%= link_to edit_offer_path(@offer), class: "btn btn-warning" do %>
4-
<%= glyph :pencil %>
5-
<%= t "global.edit" %>
1+
<% if @offer.organization == current_organization %>
2+
<p class="actions text-right">
3+
<% if admin? or @offer.user == current_user %>
4+
<%= render 'shared/post_actions', post: @offer %>
65
<% end %>
7-
<%= link_to @offer,
8-
data: { method: :DELETE, confirm: "sure?" },
9-
class: "btn btn-danger" do %>
10-
<%= glyph :trash %>
11-
<%= t "global.delete" %>
6+
<% if current_user and @offer.user != current_user %>
7+
<%= link_to new_transfer_path(id: @offer.user.id, offer: @offer.id, destination_account_id: @destination_account.id),
8+
class: "btn btn-success" do %>
9+
<%= glyph :time %>
10+
<%= t ".give_time_for" %>
11+
<% end %>
1212
<% end %>
13-
<% end %>
14-
<% if current_user and @offer.user != current_user %>
15-
<%= link_to new_transfer_path(id: @offer.user.id, offer: @offer.id, destination_account_id: @destination_account.id),
16-
class: "btn btn-success" do %>
17-
<%= glyph :time %>
18-
<%= t ".give_time_for" %>
19-
<% end %>
20-
<% end %>
21-
</p>
13+
</p>
14+
<% end %>
2215
<%= render "shared/post", post: @offer %>

app/views/shared/_post.html.erb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<% end %>
2828
</div>
2929
</div>
30-
<% if current_user && current_organization %>
30+
<% if current_user && current_organization == post.organization %>
3131
<div class="panel panel-info">
3232
<div class="panel-heading">
3333
<h3 class="panel-title">
@@ -60,13 +60,17 @@
6060
</div>
6161
</div>
6262
</div>
63+
<% if !current_user || post.organization != current_organization %>
64+
<div class="alert alert-info">
65+
<%= t 'posts.show.info',
66+
type: post.class.model_name.human,
67+
organization: post.organization.name %>
68+
</div>
69+
<% end %>
6370
<% unless current_user %>
6471
<div class="alert alert-info">
65-
<%= t "posts.show.info",
66-
type: post.class.model_name.human,
67-
organization: post.organization.name %>
6872
<%= link_to t("layouts.application.login"),
69-
new_user_session_path,
70-
class: "btn btn-primary" %>
73+
new_user_session_path,
74+
class: "btn btn-primary" %>
7175
</div>
7276
<% end %>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%= link_to post, class: "btn btn-warning" do %>
2+
<%= glyph :pencil %>
3+
<%= t "global.edit" %>
4+
<% end %>
5+
6+
<%= link_to post, data: { method: :delete, confirm: "sure?" }, class: "btn btn-danger" do %>
7+
<%= glyph :trash %>
8+
<%= t "global.delete" %>
9+
<% end %>

config/locales/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ en:
407407
impulsem-link: "Impulsem el que fas"
408408
posts:
409409
show:
410-
info: "%{type} of %{organization} to see person's details you have to"
410+
info: "This %{type} belongs to %{organization}."
411411
reports:
412412
cat_with_users:
413413
title: Offered Services

spec/controllers/offers_controller_spec.rb

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
require "spec_helper"
22

3-
describe OffersController, type: :controller do
4-
let(:test_organization) { Fabricate(:organization) }
5-
let(:member) { Fabricate(:member, organization: test_organization) }
6-
let(:another_member) { Fabricate(:member, organization: test_organization) }
3+
RSpec.describe OffersController, type: :controller do
4+
let(:organization) { Fabricate(:organization) }
5+
let(:member) { Fabricate(:member, organization: organization) }
6+
let(:another_member) { Fabricate(:member, organization: organization) }
77
let(:yet_another_member) { Fabricate(:member) }
88
let(:test_category) { Fabricate(:category) }
99
let!(:offer) do
1010
Fabricate(:offer,
1111
user: member.user,
12-
organization: test_organization,
12+
organization: organization,
1313
category: test_category)
1414
end
1515

@@ -59,29 +59,105 @@
5959
end
6060
end
6161

62-
describe "GET #show" do
63-
context "with valid params" do
64-
context "with a logged user" do
65-
before { login(another_member.user) }
66-
67-
it "assigns the requested offer to @offer" do
68-
get :show, id: offer.id
69-
expect(assigns(:offer)).to eq(offer)
62+
describe 'GET #show' do
63+
context 'when the user is logged in' do
64+
before { login(another_member.user) }
65+
66+
context 'when the requested offer' do
67+
context 'is not active' do
68+
before do
69+
offer.active = false
70+
offer.save!
71+
end
72+
73+
it 'renders the 404 page' do
74+
get :show, id: offer.id
75+
expect(response.status).to eq(404)
76+
end
7077
end
7178

72-
it 'assigns the account destination of the transfer' do
73-
get :show, id: offer.id
74-
expect(assigns(:destination_account)).to eq(member.account)
79+
context 'is active' do
80+
context 'and the user that created the offer is not active anymore' do
81+
before do
82+
member.active = false
83+
member.save!
84+
end
85+
86+
it 'renders the 404 page' do
87+
get :show, id: offer.id
88+
expect(response.status).to eq(404)
89+
end
90+
end
91+
92+
context 'and the user that created the offer is active' do
93+
it 'renders a successful response' do
94+
get :show, id: offer.id
95+
expect(response.status).to eq(200)
96+
end
97+
98+
it 'assigns the requested offer to @offer' do
99+
get :show, id: offer.id
100+
expect(assigns(:offer)).to eq(offer)
101+
end
102+
103+
it 'assigns the account destination of the transfer' do
104+
get :show, id: offer.id
105+
expect(assigns(:destination_account)).to eq(member.account)
106+
end
107+
108+
it 'displays the offer\'s user details' do
109+
get :show, id: offer.id
110+
expect(response.body).to include(offer.user.email)
111+
end
112+
end
75113
end
76114
end
77115

78-
context "without a logged in user" do
79-
it "assigns the requested offer to @offer" do
80-
get :show, id: offer.id
81-
expect(assigns(:offer)).to eq(offer)
116+
context 'when the user pertains to multiple organizations' do
117+
context 'and user\'s current organization is different than offer\'s organization' do
118+
let(:another_organization) { Fabricate(:organization) }
119+
120+
before do
121+
Fabricate(:member, user: another_member.user, organization: another_organization)
122+
allow(controller).to receive(:@current_organization).and_return(another_organization)
123+
end
124+
125+
it 'displays the offer\'s user details' do
126+
get :show, id: offer.id
127+
expect(response.body).to include(offer.user.email)
128+
end
129+
130+
it 'sets the offer\'s organization as user\'s current organization' do
131+
get :show, id: offer.id
132+
expect(session[:current_organization_id]).to eq(offer.organization_id)
133+
expect(assigns(:current_organization)).to eq(offer.organization)
134+
end
82135
end
83136
end
84137
end
138+
139+
context 'when the user is not a member of the organization where the offer is published' do
140+
let(:another_user) { Fabricate(:user) }
141+
142+
before { login(another_user) }
143+
144+
it 'doesn\'t display the offer\'s user details' do
145+
get :show, id: offer.id
146+
expect(response.body).to_not include(offer.user.email)
147+
end
148+
end
149+
150+
context 'when the user is not logged in' do
151+
it 'assigns the requested offer to @offer' do
152+
get :show, id: offer.id
153+
expect(assigns(:offer)).to eq(offer)
154+
end
155+
156+
it 'doesn\'t display the offer\'s user details' do
157+
get :show, id: offer.id
158+
expect(response.body).to_not include(offer.user.email)
159+
end
160+
end
85161
end
86162

87163
describe "POST #create" do

0 commit comments

Comments
 (0)