Sha256: 370d51afe7c3529595d0ca9df22793fef0548b4a7fc2404b94c9c39e83aa94ab

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# encoding: utf-8

module Admin
  class PageFilesController < Admin::AdminController
    before_action :find_page
    before_action :find_page_file,   only: [:show, :edit, :update, :destroy]
    before_action :redirect_to_page, only: [:index, :show, :new, :edit]

    require_authorization(
      PageFile,
      proc { @page_file },
      collection: [:index, :reorder, :new, :create]
    )

    def index
    end

    def reorder
      if params[:ids]
        files = Array(params[:ids]).map { |id| PageFile.find(id) }
        files.each_with_index { |f, i| f.update(position: i) }
      end
      if request.xhr?
        render text: "ok"
      else
        redirect_to_page
      end
    end

    def show
    end

    def new
    end

    def create
      @page_file = @page.files.new
      @page_file.update(page_file_params.merge(locale: @locale))
      flash[:notice] = "Error uploading file!" unless @page_file.valid?
      redirect_to_page
    end

    def edit
    end

    def update
      flash[:notice] = if @page_file.update(page_file_params)
                         "File updated"
                       else
                         "Error updating file!"
                       end
      redirect_to_page
    end

    def destroy
      @page_file.destroy
      flash[:notice] = "File deleted"
      redirect_to_page
    end

    protected

    def page_file_params
      params.require(:page_file).permit(:name, :filename, :file)
    end

    def redirect_to_page
      redirect_to(edit_admin_page_path(@locale, @page, anchor: "files"))
    end

    def find_page
      @page = Page.find(params[:page_id]).localize(@locale)
    end

    def find_page_file
      @page_file = @page.files.find(params[:id])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 app/controllers/admin/page_files_controller.rb