joost / phony_rails

This Gem adds useful methods to your Rails app to validate, display and save phone numbers. It uses the super awesome Phony gem (https://github.com/floere/phony).
MIT License
556 stars 111 forks source link

Conditional Normalization #166

Closed Ross-Hunter closed 7 years ago

Ross-Hunter commented 7 years ago

Allows an :if or :unless option to be passed to phony_normalize. Works with symbols that reference methods or lambdas, just like validations do.

More comprehensively fixes https://github.com/joost/phony_rails/issues/149

I had the use case for an alarm that has a recipient, which can be an email or a phone number (sms), so I wanted to be able to conditionally normalize the recipient field - otherwise it would butcher an email address with numbers in it (see tests).

phony_normalize :recipient, default_country_code: 'US', if: -> { delivery_method == 'sms' }
phony_normalize :recipient, default_country_code: 'US', if: :sms_alarm?

The code is maybe a little more complex than I would like, but the alternative was to be very verbose because there is some nested conditional stuff (if with proc, unless with proc, if with symbol, unless with symbol).

Thanks!

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.006%) to 99.809% when pulling 2d75f888c4fbfa117c46d0b5a59e418a8ff2766f on Ross-Hunter:feature/conditional-normalization into 4e0a3df4f46482bb5eef1f991256716ab373a512 on joost:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.006%) to 99.809% when pulling fad9f6e6f01862705cf9d5f32c1549b71b9aa2d3 on Ross-Hunter:feature/conditional-normalization into 4e0a3df4f46482bb5eef1f991256716ab373a512 on joost:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.006%) to 99.809% when pulling fad9f6e6f01862705cf9d5f32c1549b71b9aa2d3 on Ross-Hunter:feature/conditional-normalization into 4e0a3df4f46482bb5eef1f991256716ab373a512 on joost:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.006%) to 99.809% when pulling 7422a3c6f468ea88847a1734ec6a657b9cdcc6f2 on Ross-Hunter:feature/conditional-normalization into 4e0a3df4f46482bb5eef1f991256716ab373a512 on joost:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.006%) to 99.809% when pulling 7422a3c6f468ea88847a1734ec6a657b9cdcc6f2 on Ross-Hunter:feature/conditional-normalization into 4e0a3df4f46482bb5eef1f991256716ab373a512 on joost:master.

joost commented 7 years ago

Hi Ross. As soon as I've got time I'll merge it in. Thx.

On 14 Jun 2017, at 22:20, Ross Hunter notifications@github.com wrote:

tests).