Module: Document::Associations

Included in:
SampleManifest
Defined in:
app/models/document.rb

Instance Method Summary collapse

Instance Method Details

#has_uploaded_document(field, differentiator: field.to_s) ⇒ Object

Adds accessors for named fields and attaches documents to them


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/document.rb', line 9

def has_uploaded_document(field, differentiator: field.to_s)
  # Options
  #  differentiator - this is a string used to separate multiple documents related to your model
  #     for example, you can have both a "generated" and an "uploaded" document in one Sample Manifest

  line = __LINE__ + 1
  class_eval(%Q{
    has_one(:#{field}_document, ->(){ where(:documentable_extended => differentiator) }, :class_name => "Document", :as => :documentable, :dependent => :destroy
      )

    def #{field}
      self.#{field}_document
    end

    def #{field}=(file)
      create_#{field}_document(uploaded_data: file, documentable_extended: '#{differentiator}') unless file.blank?
    end
  }, __FILE__, line)
end