Module: SimpleAttachments::AttachmentModel::Helpers

Defined in:
lib/simple_attachments/attachment_model.rb

Instance Method Summary collapse

Instance Method Details

#attached_to(name, options = {}) ⇒ Object

Mark model as attachment and create association.

Syntax is identical to belongs_to.

attached_to :post

Do not use more than one time in one model. However you can use polymorphic associations.

attached_to :container, :polymorphic => true


16
17
18
19
20
21
22
23
24
# File 'lib/simple_attachments/attachment_model.rb', line 16

def attached_to(name, options = {})
  reflection = belongs_to name, options
  self.class.send(:attr_accessor, :container_name)
  self.container_name = reflection.name
  before_save :save_file
  after_destroy :destroy_file
  send :extend, ClassMethods
  send :include, InstanceMethods
end