Class: Formbuilder::ResponseFieldFile

Inherits:
ResponseField
  • Object
show all
Defined in:
app/models/formbuilder/response_field_file.rb

Constant Summary

Constants inherited from ResponseField

Formbuilder::ResponseField::ALLOWED_PARAMS

Instance Attribute Summary

Attributes inherited from ResponseField

#cid, #field_type, #input_field, #options_field, #search_type, #serialized, #sort_as_numeric

Instance Method Summary collapse

Methods inherited from ResponseField

#has_length_validations?, #length_validations, #max, #maxlength, #min, #min_max_length_units, #min_max_validations, #minlength, #normalize_response, #options_array, #validate_response

Instance Method Details

#audit_response(value, all_responses) ⇒ Object



59
60
61
62
# File 'app/models/formbuilder/response_field_file.rb', line 59

def audit_response(value, all_responses)
  return unless value
  all_responses["#{self.id}_filename"] = get_attachments(value).try(:first).try(:upload).try(:raw_filename)
end

#before_response_destroyed(entry) ⇒ Object



68
69
70
# File 'app/models/formbuilder/response_field_file.rb', line 68

def before_response_destroyed(entry)
  remove_entry_attachments(entry.get_responses[self.id.to_s])
end

#get_attachments(value) ⇒ Object



13
14
15
16
17
18
19
# File 'app/models/formbuilder/response_field_file.rb', line 13

def get_attachments(value)
  if value.blank?
    []
  else
    EntryAttachment.where('id IN (?)', value.split(','))
  end
end

#remove_entry_attachments(entry_attachment_ids) ⇒ Object



90
91
92
93
94
95
96
# File 'app/models/formbuilder/response_field_file.rb', line 90

def remove_entry_attachments(entry_attachment_ids)
  return unless entry_attachment_ids.present?

  entry_attachment_ids.to_s.split(',').each do |x|
    EntryAttachment.find_by(id: x).try(:destroy)
  end
end

#render_entry(value, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/formbuilder/response_field_file.rb', line 28

def render_entry(value, opts = {})
  return_str = ""

  return_str << get_attachments(value).map do |attachment|
    String.new.tap do |str|
      str << """
        <a href='#{attachment.upload.url}' target='_blank'>
      """

      if attachment.upload.send(:active_versions).include?(:thumb)
        str << """
          <img src='#{attachment.upload.thumb.url}' /><br />
        """
      end

      str << """
          #{attachment.upload.try(:raw_filename)}
        </a>
      """
    end
  end.join('<br /><br />').squish

  return_str
end

#render_entry_text(value, opts = {}) ⇒ Object



53
54
55
56
57
# File 'app/models/formbuilder/response_field_file.rb', line 53

def render_entry_text(value, opts = {})
  get_attachments(value).map { |attachment|
    attachment.upload.url
  }.join(', ')
end

#render_input(value, opts = {}) ⇒ Object



21
22
23
24
25
26
# File 'app/models/formbuilder/response_field_file.rb', line 21

def render_input(value, opts = {})
  """
    <span class='existing-filename'>#{get_attachments(value).first.try(:upload).try(:raw_filename)}</span>
    <input type='file' name='response_fields[#{self[:id]}][]' id='response_fields_#{self[:id]}' />
  """
end

#sortable_value(value) ⇒ Object



64
65
66
# File 'app/models/formbuilder/response_field_file.rb', line 64

def sortable_value(value)
  value.present? ? 1 : 0
end

#transform_raw_value(raw_value, entry, opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/formbuilder/response_field_file.rb', line 72

def transform_raw_value(raw_value, entry, opts = {})
  raw_value = [raw_value] unless raw_value.is_a?(Array)
  raw_value = raw_value.reject { |x| x.blank? }

  # if the file is already uploaded and we're not uploading another, be sure to keep it
  # @todo currently no way of removing a file
  if raw_value.empty?
    entry.responses_column_was.try(:[], self.id.to_s)
  else
    remove_entry_attachments(entry.get_responses.try(:[], self.id.to_s)) # remove old attachments
    remove_entry_attachments(entry.responses_column_was.try(:[], self.id.to_s)) # remove old attachments

    raw_value.map do |file|
      EntryAttachment.create(upload: file).id
    end.join(',')
  end
end