Sha256: e94b5b123ee93f1f06d1f4621e447571bb3d15115229738183d1f0cd1fe16fcd

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module Fae
  class StaticPagesController < ApplicationController
    before_action :set_class_variables
    before_action :set_item, only: [:edit, :update, :destroy]
    helper FormHelper

    def index
      raise 'Missing blocks method inside StaticPages\' child controller' unless fae_pages
      @items = fae_pages.map { |b| b.instance }
    end

    def edit
      build_assocs
      params[:static_page] = true
      render params[:slug]
    end

    def update
      if @item.update(item_params)
        redirect_to @index_path, notice: t('fae.save_notice')
      else
        build_assocs
        flash[:alert] = t('fae.save_error')
        render template: "#{fae.root_path.gsub('/', '')}/content_blocks/#{params[:slug]}"
      end
    end

    private

    # Use callbacks to share common setup or constraints between actions.
    def set_item
      @item = "#{params[:slug]}_page".classify.constantize.instance
      if Fae.use_form_manager
        @form_manager = FormManager.for_model(params, @item)
      end
    end

    # set up variables so that fae partial forms work
    def set_class_variables(class_name = nil)
      @klass_name = class_name || 'Pages'
      @klass_singular = @klass_name.singularize
      @klass_humanized = @klass_singular.humanize
      @index_path = "#{Rails.application.routes.url_helpers.fae_path}/#{@klass_name.underscore.downcase}"
    end

    # Only allow a trusted parameter "white list" through.
    def item_params
      model = @item.class.name.split('::').last.underscore
      params.require(:"#{model}").permit!
    end

    def build_assocs
      @item.class.reflect_on_all_associations.each do |assoc|
        @item.send(:"create_#{assoc.name}", attached_as: assoc.name.to_s) if assoc.macro == :has_one && @item.send(:"#{assoc.name}").blank?
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fae-rails-3.0.0 app/controllers/fae/static_pages_controller.rb
fae-rails-2.2.0 app/controllers/fae/static_pages_controller.rb