Class: LatoCms::PagesController
- Inherits:
-
ApplicationController
- Object
- Lato::ApplicationController
- ApplicationController
- LatoCms::PagesController
- Defined in:
- app/controllers/lato_cms/pages_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #create_action ⇒ Object
- #destroy_action ⇒ Object
- #index ⇒ Object
- #save_fields_action ⇒ Object
- #show ⇒ Object
- #toggle_component_action ⇒ Object
- #update ⇒ Object
- #update_action ⇒ Object
Instance Method Details
#create ⇒ Object
26 27 28 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 26 def create @page = LatoCms::Page.new(locale: LatoCms.config.locales.first.to_s) end |
#create_action ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 30 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
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 170 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
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 5 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 |
#save_fields_action ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 115 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 141 142 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 62 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 fields_data.each do |field_id, field_data| field = @page.fields.find_or_initialize_by( template_id: @page.template_id, template_component_id: template_component_id, component_id: component_id, field_id: field_id ) field_config = component&.dig('fields', field_id) field_type = field_config&.dig('type') || 'string' case field_type when 'file', 'image' field.save if field.new_record? if field_data[:files].present? Array(field_data[:files]).compact.each { |f| field.files.attach(f) } end if field_data[:remove_file_ids].present? Array(field_data[:remove_file_ids]).reject(&:blank?).each do |file_id_to_remove| field.files.find { |f| f.id == file_id_to_remove.to_i }&.purge end end when 'gallery' field.save if field.new_record? if field_data[:files].present? Array(field_data[:files]).compact.each { |f| field.files.attach(f) } end if field_data[:remove_file_ids].present? Array(field_data[:remove_file_ids]).reject(&:blank?).each do |file_id_to_remove| field.files.find { |f| f.id == file_id_to_remove.to_i }&.purge end end # Persist order: existing IDs in dragged order + any new file IDs appended at end order = Array(field_data[:order]).reject(&:blank?).map(&:to_s) all_ids = field.files.reload.map { |f| f.id.to_s } sorted = order.select { |id| all_ids.include?(id) } new_ids = all_ids - sorted field.value = (sorted + new_ids).to_json when 'multiselect' value = field_data[:value] value = Array(value).reject(&:blank?) field.value = value.to_json else raw_value = field_data.is_a?(ActionController::Parameters) ? field_data[:value] : field_data field.value = raw_value.to_s.presence end unless field.save errors << { field_id: field_id, errors: field.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') } } 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
19 20 21 22 23 24 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 19 def show @page = query_pages.find(params[:id]) @page.fields.load @template = @page.template @template_components = @page.template_components end |
#toggle_component_action ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 144 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 |
#update ⇒ Object
44 45 46 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 44 def update @page = query_pages.find(params[:id]) end |
#update_action ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 48 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 |