Class: MiniPaperclip::Shoulda::Matchers::ValidateAttachmentGeometryMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb

Constant Summary collapse

CallError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(attachment_name) ⇒ ValidateAttachmentGeometryMatcher

Returns a new instance of ValidateAttachmentGeometryMatcher.



23
24
25
26
27
28
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 23

def initialize(attachment_name)
  @attachment_name = attachment_name.to_sym
  @width = {}
  @height = {}
  @format = nil
end

Instance Method Details

#failure_messageObject



61
62
63
64
65
66
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 61

def failure_message
  [
    "Attachment :#{@attachment_name} got details",
    @subject.errors.details[@attachment_name]
  ].join("\n")
end

#failure_message_when_negatedObject



68
69
70
71
72
73
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 68

def failure_message_when_negated
  [
    "Attachment :#{@attachment_name} got details",
    @subject.errors.details[@attachment_name]
  ].join("\n")
end

#format(format) ⇒ Object



30
31
32
33
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 30

def format(format)
  @format = format
  self
end

#height(less_than_or_equal_to:) ⇒ Object



40
41
42
43
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 40

def height(less_than_or_equal_to:)
  @height[:less_than_or_equal_to] = less_than_or_equal_to
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 45

def matches?(subject)
  @subject = subject.class == Class ? subject.new : subject

  unless @format && !@width.empty? && !@height.empty?
    raise CallError, [
      "should call like this",
      "  it { should validate_attachment_geometry(:image)",
      "                .format(:png)",
      "                .width(less_than_or_equal_to: 3000)",
      "                .height(less_than_or_equal_to: 3000) }"
    ].join("\n")
  end

  when_valid && when_invalid
end

#width(less_than_or_equal_to:) ⇒ Object



35
36
37
38
# File 'lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb', line 35

def width(less_than_or_equal_to:)
  @width[:less_than_or_equal_to] = less_than_or_equal_to
  self
end