Class: LatoCms::PagesController

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

Constant Summary collapse

ADMIN_ONLY_ACTIONS =

Page management and translation links are reserved to the admin role. Operators keep read access plus field editing and component toggles.

%i[
  create create_action
  update update_action
  destroy_action
  translations link_translation_action unlink_translation_action
].freeze
ATTACHMENT_FIELD_TYPES =

Field types whose value lives in Active Storage attachments instead of value, so required must be checked against the attached files.

%w[file image video gallery].freeze

Instance Method Summary collapse

Instance Method Details

#clone_component_actionObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/lato_cms/pages_controller.rb', line 142

def clone_component_action
  @page = query_pages.find(params[:id])
  source = @page.translations.find_by(id: params[:source_page_id])
  template_component_id = params[:template_component_id].to_s

  respond_to do |format|
    if source && clone_component_fields(source, @page, template_component_id)
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.component_cloned') }
      format.json { render json: { message: t('lato_cms.component_cloned') } }
    else
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_clone_failed') }
      format.json { render json: { error: t('lato_cms.component_clone_failed') }, status: :unprocessable_entity }
    end
  end
end

#createObject



40
41
42
# File 'app/controllers/lato_cms/pages_controller.rb', line 40

def create
  @page = LatoCms::Page.new(locale: LatoCms.config.locales.first.to_s)
end

#create_actionObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/lato_cms/pages_controller.rb', line 44

def create_action
  @page = LatoCms::Page.new(create_params.merge(lato_spaces_group_id: @session.get(:spaces_group_id)))

  respond_to do |format|
    if @page.save
      format.html { redirect_to lato_cms.pages_path, notice: t('lato_cms.page_created') }
      format.json { render json: @page }
    else
      format.html { render :create, status: :unprocessable_entity }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end

#destroy_actionObject



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/controllers/lato_cms/pages_controller.rb', line 192

def destroy_action
  @page = query_pages.find(params[:id])

  respond_to do |format|
    if @page.destroy
      format.html { redirect_to lato_cms.pages_path, notice: t('lato_cms.page_deleted') }
      format.json { render json: { message: t('lato_cms.page_deleted') } }
    else
      format.html { redirect_to lato_cms.pages_path, alert: t('lato_cms.page_delete_failed') }
      format.json { render json: { error: t('lato_cms.page_delete_failed') }, status: :unprocessable_entity }
    end
  end
end

#indexObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/lato_cms/pages_controller.rb', line 19

def index
  pages = query_pages
  pages = pages.where(locale: params[:locale]) if params[:locale].present?

  @pages = lato_index_collection(
    pages,
    columns: %i[title permalink locale actions],
    sortable_columns: %i[title permalink locale],
    searchable_columns: %i[title permalink],
    default_sort_by: 'title|ASC',
    pagination: 20
  )
end


162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/lato_cms/pages_controller.rb', line 162

def link_translation_action
  @page = query_pages.find(params[:id])
  other_page = query_pages.find(params[:translation_page_id])

  respond_to do |format|
    if @page.link_translation(other_page)
      format.html { redirect_to lato_cms.pages_translations_path(@page), notice: t('lato_cms.translation_linked') }
      format.json { render json: @page }
    else
      format.html { redirect_to lato_cms.pages_translations_path(@page), alert: t('lato_cms.translation_link_failed') }
      format.json { render json: { error: t('lato_cms.translation_link_failed') }, status: :unprocessable_entity }
    end
  end
end

#save_fields_actionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/lato_cms/pages_controller.rb', line 76

def save_fields_action
  @page = query_pages.find(params[:id])

  template_component_id = params[:template_component_id]
  component_id = params[:component_id]
  fields_data = params[:fields] || {}

  component = LatoCms::TemplateManager.find_component(component_id)
  errors = []

  unless @page.component_effectively_enabled?(template_component_id)
    respond_to do |format|
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_disabled_cannot_save') }
      format.json { render json: { error: t('lato_cms.component_disabled_cannot_save') }, status: :unprocessable_entity }
    end
    return
  end

  template_component = @page.template_components.find { |tc| tc[:template_component_id] == template_component_id.to_s }

  if template_component&.dig(:repeater)
    save_repeater_fields(template_component, component, params[:repeater_items] || {}, params[:repeater_order] || [], errors)
  else
    fields_data.each do |field_id, field_data|
      save_field(component, template_component_id, component_id, field_id, field_id, field_data, errors)
    end
  end

  respond_to do |format|
    if errors.empty?
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.fields_saved') }
      format.json { render json: { message: t('lato_cms.fields_saved'), fields: @page.fields.reload.map(&:as_json) } }
    else
      error_messages = errors.map { |e| "#{e[:field_id]}: #{e[:errors].join(', ')}" }.join('; ')
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: error_messages }
      format.json { render json: { errors: errors }, status: :unprocessable_entity }
    end
  end
end

#showObject



33
34
35
36
37
38
# File 'app/controllers/lato_cms/pages_controller.rb', line 33

def show
  @page = query_pages.find(params[:id])
  @page.fields.load
  @template = @page.template
  @template_components = @page.template_components
end

#toggle_component_actionObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/lato_cms/pages_controller.rb', line 116

def toggle_component_action
  @page = query_pages.find(params[:id])
  template_component_id = params[:template_component_id].to_s
  enabled = ActiveModel::Type::Boolean.new.cast(params[:enabled])

  if @page.component_required?(template_component_id)
    respond_to do |format|
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_required_cannot_disable') }
      format.json { render json: { error: t('lato_cms.component_required_cannot_disable') }, status: :unprocessable_entity }
    end
    return
  end

  @page.set_component_enabled(template_component_id, enabled)

  respond_to do |format|
    if @page.save
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.component_state_updated') }
      format.json { render json: { message: t('lato_cms.component_state_updated') } }
    else
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: @page.errors.full_messages.to_sentence }
      format.json { render json: { errors: @page.errors.full_messages }, status: :unprocessable_entity }
    end
  end
end

#translationsObject



158
159
160
# File 'app/controllers/lato_cms/pages_controller.rb', line 158

def translations
  @page = query_pages.find(params[:id])
end


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

def unlink_translation_action
  @page = query_pages.find(params[:id])
  other_page = query_pages.find(params[:translation_page_id])

  respond_to do |format|
    if @page.unlink_translation(other_page)
      format.html { redirect_to lato_cms.pages_translations_path(@page), notice: t('lato_cms.translation_unlinked') }
      format.json { render json: @page }
    else
      format.html { redirect_to lato_cms.pages_translations_path(@page), alert: t('lato_cms.translation_unlink_failed') }
      format.json { render json: { error: t('lato_cms.translation_unlink_failed') }, status: :unprocessable_entity }
    end
  end
end

#updateObject



58
59
60
# File 'app/controllers/lato_cms/pages_controller.rb', line 58

def update
  @page = query_pages.find(params[:id])
end

#update_actionObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/lato_cms/pages_controller.rb', line 62

def update_action
  @page = query_pages.find(params[:id])

  respond_to do |format|
    if @page.update(update_params)
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.page_updated') }
      format.json { render json: @page }
    else
      format.html { render :update, status: :unprocessable_entity }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end