Module: Extensions::Attachable::ActiveRecord::Base::HasOneAttachment

Extended by:
ActiveSupport::Concern
Defined in:
lib/extensions/attachable/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#attachment_referenceObject Also known as: attachment


151
152
153
# File 'lib/extensions/attachable/active_record/base.rb', line 151

def attachment_reference
  attachment_references.take
end

#attachment_reference=(attachment_reference) ⇒ Object Also known as: attachment=


158
159
160
161
162
163
164
# File 'lib/extensions/attachable/active_record/base.rb', line 158

def attachment_reference=(attachment_reference)
  return self.attachment_reference if self.attachment_reference == attachment_reference

  mark_attachment_as_changed(self.attachment_reference)
  attachment_references.clear
  attachment_references << attachment_reference if attachment_reference
end

#attachment_reference_changed?Boolean Also known as: attachment_changed?

Tracks the the attachment_reference changes and support clear/revert the changes. attachment_reference is a virtual attribute, and changes tracking to such attributes are not well supported in rails 5: https://github.com/rails/rails/issues/25787

Returns:

  • (Boolean)

187
188
189
# File 'lib/extensions/attachable/active_record/base.rb', line 187

def attachment_reference_changed?
  !!@attachment_changed
end

#build_attachment_reference(attributes = {}) ⇒ Object Also known as: build_attachment


167
168
169
170
171
# File 'lib/extensions/attachable/active_record/base.rb', line 167

def build_attachment_reference(attributes = {})
  mark_attachment_as_changed(attachment_reference)
  attachment_references.clear
  attachment_references.build(attributes)
end

#clear_attachment_reference_changeObject Also known as: clear_attachment_change


198
199
200
201
# File 'lib/extensions/attachable/active_record/base.rb', line 198

def clear_attachment_reference_change
  @attachment_changed = false
  @original_attachment = nil
end

#file=(file) ⇒ Object


174
175
176
177
178
179
180
181
182
# File 'lib/extensions/attachable/active_record/base.rb', line 174

def file=(file)
  if file
    build_attachment_reference(file: file)
  else
    return nil if attachment_reference.nil?
    mark_attachment_as_changed(attachment_reference)
    attachment_references.clear
  end
end

#mark_attachment_reference_as_changed(old) ⇒ Object Also known as: mark_attachment_as_changed


192
193
194
195
# File 'lib/extensions/attachable/active_record/base.rb', line 192

def mark_attachment_reference_as_changed(old)
  @attachment_changed = true
  @original_attachment = old
end

#restore_attachment_reference_changeObject Also known as: restore_attachment_change

Restore the attachmenet_reference to its previous value.


205
206
207
208
209
# File 'lib/extensions/attachable/active_record/base.rb', line 205

def restore_attachment_reference_change
  return unless attachment_reference_changed?
  self.attachment_reference = @original_attachment
  clear_attachment_change
end