Module: Progstr::Filer::ActiveRecordInstanceMethods

Defined in:
lib/filer/activerecord.rb

Instance Method Summary collapse

Instance Method Details

#_attachmentsObject



36
37
38
39
# File 'lib/filer/activerecord.rb', line 36

def _attachments
  @_filer_attachments ||= {}
  @_filer_attachments
end

#_attachments_to_deleteObject



41
42
43
44
# File 'lib/filer/activerecord.rb', line 41

def _attachments_to_delete
  @_filer_attachments_to_delete ||= []
  @_filer_attachments_to_delete
end

#_delete_expired_attachmentsObject



101
102
103
104
105
106
# File 'lib/filer/activerecord.rb', line 101

def _delete_expired_attachments
  _attachments_to_delete.each do |attachment|
    uploader = self.class._uploaders[attachment.attribute]
    uploader.delete_attachment(attachment) unless uploader.nil?
  end
end

#_expire_all_attachmentsObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/filer/activerecord.rb', line 113

def _expire_all_attachments
  # trigger entity load
  dummy = read_attribute(:dummy)
  self.class._uploaders.each do |item|
    attribute = item[0]
    #load attachment object
    dummy = _get_attachment(attribute)
    #schedule it for deletion
    _set_attachment(attribute, nil)
  end
end

#_filer_after_saveObject



108
109
110
111
# File 'lib/filer/activerecord.rb', line 108

def _filer_after_save
  _upload_attachments
  _delete_expired_attachments
end

#_filer_before_deleteObject



125
126
127
128
# File 'lib/filer/activerecord.rb', line 125

def _filer_before_delete
  _expire_all_attachments
  _delete_expired_attachments
end

#_get_attachment(attribute) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/filer/activerecord.rb', line 46

def _get_attachment(attribute)
  if _attachments[attribute].nil?
    id = read_attribute(attribute)
    uploader_class = self.class._uploaders[attribute].class
    if id.nil?
      _attachments[attribute] = Attachment.empty(uploader_class)
    else
      _attachments[attribute] = Attachment.from_id(uploader_class, attribute, id)
    end
  else
    _attachments[attribute]
  end
end

#_set_attachment(attribute, value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/filer/activerecord.rb', line 60

def _set_attachment(attribute, value)
  old_attachment = _get_attachment(attribute)
  unless old_attachment.blank?
    _attachments_to_delete << old_attachment
  end

  uploader_class = self.class._uploaders[attribute].class
  if value.kind_of?(String)
    attachment = nil
    if value.include?("{")
      attachment = Attachment.from_json(uploader_class, attribute, value)
    else
      attachment = Attachment.from_id(uploader_class, attribute, value)
    end
    _attachments[attribute] = attachment
    write_attribute(attribute, attachment.id)
  elsif !value.nil? #file-like
    attachment = Attachment.from_file(uploader_class, attribute, value)
    _attachments[attribute] = attachment
    write_attribute(attribute, attachment.id)
  else
    _attachments[attribute] = nil
    write_attribute(attribute, nil)
  end
end

#_upload_attachment(attribute) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/filer/activerecord.rb', line 93

def _upload_attachment(attribute)
  attachment = _get_attachment(attribute)
  if attachment.need_upload?
    uploader = self.class._uploaders[attribute]
    uploader.upload_attachment(attachment) unless uploader.nil?
  end
end

#_upload_attachmentsObject



86
87
88
89
90
91
# File 'lib/filer/activerecord.rb', line 86

def _upload_attachments
  self.class._uploaders.each do |item|
    attribute = item[0]
    _upload_attachment(attribute)
  end
end