Skip to content

Commit 215c308

Browse files
committed
Remove current_organization magic in order to use standard REST urls
1 parent 252e890 commit 215c308

17 files changed

+110
-97
lines changed

app/controllers/application_controller.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class ApplicationController < ActionController::Base
99
append_before_filter :check_for_terms_acceptance!, unless: :devise_controller?
1010
before_filter :configure_permitted_parameters, if: :devise_controller?
1111
before_filter :set_locale
12-
before_filter :set_current_organization
1312
after_filter :store_location
1413

1514
rescue_from MissingTOSAcceptance, OutadedTOSAcceptance do
@@ -18,22 +17,14 @@ class ApplicationController < ActionController::Base
1817

1918
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
2019

21-
helper_method :current_organization, :admin?, :superadmin?
20+
helper_method :admin?, :superadmin?
2221

2322
protected
2423

2524
def configure_permitted_parameters
2625
devise_parameter_sanitizer.for(:sign_up) << :username
2726
end
2827

29-
def set_current_organization
30-
if org_id = session[:current_organization_id]
31-
@current_organization = Organization.find(org_id)
32-
elsif current_user
33-
@current_organization = current_user.organizations.first
34-
end
35-
end
36-
3728
def store_location
3829
# store last url - this is needed for post-login redirect to whatever the
3930
# user last visited.
@@ -66,20 +57,8 @@ def check_for_terms_acceptance!
6657
end
6758
end
6859

69-
def current_organization
70-
@current_organization ||= current_user.try(:organizations).try(:first)
71-
end
72-
73-
def current_member
74-
@current_member ||= current_user.as_member_of(current_organization) if current_user
75-
end
76-
77-
def pundit_user
78-
current_member
79-
end
80-
8160
def admin?
82-
current_user.try :manages?, current_organization
61+
current_user.try :manages?, @organization
8362
end
8463

8564
def superadmin?

app/controllers/members_controller.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
class MembersController < ApplicationController
22
before_filter :authenticate_user!
3+
before_filter :load_organization
4+
5+
# TODO: move to abstract controller for all nested resources
6+
# TODO: check authorization
7+
#
8+
def load_organization
9+
@organization = Organization.find_by_id(params[:id])
10+
11+
raise not_found unless @organization
12+
13+
@organization
14+
end
315

416
def destroy
517
find_member
@@ -34,11 +46,11 @@ def toggle_active
3446
private
3547

3648
def find_member
37-
@member ||= current_organization.members.find(params[:id])
49+
@member ||= @organization.members.find(params[:id])
3850
end
3951

4052
def toggle_active_posts
41-
current_organization.posts.where(user_id: @member.user_id).
53+
@organization.posts.where(user_id: @member.user_id).
4254
each { |post| post.update_attributes(active: false) }
4355
end
4456
end

app/controllers/organizations_controller.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
class OrganizationsController < ApplicationController
2-
before_filter :load_resource
3-
4-
def load_resource
5-
if params[:id]
6-
@organization = Organization.find(params[:id])
7-
else
8-
@organizations = Organization.all
9-
end
10-
end
2+
before_filter :load_resource, only: [:show, :update, :destroy, :give_time]
113

124
def new
135
@organization = Organization.new
146
end
157

8+
# TODO: define which organizations we should display
9+
#
1610
def index
17-
@organizations = @organizations.matching(params[:q]) if params[:q].present?
11+
context = Organization.all
12+
context = context.matching(params[:q]) if params[:q].present?
13+
14+
@organizations = context
1815
end
1916

2017
def show
@@ -56,13 +53,6 @@ def give_time
5653
@sources = find_transfer_sources_for_admin
5754
end
5855

59-
def set_current
60-
if current_user
61-
session[:current_organization_id] = @organization.id
62-
end
63-
redirect_to root_path
64-
end
65-
6656
private
6757

6858
def organization_params
@@ -71,8 +61,18 @@ def organization_params
7161
neighborhood city domain])
7262
end
7363

64+
def load_resource
65+
@organization = Organization.find_by_id(params[:id])
66+
67+
raise unless @organization
68+
69+
# TODO: authorize
70+
71+
@organization
72+
end
73+
7474
def find_transfer_offer
75-
current_organization.offers.
75+
@organization.offers.
7676
find(params[:offer]) if params[:offer].present?
7777
end
7878

app/controllers/posts_controller.rb

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class PostsController < ApplicationController
2+
before_filter :load_organization
3+
24
has_scope :by_category, as: :cat
35
has_scope :tagged_with, as: :tag
46
has_scope :by_organization, as: :org
@@ -11,9 +13,9 @@ def index
1113
type: "phrase_prefix",
1214
fields: ["title^2", "description", "tags^2"]
1315
} } ]
14-
if current_organization.present?
16+
if @organization.present?
1517
# filter by organization
16-
must << { term: { organization_id: { value: current_organization.id } } }
18+
must << { term: { organization_id: { value: @organization.id } } }
1719
end
1820
posts = model.__elasticsearch__.search(
1921
query: {
@@ -24,8 +26,8 @@ def index
2426
).page(params[:page]).per(25).records
2527
else
2628
posts = model.active.of_active_members
27-
if current_organization.present?
28-
posts = posts.merge(current_organization.posts)
29+
if @organization.present?
30+
posts = posts.merge(@organization.posts)
2931
end
3032
posts = apply_scopes(posts).page(params[:page]).per(25)
3133
end
@@ -40,23 +42,23 @@ def new
4042

4143
def create
4244
post = model.new(post_params)
43-
post.organization = current_organization
45+
post.organization = @organization
4446
if post.save
45-
redirect_to send("#{resource}_path", post)
47+
redirect_to polymorphic_url([@organization, post])
4648
else
4749
instance_variable_set("@#{resource}", post)
4850
render action: :new
4951
end
5052
end
5153

5254
def edit
53-
post = current_organization.posts.find params[:id]
55+
post = @organization.posts.find params[:id]
5456
instance_variable_set("@#{resource}", post)
5557
end
5658

5759
def show
5860
scope = if current_user.present?
59-
current_organization.posts.active.of_active_members
61+
@organization.posts.active.of_active_members
6062
else
6163
model.all.active.of_active_members
6264
end
@@ -65,34 +67,36 @@ def show
6567
end
6668

6769
def update
68-
post = current_organization.posts.find params[:id]
70+
post = @organization.posts.find params[:id]
6971
authorize post
7072
instance_variable_set("@#{resource}", post)
7173
if post.update_attributes(post_params)
72-
redirect_to post
74+
redirect_to polymorphic_url([@organization, post])
7375
else
7476
render action: :edit, status: :unprocessable_entity
7577
end
7678
end
7779

7880
def destroy
79-
post = current_organization.posts.find params[:id]
81+
post = @organization.posts.find params[:id]
8082
authorize post
81-
redirect_to send("#{resources}_path") if post.update!(active: false)
83+
redirect_to polymorphic_url([@organization, resources]) if post.update!(active: false)
8284
end
8385

8486
private
8587

88+
# TODO: Investigate why we need this
8689
def resource
8790
controller_name.singularize
8891
end
8992

93+
# TODO: Investigate why we need this
9094
def resources
9195
controller_name
9296
end
9397

9498
def set_user_id(p)
95-
if current_user.manages?(current_organization)
99+
if current_user.manages?(@organization)
96100
p.update publisher_id: current_user.id
97101
p.reverse_merge! user_id: current_user.id
98102
else
@@ -109,4 +113,15 @@ def post_params
109113
set_user_id(p)
110114
end
111115
end
116+
117+
# TODO: move to abstract controller for all nested resources
118+
# TODO: check authorization
119+
#
120+
def load_organization
121+
@organization = Organization.find_by_id(params[:organization_id])
122+
123+
raise not_found unless @organization
124+
125+
@organization
126+
end
112127
end

app/controllers/users_controller.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
class UsersController < ApplicationController
22
before_filter :authenticate_user!
3+
before_filter :load_organization, only: [:index]
4+
5+
# TODO: move to abstract controller for all nested resources
6+
def load_organization
7+
@organization = Organization.find_by_id(params[:organization_id])
8+
9+
raise not_found unless @organization
10+
11+
@organization
12+
end
313

414
def scoped_users
5-
current_organization.users
15+
@organization.users
616
end
717

818
def index
919
@users = scoped_users
10-
@memberships = current_organization.members.
20+
@memberships = @organization.members.
1121
where(user_id: @users.map(&:id)).
1222
includes(:account).each_with_object({}) do |mem, ob|
1323
ob[mem.user_id] = mem
@@ -16,7 +26,9 @@ def index
1626

1727
def show
1828
@user = find_user
19-
@member = @user.as_member_of(current_organization)
29+
authorize @user
30+
31+
@member = @user.as_member_of(@organization)
2032
@movements = @member.movements.order("created_at DESC").page(params[:page]).
2133
per(10)
2234
end
@@ -42,7 +54,7 @@ def create
4254
@user.setup_and_save_user
4355

4456
if @user.persisted?
45-
@user.tune_after_persisted(current_organization)
57+
@user.tune_after_persisted(@organization)
4658
redirect_to_after_create
4759
else
4860
@user.email = "" if empty_email
@@ -64,7 +76,7 @@ def update
6476
def give_time
6577
@user = scoped_users.find(params[:id])
6678
@destination = @user.members.
67-
find_by(organization: current_organization).account.id
79+
find_by(organization: @organization).account.id
6880
@source = find_transfer_source
6981
@offer = find_transfer_offer
7082
@transfer = Transfer.new(source: @source,
@@ -86,19 +98,19 @@ def user_params
8698
end
8799

88100
def find_transfer_offer
89-
current_organization.offers.
101+
@organization.offers.
90102
find(params[:offer]) if params[:offer].present?
91103
end
92104

93105
def find_transfer_source
94106
current_user.members.
95-
find_by(organization: current_organization).account.id
107+
find_by(organization: @organization).account.id
96108
end
97109

98110
def find_transfer_sources_for_admin
99111
return unless admin?
100-
[current_organization.account] +
101-
current_organization.member_accounts.where("members.active is true")
112+
[@organization.account] +
113+
@organization.member_accounts.where("members.active is true")
102114
end
103115

104116
def find_user
@@ -110,7 +122,7 @@ def find_user
110122
end
111123

112124
def redirect_to_after_create
113-
id = @user.member(current_organization).member_uid
125+
id = @user.member(@organization).member_uid
114126
if params[:more]
115127
redirect_to new_user_path,
116128
notice: I18n.t("users.new.user_created_add",

app/helpers/posts_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
module PostsHelper
22
# Returns the right path to index list depending on type of post
3+
#
4+
# TODO: doesn't Rails URL helpers already provide this?
35
def get_index_path(post, hparams)
46
klass = post.class
7+
hparams[:organization_id] = @organization.id
58

69
case
710
when klass == String
8-
post.eql?("offers") ? offers_path(hparams) : inquiries_path(hparams)
11+
post.eql?("offers") ? organization_offers_path(hparams) : organization_inquiries_path(hparams)
912
else
10-
post.type.eql?("Offer") ? offers_path(hparams) : inquiries_path(hparams)
13+
post.type.eql?("Offer") ? organization_offers_path(hparams) : organization_inquiries_path(hparams)
1114
end
1215
end
1316
end

0 commit comments

Comments
 (0)