Skip to content

Commit

Permalink
Rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost Hietbrink committed Feb 23, 2021
1 parent 1a78235 commit 5b32882
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
11 changes: 4 additions & 7 deletions lib/phony_rails.rb
Expand Up @@ -50,7 +50,7 @@ def self.normalize_number(number, options = {}, current_instance = nil)
original_number = number
number = number.dup # Just to be sure, we don't want to change the original.
number, ext = extract_extension(number)
number.gsub!(/[^\(\)\d\+]/, '') # Strips weird stuff from the number
number.gsub!(/[^()\d+]/, '') # Strips weird stuff from the number
return if number.blank?

if _country_number = options[:country_number] || country_number_for(options[:country_code])
Expand All @@ -67,8 +67,7 @@ def self.normalize_number(number, options = {}, current_instance = nil)
normalized_number = options[:add_plus] ? "+#{normalized_number}" : normalized_number

options[:extension] = true if options[:extension].nil?
normalized_number = options[:extension] ? format_extension(normalized_number, ext) : normalized_number
normalized_number
options[:extension] ? format_extension(normalized_number, ext) : normalized_number
rescue StandardError
original_number # If all goes wrong .. we still return the original input.
end
Expand Down Expand Up @@ -129,7 +128,7 @@ def self.plausible_number?(number, options = {})
false
end

COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i.freeze
COMMON_EXTENSIONS = / *(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i.freeze

def self.extract_extension(number_and_ext)
return [nil, nil] if number_and_ext.nil?
Expand Down Expand Up @@ -203,9 +202,7 @@ module ClassMethods
def phony_normalize(*attributes)
options = attributes.last.is_a?(Hash) ? attributes.pop : {}
options.assert_valid_keys(*PHONY_RAILS_COLLECTION_VALID_KEYS)
if options[:as].present?
raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1
end
raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if options[:as].present? && (attributes.size > 1)

options[:enforce_record_country] = true if options[:enforce_record_country].nil?

Expand Down
1 change: 1 addition & 0 deletions phony_rails.gemspec
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.name = 'phony_rails'
gem.require_paths = ['lib']
gem.version = PhonyRails::VERSION
gem.required_ruby_version = '>= 2.4'

gem.post_install_message = "PhonyRails v0.10.0 changes the way numbers are stored!\nIt now adds a ' + ' to the normalized number when it starts with a country number!"

Expand Down
8 changes: 5 additions & 3 deletions spec/lib/phony_rails_spec.rb
Expand Up @@ -2,9 +2,8 @@

require 'spec_helper'

EXT_PREFIXES = %w[ext ex x xt # :].freeze
describe PhonyRails do
EXT_PREFIXES = %w[ext ex x xt # :].freeze

it 'should not pollute the global namespace with a Country class' do
should_not be_const_defined 'Country'
end
Expand Down Expand Up @@ -200,11 +199,14 @@
end
end

# rubocop:disable Lint/ConstantDefinitionInBlock
class NormalHome < ActiveRecord::Base
attr_accessor :phone_number

phony_normalize :phone_number, default_country_code: 'US'
validates :phone_number, phony_plausible: true
end
# rubocop:enable Lint/ConstantDefinitionInBlock

normal = NormalHome.new
normal.phone_number = 'HAHA'
Expand Down Expand Up @@ -510,7 +512,7 @@ class NormalHome < ActiveRecord::Base
after { PhonyRails.default_country_code = nil }

it 'can set a global default country code' do
expect(PhonyRails.default_country_code). to eq 'US'
expect(PhonyRails.default_country_code).to eq 'US'
end

it 'can set a global default country code' do
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/validators/phony_validator_spec.rb
Expand Up @@ -69,73 +69,85 @@
#--------------------
class SimpleHome < ActiveRecord::Base
attr_accessor :phone_number

validates :phone_number, phony_plausible: true
end

#--------------------
class HelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number
end

#--------------------
class RequiredHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, presence: true
end

#--------------------
class OptionalHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, presence: false
end

#--------------------
class FormattedHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, with: /\A\+\d+/
end

#--------------------
class NotFormattedHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, without: /\A\+\d+/
end

#--------------------
class NormalizableHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, normalized_country_code: 'US'
end

#--------------------
class AustralianHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, country_number: '61'
end

#--------------------
class PolishHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, country_code: 'PL'
end

#--------------------
class BigHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/, country_number: '33'
end

#--------------------
class MismatchedHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number, :country_code

validates :phone_number, phony_plausible: { ignore_record_country_code: true }
end

#--------------------

class InvalidCountryCodeHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number

validates_plausible_phone :phone_number

def country_code
Expand All @@ -146,33 +158,39 @@ def country_code
#--------------------
class SymbolizableHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number, :phone_number_country_code

validates_plausible_phone :phone_number, country_code: :phone_number_country_code
end

#--------------------
class NoModelMethod < HelpfulHome
attr_accessor :phone_number

validates_plausible_phone :phone_number, country_code: :nonexistent_method
end

#--------------------
class MessageOptionUndefinedInModel < HelpfulHome
attr_accessor :phone_number

validates_plausible_phone :phone_number, message: :email
end

#--------------------
class MessageOptionSameAsModelMethod < HelpfulHome
attr_accessor :phone_number

validates_plausible_phone :phone_number, message: :email

def email
'user@example.com'
end
end

#--------------------
class NormalizabledPhoneHome < ActiveRecord::Base
attr_accessor :phone_number, :phone_number2, :country_code

validates_plausible_phone :phone_number
validates_plausible_phone :phone_number2
phony_normalize :phone_number, country_code: 'PL', normalize_when_valid: true
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -35,6 +35,7 @@ module SharedModelMethods
:country_code, :country_code_attribute, :custom_country_code, :delivery_method,
:home_country, :phone_method, :phone1_method, :recipient, :symboled_phone_method
)

phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
phony_normalized_method :phone_method # adds normalized_phone_method method
phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
Expand Down

0 comments on commit 5b32882

Please sign in to comment.