Class: Spina::Admin::PagesController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/spina/admin/pages_controller.rb

Instance Method Summary collapse

Methods inherited from AdminController

#current_admin_path

Instance Method Details

#childrenObject



100
101
102
103
# File 'app/controllers/spina/admin/pages_controller.rb', line 100

def children
  @children = @page.children.active.sorted
  render layout: false
end

#createObject



26
27
28
29
30
31
32
33
# File 'app/controllers/spina/admin/pages_controller.rb', line 26

def create
  @page = Page.new(page_params.merge(draft: true))
  if @page.save
    redirect_to spina.edit_admin_page_url(@page)
  else
    render turbo_stream: turbo_stream.update(view_context.dom_id(@page, :new_page_form), partial: "new_page_form")
  end
end

#destroyObject



105
106
107
108
109
110
# File 'app/controllers/spina/admin/pages_controller.rb', line 105

def destroy
  flash[:info] = t("spina.pages.deleted")
  @page.destroy

  redirect_to spina.admin_pages_url(resource_id: @page.resource_id)
end

#editObject



35
36
37
38
# File 'app/controllers/spina/admin/pages_controller.rb', line 35

def edit
  add_index_breadcrumb
  add_breadcrumb @page.title
end

#edit_contentObject



40
41
42
43
44
# File 'app/controllers/spina/admin/pages_controller.rb', line 40

def edit_content
  @parts = current_theme.view_templates.find do |view_template|
    view_template[:name].to_s == @page.view_template.to_s
  end&.dig(:parts) || []
end

#edit_templateObject



46
47
48
# File 'app/controllers/spina/admin/pages_controller.rb', line 46

def edit_template
  render layout: false
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/spina/admin/pages_controller.rb', line 8

def index
  add_breadcrumb I18n.t("spina.website.pages"), spina.admin_pages_path

  if params[:resource_id]
    @resource = Resource.find(params[:resource_id])
    @page_templates = Spina::Current.theme.new_page_templates(resource: @resource)
    @pages = @resource.pages.active.roots.includes(:translations).page(params[:page]).per(Spina.config.resource_pages_limit_value)
  else
    @pages = Page.active.sorted.roots.main.includes(:translations)
    @page_templates = Spina::Current.theme.new_page_templates
  end
end

#newObject



21
22
23
24
# File 'app/controllers/spina/admin/pages_controller.rb', line 21

def new
  resource = Resource.find_by(id: params[:resource_id])
  @page = Page.new(view_template: params[:view_template], resource: resource)
end

#sortObject



69
70
71
72
73
74
75
76
# File 'app/controllers/spina/admin/pages_controller.rb', line 69

def sort
  params[:ids].each.with_index do |id, index|
    Page.where(id: id).update_all(position: index + 1)
  end

  flash.now[:info] = t("spina.pages.sorting_saved")
  render_flash
end

#sort_oneObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/spina/admin/pages_controller.rb', line 78

def sort_one
  current_position = @page.position

  if params[:direction] == "up"
    @bottom_page = @page
    @top_page = @target_page = @page.siblings.where(resource_id: @page.resource_id).sorted.where("position < ?", current_position).last
  else
    @bottom_page = @target_page = @page.siblings.where(resource_id: @page.resource_id).sorted.where("position > ?", current_position).first
    @top_page = @page
  end

  if @target_page
    @page.transaction do
      @page.update(position: @target_page.position)
      @target_page.update(position: current_position)
    end
    flash.now[:info] = t("spina.pages.sorting_saved")
  else
    head :ok
  end
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/spina/admin/pages_controller.rb', line 50

def update
  Mobility.locale = @locale
  if @page.update(page_params)
    if @page.saved_change_to_draft? && @page.live?
      flash[:confetti] = t("spina.pages.published")
    else
      flash[:success] = t("spina.pages.saved")
    end

    redirect_to spina.edit_admin_page_url(@page, params: {locale: @locale})
  else
    add_index_breadcrumb
    Mobility.locale = I18n.locale
    add_breadcrumb @page.title
    flash.now[:error] = t("spina.pages.couldnt_be_saved")
    render :edit, status: :unprocessable_entity
  end
end