Class: LatoCms::PagesController
- Inherits:
-
ApplicationController
- Object
- Lato::ApplicationController
- ApplicationController
- LatoCms::PagesController
- 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
Instance Method Summary collapse
- #clone_component_action ⇒ Object
- #create ⇒ Object
- #create_action ⇒ Object
- #destroy_action ⇒ Object
- #index ⇒ Object
- #link_translation_action ⇒ Object
- #save_fields_action ⇒ Object
- #show ⇒ Object
- #sitemap_export_action ⇒ Object
- #toggle_component_action ⇒ Object
- #translations ⇒ Object
- #unlink_translation_action ⇒ Object
- #update ⇒ Object
- #update_action ⇒ Object
Instance Method Details
#clone_component_action ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 152 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 translate = ActiveModel::Type::Boolean.new.cast(params[:translate]) && LatoCms.config.llm_configured? unless source && clone_component_fields(source, @page, template_component_id) respond_to do |format| 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 return end unless translate respond_to do |format| 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') } } end return end # The clone above already happened synchronously and succeeded; only the # translation step (a real network call to the LLM) runs as a # Lato::Operation, so it can never block this request or time it out. operation = Lato::Operation.generate( 'LatoCms::TranslateComponentFieldsJob', { page_id: @page.id, template_component_id: template_component_id }, @session.user_id ) if operation.start redirect_to lato.operation_path(operation) else redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_clone_translate_failed') end end |
#create ⇒ Object
50 51 52 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 50 def create @page = LatoCms::Page.new(locale: LatoCms.config.locales.first.to_s) end |
#create_action ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 54 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_action ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 224 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 |
#index ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 15 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 |
#link_translation_action ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 194 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_action ⇒ Object
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 115 116 117 118 119 120 121 122 123 124 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 86 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 = errors.map { |e| "#{e[:field_id]}: #{e[:errors].join(', ')}" }.join('; ') format.html { redirect_to lato_cms.pages_show_path(@page), alert: } format.json { render json: { errors: errors }, status: :unprocessable_entity } end end end |
#show ⇒ Object
43 44 45 46 47 48 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 43 def show @page = query_pages.find(params[:id]) @page.fields.load @template = @page.template @template_components = @page.template_components end |
#sitemap_export_action ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 29 def sitemap_export_action @operation = Lato::Operation.generate( 'LatoCms::ExportSitemapCsvJob', { lato_spaces_group_id: @session.get(:spaces_group_id) }, @session.user_id ) if @operation.start redirect_to lato.operation_path(@operation) else redirect_to lato_cms.pages_path, alert: t('lato_cms.sitemap_export_failed') end end |
#toggle_component_action ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 126 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..to_sentence } format.json { render json: { errors: @page.errors. }, status: :unprocessable_entity } end end end |
#translations ⇒ Object
190 191 192 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 190 def translations @page = query_pages.find(params[:id]) end |
#unlink_translation_action ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 209 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 |
#update ⇒ Object
68 69 70 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 68 def update @page = query_pages.find(params[:id]) end |
#update_action ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 72 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 |