Class: Postnhost::ArticlesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- Postnhost::ArticlesController
- Defined in:
- app/controllers/postnhost/articles_controller.rb
Constant Summary collapse
- PAGE_SIZE =
15
Instance Method Summary collapse
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #publish ⇒ Object
- #rollback ⇒ Object
- #unpublish ⇒ Object
- #update ⇒ Object
- #version_preview ⇒ Object
- #versions ⇒ Object
Instance Method Details
#destroy ⇒ Object
105 106 107 108 |
# File 'app/controllers/postnhost/articles_controller.rb', line 105 def destroy @article.destroy redirect_to articles_url end |
#edit ⇒ Object
29 30 31 32 33 34 |
# File 'app/controllers/postnhost/articles_controller.rb', line 29 def edit @categories = Postnhost::Category.all @users = Postnhost::User.order(:name, :email) @languages = Postnhost::Language.order(default: :desc, name: :asc) @articles = current_user.articles.where.not(id: @article.id).order(:title) end |
#index ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/postnhost/articles_controller.rb', line 10 def index @status = Postnhost::Article.normalize_status(params[:status]) @sort = Postnhost::Article.normalize_sort(params[:sort]) articles = current_user.articles .includes(:language, :article_snapshot) .with_status(@status) .sorted_by(@sort) @pagy, @articles = pagy(:offset, articles, limit: PAGE_SIZE) end |
#new ⇒ Object
22 23 24 25 26 27 |
# File 'app/controllers/postnhost/articles_controller.rb', line 22 def new @article = current_user.articles.new @article.language = Postnhost::Language.blog_default @article.save! redirect_to edit_article_path(@article) end |
#publish ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/postnhost/articles_controller.rb', line 53 def publish result = Postnhost::Publishing::Articles::Publish.call(article: @article) @article.reload respond_to do |format| format.html do redirect_to edit_article_path(@article), **publication_flash(result, success: "Article has been published.") end format.turbo_stream do flash.now[result.success? ? :notice : :alert] = result.success? ? "Article has been published." : result.errors.to_sentence render :publish, status: result.status end end end |
#rollback ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'app/controllers/postnhost/articles_controller.rb', line 83 def rollback version = @article.versions.find_by(id: params[:version_id]) result = version && Postnhost::Publishing::Versions::RestoreDraft.call(record: @article, version:) if result&.success? redirect_to edit_article_path(@article), notice: "Article has been rolled back to the selected version." else redirect_to edit_article_path(@article), alert: result&.errors&.to_sentence || "Version was not found." end end |
#unpublish ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/controllers/postnhost/articles_controller.rb', line 68 def unpublish result = Postnhost::Publishing::Articles::Unpublish.call(article: @article) @article.reload respond_to do |format| format.html do redirect_to edit_article_path(@article), **publication_flash(result, success: "Article has been unpublished.") end format.turbo_stream do flash.now[result.success? ? :notice : :alert] = result.success? ? "Article has been unpublished." : result.errors.to_sentence render :unpublish, status: result.status end end end |
#update ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/postnhost/articles_controller.rb', line 36 def update if @article.update(article_params) @article.sync_publication_schedule! respond_to do |format| format.html { redirect_to articles_path } format.json { head :no_content } format.turbo_stream end else @categories = Postnhost::Category.all @users = Postnhost::User.order(:name, :email) @languages = Postnhost::Language.order(default: :desc, name: :asc) @articles = current_user.articles.where.not(id: @article.id).order(:title) render :edit, status: :unprocessable_content end end |
#version_preview ⇒ Object
100 101 102 103 |
# File 'app/controllers/postnhost/articles_controller.rb', line 100 def version_preview @version = @article.versions.find(params[:version_id]) @versioned_article = @version.reify end |
#versions ⇒ Object
93 94 95 96 97 98 |
# File 'app/controllers/postnhost/articles_controller.rb', line 93 def versions publication_version_id = @article.article_snapshot&.paper_trail_version_id @versions = @article.versions.includes(:item).sort_by do |version| [version.id == publication_version_id ? 0 : 1, -version.created_at.to_i] end end |