-
Notifications
You must be signed in to change notification settings - Fork 69
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
base: develop
Are you sure you want to change the base?
Changes from all commits
94f1aab
7b38ec5
bca8028
c2407e6
644435e
bedceea
0bdae4c
ead6d70
980cd1f
a56dab6
389fafc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? | ||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -34,11 +46,11 @@ def toggle_active | |
private | ||
|
||
def find_member | ||
@member ||= current_organization.members.find(params[:id]) | ||
@member ||= @organization.members.find(params[:id]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. find_by_id or rescue meee 🎤 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not used? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 | ||
|
@@ -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: { | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
@organization | ||
end | ||
end |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @sseerrggii 😬