diff --git a/README.md b/README.md index 5a2e0eb..f09a566 100755 --- a/README.md +++ b/README.md @@ -318,7 +318,7 @@ Very simple example of validation with file attached, content type check and cus [![Sample](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png) ## Test matchers -Provides RSpec-compatible and Minitest-compatible matchers for testing the validators. Only `aspect_ratio`, `attached`, `content_type`, `limit`, `dimension` and `size` validators currently have their matcher developed. +Provides RSpec-compatible and Minitest-compatible matchers for testing the validators. ### RSpec diff --git a/lib/active_storage_validations/matchers/concerns/attachable.rb b/lib/active_storage_validations/matchers/concerns/attachable.rb index 9bbe313..7155202 100644 --- a/lib/active_storage_validations/matchers/concerns/attachable.rb +++ b/lib/active_storage_validations/matchers/concerns/attachable.rb @@ -8,6 +8,23 @@ def attach_file(file = dummy_file) @subject.public_send(@attribute_name) end + def attach_files(count) + return unless count.positive? + + file_array = Array.new(count, dummy_file) + + attach_file(file_array) + end + + def detach_file + @subject.attachment_changes.delete(@attribute_name.to_s) + end + alias :detach_files :detach_file + + def file_attached? + @subject.public_send(@attribute_name).attached? + end + def dummy_file { io: io, @@ -35,14 +52,6 @@ def not_processable_image def io @io ||= Tempfile.new('Hello world!') end - - def detach_file - @subject.attachment_changes.delete(@attribute_name.to_s) - end - - def file_attached? - @subject.public_send(@attribute_name).attached? - end end end end diff --git a/lib/active_storage_validations/matchers/limit_validator_matcher.rb b/lib/active_storage_validations/matchers/limit_validator_matcher.rb index e064196..e4822a9 100644 --- a/lib/active_storage_validations/matchers/limit_validator_matcher.rb +++ b/lib/active_storage_validations/matchers/limit_validator_matcher.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require 'pry' + require_relative 'concerns/active_storageable' require_relative 'concerns/allow_blankable' require_relative 'concerns/attachable' @@ -42,13 +42,13 @@ def failure_message message.join("\n") end - def min(number) - @min = number + def min(count) + @min = count self end - def max(number) - @max = number + def max(count) + @max = count self end @@ -58,12 +58,12 @@ def matches?(subject) is_a_valid_active_storage_attribute? && is_context_valid? && is_custom_message_valid? && - file_number_not_smaller_than_min? && - file_number_equal_min? && - file_number_larger_than_min? && - file_number_smaller_than_max? && - file_number_equal_max? && - file_number_not_larger_than_max? + file_count_not_smaller_than_min? && + file_count_equal_min? && + file_count_larger_than_min? && + file_count_smaller_than_max? && + file_count_equal_max? && + file_count_not_larger_than_max? end private @@ -73,32 +73,32 @@ def build_failure_message(message) message << " but there seem to have issues with the matcher methods you used, since:" @failure_message_artefacts.each do |error_case| - message << " validation failed when provided with a #{error_case[:count]} file(s)" + message << " validation failed when provided with #{error_case[:count]} file(s)" end message << " whereas it should have passed" end - def file_number_not_smaller_than_min? + def file_count_not_smaller_than_min? @min.nil? || @min.zero? || !passes_validation_with_limits(@min - 1) end - def file_number_equal_min? + def file_count_equal_min? @min.nil? || @min.zero? || passes_validation_with_limits(@min) end - def file_number_larger_than_min? + def file_count_larger_than_min? @min.nil? || @min.zero? || @min == @max || passes_validation_with_limits(@min + 1) end - def file_number_smaller_than_max? + def file_count_smaller_than_max? @max.nil? || @min == @max || passes_validation_with_limits(@max - 1) end - def file_number_equal_max? + def file_count_equal_max? @max.nil? || passes_validation_with_limits(@max) end - def file_number_not_larger_than_max? + def file_count_not_larger_than_max? @max.nil? || !passes_validation_with_limits(@max + 1) end @@ -126,26 +126,6 @@ def add_failure_message_artefact(count) @failure_message_artefacts << { count: count } false end - - def attach_files(count) - return if count.negative? || count.zero? - - file_array = [] - (1..count).each do |i| - dummy_file = { - io: Tempfile.new('.'), - filename: "dummy_#{i}.txt", - content_type: 'text/plain' - } - file_array << dummy_file - end - - @subject.public_send(@attribute_name).attach(file_array) - end - - def detach_files - @subject.attachment_changes.delete(@attribute_name.to_s) - end end end end diff --git a/test/matchers/shared_examples/has_valid_rspec_message_methods.rb b/test/matchers/shared_examples/has_valid_rspec_message_methods.rb index 66f3667..ed63381 100644 --- a/test/matchers/shared_examples/has_valid_rspec_message_methods.rb +++ b/test/matchers/shared_examples/has_valid_rspec_message_methods.rb @@ -37,7 +37,7 @@ module HasValidRspecMessageMethods <<~FAILURE_MESSAGE is expected to validate limit file of :#{model_attribute} but there seem to have issues with the matcher methods you used, since: - validation failed when provided with a 6 file(s) + validation failed when provided with 6 file(s) whereas it should have passed FAILURE_MESSAGE when :content_type @@ -110,8 +110,8 @@ module HasValidRspecMessageMethods <<~FAILURE_MESSAGE is expected not to validate limit file of :#{model_attribute} but there seem to have issues with the matcher methods you used, since: - validation failed when provided with a 0 file(s) - validation failed when provided with a 6 file(s) + validation failed when provided with 0 file(s) + validation failed when provided with 6 file(s) whereas it should have passed FAILURE_MESSAGE when :content_type