Module: MiniPaperclip::ClassMethods

Defined in:
lib/mini_paperclip/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#has_attached_file(attachment_name, option_config = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mini_paperclip/class_methods.rb', line 5

def has_attached_file(attachment_name, option_config = {})
  define_method(attachment_name) do
    instance_variable_get("@#{attachment_name}") or
      instance_variable_set("@#{attachment_name}", Attachment.new(self, attachment_name, option_config))
  end
  define_method("#{attachment_name}=") do |file|
    a = public_send(attachment_name)
    a.assign(file)
    instance_variable_set("@#{attachment_name}", a)
  end
  after_save do
    if valid?
      instance_variable_get("@#{attachment_name}")&.tap do |a|
        a.dirty? && a.process_and_store
      end
    end
  end
  before_destroy do
    public_send(attachment_name).push_delete_files
  end
  after_commit(on: :destroy) do
    public_send(attachment_name).do_delete_files
  end
  validates_with Validators::MediaTypeSpoofValidator, {
    attributes: attachment_name,
    if: -> { instance_variable_get("@#{attachment_name}")&.dirty? }
  }
end

#validates_attachment(attachment_name, content_type: nil, size: nil, presence: nil, geometry: nil, **opts) ⇒ Object



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
# File 'lib/mini_paperclip/class_methods.rb', line 34

def validates_attachment(attachment_name, content_type: nil, size: nil, presence: nil, geometry: nil, **opts)
  if content_type
    validates_with Validators::ContentTypeValidator, {
      attributes: attachment_name.to_sym,
      **content_type,
      **opts,
    }
  end

  if size
    validates_with Validators::FileSizeValidator, {
      attributes: attachment_name.to_sym,
      **size,
      **opts,
    }
  end

  if !presence.nil?
    validates_with Validators::PresenceValidator, {
      attributes: attachment_name.to_sym,
      presence: presence,
      **opts,
    }
  end

  if geometry
    validates_with Validators::GeometryValidator, {
      attributes: attachment_name.to_sym,
      **geometry,
      **opts,
    }
  end
end