Verifiable

Description

Creates verified associations. Verified associations are similar to regular associations, except that a step has been taken to verify the integrity of the association.

Usage

Add verifiable to your Gemfile (or whatever if you’re not using bundler):

gem "verifiable"

Run the generator to create the necessary database migration and run it:

script/generate verifiable_migration
rake db:migrate

Include Verifiable::Associations in your classes and specify the correct type of association:

class User < ActiveRecord::Base

  include Verifiable::Associations
  has_many_verifiable :phone_numbers

end

class PhoneNumber < ActiveRecord::Base

  include Verifiable::Associations
  verifiable_for :users

end

Create some associations that need verification:

user = User.create
phone_number = user.phone_numbers.create

Associated objects don’t show up until they’re verified:

>> user.phone_numbers == []
=> true

Get unverified objects like this:

>> user.unverified_phone_numbers == [phone_number]
=> true

Get the verification code for an association (code is a random 4 digit string):

code = user.verification_code_for(phone_number)

Verify the association (returns true if code is correct):

user.verify!(phone_number, code)

Once verified, the associated objects show up:

>> user.phone_numbers == [phone_number]
=> true

To create a verification that has a different type of code:

user = User.create
phone_number = PhoneNumber.create
Verifiable::Verification.create(:code => "abcd", :object => user, :verifiable => phone_number)

LICENSE

The MIT License, see LICENSE

TODO

  • Make singular associations work (has_one)

  • Figure out a way to make association specific verification code generators