Method: Infold::ModelWriter#active_storage_attachment_code

Defined in:
lib/infold/writers/model_writer.rb

#active_storage_attachment_codeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/infold/writers/model_writer.rb', line 45

def active_storage_attachment_code
  code = ''
  @resource.active_storage_fields&.each do |active_storage_field|
    base = "has_one_attached :#{active_storage_field.name}"
    thumb = active_storage_field.active_storage.thumb
    if thumb
      code += <<-CODE.gsub(/^\s+/, '')
        #{base} do |attachable|
        [TAB]attachable.variant :thumb, resize_to_#{thumb.kind}: [#{thumb.width}, #{thumb.height}]
        end
      CODE
    else
      code += "#{base}\n"
    end
    code +=  <<-CODE.gsub(/^\s+/, '')
      attr_accessor :remove_#{active_storage_field.name}
      before_validation { self.#{active_storage_field.name} = nil if remove_#{active_storage_field.name}.to_s == '1' }
    CODE
    code += "\n"
  end
  indent(code, 2).presence
end