-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from Ross-Hunter/feature/conditional-normaliz…
…ation Conditional Normalization
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -702,6 +702,56 @@ class NormalHome < ActiveRecord::Base | |
expect(model).to be_valid | ||
expect(model.symboled_phone).to eql('+33606060606') | ||
end | ||
|
||
context 'conditional normalization' do | ||
context 'standalone methods' do | ||
it 'should only normalize if the :if conditional is true' do | ||
model_klass.phony_normalize :recipient, default_country_code: 'US', if: :use_phone? | ||
|
||
sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms' | ||
email_alarm = model_klass.new recipient: '[email protected]', delivery_method: 'email' | ||
expect(sms_alarm).to be_valid | ||
expect(email_alarm).to be_valid | ||
expect(sms_alarm.recipient).to eq('+12223334444') | ||
expect(email_alarm.recipient).to eq('[email protected]') | ||
end | ||
|
||
it 'should only normalize if the :unless conditional is false' do | ||
model_klass.phony_normalize :recipient, default_country_code: 'US', unless: :use_email? | ||
|
||
sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms' | ||
email_alarm = model_klass.new recipient: '[email protected]', delivery_method: 'email' | ||
expect(sms_alarm).to be_valid | ||
expect(email_alarm).to be_valid | ||
expect(sms_alarm.recipient).to eq('+12223334444') | ||
expect(email_alarm.recipient).to eq('[email protected]') | ||
end | ||
end | ||
|
||
context 'using lambdas' do | ||
it 'should only normalize if the :if conditional is true' do | ||
model_klass.phony_normalize :recipient, default_country_code: 'US', if: -> { delivery_method == 'sms' } | ||
|
||
sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms' | ||
email_alarm = model_klass.new recipient: '[email protected]', delivery_method: 'email' | ||
expect(sms_alarm).to be_valid | ||
expect(email_alarm).to be_valid | ||
expect(sms_alarm.recipient).to eq('+12223334444') | ||
expect(email_alarm.recipient).to eq('[email protected]') | ||
end | ||
|
||
it 'should only normalize if the :unless conditional is false' do | ||
model_klass.phony_normalize :recipient, default_country_code: 'US', unless: -> { delivery_method == 'email' } | ||
|
||
sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms' | ||
email_alarm = model_klass.new recipient: '[email protected]', delivery_method: 'email' | ||
expect(sms_alarm).to be_valid | ||
expect(email_alarm).to be_valid | ||
expect(sms_alarm.recipient).to eq('+12223334444') | ||
expect(email_alarm.recipient).to eq('[email protected]') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters