Skip to content

Scope resources under organization resource #250

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

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
27 changes: 3 additions & 24 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ApplicationController < ActionController::Base
append_before_filter :check_for_terms_acceptance!, unless: :devise_controller?
before_filter :configure_permitted_parameters, if: :devise_controller?
before_filter :set_locale
before_filter :set_current_organization
after_filter :store_location

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

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

helper_method :current_organization, :admin?, :superadmin?
helper_method :admin?, :superadmin?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end

def set_current_organization
if org_id = session[:current_organization_id]
@current_organization = Organization.find(org_id)
elsif current_user
@current_organization = current_user.organizations.first
end
end

def store_location
# store last url - this is needed for post-login redirect to whatever the
# user last visited.
Expand All @@ -47,7 +38,7 @@ def store_location

def after_sign_in_path_for(user)
if user.members.present?
users_path
root_path
else
page_path("about")
end
Expand All @@ -66,20 +57,8 @@ def check_for_terms_acceptance!
end
end

def current_organization
@current_organization ||= current_user.try(:organizations).try(:first)
end

def current_member
@current_member ||= current_user.as_member_of(current_organization) if current_user
end

def pundit_user
current_member
end

def admin?
current_user.try :manages?, current_organization
current_user.try :manages?, @organization
end

def superadmin?
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
class HomeController < ApplicationController

# TODO: what happens when the user doesn't pertain to any organization?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would redirect them to a page where we tell them to join some organization. Do we have something alike @sseerrggii ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @sseerrggii 😬

def index
redirect_to :users if current_user
return unless current_user

organization_id = current_user.organization_ids.first

redirect_to organization_path(id: organization_id)
end
end
16 changes: 14 additions & 2 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
class MembersController < ApplicationController
before_filter :authenticate_user!
before_filter :load_organization

# TODO: move to abstract controller for all nested resources
# TODO: check authorization
#
def load_organization
@organization = Organization.find_by_id(params[:id])

raise not_found unless @organization

@organization
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could achive the same behaviour without needing this code if we rescued ActiveRecord::RecordNotFound in a rescue_from block. Furthermore, this would work for any model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sure. We need to move somewhere else and DRY it a bit firts though.


def destroy
find_member
Expand Down Expand Up @@ -34,11 +46,11 @@ def toggle_active
private

def find_member
@member ||= current_organization.members.find(params[:id])
@member ||= @organization.members.find(params[:id])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_by_id or rescue meee 🎤

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uff this repo is full of this kind of stuff :(

Yep, will do. but maybe once I finish to move the resources under the /organizations scope.

end

def toggle_active_posts
current_organization.posts.where(user_id: @member.user_id).
@organization.posts.where(user_id: @member.user_id).
each { |post| post.update_attributes(active: false) }
end
end
16 changes: 0 additions & 16 deletions app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
# Managems of offer-type posts
#
class OffersController < PostsController
def model
Offer
end

def dashboard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not anymore.

initial_scope =
if current_organization
current_organization.offers.active.of_active_members
else
Offer.all.active.of_active_members
end
@offers = Category.all.sort_by { |a| a.name.downcase }.
each_with_object({}) do |category, offers|
list = initial_scope.merge(category.posts).limit(5)
offers[category] = list if list.present?
end
end
end
40 changes: 21 additions & 19 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
class OrganizationsController < ApplicationController
before_filter :load_resource

def load_resource
if params[:id]
@organization = Organization.find(params[:id])
else
@organizations = Organization.all
end
end
before_filter :load_resource, only: [:show, :update, :destroy, :give_time]

def new
@organization = Organization.new
end

# TODO: define which organizations we should display
#
def index
@organizations = @organizations.matching(params[:q]) if params[:q].present?
context = Organization.all
context = context.matching(params[:q]) if params[:q].present?

@organizations = context
end

def show
Expand All @@ -27,7 +24,7 @@ def show
end

def create
@organization = @organizations.build organization_params
@organization = Organization.new(organization_params)
if @organization.save
redirect_to @organization, status: :created
else
Expand All @@ -48,6 +45,8 @@ def destroy
redirect_to organizations_path, notice: "deleted"
end

# Transfer time to the current organization
#
def give_time
@destination = @organization.account.id
@source = find_transfer_source
Expand All @@ -56,13 +55,6 @@ def give_time
@sources = find_transfer_sources_for_admin
end

def set_current
if current_user
session[:current_organization_id] = @organization.id
end
redirect_to root_path
end

private

def organization_params
Expand All @@ -71,8 +63,18 @@ def organization_params
neighborhood city domain])
end

def load_resource
@organization = Organization.find_by_id(params[:id])

raise unless @organization

# TODO: authorize

@organization
end

def find_transfer_offer
current_organization.offers.
@organization.offers.
find(params[:offer]) if params[:offer].present?
end

Expand Down
41 changes: 28 additions & 13 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class PostsController < ApplicationController
before_filter :load_organization

has_scope :by_category, as: :cat
has_scope :tagged_with, as: :tag
has_scope :by_organization, as: :org
Expand All @@ -11,9 +13,9 @@ def index
type: "phrase_prefix",
fields: ["title^2", "description", "tags^2"]
} } ]
if current_organization.present?
if @organization.present?
# filter by organization
must << { term: { organization_id: { value: current_organization.id } } }
must << { term: { organization_id: { value: @organization.id } } }
end
posts = model.__elasticsearch__.search(
query: {
Expand All @@ -24,8 +26,8 @@ def index
).page(params[:page]).per(25).records
else
posts = model.active.of_active_members
if current_organization.present?
posts = posts.merge(current_organization.posts)
if @organization.present?
posts = posts.merge(@organization.posts)
end
posts = apply_scopes(posts).page(params[:page]).per(25)
end
Expand All @@ -40,23 +42,23 @@ def new

def create
post = model.new(post_params)
post.organization = current_organization
post.organization = @organization
if post.save
redirect_to send("#{resource}_path", post)
redirect_to polymorphic_url([@organization, post])
else
instance_variable_set("@#{resource}", post)
render action: :new
end
end

def edit
post = current_organization.posts.find params[:id]
post = @organization.posts.find params[:id]
instance_variable_set("@#{resource}", post)
end

def show
scope = if current_user.present?
current_organization.posts.active.of_active_members
@organization.posts.active.of_active_members
else
model.all.active.of_active_members
end
Expand All @@ -65,34 +67,36 @@ def show
end

def update
post = current_organization.posts.find params[:id]
post = @organization.posts.find params[:id]
authorize post
instance_variable_set("@#{resource}", post)
if post.update_attributes(post_params)
redirect_to post
redirect_to polymorphic_url([@organization, post])
else
render action: :edit, status: :unprocessable_entity
end
end

def destroy
post = current_organization.posts.find params[:id]
post = @organization.posts.find params[:id]
authorize post
redirect_to send("#{resources}_path") if post.update!(active: false)
redirect_to polymorphic_url([@organization, resources]) if post.update!(active: false)
end

private

# TODO: Investigate why we need this
def resource
controller_name.singularize
end

# TODO: Investigate why we need this
def resources
controller_name
end

def set_user_id(p)
if current_user.manages?(current_organization)
if current_user.manages?(@organization)
Copy link
Collaborator

@sauloperez sauloperez Jun 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could avoid lots of these ivar changes by implemanting the getter method in the application controller as:

def current_organization
  @organization
end

This would reduce the number of changes considerably and increase the readability of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is part of the abstracion part, moving the loading/authorizing of the organization somewhere else and DRY it.

p.update publisher_id: current_user.id
p.reverse_merge! user_id: current_user.id
else
Expand All @@ -109,4 +113,15 @@ def post_params
set_user_id(p)
end
end

# TODO: move to abstract controller for all nested resources
# TODO: check authorization
#
def load_organization
@organization = Organization.find_by_id(params[:organization_id])

raise not_found unless @organization
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


@organization
end
end
Loading