Sha256: 74061f8bb839d71255dd7188776119a430eedfde3787ed2c59b74ca4918ad3c8

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

require 'mongoid'

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

  field :folder_id, type: String
  field :label, type: String
  field :document_ids, type: Array, default: []

  #recursively_embeds_many
  embeds_many :child_folders, :class_name => "ContentFolder", :cyclic => true
  accepts_nested_attributes_for :child_folders

  key :folder_id

  validates_presence_of :folder_id, :label
  validate :validate_has_content

  def validate_has_content
    if child_folders.empty? && (document_ids.nil? || document_ids.empty?)
      errors[:base] << 'Either child_folders or document_ids are required'
    end
  end

  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
end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
daengine-0.6.21 app/models/content_folder.rb
daengine-0.6.20 app/models/content_folder.rb
daengine-0.6.19 app/models/content_folder.rb
daengine-0.6.18 app/models/content_folder.rb
daengine-0.6.17 app/models/content_folder.rb