Module: Droom::Folders::FolderClassMethods

Defined in:
lib/droom/folders.rb

Instance Method Summary collapse

Instance Method Details

#has_folder(options = {}) ⇒ Object

In most cases all you need is a ‘has_folder` line in the model class definition.

class Thing < ActiveRecord::Base
  has_folder
end

This will bring in the folder and document associations and all the instance methods involved in adding and removing documents. The folder itself is lazy-loaded: the first time it is requested, it will be created.

You can specify that the folder should be created inside another folder. For example, the document folder associated with an agenda_category is always created within the folder of its event, so that the filing system reflects the organisation of the event. To achieve this, pass in the name of an associate as the ‘:within` option:

class Subthing < ActiveRecord::Base
  belongs_to :thing
  has_folder :within => :thing
end


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/droom/folders.rb', line 41

def has_folder( options={} )
  return if has_folder?
  has_one :folder, :as => :holder, :class_name => "Droom::Folder"
  has_many :documents, :through => :folder, :class_name => "Droom::Document"
  # The :within option is stored as a class variable that will be consulted when the folder is created.
  class_variable_set(:"@@parent_folder_holder", options[:within])
  
  class_eval {
    extend Droom::Folders::FolderedClassMethods
    include Droom::Folders::FolderedInstanceMethods
    alias_method_chain :folder, :lazy_load
  }
end

#has_folder?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/droom/folders.rb', line 17

def has_folder?
  false
end