Extract component to show a user's public activity
This commit is contained in:
35
app/components/users/public_activity_component.html.erb
Normal file
35
app/components/users/public_activity_component.html.erb
Normal file
@@ -0,0 +1,35 @@
|
||||
<% load_filtered_activity if valid_access? %>
|
||||
<% if valid_access? %>
|
||||
<ul class="menu simple margin-top">
|
||||
<% valid_filters.each do |filter| %>
|
||||
<% if @activity_counts[filter] > 0 %>
|
||||
<% if current_filter == filter %>
|
||||
<li class="is-active">
|
||||
<h2><%= t("users.show.filters.#{filter}", count: @activity_counts[filter]) %></h2>
|
||||
</li>
|
||||
<% else %>
|
||||
<li>
|
||||
<%= link_to t("users.show.filters.#{filter}", count: @activity_counts[filter]),
|
||||
current_path_with_query_params(filter: filter, page: 1) %>
|
||||
</li>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<% if @activity_counts.values.inject(&:+) == 0 %>
|
||||
<div class="callout primary">
|
||||
<%= t("users.show.no_activity") %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= render "users/following", user: user, follows: @follows if @follows.present? %>
|
||||
<%= render "users/proposals", proposals: @proposals if @proposals.present? && feature?(:proposals) %>
|
||||
<%= render "users/debates", debates: @debates if @debates.present? && feature?(:debates) %>
|
||||
<%= render "users/budget_investments", budget_investments: @budget_investments if @budget_investments.present? && feature?(:budgets) %>
|
||||
<%= render "users/comments", comments: @comments if @comments.present? %>
|
||||
<% else %>
|
||||
<div class="callout warning margin">
|
||||
<%= t("users.show.private_activity") %>
|
||||
</div>
|
||||
<% end %>
|
||||
89
app/components/users/public_activity_component.rb
Normal file
89
app/components/users/public_activity_component.rb
Normal file
@@ -0,0 +1,89 @@
|
||||
class Users::PublicActivityComponent < ApplicationComponent
|
||||
attr_reader :user
|
||||
delegate :authorized_current_user?, :current_path_with_query_params, :valid_filters, :current_filter, to: :helpers
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def valid_access?
|
||||
user.public_activity || authorized_current_user?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_activity_counts
|
||||
@activity_counts = ActiveSupport::HashWithIndifferentAccess.new(
|
||||
proposals: Proposal.where(author_id: @user.id).count,
|
||||
debates: (Setting["process.debates"] ? Debate.where(author_id: @user.id).count : 0),
|
||||
budget_investments: (Setting["process.budgets"] ? Budget::Investment.where(author_id: @user.id).count : 0),
|
||||
comments: only_active_commentables.count,
|
||||
follows: @user.follows.map(&:followable).compact.count)
|
||||
end
|
||||
|
||||
def load_filtered_activity
|
||||
set_activity_counts
|
||||
case params[:filter]
|
||||
when "proposals" then load_proposals
|
||||
when "debates" then load_debates
|
||||
when "budget_investments" then load_budget_investments
|
||||
when "comments" then load_comments
|
||||
when "follows" then load_follows
|
||||
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[:budget_investments] > 0
|
||||
load_budget_investments
|
||||
@current_filter = "budget_investments"
|
||||
elsif @activity_counts[:comments] > 0
|
||||
load_comments
|
||||
@current_filter = "comments"
|
||||
elsif @activity_counts[:follows] > 0
|
||||
load_follows
|
||||
@current_filter = "follows"
|
||||
end
|
||||
end
|
||||
|
||||
def load_proposals
|
||||
@proposals = Proposal.created_by(@user).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 = only_active_commentables.includes(:commentable).order(created_at: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def load_budget_investments
|
||||
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def load_follows
|
||||
@follows = @user.follows.group_by(&:followable_type)
|
||||
end
|
||||
|
||||
def only_active_commentables
|
||||
disabled_commentables = []
|
||||
disabled_commentables << "Debate" unless Setting["process.debates"]
|
||||
disabled_commentables << "Budget::Investment" unless Setting["process.budgets"]
|
||||
if disabled_commentables.present?
|
||||
all_user_comments.where.not(commentable_type: disabled_commentables)
|
||||
else
|
||||
all_user_comments
|
||||
end
|
||||
end
|
||||
|
||||
def all_user_comments
|
||||
Comment.not_valuations.not_as_admin_or_moderator.where(user_id: @user.id)
|
||||
end
|
||||
end
|
||||
@@ -3,77 +3,13 @@ class UsersController < ApplicationController
|
||||
|
||||
load_and_authorize_resource
|
||||
helper_method :valid_interests_access?
|
||||
helper_method :authorized_current_user?
|
||||
|
||||
def show
|
||||
load_filtered_activity if valid_access?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_activity_counts
|
||||
@activity_counts = ActiveSupport::HashWithIndifferentAccess.new(
|
||||
proposals: Proposal.where(author_id: @user.id).count,
|
||||
debates: (Setting["process.debates"] ? Debate.where(author_id: @user.id).count : 0),
|
||||
budget_investments: (Setting["process.budgets"] ? Budget::Investment.where(author_id: @user.id).count : 0),
|
||||
comments: only_active_commentables.count,
|
||||
follows: @user.follows.map(&:followable).compact.count)
|
||||
end
|
||||
|
||||
def load_filtered_activity
|
||||
set_activity_counts
|
||||
case params[:filter]
|
||||
when "proposals" then load_proposals
|
||||
when "debates" then load_debates
|
||||
when "budget_investments" then load_budget_investments
|
||||
when "comments" then load_comments
|
||||
when "follows" then load_follows
|
||||
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[:budget_investments] > 0
|
||||
load_budget_investments
|
||||
@current_filter = "budget_investments"
|
||||
elsif @activity_counts[:comments] > 0
|
||||
load_comments
|
||||
@current_filter = "comments"
|
||||
elsif @activity_counts[:follows] > 0
|
||||
load_follows
|
||||
@current_filter = "follows"
|
||||
end
|
||||
end
|
||||
|
||||
def load_proposals
|
||||
@proposals = Proposal.created_by(@user).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 = only_active_commentables.includes(:commentable).order(created_at: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def load_budget_investments
|
||||
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def load_follows
|
||||
@follows = @user.follows.group_by(&:followable_type)
|
||||
end
|
||||
|
||||
def valid_access?
|
||||
@user.public_activity || authorized_current_user?
|
||||
end
|
||||
|
||||
def valid_interests_access?
|
||||
@user.public_interests || authorized_current_user?
|
||||
end
|
||||
@@ -81,19 +17,4 @@ class UsersController < ApplicationController
|
||||
def authorized_current_user?
|
||||
@authorized_current_user ||= current_user && (current_user == @user || current_user.moderator? || current_user.administrator?)
|
||||
end
|
||||
|
||||
def all_user_comments
|
||||
Comment.not_valuations.not_as_admin_or_moderator.where(user_id: @user.id)
|
||||
end
|
||||
|
||||
def only_active_commentables
|
||||
disabled_commentables = []
|
||||
disabled_commentables << "Debate" unless Setting["process.debates"]
|
||||
disabled_commentables << "Budget::Investment" unless Setting["process.budgets"]
|
||||
if disabled_commentables.present?
|
||||
all_user_comments.where.not(commentable_type: disabled_commentables)
|
||||
else
|
||||
all_user_comments
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
<%= render "following", user: @user, follows: @follows if @follows.present? %>
|
||||
<%= render "proposals", proposals: @proposals if @proposals.present? && feature?(:proposals) %>
|
||||
<%= render "debates", debates: @debates if @debates.present? && feature?(:debates) %>
|
||||
<%= render "budget_investments", budge_investments: @budge_investments if @budge_investments.present? && feature?(:budgets) %>
|
||||
<%= render "comments", comments: @comments if @comments.present? %>
|
||||
@@ -22,36 +22,7 @@
|
||||
<% end %>
|
||||
</h2>
|
||||
|
||||
<% if @user.public_activity || @authorized_current_user %>
|
||||
<ul class="menu simple margin-top">
|
||||
<% @valid_filters.each do |filter| %>
|
||||
<% if @activity_counts[filter] > 0 %>
|
||||
<% if @current_filter == filter %>
|
||||
<li class="is-active">
|
||||
<h2><%= t("users.show.filters.#{filter}", count: @activity_counts[filter]) %></h2>
|
||||
</li>
|
||||
<% else %>
|
||||
<li>
|
||||
<%= link_to t("users.show.filters.#{filter}", count: @activity_counts[filter]),
|
||||
current_path_with_query_params(filter: filter, page: 1) %>
|
||||
</li>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<% if @activity_counts.values.inject(&:+) == 0 %>
|
||||
<div class="callout primary">
|
||||
<%= t("users.show.no_activity") %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= render "activity_page" %>
|
||||
<% else %>
|
||||
<div class="callout warning margin">
|
||||
<%= t("users.show.private_activity") %>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= render Users::PublicActivityComponent.new(@user) %>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
Reference in New Issue
Block a user