Skip to content
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

Decouple members and users resources #260

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AccountController < ApplicationController
# GET /account
#
def show
end

# GET /account/edit
#
def edit
@user = current_user
end
end
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def store_location

def after_sign_in_path_for(user)
if user.members.present?
users_path
members_path
else
page_path("about")
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class HomeController < ApplicationController
def index
redirect_to :users if current_user
redirect_to :members if current_user
end
end
38 changes: 36 additions & 2 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
class MembersController < ApplicationController
before_filter :authenticate_user!

# GET /members
#
def index
@users = current_organization.users
@memberships = current_organization
.members
.where(user_id: @users.map(&:id))
.includes(:account)
.each_with_object({}) do |mem, ob|
ob[mem.user_id] = mem
end
end

# GET /members/:member_uid
#
def show
find_member
@user = @member.user
@movements = @member
.movements
.order('created_at DESC')
.page(params[:page])
.per(10)
end

# DELETE /members/:member_uid
#
def destroy
find_member
toggle_active_posts
@member.destroy
redirect_to users_path
redirect_to members_path
end

def toggle_manager
Expand Down Expand Up @@ -33,8 +60,15 @@ def toggle_active

private

# TODO: rely on organization scope instead of current_organization
def find_member
@member ||= current_organization.members.find(params[:id])
@member ||= Member.where(
organization_id: current_organization.id,
member_uid: params[:member_uid]
).first

# TODO: better not found management please
raise unless @member
end

def toggle_active_posts
Expand Down
18 changes: 5 additions & 13 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ def scoped_users
current_organization.users
end

def index
@users = scoped_users
@memberships = current_organization.members.
where(user_id: @users.map(&:id)).
includes(:account).each_with_object({}) do |mem, ob|
ob[mem.user_id] = mem
end
end

# GET /users/:id
#
def show
@user = find_user
@member = @user.as_member_of(current_organization)
@movements = @member.movements.order("created_at DESC").page(params[:page]).
per(10)
@user = User.find_by_id(params[:id])
raise unless @user
Copy link
Contributor Author

Choose a reason for hiding this comment

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

raise WAT!?

authorize @user
end

def new
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ def users_as_json
{
id: user.id,
avatar: avatar_url(user),
member_id: membership.member_uid,
member_uid: membership.member_uid,
username: user.username,
email: user.email_if_real,
unconfirmed_email: user.unconfirmed_email,
phone: user.phone,
alt_phone: user.alt_phone,
balance: membership.account_balance.to_i,

url: user_path(user),
url: member_path(membership.member_uid),
edit_link: edit_user_path(user),
cancel_link: cancel_member_path(membership),
toggle_manager_link: toggle_manager_member_path(membership),
Expand Down
8 changes: 8 additions & 0 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ def new?
user.admins?(organization)
end

def show?
return true if user.id == record.id

record.organizations.any? do |org|
user.admins?(org)
end
end

def create?
user.admins?(organization)
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/account/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>
<%= @user.username %>
<small><%= t ".edit_user" %></small>
</h1>

<%= render "users/form" %>
54 changes: 54 additions & 0 deletions app/views/account/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<div class="panel user-profile">
<div class="panel-body">
<h1>
<%= current_user.username %>
<small>
<% if current_user.superadmin? %>
<span class="label label-important">
<%= t ".superadmin" %>
</span>
<% end %>
</small>
</h1>
<div class="row">
<div class="col-sm-3 col-xs-5 text-center">
<a href="https://www.gravatar.com" target="_blank">
<%= image_tag avatar_url(current_user, 160) %>
</a>
</div>
<div class="col-sm-9 col-xs-7 break-word">
<%= m current_user.description %>
<ul class="nav nav-pills pull-right">
<li>
<%= link_to account_edit_path do %>
<%= glyph :pencil %>
<%= t "global.edit" %>
<% end %>
</li>
</ul>
<dl class="dl-horizontal">
<h3>
<%= t "global.contact_details" %>
</h3>
<% if current_user.email_if_real != "" %>
<dt>
<%= User.human_attribute_name(:email) %>
</dt>
<dd>
<%= current_user.email_if_real %>
</dd>
<% end %>
<% phones = [current_user.phone, current_user.alt_phone].select(&:present?) %>
<% if phones.present? %>
<dt>
<%= t(".phone", count: phones.size) %>
</dt>
<dd>
<%= phones.map(&:to_s).join('—') %>
</dd>
<% end %>
</dl>
</div>
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/application/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<ul class="nav nav-pills actions-menu">
<%= render 'application/menus/user_list_link' %>
<%= render 'application/menus/members_list_link' %>
<%= render 'application/menus/offers_dashboard_link' %>
<%= render 'application/menus/inquiries_list_link' %>
<%= render 'application/menus/offers_by_tag_link' %>
Expand All @@ -63,4 +63,4 @@
</div>
</div>
</nav>
<% end %>
<% end %>
6 changes: 6 additions & 0 deletions app/views/application/menus/_members_list_link.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<li class="<%= "active" if current_page?(members_path) %>">
<%= link_to members_path do %>
<%= glyph :user %>
<%= Member.model_name.human.pluralize %>
<% end %>
</li>
2 changes: 1 addition & 1 deletion app/views/application/menus/_user_admin_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<li class="divider" role="presentation"></li>
<% end %>
<li role="presentation">
<%= link_to current_user do %>
<%= link_to account_path do %>
<%= glyph :user %>
<%= t "layouts.application.edit_profile" %>
<% end %>
Expand Down
6 changes: 0 additions & 6 deletions app/views/application/menus/_user_list_link.html.erb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<td>
<img ng-src="{{user.avatar}}" height: "32px" width: "32px">
</td>
<td>{{user.member_id}}</td>
<td>{{user.member_uid}}</td>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

member.member_uid?

<td>
<span class="glyphicon glyphicon-time" ng-if="!user.active"></span>
<a ng-href="{{user.url}}">{{user.username}}</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div ng-controller="UserListCtrl">
<h1>
<%= User.model_name.human.pluralize %>
<%= Member.model_name.human.pluralize %>
<%= link_to current_organization.name,
organization_path(current_organization) %>
Expand All @@ -23,7 +23,7 @@
<li>
<a href="<%= new_user_path %>">
<span class="glyphicon glyphicon-plus"></span>
<%= t "helpers.submit.create", model: User.model_name.human %>
<%= t "helpers.submit.create", model: Member.model_name.human %>
</a>
</li>
<% end %>
Expand Down Expand Up @@ -78,7 +78,7 @@
</tr>
</thead>
<tbody>
<%= render "user_rows", users: @users %>
<%= render "members_rows", users: @users %>
</tbody>
</table>
</div>
Expand Down
Loading