Skip to content

Commit 65de03e

Browse files
authored
Merge pull request #388 from roolrool/feature/aspect-ratio-multiple-values-support
Add tests for supporting several passed aspect_ratios for allowing / rejecting methods
2 parents 9084286 + b8b9bba commit 65de03e

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ Matcher methods available:
787787
describe User do
788788
# aspect_ratio:
789789
# #allowing, #rejecting
790-
it { is_expected.to validate_aspect_ratio_of(:avatar).allowing(:square) }
791-
it { is_expected.to validate_aspect_ratio_of(:avatar).rejecting(:portrait) }
790+
it { is_expected.to validate_aspect_ratio_of(:avatar).allowing(:square, :portrait) } # possible to use an Array or *splatted array
791+
it { is_expected.to validate_aspect_ratio_of(:avatar).rejecting(:square, :landscape) } # possible to use an Array or *splatted array
792792
793793
# attached
794794
it { is_expected.to validate_attached_of(:avatar) }

test/dummy/app/models/aspect_ratio/matcher.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class AspectRatio::Matcher < ApplicationRecord
2020
has_one_attached :"allowing_one_#{aspect_ratio}"
2121
validates :"allowing_one_#{aspect_ratio}", aspect_ratio: aspect_ratio
2222
end
23+
24+
has_one_attached :allowing_several
25+
validates :allowing_several, aspect_ratio: [ :square, :portrait ]
26+
2327
has_one_attached :allowing_one_is_x_y
2428
validates :allowing_one_is_x_y, aspect_ratio: :is_16_9
2529

test/matchers/aspect_ratio_validator_matcher_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@
109109
end
110110
end
111111
end
112+
113+
describe "several" do
114+
describe "when all specified aspect ratios exactly match the allowed list" do
115+
let(:model_attribute) { :allowing_several }
116+
subject { matcher.allowing(:portrait, :square) }
117+
118+
it { is_expected_to_match_for(klass) }
119+
end
120+
121+
describe "when some specified aspect ratios match but not all" do
122+
let(:model_attribute) { :allowing_several }
123+
subject { matcher.allowing(:landscape, :square) }
124+
125+
it { is_expected_not_to_match_for(klass) }
126+
end
127+
128+
describe "when none of the specified aspect ratios match" do
129+
let(:model_attribute) { :allowing_several }
130+
subject { matcher.allowing(:landscape, :is_4_3) }
131+
132+
it { is_expected_not_to_match_for(klass) }
133+
end
134+
end
112135
end
113136

114137
describe "#rejecting" do
@@ -172,6 +195,29 @@
172195
end
173196
end
174197
end
198+
199+
describe "several" do
200+
describe "when rejecting aspect ratios that are not in the allowed list" do
201+
let(:model_attribute) { :allowing_several }
202+
subject { matcher.rejecting(:landscape, :square) }
203+
204+
it { is_expected_to_match_for(klass) }
205+
end
206+
207+
describe "when rejecting some aspect ratios that overlap with the allowed list" do
208+
let(:model_attribute) { :allowing_several }
209+
subject { matcher.rejecting(:landscape, :square) }
210+
211+
it { is_expected_not_to_match_for(klass) }
212+
end
213+
214+
describe "when rejecting aspect ratios that are in the allowed list" do
215+
let(:model_attribute) { :allowing_several }
216+
subject { matcher.rejecting(:square, :portrait) }
217+
218+
it { is_expected_not_to_match_for(klass) }
219+
end
220+
end
175221
end
176222

177223
describe "Combinations" do

0 commit comments

Comments
 (0)