Module: Attachable::ClassMethods

Defined in:
lib/attachable.rb

Instance Method Summary collapse

Instance Method Details

#attachable(options = {}) ⇒ Object

Loads the attachable methods, scope, and config into the model.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/attachable.rb', line 12

def attachable(options = {})
  # Store the default prefix for file data
  # Defaults to "file"
  cattr_accessor :attachment_file_prefix
  self.attachment_file_prefix = (options[:file_prefix] || :file).to_s
  
  # Setup the default scope so the file data isn't included by default.
  # Generate the default scope, which includes every column except for the data column.
  # We use this so queries, by default, don't include the file data which could be quite large.
  default_scope { select(column_names.reject { |n| n == "#{attachment_file_prefix}_data" }.collect {|n| "#{table_name}.#{n}" }.join(',')) }
  
  # Include all the important stuff
  include InstanceMethods
end