Class: Tessa::DynamicExtensions::SingleRecord
- Inherits:
-
Tessa::DynamicExtensions
- Object
- Tessa::DynamicExtensions
- Tessa::DynamicExtensions::SingleRecord
- Defined in:
- lib/tessa/model/dynamic_extensions.rb
Instance Attribute Summary
Attributes inherited from Tessa::DynamicExtensions
Instance Method Summary collapse
Methods inherited from Tessa::DynamicExtensions
Constructor Details
This class inherits a constructor from Tessa::DynamicExtensions
Instance Method Details
#build(mod) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tessa/model/dynamic_extensions.rb', line 17 def build(mod) mod.class_eval <<~CODE, __FILE__, __LINE__ + 1 def #{name} # has_one_attached defines the getter using class_eval so we can't call # super() here. if #{name}_attachment.present? return Tessa::ActiveStorage::AssetWrapper.new(#{name}_attachment) end # fall back to old Tessa fetch if not present if field = self.class.tessa_fields["#{name}".to_sym] @#{name} ||= fetch_tessa_remote_assets(field.id(on: self)) end end def #{field.id_field} # Use the attachment's key return #{name}_attachment.key if #{name}_attachment.present? # fallback to Tessa's database column super end def #{name}=(attachable) # Every new upload is going to ActiveStorage a = @active_storage_attached_#{name} ||= ::ActiveStorage::Attached::One.new("#{name}", self, dependent: :purge_later) case attachable when Tessa::AssetChangeSet attachable.changes.select(&:remove?).each { a.detach } attachable.changes.select(&:add?).each do |change| next if #{field.id_field} == change.id a.attach(change.id) end when nil a.detach else a.attach(attachable) end # overwrite the tessa ID in the database self.#{field.id_field} = nil end def attributes super.merge({ '#{field.id_field}' => #{field.id_field}, '_tessa_#{field.id_field}' => super['#{field.id_field}'] }) end CODE mod end |