adds user's public show page

This commit is contained in:
Juanjo Bazán
2015-10-29 18:26:33 +01:00
committed by Juanjo Bazán
parent 4694cf1f64
commit 7f1fe034a3
6 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
class UsersController < ApplicationController
has_filters %w{proposals debates comments}, only: :show
load_and_authorize_resource
before_action :set_activity_counts, only: :show
def show
load_filtered_activity
end
private
def set_activity_counts
@activity_counts = HashWithIndifferentAccess.new(
proposals: Proposal.where(author_id: @user.id).count,
debates: Debate.where(author_id: @user.id).count,
comments: Comment.where(user_id: @user.id).count)
end
def load_filtered_activity
case params[:filter]
when "proposals" then load_proposals
when "debates" then load_debates
when "comments" then load_comments
else load_available_activity
end
end
def load_available_activity
if @activity_counts[:proposals] > 0
load_proposals
@current_filter = "proposals"
elsif @activity_counts[:debates] > 0
load_debates
@current_filter = "debates"
elsif @activity_counts[:comments] > 0
load_comments
@current_filter = "comments"
end
end
def load_proposals
@proposals = Proposal.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
end
def load_debates
@debates = Debate.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
end
def load_comments
@comments = Comment.where(user_id: @user.id).includes(:commentable).order(created_at: :desc).page(params[:page])
end
end

View File

@@ -5,6 +5,7 @@ module Abilities
def initialize(user) def initialize(user)
can :read, Debate can :read, Debate
can :read, Proposal can :read, Proposal
can :read, User
end end
end end
end end

View File

@@ -0,0 +1,15 @@
<h1><%= avatar_image(@user, seed: @user.id, size: 60) %> <%= @user.name %></h1>
<dl class="sub-nav">
<% @valid_filters.each do |filter| %>
<% if @activity_counts[filter] > 0 %>
<% if @current_filter == filter %>
<dd class="active"> <%= t("users.show.filters.#{filter}", count: @activity_counts[filter]) %></dd>
<% else %>
<dd><%= link_to t("users.show.filters.#{filter}", count: @activity_counts[filter]),
current_path_with_query_params(filter: filter, page: 1) %></dd>
<% end %>
<% end %>
<% end %>
<%= t("users.show.no_activity") if @activity_counts.values.inject(&:+) == 0 %>
</dl>

View File

@@ -324,6 +324,19 @@ en:
check: "Select" check: "Select"
check_all: "All" check_all: "All"
check_none: "None" check_none: "None"
users:
show:
filters:
proposals:
one: "1 Proposal"
other: "%{count} Proposals"
debates:
one: "1 Debate"
other: "%{count} Debates"
comments:
one: "1 Comment"
other: "%{count} Comments"
no_activity: "Usuario sin actividad pública"
unauthorized: unauthorized:
default: "You do not have permission to access this page." default: "You do not have permission to access this page."
manage: manage:

View File

@@ -324,6 +324,19 @@ es:
check: "Seleccionar" check: "Seleccionar"
check_all: "Todos" check_all: "Todos"
check_none: "Ninguno" check_none: "Ninguno"
users:
show:
filters:
proposals:
one: "1 Propuesta"
other: "%{count} Propuestas"
debates:
one: "1 Debate"
other: "%{count} Debates"
comments:
one: "1 Comentario"
other: "%{count} Comentarios"
no_activity: "Usuario sin actividad pública"
unauthorized: unauthorized:
default: "No tienes permiso para acceder a esta página." default: "No tienes permiso para acceder a esta página."
manage: manage:

View File

@@ -61,6 +61,8 @@ Rails.application.routes.draw do
end end
end end
resources :users, only: [:show]
resource :account, controller: "account", only: [:show, :update, :delete] do resource :account, controller: "account", only: [:show, :update, :delete] do
collection { get :erase } collection { get :erase }
end end