Class: Tessa::DynamicExtensions::MultipleRecord

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tessa/model/dynamic_extensions.rb', line 67

def build(mod)
  mod.class_eval "      def \#{name}\n        field = self.class.tessa_fields[\"\#{name}\".to_sym]\n        tessa_ids = field.id(on: self) - \#{name}_attachments.map(&:key)\n        \n        @\#{name} ||= [\n          *\#{name}_attachments.map { |a| Tessa::ActiveStorage::AssetWrapper.new(a) },\n        ]\n      end\n\n      def \#{field.id_field}\n        [\n          # Use the attachment's key\n          *\#{name}_attachments.map(&:key),\n        ]\n      end\n\n      def \#{name}=(attachables)\n        # Every new upload is going to ActiveStorage\n        a = @active_storage_attached_\#{name} ||=\n          ::ActiveStorage::Attached::Many.new(\"\#{name}\", self, dependent: :purge_later)\n\n        case attachables\n        when Tessa::AssetChangeSet\n          attachables.changes.select(&:remove?).each do |change|\n            if existing = \#{name}_attachments.find { |a| a.key == change.id }\n              existing.destroy\n            else\n              ids = self.\#{field.id_field}\n              ids&.delete(change.id.to_i)\n              self.\#{field.id_field} = ids&.any? ? ids : nil\n            end\n          end\n          attachables.changes.select(&:add?).each do |change|\n            next if \#{field.id_field}.include? change.id\n\n            a.attach(change.id)\n          end\n        when nil\n          a.detach\n          self.\#{field.id_field} = nil\n        else\n          a.attach(*attachables)\n          self.\#{field.id_field} = nil\n        end\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