Module: OuvragesFileUploader::ActiveRecordHelpers

Defined in:
lib/ouvrages_file_uploader.rb

Instance Method Summary collapse

Instance Method Details

#accepts_uploaded_file_for(attachment) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ouvrages_file_uploader.rb', line 9

def accepts_uploaded_file_for(attachment)
  attachment_uploaded_file_id = "#{attachment}_uploaded_file_id"
  attr_reader attachment_uploaded_file_id.to_sym
  attr_accessible attachment_uploaded_file_id.to_sym
  define_method("#{attachment_uploaded_file_id}=") do |id|
    unless id.blank?
      instance_variable_set("@#{attachment_uploaded_file_id}", id)
      if uploaded_file = UploadedFile.where(id: id).first
        self.send("#{attachment}=",  File.open(uploaded_file.file.path))
      end
    end
  end
end