Module: ActiveStorageValidations::Matchers

Defined in:
lib/active_storage_validations/matchers.rb,
lib/active_storage_validations/matchers/shared/asv_rspecable.rb,
lib/active_storage_validations/matchers/shared/asv_attachable.rb,
lib/active_storage_validations/matchers/shared/asv_contextable.rb,
lib/active_storage_validations/matchers/shared/asv_messageable.rb,
lib/active_storage_validations/matchers/shared/asv_timeoutable.rb,
lib/active_storage_validations/matchers/shared/asv_validatable.rb,
lib/active_storage_validations/matchers/size_validator_matcher.rb,
lib/active_storage_validations/matchers/limit_validator_matcher.rb,
lib/active_storage_validations/matchers/pages_validator_matcher.rb,
lib/active_storage_validations/matchers/shared/asv_except_onable.rb,
lib/active_storage_validations/matchers/attached_validator_matcher.rb,
lib/active_storage_validations/matchers/duration_validator_matcher.rb,
lib/active_storage_validations/matchers/shared/asv_allow_blankable.rb,
lib/active_storage_validations/matchers/dimension_validator_matcher.rb,
lib/active_storage_validations/matchers/total_size_validator_matcher.rb,
lib/active_storage_validations/matchers/with_audio_validator_matcher.rb,
lib/active_storage_validations/matchers/shared/asv_active_storageable.rb,
lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb,
lib/active_storage_validations/matchers/content_type_validator_matcher.rb,
lib/active_storage_validations/matchers/shared/asv_spoofing_protectable.rb,
lib/active_storage_validations/matchers/base_comparison_validator_matcher.rb,
lib/active_storage_validations/matchers/processable_file_validator_matcher.rb

Defined Under Namespace

Modules: ASVActiveStorageable, ASVAllowBlankable, ASVAttachable, ASVContextable, ASVExceptOnable, ASVMessageable, ASVRspecable, ASVSpoofingProtectable, ASVTimeoutable, ASVValidatable Classes: AspectRatioValidatorMatcher, AttachedValidatorMatcher, BaseComparisonValidatorMatcher, ContentTypeValidatorMatcher, DimensionValidatorMatcher, DurationValidatorMatcher, LimitValidatorMatcher, PagesValidatorMatcher, ProcessableFileValidatorMatcher, SizeValidatorMatcher, TotalSizeValidatorMatcher, WithAudioValidatorMatcher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mock_metadata(attachment, metadata = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_storage_validations/matchers.rb', line 31

def self.(attachment,  = {})
   = { width: nil, height: nil, duration: nil, content_type: nil }
  merged = .merge()
  # Stubbing Analyzer.new also covers content-type sniffers (File / Magika),
  # which call #content_type — not #metadata. Default to the dummy file type
  # so stacked validators with spoofing_protection still pass under the mock.
  detected_content_type = merged[:content_type].presence || "image/png"
  mock = Struct.new(:metadata, :content_type).new(
    merged,
    { content_type: detected_content_type, content_type_backend: "file" }
  )

  stub_method(ActiveStorageValidations::Analyzer, :new, mock) do
    yield
  end
end

.stub_method(object, method, result) ⇒ Object

Helper to stub a method with either RSpec or Minitest (whatever is available)



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_storage_validations/matchers.rb', line 18

def self.stub_method(object, method, result)
  if defined?(Minitest::Mock)
    object.stub(method, result) do
      yield
    end
  elsif defined?(RSpec::Mocks)
    RSpec::Mocks.allow_message(object, method) { result }
    yield
  else
    raise "Need either Minitest::Mock or RSpec::Mocks to run this validator matcher"
  end
end

Instance Method Details

#validate_aspect_ratio_of(attribute_name) ⇒ Object



15
16
17
# File 'lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb', line 15

def validate_aspect_ratio_of(attribute_name)
  AspectRatioValidatorMatcher.new(attribute_name)
end

#validate_attached_of(attribute_name) ⇒ Object



13
14
15
# File 'lib/active_storage_validations/matchers/attached_validator_matcher.rb', line 13

def validate_attached_of(attribute_name)
  AttachedValidatorMatcher.new(attribute_name)
end

#validate_content_type_of(attribute_name) ⇒ Object



19
20
21
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 19

def validate_content_type_of(attribute_name)
  ContentTypeValidatorMatcher.new(attribute_name)
end

#validate_dimensions_of(attribute_name) ⇒ Object



15
16
17
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 15

def validate_dimensions_of(attribute_name)
  DimensionValidatorMatcher.new(attribute_name)
end

#validate_duration_of(attribute_name) ⇒ Object



8
9
10
# File 'lib/active_storage_validations/matchers/duration_validator_matcher.rb', line 8

def validate_duration_of(attribute_name)
  DurationValidatorMatcher.new(attribute_name)
end

#validate_limits_of(name) ⇒ Object



14
15
16
# File 'lib/active_storage_validations/matchers/limit_validator_matcher.rb', line 14

def validate_limits_of(name)
  LimitValidatorMatcher.new(name)
end

#validate_pages_of(attribute_name) ⇒ Object



8
9
10
# File 'lib/active_storage_validations/matchers/pages_validator_matcher.rb', line 8

def validate_pages_of(attribute_name)
  PagesValidatorMatcher.new(attribute_name)
end

#validate_processable_file_of(name) ⇒ Object



15
16
17
# File 'lib/active_storage_validations/matchers/processable_file_validator_matcher.rb', line 15

def validate_processable_file_of(name)
  ProcessableFileValidatorMatcher.new(name)
end

#validate_size_of(attribute_name) ⇒ Object



7
8
9
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 7

def validate_size_of(attribute_name)
  SizeValidatorMatcher.new(attribute_name)
end

#validate_total_size_of(attribute_name) ⇒ Object



7
8
9
# File 'lib/active_storage_validations/matchers/total_size_validator_matcher.rb', line 7

def validate_total_size_of(attribute_name)
  TotalSizeValidatorMatcher.new(attribute_name)
end

#validate_with_audio_of(attribute_name) ⇒ Object



15
16
17
# File 'lib/active_storage_validations/matchers/with_audio_validator_matcher.rb', line 15

def validate_with_audio_of(attribute_name)
  WithAudioValidatorMatcher.new(attribute_name)
end