docbrown/app/controllers/admin/articles_controller.rb
rhymes 99ddd31058
Pin posts to feed (#13807)
* Add SiteConfig.feed_pinned_article and validation

* Display pinned article at the top of feed

* Add (basic) functionality to pin/unpin post

* Admins can pin other users posts as well

* Hide the button if looking at the non pinned post

* Add pinned/unpinned snackbar message

* Rename SiteConfig usage to Settings::General

* Add pinned article to the Admin articles index

* Show the pin post button when there's no pinned article

* Move pinning to a separate controller

* Fix SiteConfig reference

* Hide PinController actions to unauthorized users

* PinnedArticlesController#show action and refactor some of the code

* Add Modal interaction

* Fix modal-pinned checkbox interaction

* Fixed pin/unpin post

* Add ArticleDecorator#pinned? specs

* Add PinnedArticlePolicy and PinnedArticlesController specs

* Add ability to actually pin an article from the admin after submit

* Add partial Cypress pin/unpin spec

* Fix pinned article and add basic Cypress interaction tests

* Add Crayons styling to modal

* Only render the pinned article on the default Feed page

* Use persisted?

* Add some comments

* Update app/javascript/articles/Article.jsx

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update app/javascript/packs/homePageFeed.jsx

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Fix Cypress tests

* Update app/javascript/admin/controllers/article_controller.js

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Fix pinning in article show page

* Used PinnedArticle domain model

* Fix spec

* Update cypress/integration/adminFlows/articles/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/adminFlows/articles/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/adminFlows/articles/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/adminFlows/articles/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/adminFlows/articles/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/articleFlows/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update cypress/integration/articleFlows/pinArticle.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Update app/views/admin/articles/index.html.erb

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Fix merge woes

* Add missing article pin post flows

* Add missing admin article flows

* Add Unpin to Admin as well

* Add Audit::Log entries for pin/unpin actions

* Update app/controllers/stories/feeds_controller.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Do not rate limit in E2E tests

* Use .find instead of .filter

* Rename ArticleIdValidator to ExistingArticleIdValidator

* Treat draft and deleted articles the same

* Make sure posts can be pinned after the pinned article is unpublished or deleted

* Use .get directly

* Fix spec and fix PinnedArticlesController#show

* Strengthen pinArticle Cypress tests

* Add Cypress test heading guard

* Add another Cypress test heading guard

* Remove duplicate validator

* Try using the Tools: header instead of the article title

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
Co-authored-by: Michael Kohl <citizen428@dev.to>
2021-06-04 10:54:53 +02:00

125 lines
3.3 KiB
Ruby

module Admin
class ArticlesController < Admin::ApplicationController
layout "admin"
after_action only: %i[update unpin] do
Audit::Logger.log(:moderator, current_user, params.dup)
end
def index
@pinned_article = PinnedArticle.get
case params[:state]
when /top-/
months_ago = params[:state].split("-")[1].to_i.months.ago
@articles = articles_top(months_ago)
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])
if article.update(article_params)
PinnedArticle.set(article) if params.dig(:article, :pinned)
flash[:success] = "Article saved!"
else
flash[:danger] = article.errors_as_sentence
end
redirect_to admin_article_path(article.id)
end
def unpin
article = Article.find(params[:id])
PinnedArticle.remove
respond_to do |format|
format.html { redirect_to admin_article_path(article.id) }
format.js do
render partial: "admin/articles/individual_article", locals: { article: article }, content_type: "text/html"
end
end
end
private
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_boosted_additional
Article.boosted_via_additional_articles
.includes(:user)
.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)
.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_list
published_at]
params.require(:article).permit(allowed_params)
end
def authorize_admin
authorize Article, :access?, policy_class: InternalPolicy
end
end
end