Skip to content

Commit

Permalink
Use published scope for articles (forem#2374)
Browse files Browse the repository at this point in the history
* Use published scope for articles

* Fix variable name

* The sequencing in the union query matters

* Fix spec

* Restore a published: true condition

* This test is actually misleading due to the union

* Revert this change, not sure why it's not a relation

* Fix useless diff
  • Loading branch information
rhymes authored and benhalpern committed Apr 11, 2019
1 parent a09bf20 commit 5782ffd
Show file tree
Hide file tree
Showing 33 changed files with 124 additions and 172 deletions.
2 changes: 2 additions & 0 deletions .erb-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ linters:
rubocop_config:
inherit_from:
- .rubocop.yml
Layout/InitialIndentation:
Enabled: false
Layout/TrailingBlankLines:
Enabled: false
Lint/UselessAssignment:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v0/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def index
end

def show
relation = Article.includes(:user).where(published: true)
relation = Article.published.includes(:user)
@article = if params[:id] == "by_path"
relation.find_by!(path: params[:url]).decorate
else
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v0/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class VideosController < ApiController

def index
@page = params[:page]
@video_articles = Article.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).
@video_articles = Article.published.
where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).
where("score > ?", -4).
where(published: true).
order("hotness_score DESC").
page(params[:page].to_i).per(24)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ArticlesController < ApplicationController
def feed
skip_authorization

@articles = Article.where(published: true).
@articles = Article.published.
select(:published_at, :processed_html, :user_id, :organization_id, :title, :path).
order(published_at: :desc).
page(params[:page].to_i).per(12)
Expand Down
57 changes: 22 additions & 35 deletions app/controllers/internal/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,57 @@ class Internal::ArticlesController < Internal::ApplicationController

def index
@pending_buffer_updates = BufferUpdate.where(status: "pending").includes(:article)
case params[:state]

case params[:state]
when /not\-buffered/
days_ago = params[:state].split("-")[2].to_f
@articles = Article.
where(last_buffered: nil, published: true).
includes(:user).
includes(:buffer_updates).
order("positive_reactions_count DESC").
page(params[:page]).
@articles = Article.published.where(last_buffered: nil).
where("published_at > ? OR crossposted_at > ?", days_ago.days.ago, days_ago.days.ago).
includes(:user, :buffer_updates).
limited_columns_internal_select.
order("positive_reactions_count DESC").
page(params[:page]).
per(50)
when /top\-/
@articles = Article.
where(published: true).
@articles = Article.published.
where("published_at > ?", params[:state].split("-")[1].to_i.months.ago).
includes(:user).
includes(:buffer_updates).
includes(:user, :buffer_updates).
limited_columns_internal_select.
order("positive_reactions_count DESC").
page(params[:page]).
limited_columns_internal_select.
per(50)
when "satellite"
@articles = Article.
where(last_buffered: nil, published: true).
includes(:user).
includes(:buffer_updates).
order("hotness_score DESC").
@articles = Article.published.where(last_buffered: nil).
includes(:user, :buffer_updates).
tagged_with(Tag.bufferized_tags, any: true).
page(params[:page]).
limited_columns_internal_select.
order("hotness_score DESC").
page(params[:page]).
per(60)
when "boosted-additional-articles"
@articles = Article.
includes(:user).
@articles = Article.boosted_via_additional_articles.
includes(:user, :buffer_updates).
limited_columns_internal_select.
order("published_at DESC").
includes(:buffer_updates).
boosted_via_additional_articles.
page(params[:page]).
per(100).
limited_columns_internal_select
per(100)
when "chronological"
@articles = Article.
where(published: true).
@articles = Article.published.
limited_columns_internal_select.
order("published_at DESC").
page(params[:page]).
limited_columns_internal_select.
per(50)
else # MIX
@articles = Article.
where(published: true).
@articles = Article.published.
limited_columns_internal_select.
order("hotness_score DESC").
page(params[:page]).
limited_columns_internal_select.
per(30)

@featured_articles = Article.
where(published: true).
or(Article.where(published_from_feed: true)).
@featured_articles = Article.published.or(Article.where(published_from_feed: true)).
where(featured: true).
where("featured_number > ?", Time.current.to_i).
includes(:user).
includes(:buffer_updates).
includes(:user, :buffer_updates).
limited_columns_internal_select.
order("featured_number DESC")
end
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/moderations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ def index
skip_authorization
return unless current_user&.trusted

@articles = Article.where(published: true).
@articles = Article.published.
includes(:rating_votes).
where("rating_votes_count < 3").
where("score > -5").
order("hotness_score DESC").limit(100)
if params[:tag].present?
@articles = @articles.
cached_tagged_with(params[:tag])
end

@articles = @articles.cached_tagged_with(params[:tag]) if params[:tag].present?

@articles = @articles.decorate
end

Expand Down
11 changes: 5 additions & 6 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def live
end

def shecoded
@top_articles = Article.tagged_with(%w[shecoded shecodedally theycoded], any: true).
where(published: true, approved: true).where("published_at > ? AND score > ?", 3.weeks.ago, 28).
@top_articles = Article.published.tagged_with(%w[shecoded shecodedally theycoded], any: true).
where(approved: true).where("published_at > ? AND score > ?", 3.weeks.ago, 28).
order(Arel.sql("RANDOM()")).
includes(:user).decorate
@articles = Article.tagged_with(%w[shecoded shecodedally theycoded], any: true).
where(published: true, approved: true).where("published_at > ? AND score > ?", 3.weeks.ago, -8).
@articles = Article.published.tagged_with(%w[shecoded shecodedally theycoded], any: true).
where(approved: true).where("published_at > ? AND score > ?", 3.weeks.ago, -8).
order(Arel.sql("RANDOM()")).
where.not(id: @top_articles.pluck(:id)).
includes(:user).decorate
Expand All @@ -77,8 +77,7 @@ def shecoded
private # helpers

def latest_published_welcome_thread
Article.where(user_id: ApplicationConfig["DEVTO_USER_ID"], published: true).
tagged_with("welcome").last
Article.published.where(user_id: ApplicationConfig["DEVTO_USER_ID"]).tagged_with("welcome").last
end

def members_for_display
Expand Down
12 changes: 3 additions & 9 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ def handle_podcast_index

def handle_organization_index
@user = @organization
@stories = ArticleDecorator.decorate_collection(@organization.articles.
where(published: true).
@stories = ArticleDecorator.decorate_collection(@organization.articles.published.
limited_column_select.
includes(:user).
order("published_at DESC").page(@page).per(8))
Expand All @@ -159,8 +158,7 @@ def handle_user_index
return
end
assign_user_comments
@stories = ArticleDecorator.decorate_collection(@user.
articles.where(published: true).
@stories = ArticleDecorator.decorate_collection(@user.articles.published.
limited_column_select.
order("published_at DESC").page(@page).per(user_signed_in? ? 2 : 5))
@article_index = true
Expand Down Expand Up @@ -257,11 +255,7 @@ def assign_podcasts

def article_finder(num_articles)
tag = params[:tag]
articles = Article.where(published: true).
includes(:user).
limited_column_select.
page(@page).
per(num_articles)
articles = Article.published.includes(:user).limited_column_select.page(@page).per(num_articles)
articles = articles.cached_tagged_with(tag) if tag.present? # More efficient than tagged_with
articles
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def new
end

def index
@video_articles = Article.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).
@video_articles = Article.published.
where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).
where("score > ?", -4).
where(published: true).
order("hotness_score DESC").
page(params[:page].to_i).per(24)
set_surrogate_key_header "videos_landing_page"
Expand Down
3 changes: 2 additions & 1 deletion app/facades/dashboard/pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def initialize(user_or_org)

def user_or_org_article_ids
@user_or_org_article_ids ||=
Article.where("#{user_or_org.class.name.downcase}_id" => user_or_org.id, published: true).pluck(:id)
Article.published.where("#{user_or_org.class.name.downcase}_id" => user_or_org.id).
pluck(:id)
end

def this_week_reactions
Expand Down
5 changes: 2 additions & 3 deletions app/labor/article_suggester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def articles(num = 4)
end

def other_suggestions(num = 4)
Article.where(featured: true, published: true).
Article.published.where(featured: true).
where.not(id: article.id).
order("hotness_score DESC").
includes(:user).
Expand All @@ -22,8 +22,7 @@ def other_suggestions(num = 4)
end

def suggestions_by_tag
Article.tagged_with(article.tag_list, any: true).
where(published: true).
Article.published.tagged_with(article.tag_list, any: true).
where.not(id: article.id).
order("hotness_score DESC").
includes(:user).
Expand Down
2 changes: 1 addition & 1 deletion app/labor/badge_rewarder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def self.award_contributor_badges_from_github(since = 1.day.ago, message_markdow
end

def self.award_streak_badge(num_weeks)
article_user_ids = Article.where(published: true).where("published_at > ? AND score > ?", 1.week.ago, -25).pluck(:user_id) # No cred for super low quality
article_user_ids = Article.published.where("published_at > ? AND score > ?", 1.week.ago, -25).pluck(:user_id) # No cred for super low quality
message = "Congrats on achieving this streak! Consistent writing is hard. The next streak badge you can get is the #{num_weeks * 2} Week Badge. 😉"
users = User.where(id: article_user_ids).where("articles_count >= ?", num_weeks)
usernames = []
Expand Down
10 changes: 5 additions & 5 deletions app/labor/cache_buster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def bust(path)
end

def bust_comment(commentable, username)
bust("/") if Article.where(published: true).order("hotness_score DESC").limit(3).pluck(:id).include?(commentable.id)
bust("/") if Article.published.order("hotness_score DESC").limit(3).pluck(:id).include?(commentable.id)
if commentable.decorate.cached_tag_list_array.include?("discuss") &&
commentable.featured_number.to_i > 35.hours.ago.to_i
bust("/")
Expand Down Expand Up @@ -63,7 +63,7 @@ def bust_home_pages(article)
CacheBuster.new.bust "/videos?i=i"
end
TIMEFRAMES.each do |timeframe|
if Article.where(published: true).where("published_at > ?", timeframe[0]).
if Article.published.where("published_at > ?", timeframe[0]).
order("positive_reactions_count DESC").limit(3).pluck(:id).include?(article.id)
bust("/top/#{timeframe[1]}")
bust("/top/#{timeframe[1]}?i=i")
Expand All @@ -74,7 +74,7 @@ def bust_home_pages(article)
bust("/latest")
bust("/latest?i=i")
end
bust("/") if Article.where(published: true).order("hotness_score DESC").limit(4).pluck(:id).include?(article.id)
bust("/") if Article.published.order("hotness_score DESC").limit(4).pluck(:id).include?(article.id)
end

def bust_tag_pages(article)
Expand All @@ -86,7 +86,7 @@ def bust_tag_pages(article)
bust("/t/#{tag}/latest?i=i")
end
TIMEFRAMES.each do |timeframe|
if Article.where(published: true).where("published_at > ?", timeframe[0]).tagged_with(tag).
if Article.published.where("published_at > ?", timeframe[0]).tagged_with(tag).
order("positive_reactions_count DESC").limit(3).pluck(:id).include?(article.id)
bust("/top/#{timeframe[1]}")
bust("/top/#{timeframe[1]}?i=i")
Expand All @@ -97,7 +97,7 @@ def bust_tag_pages(article)
end
end
if rand(2) == 1 &&
Article.where(published: true).tagged_with(tag).
Article.published.tagged_with(tag).
order("hotness_score DESC").limit(2).pluck(:id).include?(article.id)
bust("/t/#{tag}")
bust("/t/#{tag}?i=i")
Expand Down
4 changes: 2 additions & 2 deletions app/labor/email_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def get_articles_to_send
order("score DESC").
limit(8)
else
Article.
Article.published.
where("published_at > ?", fresh_date).
where(published: true, featured: true, email_digest_eligible: true).
where(featured: true, email_digest_eligible: true).
where.not(user_id: @user.id).
where("score > ?", 25).
order("score DESC").
Expand Down
4 changes: 1 addition & 3 deletions app/labor/rate_limit_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def limit_by_situation(situation)
when "comment_creation"
user.comments.where("created_at > ?", 30.seconds.ago).size > 9
when "published_article_creation"
user.articles.
where(published: true).
where("created_at > ?", 30.seconds.ago).size > 9
user.articles.published.where("created_at > ?", 30.seconds.ago).size > 9
else
false
end
Expand Down
9 changes: 3 additions & 6 deletions app/labor/sticky_article_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def initialize(article, author)
end

def user_stickies
author.articles.
where(published: true).
author.articles.published.
limited_column_select.
tagged_with(article_tags, any: true).
where.not(id: article.id).order("published_at DESC").
Expand All @@ -21,10 +20,9 @@ def suggested_stickies
end

def tag_articles
@tag_articles ||= Article.tagged_with(article_tags, any: true).
@tag_articles ||= Article.published.tagged_with(article_tags, any: true).
includes(:user).
where("positive_reactions_count > ? OR comments_count > ?", reaction_count_num, comment_count_num).
where(published: true).
where.not(id: article.id, user_id: article.user_id).
where("featured_number > ?", 5.days.ago.to_i).
order(Arel.sql("RANDOM()")).
Expand All @@ -34,10 +32,9 @@ def tag_articles
def more_articles
return [] if tag_articles.size > 6

Article.tagged_with(%w[career productivity discuss explainlikeimfive], any: true).
Article.published.tagged_with(%w[career productivity discuss explainlikeimfive], any: true).
includes(:user).
where("comments_count > ?", comment_count_num).
where(published: true).
where.not(id: article.id, user_id: article.user_id).
where("featured_number > ?", 5.days.ago.to_i).
order(Arel.sql("RANDOM()")).
Expand Down
2 changes: 1 addition & 1 deletion app/labor/user_states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def cached_onboarding_checklist
end

def made_first_article
user.articles.where(published: true).any?
user.articles.published.any?
end

def follows_a_tag
Expand Down
Loading

0 comments on commit 5782ffd

Please sign in to comment.