Sha256: 5f7b36a19a5e5da14a68da9d09bbdf8d09d05b964b2e894e344acc64724ce056

Contents?: true

Size: 933 Bytes

Versions: 17

Compression:

Stored size: 933 Bytes

Contents

require "awesome_nested_set"
require "ds_node"

module DSMediaLibrary
  class Folder < ActiveRecord::Base
    self.table_name = :folders

    def self.root
      klass = Struct.new(:children, :resources)
      children = Folder.roots.to_a.sort_by(&:name)
      resources = DSNode::Resource.where(folder_id: nil).order(:original_file_name)
      klass.new(children, resources)
    end

    acts_as_nested_set

    has_many :resources, -> { order(:original_file_name) }, class_name: "DSNode::Resource"

    def children
      super.order(:name)
    end

    def all_folders
      self.class.all
    end

    before_destroy :move_all_contents_to_parent!

    private

    def move_all_contents_to_parent!
      transaction do
        resources.each do |resource|
          resource.update! folder: parent
        end
        children.each do |resource|
          resource.update! parent: parent
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ds_media_library-0.7.2 app/models/ds_media_library/folder.rb
ds_media_library-0.7.1 app/models/ds_media_library/folder.rb
ds_media_library-0.7.0 app/models/ds_media_library/folder.rb
ds_media_library-0.6.1 app/models/ds_media_library/folder.rb
ds_media_library-0.6.0 app/models/ds_media_library/folder.rb
ds_media_library-0.5.6 app/models/ds_media_library/folder.rb
ds_media_library-0.5.5 app/models/ds_media_library/folder.rb
ds_media_library-0.5.4 app/models/ds_media_library/folder.rb
ds_media_library-0.5.3 app/models/ds_media_library/folder.rb
ds_media_library-0.5.2 app/models/ds_media_library/folder.rb
ds_media_library-0.5.1 app/models/ds_media_library/folder.rb
ds_media_library-0.5.0 app/models/ds_media_library/folder.rb
ds_media_library-0.4.0 app/models/ds_media_library/folder.rb
ds_media_library-0.3.1 app/models/ds_media_library/folder.rb
ds_media_library-0.3.0 app/models/ds_media_library/folder.rb
ds_media_library-0.2.0 app/models/ds_media_library/folder.rb
ds_media_library-0.1.0 app/models/ds_media_library/folder.rb