Module: ActiveStorageValidations::Matchers

Defined in:
lib/active_storage_validations/matchers.rb,
lib/active_storage_validations/matchers/concerns/rspecable.rb,
lib/active_storage_validations/matchers/concerns/contextable.rb,
lib/active_storage_validations/matchers/concerns/messageable.rb,
lib/active_storage_validations/matchers/concerns/validatable.rb,
lib/active_storage_validations/matchers/size_validator_matcher.rb,
lib/active_storage_validations/matchers/concerns/allow_blankable.rb,
lib/active_storage_validations/matchers/attached_validator_matcher.rb,
lib/active_storage_validations/matchers/concerns/active_storageable.rb,
lib/active_storage_validations/matchers/dimension_validator_matcher.rb,
lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb,
lib/active_storage_validations/matchers/content_type_validator_matcher.rb

Defined Under Namespace

Modules: ActiveStorageable, AllowBlankable, Contextable, Messageable, Rspecable, Validatable Classes: AspectRatioValidatorMatcher, AttachedValidatorMatcher, ContentTypeValidatorMatcher, DimensionValidatorMatcher, SizeValidatorMatcher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mock_metadata(attachment, width, height) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_storage_validations/matchers.rb', line 25

def self.(attachment, width, height)
  if Rails.gem_version >= Gem::Version.new('6.0.0')
    # Mock the Metadata class for rails 6
    mock = OpenStruct.new(metadata: { width: width, height: height })
    stub_method(ActiveStorageValidations::Metadata, :new, mock) do
      yield
    end
  else
    # Stub the metadata analysis for rails 5
    stub_method(attachment, :analyze, true) do
      stub_method(attachment, :analyzed?, true) do
        stub_method(attachment, :metadata, { width: width, height: height }) do
          yield
        end
      end
    end
  end
end

.stub_method(object, method, result) ⇒ Object

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_storage_validations/matchers.rb', line 12

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(name, expected_aspect_ratio) ⇒ Object



12
13
14
# File 'lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb', line 12

def validate_aspect_ratio_of(name, expected_aspect_ratio)
  AspectRatioValidatorMatcher.new(name, expected_aspect_ratio)
end

#validate_attached_of(name) ⇒ Object



11
12
13
# File 'lib/active_storage_validations/matchers/attached_validator_matcher.rb', line 11

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

#validate_content_type_of(name) ⇒ Object



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

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

#validate_dimensions_of(name) ⇒ Object



12
13
14
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 12

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

#validate_size_of(name) ⇒ Object



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

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