Class: Postnhost::ArticleVariantsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/postnhost/article_variants_controller.rb

Constant Summary collapse

PAGE_SIZE =
15

Instance Method Summary collapse

Instance Method Details

#bulk_destroyObject



104
105
106
107
108
109
110
# File 'app/controllers/postnhost/article_variants_controller.rb', line 104

def bulk_destroy
  variants = @article.article_variants.where(id: params[:ids]).where.not(generating: true)
  count = variants.count
  variants.destroy_all

  redirect_to article_variants_path(@article), notice: "#{count} translation(s) deleted successfully."
end

#bulk_publishObject



88
89
90
91
92
93
94
# File 'app/controllers/postnhost/article_variants_controller.rb', line 88

def bulk_publish
  result = Postnhost::Publishing::ArticleVariants::PublishMany.call(article: @article, ids: params[:ids])
  error = result.errors.to_sentence unless result.success?
  error = "#{result.value[:failures].count} translation(s) failed." if result.success? && result.value[:failures].any?

  redirect_to article_variants_path(@article), **(error.present? ? { alert: error } : {})
end

#bulk_unpublishObject



96
97
98
99
100
101
102
# File 'app/controllers/postnhost/article_variants_controller.rb', line 96

def bulk_unpublish
  result = Postnhost::Publishing::ArticleVariants::UnpublishMany.call(article: @article, ids: params[:ids])
  error = result.errors.to_sentence unless result.success?
  error = "#{result.value[:failures].count} translation(s) failed." if result.success? && result.value[:failures].any?

  redirect_to article_variants_path(@article), **(error.present? ? { alert: error } : {})
end

#destroyObject



83
84
85
86
# File 'app/controllers/postnhost/article_variants_controller.rb', line 83

def destroy
  @article_variant.destroy
  redirect_to article_variants_path(@article)
end

#editObject



35
# File 'app/controllers/postnhost/article_variants_controller.rb', line 35

def edit; end

#indexObject



12
13
14
15
16
17
# File 'app/controllers/postnhost/article_variants_controller.rb', line 12

def index
  variants = @article.article_variants.includes(:language, :article_variant_snapshot).order(:created_at)
  @pagy, @variants = pagy(:offset, variants, limit: PAGE_SIZE)
  @available_languages = Postnhost::Language.where.not(id: @article.article_variants.select(:language_id))
  @available_languages = @available_languages.where.not(id: @article.language_id) if @article.language_id.present?
end

#newObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/postnhost/article_variants_controller.rb', line 19

def new
  @article_variant = @article.article_variants.new
  @article_variant.language = Postnhost::Language.find_by(id: params[:language_id]) if params[:language_id]
  @article_variant.title = @article.title
  @article_variant.title_tag = @article.title_tag
  @article_variant.og_title = @article.og_title
  @article_variant.schema_headline = @article.schema_headline
  @article_variant.meta_description = @article.meta_description
  @article_variant.custom_excerpt = @article.custom_excerpt
  @article_variant.use_excerpt_as_meta_description = @article.use_excerpt_as_meta_description
  @article_variant.content = @article.content
  @article_variant.save!

  redirect_to edit_article_variant_path(@article, @article_variant)
end

#publishObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/postnhost/article_variants_controller.rb', line 51

def publish
  result = Postnhost::Publishing::ArticleVariants::Publish.call(article_variant: @article_variant)
  @article_variant.reload
  error = result.errors.to_sentence unless result.success?

  respond_to do |format|
    format.html do
      redirect_to edit_article_variant_path(@article, @article_variant), **(error.present? ? { alert: error } : {})
    end
    format.turbo_stream do
      flash.now[:alert] = error if error.present?
      render :publish, status: result.status
    end
  end
end

#unpublishObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/postnhost/article_variants_controller.rb', line 67

def unpublish
  result = Postnhost::Publishing::ArticleVariants::Unpublish.call(article_variant: @article_variant)
  @article_variant.reload
  error = result.errors.to_sentence unless result.success?

  respond_to do |format|
    format.html do
      redirect_to edit_article_variant_path(@article, @article_variant), **(error.present? ? { alert: error } : {})
    end
    format.turbo_stream do
      flash.now[:alert] = error if error.present?
      render :unpublish, status: result.status
    end
  end
end

#updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/postnhost/article_variants_controller.rb', line 37

def update
  if @article_variant.update(variant_params)
    respond_to do |format|
      format.html do
        redirect_to edit_article_variant_path(@article, @article_variant)
      end
      format.json { head :no_content }
      format.turbo_stream
    end
  else
    render :edit, status: :unprocessable_content
  end
end