Sha256: 00a42c34344a8c85efbbee9ea8828c014ee1bc251f7731f4e40cf61915a6f698

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

module DSMediaLibrary
  class ResourcesController < ApplicationController
    expose :root, :resource, :folders

    def index
      @root = DSMediaLibrary::Folder.root
      @folders = folders_with_children
      render "ds_media_library/manage/index"
    end

    def new
      @resource = DSNode::Resource.new
      render "ds_media_library/manage/resources/form"
    end

    def create
      resources_params.each do |resource_params|
        DSNode::Resource.create! resource_params
      end
      redirect_to :resources, notice: "Media created"
    end

    def edit
      render "ds_media_library/manage/resources/form"
    end

    def update
      resource.update! resource_params
      redirect_to :resources, notice: "Media updated"
    end

    def destroy
      ids = Array(params[:id]) + Array(params[:resource_ids])
      DSNode::Resource.where(resourcesid: ids).update hidden: true
      redirect_to :resources, notice: "Media deleted"
    end

    def move_many
      DSNode::Resource.where(resourcesid: params[:resource_ids]).update folder_id: params[:folder_id]
      redirect_to :resources, notice: "Media updated"
    end

    private

    def resource
      @resource ||= DSNode::Resource.find(params[:id])
    end

    def folders_with_children
      recurse_folders(DSMediaLibrary::Folder.roots.sort_by(&:name))
    end

    def recurse_folders folders
      folders.flat_map do |folder|
        [folder] + recurse_folders(folder.children)
      end
    end

    def resource_params
      resource_params = params.require(:ds_node_resource).permit(:folder_id, :file)
      # FIXME extract to DSNode gem
      resource_params.merge!(original_file_name: params[:ds_node_resource][:file].original_filename) if params[:ds_node_resource][:file]
      resource_params
    end

    def resources_params
      params[:ds_node_resource][:file].map do |file|
        params.require(:ds_node_resource).permit(:folder_id).merge(file: file)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ds_media_library-0.7.2 app/controllers/ds_media_library/resources_controller.rb
ds_media_library-0.7.1 app/controllers/ds_media_library/resources_controller.rb
ds_media_library-0.7.0 app/controllers/ds_media_library/resources_controller.rb
ds_media_library-0.6.1 app/controllers/ds_media_library/resources_controller.rb