docbrown/app/controllers/admin/articles_controller.rb
Julianna Tetreault c87974b7ac
[deploy] Add co_author_ids to Articles (#10339)
* Replaces second_user_id and third_user_id with co_author_ids on the Articles table

* Adjusts migration that adds co_author_ids to also add array: true

* Replaces second_user_id and third_user_id with co_author_ids in all applicable files
  - Use co_author_ids in _individual_article view
  - Use co_author_ids in articles and stories controllers
  - Use co_author_ids in article and podcast_episode models
  - Use co_author_ids in article_dashboard
  - Use co_author_ids in comments_helper
  - Use co_author_ids in articles_spec

* Replaces second_user_id and third_user_id with co_author_ids in article.rb model

* Remove unused #assign_co_authors method from the Stories::Controller

* Add #co_authors and #co_author_name_and_path to article_decorator.rb

* Adds a descriptive form field for co_author_ids for Admins in /admin/articles

* Adds a conditional in article/show.html.erb to render co_author names on an Article in a human-readable way

* Replaces get_co_author_name_and_path with proper method name, co_author_name_and_path in show.html.erb

* Adds latest schema to (hopefully) resolve Travis conflicts

* Adds the safe operator to show.html.erb

* Adjusts articles_spec.rb to use an array in the first test

* Replaces unless &.empty? with if ./present? in articles/show.html.erb

* Replaces second_user_id with co_author_ids in stories_show_spec.rb test

* Replaces elsif with else in articles/show.html.erb

* Cleans up show.html.erb by removing conditional in favor of decorator method

* Refactors splitting of params in Articles::Controller and optimizes query in #co_authors

* Reverts removal of second_user_id and third_user_id and migration file

* Adds a data_update script to update co_author_ids with existing second and third user_ids

* Adds validations to co_authors and flash_messages to indicate whether an update was successful or not
  - Adds to methods to article.rb to validate the IDs of co_authors and authors
  - Adds flash messages to the Admin::Articles::Controller and a redirect to the show page
  - Removes the JS highlighting upon submit when updating an Article in Admin
  - Refactors #update action in Admin::Articles::Controller

* Adds tests to article_spec.rb around co_author_id validations in article.rb

* Adjusts #validate_co_authors_must_not_be_the_same to use .include? instead

* Uses Field::Select.with_options in article_dashboard.rb to properly display co_author_ids

* Reverts removal of assigning co_author_ids in the Stories::Controller

* Adjusts error message and adjusts return logic in article.rb (thanks, Fernando!)

* Fixes failing article_spec.rb test that checks for co_author_ids uniqueness

* Adds a default array to co_author_ids and checks if they are .blank?

* Refactor data_update script to use a single SQL statement (thank you, Michael)

* Preserve array order of co_author_ids in article_decorator.rb

* Add db file for default: []

* Add validation to fix bug related to text inputs and invalid users when adding co_authors

* Adds tests to ensure that co_author_ids are both an integer and an integer > 0

* Updates admin/articles_spec.rb to default [] instead of nil

* Adjusts validations in article.rb to be DRY-er and more consistent

* Consolidates validations further

* Refactors validations in article.rb to use procs

* Refactors data_update script to remove nil values from array
2020-10-02 11:06:11 -04:00

146 lines
4.6 KiB
Ruby

module Admin
class ArticlesController < Admin::ApplicationController
layout "admin"
after_action only: [:update] do
Audit::Logger.log(:moderator, current_user, params.dup)
end
def index
@pending_buffer_updates = BufferUpdate.where(status: "pending").includes(:article)
@user_buffer_updates = BufferUpdate.where(status: "sent_direct", approver_user_id: current_user.id).where(
"created_at > ?", 24.hours.ago
)
case params[:state]
when /not-buffered/
days_ago = params[:state].split("-")[2].to_f
@articles = articles_not_buffered(days_ago)
when /top-/
months_ago = params[:state].split("-")[1].to_i.months.ago
@articles = articles_top(months_ago)
when "satellite"
@articles = articles_satellite
when "satellite-not-bufffered"
@articles = articles_satellite.where(last_buffered: nil)
when "boosted-additional-articles"
@articles = articles_boosted_additional
when "chronological"
@articles = articles_chronological
else
@articles = articles_mixed
@featured_articles = articles_featured
end
end
def show
@article = Article.find(params[:id])
end
def update
article = Article.find(params[:id])
article.featured = article_params[:featured].to_s == "true"
article.approved = article_params[:approved].to_s == "true"
article.email_digest_eligible = article_params[:email_digest_eligible].to_s == "true"
article.boosted_additional_articles = article_params[:boosted_additional_articles].to_s == "true"
article.boosted_dev_digest_email = article_params[:boosted_dev_digest_email].to_s == "true"
article.user_id = article_params[:user_id].to_i
article.co_author_ids = article_params[:co_author_ids].split(",").map(&:strip)
if article.save
flash[:success] = "Article saved!"
else
flash[:danger] = article.errors_as_sentence
end
redirect_to admin_article_path(article.id)
end
private
def articles_not_buffered(days_ago)
Article.published
.where(last_buffered: nil)
.where("published_at > ? OR crossposted_at > ?", days_ago.days.ago, days_ago.days.ago)
.includes(:user)
.limited_columns_internal_select
.order(public_reactions_count: :desc)
.page(params[:page])
.per(50)
end
def articles_top(months_ago)
Article.published
.where("published_at > ?", months_ago)
.includes(user: [:notes])
.limited_columns_internal_select
.order(public_reactions_count: :desc)
.page(params[:page])
.per(50)
end
def articles_satellite
Article.published.where(last_buffered: nil)
.includes(:user, :buffer_updates)
.tagged_with(Tag.bufferized_tags, any: true)
.limited_columns_internal_select
.order(hotness_score: :desc)
.page(params[:page])
.per(60)
end
def articles_boosted_additional
Article.boosted_via_additional_articles
.includes(:user, :buffer_updates)
.limited_columns_internal_select
.order(published_at: :desc)
.page(params[:page])
.per(100)
end
def articles_chronological
Article.published
.includes(user: [:notes])
.limited_columns_internal_select
.order(published_at: :desc)
.page(params[:page])
.per(50)
end
def articles_mixed
Article.published
.includes(user: [:notes])
.limited_columns_internal_select
.order(hotness_score: :desc)
.page(params[:page])
.per(30)
end
def articles_featured
Article.published.or(Article.where(published_from_feed: true))
.where(featured: true)
.where("featured_number > ?", Time.current.to_i)
.includes(:user, :buffer_updates)
.limited_columns_internal_select
.order(featured_number: :desc)
end
def article_params
allowed_params = %i[featured
social_image
body_markdown
approved
email_digest_eligible
boosted_additional_articles
boosted_dev_digest_email
main_image_background_hex_color
featured_number
user_id
co_author_ids
last_buffered]
params.require(:article).permit(allowed_params)
end
def authorize_admin
authorize Article, :access?, policy_class: InternalPolicy
end
end
end