Class: Tessa::DynamicExtensions::SingleRecord

Inherits:
Tessa::DynamicExtensions show all
Defined in:
lib/tessa/model/dynamic_extensions.rb

Instance Attribute Summary

Attributes inherited from Tessa::DynamicExtensions

#field

Instance Method Summary collapse

Methods inherited from Tessa::DynamicExtensions

#initialize, #name

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
# 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\n        # fall back to old Tessa fetch if not present\n        if field = self.class.tessa_fields[\"\#{name}\".to_sym]\n          @\#{name} ||= fetch_tessa_remote_assets(field.id(on: self))\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\n        # fallback to Tessa's database column\n        super\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.each do |change|\n            a.attach(change.id) if change.add?\n            a.detach if change.remove?\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        })\n      end\n    CODE\n  mod\nend\n", __FILE__, __LINE__ + 1