Class: Jigsaw::PagesController

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

Constant Summary collapse

DEFAULT_GRID_CONFIG =
{
  "type"        => "grid",
  "areas"       => [["main"]],
  "columns"     => ["1fr"],
  "rows"        => ["1fr"],
  "gridWidth"   => "100%",
  "gridHeight"  => "100%",
  "rowGap"      => 8,
  "colGap"      => 8,
  "rowGapUnit"  => "px",
  "colGapUnit"  => "px"
}.freeze

Instance Method Summary collapse

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/jigsaw/pages_controller.rb', line 28

def create
  @page = Page.new(page_params)
  if @page.save
    layout_template_id = params[:page][:layout_template_id]
    if layout_template_id.present?
      lt = LayoutTemplate.find(layout_template_id)
      @page.create_layout!(
        name: "#{@page.title} Layout",
        config: lt.config.deep_dup,
        layout_template: lt,
        linked_to_template: true
      )
    else
      @page.create_layout!(name: "#{@page.title} Layout", config: DEFAULT_GRID_CONFIG.deep_dup)
    end
    @page.layout.sync_slots
    redirect_to edit_page_path(@page), notice: "Page created"
  else
    @layout_templates = LayoutTemplate.order(:name)
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



79
80
81
82
# File 'app/controllers/jigsaw/pages_controller.rb', line 79

def destroy
  @page.destroy!
  redirect_to pages_path, notice: "Page deleted"
end

#editObject



51
52
53
54
55
56
# File 'app/controllers/jigsaw/pages_controller.rb', line 51

def edit
  if @layout && !@layout.linked_to_template?
    @layout.sync_slots
  end
  @slots = @layout&.slots&.order(:position) || []
end

#indexObject



19
20
21
# File 'app/controllers/jigsaw/pages_controller.rb', line 19

def index
  @pages = Page.order(:title)
end

#newObject



23
24
25
26
# File 'app/controllers/jigsaw/pages_controller.rb', line 23

def new
  @page = Page.new
  @layout_templates = LayoutTemplate.order(:name)
end

#showObject



68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/jigsaw/pages_controller.rb', line 68

def show
  @page = Page.find_by!(path: params[:path].to_s.delete_prefix("/"))
  @layout = @page.layout

  if @layout
    @slots = @layout.slots.order(:position)
  else
    @slots = []
  end
end


84
85
86
87
# File 'app/controllers/jigsaw/pages_controller.rb', line 84

def unlink_template
  @layout&.unlink_from_template!
  redirect_to edit_page_path(@page), notice: "Unlinked from layout template"
end

#updateObject



58
59
60
61
62
63
64
65
66
# File 'app/controllers/jigsaw/pages_controller.rb', line 58

def update
  if @page.update(page_params)
    @page.layout&.sync_slots unless @page.layout&.linked_to_template?
    redirect_to edit_page_path(@page), notice: "Page updated"
  else
    @slots = @layout&.slots&.order(:position) || []
    render :edit, status: :unprocessable_entity
  end
end