docbrown/app/controllers/dashboards_controller.rb
rhymes 6a626e819d Refactor: fix issues raised by bullet (#2965)
* Preload organization, not user

* Preload users only when needed

* Pre-load podcasts for podcast episodes in API

* Avoid eager loading error by loading rating votes separately

* Preload associations for moderation

* Preload user comments in trees

* Preload organization for non org dashboard and cleanup queries

* Optimize ArticleSuggester to only load N articles at need

* Remove eager loading and pass variables to partials for easier debug

* Reorganize tags validation code and ignore actsastaggableon eager loading issues

* Remove unused eager loading and bring up comments relation

* Preload podcasts when loading podcast episodes

* Fix views specs

* Make sure ArticleSuggester never returns duplicates

* Remove commented code

* Re-trigger build

* Move suggested articles back to view to respect fragment caching
2019-05-28 17:32:53 -04:00

69 lines
2.4 KiB
Ruby

class DashboardsController < ApplicationController
before_action :set_no_cache_header
before_action :authenticate_user!
after_action :verify_authorized
def show
fetch_and_authorize_user
@org_dashboard = @user&.organization && @user&.org_admin && params[:which] == "organization"
if @org_dashboard
articles = @user.organization.articles
@org_members = @user.organization.users.pluck(:name, :id)
else
articles = @user.articles.includes(:organization)
end
@articles = articles.sorting(params[:sort]).decorate
# Updates analytics in background if appropriate:
ArticleAnalyticsFetcher.new.delay.update_analytics(current_user.id) if @articles && ApplicationConfig["GA_FETCH_RATE"] < 50 # Rate limit concerned, sometimes we throttle down.
end
def following
fetch_and_authorize_user
@follows = @user.follows_by_type("User").
order("created_at DESC").includes(:followable).limit(80)
@followed_tags = @user.follows_by_type("ActsAsTaggableOn::Tag").
order("points DESC").includes(:followable).limit(80)
@followed_organizations = @user.follows_by_type("Organization").
order("created_at DESC").includes(:followable).limit(80)
@followed_podcasts = @user.follows_by_type("Podcast").
order("created_at DESC").includes(:followable).limit(80)
end
def followers
fetch_and_authorize_user
if params[:which] == "user_followers"
@follows = Follow.where(followable_id: @user.id, followable_type: "User").
includes(:follower).order("created_at DESC").limit(80)
elsif params[:which] == "organization_user_followers"
@follows = Follow.where(followable_id: @user.organization_id, followable_type: "Organization").
includes(:follower).order("created_at DESC").limit(80)
end
end
def pro
user_or_org = if params[:org_id]
org = Organization.find_by(id: params[:org_id])
authorize org, :pro_org_user?
org
else
authorize current_user, :pro_user?
current_user
end
@dashboard = Dashboard::Pro.new(user_or_org)
end
private
def fetch_and_authorize_user
@user = if params[:username] && current_user.any_admin?
User.find_by(username: params[:username])
else
current_user
end
authorize (@user || User), :dashboard_show?
end
end