Class: Postnhost::PagesController

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



66
67
68
69
# File 'app/controllers/postnhost/pages_controller.rb', line 66

def destroy
  @page.destroy
  redirect_to pages_path
end

#editObject



20
# File 'app/controllers/postnhost/pages_controller.rb', line 20

def edit; end

#indexObject



8
9
10
11
# File 'app/controllers/postnhost/pages_controller.rb', line 8

def index
  @pages = Postnhost::Page.includes(:page_snapshot).order(updated_at: :desc)
  @static_page_slugs = static_page_slugs
end

#newObject



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

def new
  @page = current_user.pages.new
  @page.language = Postnhost::Language.blog_default
  @page.save!
  redirect_to edit_page_path(@page)
end

#publishObject



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

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

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

#unpublishObject



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

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

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

#updateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/postnhost/pages_controller.rb', line 22

def update
  if @page.update(page_params)
    respond_to do |format|
      format.html { redirect_to edit_page_path(@page) }
      format.json { head :no_content }
      format.turbo_stream { head :no_content }
    end
  else
    respond_to do |format|
      format.html { render :edit, status: :unprocessable_content }
      format.json { render json: { errors: @page.errors.full_messages }, status: :unprocessable_content }
      format.turbo_stream { head :unprocessable_content }
    end
  end
end