Sha256: 39cfc84866d8414769aa377f1be2844f9c6c81e71fdd209fb1b3608386bf7984

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module CMS
  class ViewablesController < RailsAdminCMS::Config.parent_controller
    before_action :authenticate_admin_user!

    # used by the add viewable link
    def create
      current_count = UniqueKey.where(list_key_params).count

      if params[:max] == 'Infinity' || current_count < params[:max].to_i
        unique_key = list_key_params.merge(position: current_count + 1)

        viewable = UniqueKey.create_localized_viewable!(unique_key)

        if unique_key[:viewable_type] != 'Viewable::Block'
          path = rails_admin.edit_path(model_name: unique_key[:viewable_type].to_s.underscore.gsub('/', '~'), id: viewable.id)

          redirect_to path
        else
          redirect_to :back
        end
      else
        redirect_to :back
      end
    end

    # used by [data-js-cms-sortable] element to modify the viewable position within the list
    def update
      unique_key = UniqueKey.find(params[:id])

      if unique_key.update(unique_key_params)
        flash_now!(:success)
      else
        flash_now!(error: unique_key.errors.full_messages.first)
      end

      respond_to do |format|
        format.js {}
      end
    end

    private

    def list_key_params
      params.require(:list_key).permit(:viewable_type, :view_path, :name, :locale)
    end

    def unique_key_params
      params.require(:unique_key).permit(:position)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_admin_cms-0.1.1 app/controllers/cms/viewables_controller.rb
rails_admin_cms-0.0.9 app/controllers/cms/viewables_controller.rb