Sha256: f9d9defb7442b1a15acad9a5f2847677f2570536ff266be4a6a3191674393463

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

require 'mongoid'

class ContentFolder
  include Mongoid::Document
  #include Mongoid::Timestamps

  field :folder_id, type: String
  field :label, type: String
  has_and_belongs_to_many :documents, :class_name => "DigitalAsset", inverse_of: nil
  #recursively_embeds_many
  embeds_many :child_folders, :class_name => "ContentFolder", :cyclic => true
  accepts_nested_attributes_for :child_folders

  validates_presence_of :folder_id, :label
  #validates_uniqueness_of :folder_id

  def self.lit_center_folder
    find_folder(FolderIds::LIT_CENTER)
  end

  def self.merrill_lynch_folder
    find_folder(FolderIds::MERRILL_LYNCH)
  end

  def self.plan_529_folder
    find_folder(FolderIds::PLANS_529)
  end

  def self.find_folder(id)
    root = ContentFolder.last
    child_folders = root.nil? ? [] : Array(root.child_folders)
    child_folders.find { |folder| id == folder.folder_id } unless child_folders.nil?
  end

  def self.find_folder_by_relative_path(path)
    folders = path.split('/')
    root = ContentFolder.last
    root.nil? ? nil : find_child_folder(root, folders)
  end

  def self.find_child_folder(parent_folder, folders)
    child_folders = Array(parent_folder.child_folders)
    f = child_folders.find { |folder| folders[0] == folder.folder_id }
    unless f.nil?
      folders.shift
      if folders.count > 0
        return find_child_folder(f, folders)
      else
        return f
      end
    end
  end

end

class ContentFolder::FolderIds
  LIT_CENTER = 'lit_center'
  MERRILL_LYNCH = 'merrill_lynch'
  PLANS_529 = '529'
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
daengine-0.7.3 app/models/content_folder.rb
daengine-0.7.2 app/models/content_folder.rb
daengine-0.7.1 app/models/content_folder.rb
daengine-0.7 app/models/content_folder.rb
daengine-0.6.23 app/models/content_folder.rb
daengine-0.6.22 app/models/content_folder.rb