Class: Journeybook::Admin::PagesController

Inherits:
Journeybook::ApplicationController show all
Defined in:
app/controllers/journeybook/admin/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_componentObject



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

def add_component
  component = component_for(params[:component_name])

  unless component
    redirect_to new_component_admin_page_path(@page, site_id: @page.site_id), alert: "Component not found."
    return
  end

  component_index = ComponentParser.new.parse(@page.markdown).length
  markdown = ComponentTagUpdater.new.append(@page.markdown, component.name, blank_props(component))

  if @page.update(markdown: markdown)
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout", component: component_index), notice: "Component added."
  else
    @components = available_components
    @selected_component = component
    render :new_component, status: :unprocessable_entity
  end
end

#componentObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 56

def component
  component_index = params[:component_index].to_i
  markdown = ComponentTagUpdater.new.update(@page.markdown, component_index, component_props)

  if @page.update(markdown: markdown)
    redirect_params = { site_id: @page.site_id, tab: "layout" }
    redirect_params[:component] = component_index unless params[:save_and_back].present?
    redirect_to edit_admin_page_path(@page, **redirect_params), notice: "Component updated."
  else
    set_sites
    set_editor_state
    render :edit, status: :unprocessable_entity
  end
rescue IndexError
  redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), alert: "Component not found."
end

#createObject



27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 27

def create
  @page = Page.new(page_params)
  template = template_for(params[:template_key], @page.site)
  apply_template_path(template) if template

  if @page.save
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id), notice: "Page created."
  else
    render :new, status: :unprocessable_entity
  end
end

#delete_componentObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 130

def delete_component
  markdown = ComponentTagUpdater.new.delete(@page.markdown, component_index)

  if @page.update(markdown: markdown)
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), notice: "Component deleted."
  else
    set_sites
    set_editor_state
    render :edit, status: :unprocessable_entity
  end
rescue IndexError
  redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), alert: "Component not found."
end

#destroyObject



144
145
146
147
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 144

def destroy
  @page.destroy
  redirect_to admin_pages_path(site_id: current_site&.id), notice: "Page deleted."
end

#draftObject



173
174
175
176
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 173

def draft
  @page.update!(status: "draft")
  redirect_to admin_pages_path(site_id: @page.site_id), notice: "Page moved to draft."
end

#duplicateObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 178

def duplicate
  duplicate = @page.dup
  duplicate.title = duplicate_title
  duplicate.slug = nil
  duplicate.path = duplicate_path
  duplicate.status = "draft"
  duplicate.published_at = nil

  if duplicate.save
    redirect_to edit_admin_page_path(duplicate, site_id: duplicate.site_id), notice: "Page duplicated as a draft."
  else
    redirect_to admin_pages_path(site_id: @page.site_id), alert: duplicate.errors.full_messages.to_sentence
  end
end

#editObject



20
21
22
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 20

def edit
  set_editor_state
end

#errorsObject



161
162
163
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 161

def errors
  @publishing_errors = @page.component_validation_errors
end

#indexObject



7
8
9
10
11
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 7

def index
  scope = current_site ? current_site.pages.includes(:site) : Page.none
  scope = scope.by_page_type(params[:type]) if params[:type].present?
  @pages = scope.order(updated_at: :desc)
end

#move_componentObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 98

def move_component
  markdown = ComponentTagUpdater.new.move(@page.markdown, component_index, params[:direction])

  if @page.update(markdown: markdown)
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), notice: "Component moved."
  else
    set_sites
    set_editor_state
    render :edit, status: :unprocessable_entity
  end
rescue IndexError
  redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), alert: "Component cannot be moved."
end

#newObject



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

def new
  @page = Page.new(site: current_site || Site.order(:name).first, locale: current_site&.default_locale || "en", status: "draft", markdown: starter_markdown)
  @templates = Journeybook.templates.for_site(@page.site)
  @selected_template = template_for(params[:template], @page.site)
  apply_template if @selected_template
end

#new_componentObject



73
74
75
76
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 73

def new_component
  @components = available_components.sort_by { |component| component.name.to_s }
  @selected_component = component_for(params[:component]) || @components.first
end

#previewObject



149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 149

def preview
  @journeybook_page = @page
  @journeybook_html = PageRenderer.new.render(
    @page,
    form_authenticity_token: form_authenticity_token,
    component_index: preview_component_index,
    component_anchors: preview_component_index.nil?
  )

  render "journeybook/admin/pages/public_preview", layout: @page.layout_name
end

#publishObject



165
166
167
168
169
170
171
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 165

def publish
  if @page.update(status: "published", published_at: Time.current)
    redirect_to admin_pages_path(site_id: @page.site_id), notice: "Page published."
  else
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), alert: @page.errors.full_messages.to_sentence
  end
end

#reorder_componentsObject



112
113
114
115
116
117
118
119
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 112

def reorder_components
  markdown = ComponentTagUpdater.new.reorder(@page.markdown, component_indexes)
  @page.update!(markdown: markdown)

  head :no_content
rescue ArgumentError
  head :unprocessable_entity
end

#sourceObject



24
25
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 24

def source
end

#toggle_component_visibilityObject



121
122
123
124
125
126
127
128
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 121

def toggle_component_visibility
  markdown = ComponentTagUpdater.new.toggle_visibility(@page.markdown, component_index)
  @page.update!(markdown: markdown)

  redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), notice: "Component visibility updated."
rescue IndexError
  redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: "layout"), alert: "Component not found."
end

#updateObject



39
40
41
42
43
44
45
46
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 39

def update
  if @page.update(page_params)
    redirect_to edit_admin_page_path(@page, site_id: @page.site_id, tab: params[:tab].presence || "settings"), notice: "Page updated."
  else
    set_editor_state
    render :edit, status: :unprocessable_entity
  end
end

#update_sourceObject



48
49
50
51
52
53
54
# File 'app/controllers/journeybook/admin/pages_controller.rb', line 48

def update_source
  if @page.update(source_params)
    redirect_to source_admin_page_path(@page, site_id: @page.site_id), notice: "Page source updated."
  else
    render :source, status: :unprocessable_entity
  end
end