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 all 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
69 changes: 67 additions & 2 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
class MembersController < ApplicationController
before_filter :authenticate_user!

# GET /members
#
def index
context = current_organization
.members
.includes(:account, :user)
context = context.where(active: true) unless (admin? || superadmin?)

@memberships = context
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 All @@ -31,14 +56,54 @@ def toggle_active
end
end

# TODO: move to service and probably different controller
def give_time
find_member
@destination = @member.account.id
@source = find_transfer_source
@offer = find_transfer_offer
@transfer = Transfer.new(
source: @source,
destination: @destination,
post: @offer
)
@sources = find_transfer_sources_for_admin
end

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
current_organization.posts.where(user_id: @member.user_id).
each { |post| post.update_attributes(active: false) }
end

# TODO: move to service and probably different controller
def find_transfer_offer
current_organization.offers.
find(params[:offer]) if params[:offer].present?
end

# TODO: move to service and probably different controller
def find_transfer_source
current_user.members.
find_by(organization: current_organization).account.id
end

# TODO: move to service and probably different controller
def find_transfer_sources_for_admin
return unless admin?
[current_organization.account] +
current_organization.member_accounts.where("members.active is true")
end
end
6 changes: 5 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def show
else
model.all.active.of_active_members
end
post = scope.find params[:id]
post = scope.find(params[:id])
@member = Member.where(
organization: post.organization,
user: post.user
)
instance_variable_set("@#{resource}", post)
end

Expand Down
47 changes: 6 additions & 41 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ 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])
# TODO: better not found management please
raise unless @user
authorize @user
end

def new
Expand Down Expand Up @@ -61,18 +54,6 @@ def update
end
end

def give_time
@user = scoped_users.find(params[:id])
@destination = @user.members.
find_by(organization: current_organization).account.id
@source = find_transfer_source
@offer = find_transfer_offer
@transfer = Transfer.new(source: @source,
destination: @destination,
post: @offer)
@sources = find_transfer_sources_for_admin
end

private

def user_params
Expand All @@ -85,22 +66,6 @@ def user_params
params.require(:user).permit *fields_to_permit
end

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

def find_transfer_source
current_user.members.
find_by(organization: current_organization).account.id
end

def find_transfer_sources_for_admin
return unless admin?
[current_organization.account] +
current_organization.member_accounts.where("members.active is true")
end

def find_user
if current_user.id == params[:id].to_i
current_user
Expand Down
28 changes: 13 additions & 15 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
module UsersHelper
# TODO refactor or eliminate - poosibly the second.
# TODO refactor or eliminate - possibly the second.
def users_as_json
@users = (admin? || superadmin?) ? @users : @users.actives
@users.map do |user|
membership = @memberships[user.id]
@memberships.map do |membership|
{
id: user.id,
avatar: avatar_url(user),
member_id: membership.member_uid,
username: user.username,
email: user.email_if_real,
unconfirmed_email: user.unconfirmed_email,
phone: user.phone,
alt_phone: user.alt_phone,
id: membership.user_id,
avatar: avatar_url(membership.user),
member_uid: membership.member_uid,
username: membership.user.username,
email: membership.user.email_if_real,
unconfirmed_email: membership.user.unconfirmed_email,
phone: membership.user.phone,
alt_phone: membership.user.alt_phone,
balance: membership.account_balance.to_i,

url: user_path(user),
edit_link: edit_user_path(user),
url: member_path(membership.member_uid),
edit_link: edit_user_path(membership.user),
cancel_link: cancel_member_path(membership),
toggle_manager_link: toggle_manager_member_path(membership),
manager: !!membership.manager,
toggle_active_link: toggle_active_member_path(membership),
active: membership.active?,
valid_email: user.has_valid_email?
valid_email: membership.user.has_valid_email?
}
end.to_json.html_safe
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ def self.inherited(child)
end
end

# TODO: what is this? Can we delete it?
attr_reader :member_id

belongs_to :category

belongs_to :user
belongs_to :organization
# TODO: what is this?
belongs_to :publisher, class_name: "User", foreign_key: "publisher_id"
# TODO: what is this? Can we delete it?
# belongs_to :member, class_name: "Member", foreign_key: "user_id"
has_many :user_members, class_name: "Member", through: :user, source: :members
has_many :transfers
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 All @@ -21,10 +21,6 @@
<td> {{user.balance | timeBalance}} </td>
<% if current_user.manages?(current_organization) %>
<td class="hover-actions">
<a class="action" ng-if="user.edit_link" ng-href="{{user.edit_link}}">
<%= glyph :pencil %>
<%= t "global.edit" %>
</a>
<a class="action"
ng-if="user.cancel_link && !user.active"
ng-href="{{user.cancel_link}}"
Expand Down
Loading