Skip to content

Commit 9e540e1

Browse files
committed
use default only when has no filter declared
1 parent c8ef4dd commit 9e540e1

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/normalizy/rspec/matcher.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ def matches?(subject)
4040

4141
if @with.present?
4242
options = @subject.class.normalizy_rules[@attribute]
43-
options = (options || []) + extract_defaults
43+
44+
return false if options.blank?
45+
46+
if options.map { |option| option[:rules] }.compact.blank?
47+
options = default_rules
48+
end
4449

4550
return false if options.blank?
4651

@@ -81,7 +86,7 @@ def actual_value
8186
value.is_a?(String) ? %("#{value}") : value
8287
end
8388

84-
def extract_defaults
89+
def default_rules
8590
[Normalizy.config.default_filters].flatten.compact.map do |rule|
8691
{ rules: rule }
8792
end

spec/normalizy/rspec/matcher/matches_spec.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
config.default_filters = :squish
5252
end
5353

54-
matcher = described_class.new(:downcase_field)
54+
matcher = described_class.new(:alone)
5555

5656
matcher.with :squish
5757

@@ -63,7 +63,7 @@
6363
config.default_filters = [:squish]
6464
end
6565

66-
matcher = described_class.new(:downcase_field)
66+
matcher = described_class.new(:alone)
6767

6868
matcher.with :squish
6969

@@ -75,12 +75,24 @@
7575
config.default_filters = [{ strip: { side: :left } }]
7676
end
7777

78-
matcher = described_class.new(:downcase_field)
78+
matcher = described_class.new(:alone)
7979

8080
matcher.with(strip: { side: :left })
8181

8282
expect(matcher.matches?(object)).to eq true
8383
end
84+
85+
specify do
86+
Normalizy.configure do |config|
87+
config.default_filters = :squish
88+
end
89+
90+
matcher = described_class.new(:downcase_field)
91+
92+
matcher.with :squish
93+
94+
expect(matcher.matches?(object)).to eq false
95+
end
8496
end
8597

8698
context 'when .with is not called' do

spec/support/models/match.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
class Match < ActiveRecord::Base
4+
normalizy :alone
45
normalizy :downcase_field , with: :downcase
56
normalizy :trim_side_left , with: { trim: { side: :left } }
67
normalizy :trim_side_left_array, with: [{ trim: { side: :left } }]

0 commit comments

Comments
 (0)