Giter Site home page Giter Site logo

centronet-marketing / phony_rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joost/phony_rails

0.0 3.0 0.0 109 KB

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).

License: MIT License

phony_rails's Introduction

PhonyRails

(In its early days :) called PhonyNumber)

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).

Installation

Add this line to your application's Gemfile:

gem 'phony_rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install phony_rails

Usage

Normalization / ActiveRecord Extension

In your model add:

class SomeModel < ActiveRecord::Base

  # Normalizes the attribute itself before validation
  phony_normalize :phone_number, :default_country_code => 'US'

  # Normalizes attribute before validation and saves into other attribute
  phony_normalize :phone_number, :as => :phone_number_normalized_version, :default_country_code => 'US'

  # Creates method normalized_fax_number that returns the normalized version of fax_number
  phony_normalized_method :fax_number
end

The :default_country_code options is used to specify a country_code when normalizing.

PhonyRails will also check your model for a country_code method to use when normalizing the number. So '070-12341234' with country_code 'NL' will get normalized to '317012341234'.

You can also do-it-yourself and call:

# Options:
#   :country_code => The country code we should use (forced).
#   :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).

PhonyRails.normalize_number('some number', :country_code => 'NL')

PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '464790909090' (forced to +46)
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '4790909090' (still +47 so not changed)

Validation

In your model use the Phony.plausible method to validate an attribute:

validates :phone_number, :phony_plausible => true

or the helper method:

validates_plausible_phone :phone_number

this method use other validators under the hood to provide:

  • presence validation using ActiveModel::Validations::PresenceValidator
  • format validation using ActiveModel::Validations::FormatValidator

so we can use:

validates_plausible_phone :phone_number, :presence => true
validates_plausible_phone :phone_number, :with => /^\+\d+/
validates_plausible_phone :phone_number, :without => /^\+\d+/
validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/

the i18n key is :improbable_phone

Display / Views

In your views use:

<%= "311012341234".phony_formatted(:format => :international, :spaces => '-') %>
<%= "+31-10-12341234".phony_formatted(:format => :international, :spaces => '-') %>
<%= "+31(0)1012341234".phony_formatted(:format => :international, :spaces => '-') %>

To first normalize the String to a certain country use:

<%= "010-12341234".phony_formatted(normalize => :NL, :format => :international, :spaces => '-') %>

You can also use the bang method (phony_formatted!):

number = "010-12341234"
number.phony_formatted!(normalize => :NL, :format => :international)
number # => "+31 10 12341234"

Find by normalized number

Say you want to find a record by a phone number. Best is to normalize user input and compare to an attribute stored in the db.

Home.find_by_normalized_phone_number(PhonyRails.normalize_number(params[:phone_number]))

Changelog

0.3.0

0.2.1

  • Better error handling by @k4nar

0.1.12

  • Further loosened gemspec dependencies.

0.1.11

  • Better gemspec dependency versions by @rjhaveri.

0.1.10

  • Changes from henning-koch.
  • Some pending fixes.

0.1.8

  • Improved validation methods by @ddidier.

0.1.6

  • Added :as option to phony_normalize.

0.1.5

  • some tests and a helper method by @ddidier.

0.1.2

  • Using countries gem as suggested by @brutuscat.
  • Fixes bugs mentioned by @ddidier.

0.1.0

  • Added specs.

0.0.10

  • Same fix as 0.0.9 but for phony_formatted method.

0.0.9

  • Fix for using same options Hash for all models.

0.0.8

  • Improved number cleaning not to remove '+' and leading 0's. Now works with national numbers starting with 0 followed by country_code. Eg. 032 in BE.

0.0.7

  • Fixed problem with '+' number

0.0.6

  • Fixed problem with '070-4157134' being parsed as US number

TODO

  • Make this work: Home.find_by_normalized_phone_number(Home.normalize_number(params[:phone_number])) So we use Home.normalize_number instead of PhonyRails.normalize_number. This way we can use the same default_country_code.
  • Make country_code method configurable.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks to all contributors: ddidier, brutuscat, henning-koch.

phony_rails's People

Contributors

cinconnu avatar jell avatar espen avatar triskweline avatar siong1987 avatar ghiculescu avatar floere avatar jcoleman avatar rodeoclash avatar k4nar avatar rjhaveri avatar

Watchers

Mika Cohen avatar James Cloos avatar Alejandro Perez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.