Module: Models::DragonflyAccessor::ClassMethods

Defined in:
app/concerns/models/dragonfly_accessor.rb

Instance Method Summary collapse

Instance Method Details

#dragonfly_accessor(attribute, opts = {}, &config_block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/concerns/models/dragonfly_accessor.rb', line 15

def dragonfly_accessor(attribute, opts={}, &config_block)
  super(attribute, opts) do
    config_block.call if config_block

    mime_attribute = "#{attribute}_mime_type".to_sym if opts.delete(:mime_type)

    after_assign do |attachment|
      attrs = { "#{attribute}_name": FileNamingHelper.sanitize_file_name(attachment.name) }
      attrs[mime_attribute] = attachment.mime_type if mime_attribute
      self.assign_attributes(attrs)
    end

    after_unassign do |attachment|
      self.assign_attributes(mime_attribute => nil) if mime_attribute
    end

    storage_options do |a|
      {
        path: a.app.datastore.send(:generate_uid, a.name || "file")
      }
    end

  end
end