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
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 128 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 |
# 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 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') } } 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
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 |
# File 'app/controllers/lato_cms/pages_controller.rb', line 102 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 |