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 |
# File 'lib/tessa/model/dynamic_extensions.rb', line 17 def build(mod) mod.class_eval " def \#{name}\n # has_one_attached defines the getter using class_eval so we can't call\n # super() here.\n if \#{name}_attachment.present?\n return Tessa::ActiveStorage::AssetWrapper.new(\#{name}_attachment)\n end\n end\n\n def \#{field.id_field}\n # Use the attachment's key\n return \#{name}_attachment.key if \#{name}_attachment.present?\n end\n\n def \#{name}=(attachable)\n # Every new upload is going to ActiveStorage\n a = @active_storage_attached_\#{name} ||=\n ::ActiveStorage::Attached::One.new(\"\#{name}\", self, dependent: :purge_later)\n \n case attachable\n when Tessa::AssetChangeSet\n attachable.changes.select(&:remove?).each { a.detach }\n attachable.changes.select(&:add?).each do |change|\n next if \#{field.id_field} == change.id\n\n a.attach(change.id)\n end\n when nil\n a.detach\n else\n a.attach(attachable)\n end\n\n # overwrite the tessa ID in the database\n self.\#{field.id_field} = nil\n end\n\n def attributes\n super.merge({\n '\#{field.id_field}' => \#{field.id_field},\n '_tessa_\#{field.id_field}' => super['\#{field.id_field}']\n })\n end\n CODE\n mod\nend\n", __FILE__, __LINE__ + 1 |