verifiable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
5
+ test/.verifiable.sqlite3
6
+ test/test.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in verifiable.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ verifiable (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activerecord (2.3.11)
10
+ activesupport (= 2.3.11)
11
+ activesupport (2.3.11)
12
+ columnize (0.3.2)
13
+ linecache (0.43)
14
+ rake (0.8.7)
15
+ ruby-debug (0.10.4)
16
+ columnize (>= 0.1)
17
+ ruby-debug-base (~> 0.10.4.0)
18
+ ruby-debug-base (0.10.4)
19
+ linecache (>= 0.3)
20
+ sqlite3 (1.3.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ activerecord (= 2.3.11)
27
+ bundler (>= 1.0.0)
28
+ rake (= 0.8.7)
29
+ ruby-debug
30
+ sqlite3 (= 1.3.3)
31
+ verifiable!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Evan Petrie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = Verifiable
2
+
3
+ * http://github.com/kuinak/verifiable
4
+
5
+ == Description
6
+
7
+ Creates verified associations. Verified associations are similar to regular
8
+ associations, except that a step has been taken to verify the integrity of the
9
+ association.
10
+
11
+ == Usage
12
+
13
+ Add verifiable to your Gemfile (or whatever if you're not using bundler):
14
+
15
+ gem "verifiable"
16
+
17
+ Run the generator to create the necessary database migration and run it:
18
+
19
+ script/generate verifiable_migration
20
+ rake db:migrate
21
+
22
+ Include Verifiable::Associations in your classes and specify the correct type
23
+ of association:
24
+
25
+ class User < ActiveRecord::Base
26
+
27
+ include Verifiable::Associations
28
+ has_many_verifiable :phone_numbers
29
+
30
+ end
31
+
32
+ class PhoneNumber < ActiveRecord::Base
33
+
34
+ include Verifiable::Associations
35
+ verifiable_for :users
36
+
37
+ end
38
+
39
+ Create some associations that need verification:
40
+
41
+ user = User.create
42
+ phone_number = user.phone_numbers.create
43
+
44
+ Associated objects don't show up until they're verified:
45
+
46
+ >> user.phone_numbers == []
47
+ => true
48
+
49
+ Get unverified objects like this:
50
+
51
+ >> user.unverified_phone_numbers == [phone_number]
52
+ => true
53
+
54
+ Get the verification code for an association (code is a random 4 digit string):
55
+
56
+ code = user.verification_code_for(phone_number)
57
+
58
+ Verify the association (returns true if code is correct):
59
+
60
+ user.verify!(phone_number, code)
61
+
62
+ Once verified, the associated objects show up:
63
+
64
+ >> user.phone_numbers == [phone_number]
65
+ => true
66
+
67
+ To create a verification that has a different type of code:
68
+
69
+ user = User.create
70
+ phone_number = PhoneNumber.create
71
+ Verifiable::Verification.create(:code => "abcd", :object => user, :verifiable => phone_number)
72
+
73
+ == LICENSE
74
+
75
+ The MIT License, see LICENSE
76
+
77
+ == TODO
78
+
79
+ * Make singular associations work (has_one)
80
+ * Figure out a way to make association specific verification code generators
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ Rake::TestTask.new("test") { |t|
8
+ t.pattern = 'test/*_test.rb'
9
+ t.verbose = true
10
+ t.warning = true
11
+ }
@@ -0,0 +1,41 @@
1
+ module Verifiable
2
+ module Associations
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ def verify!(object, code)
8
+ verification = verification_for(object)
9
+ if code == verification.code
10
+ verification.update_attributes(:verified_at => Time.now)
11
+ true
12
+ else
13
+ false
14
+ end
15
+ end
16
+
17
+ def verification_code_for(object)
18
+ verification_for(object).code
19
+ end
20
+
21
+ def verification_for(object)
22
+ Verification.find(:first, :conditions => {:object_id => object.id, :object_type => object.class.to_s, :verifiable_id => self.id, :verifiable_type => self.class.to_s}) || Verification.find(:first, :conditions => {:object_id => self.id, :object_type => self.class.to_s, :verifiable_id => object.id, :verifiable_type => object.class.to_s})
23
+ end
24
+
25
+ module ClassMethods
26
+ def has_many_verifiable(things, options = {})
27
+ source_type = things.to_s.singularize.camelize
28
+ has_many :verifications, :as => :object, :dependent => :destroy, :class_name => "Verifiable::Verification" unless reflect_on_association(:verifications)
29
+ has_many things, :through => :verifications, :source => :verifiable, :source_type => source_type, :conditions => "verified_at IS NOT NULL"
30
+ has_many "unverified_#{things}", :through => :verifications, :source => :verifiable, :source_type => source_type, :conditions => "verified_at IS NULL"
31
+ end
32
+
33
+ def verifiable_for(things, options = {})
34
+ source_type = things.to_s.singularize.camelize
35
+ has_many :verifications, :as => :verifiable, :dependent => :destroy, :class_name => "Verifiable::Verification" unless reflect_on_association(:verifications)
36
+ has_many things, :through => :verifications, :source => :object, :source_type => source_type, :conditions => "verified_at IS NOT NULL"
37
+ has_many "unverified_#{things}", :through => :verifications, :source => :object, :source_type => source_type, :conditions => "verified_at IS NULL"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ require "active_record"
2
+
3
+ module Verifiable
4
+ class Verification < ActiveRecord::Base
5
+
6
+ belongs_to :object, :polymorphic => true
7
+ belongs_to :verifiable, :polymorphic => true
8
+
9
+ validates_uniqueness_of :object_id, :scope => [:object_type, :verifiable_id, :verifiable_type]
10
+ validates_presence_of :code
11
+
12
+ def initialize(attrs = {})
13
+ super({:code => sprintf("%04d", rand(10000))}.merge(attrs || {}))
14
+ end
15
+
16
+ def verified?
17
+ !verified_at.nil?
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Verifiable
2
+ VERSION = "0.0.1"
3
+ end
data/lib/verifiable.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Verifiable
2
+ require "verifiable/associations"
3
+ require "verifiable/verification"
4
+ end
@@ -0,0 +1,18 @@
1
+ class VerifiableMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :verifications do |t|
4
+ t.integer :object_id
5
+ t.string :object_type
6
+ t.integer :verifiable_id
7
+ t.string :verifiable_type
8
+ t.datetime :verified_at
9
+ t.string :code
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :verifications
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ class VerifiableMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'verifiable_migration.rb', 'db/migrate', :migration_file_name => "verifiable_migration"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ class PhoneNumber < ActiveRecord::Base
2
+
3
+ include Verifiable::Associations
4
+ verifiable_for :users
5
+
6
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,22 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+
3
+ create_table "users", :force => true do |t|
4
+ t.integer "id"
5
+ end
6
+
7
+ create_table "phone_numbers", :force => true do |t|
8
+ t.integer "id"
9
+ end
10
+
11
+ create_table "verifications", :force => true do |t|
12
+ t.integer "object_id"
13
+ t.string "object_type"
14
+ t.integer "verifiable_id"
15
+ t.string "verifiable_type"
16
+ t.datetime "verified_at"
17
+ t.string "code"
18
+ t.datetime "created_at"
19
+ t.datetime "updated_at"
20
+ end
21
+
22
+ end
@@ -0,0 +1,21 @@
1
+ require "verifiable"
2
+ require "test/unit"
3
+ require "test/user"
4
+ require "test/phone_number"
5
+ require "sqlite3"
6
+ require "ruby-debug"
7
+
8
+ module Test::Unit
9
+ class TestCase
10
+ def setup
11
+ $VERBOSE = nil
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "test/.verifiable.sqlite3")
13
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "test.log"))
14
+
15
+ ActiveRecord::Base.silence do
16
+ ActiveRecord::Migration.verbose = false
17
+ load(File.join(File.dirname(__FILE__), "schema.rb"))
18
+ end
19
+ end
20
+ end
21
+ end
data/test/user.rb ADDED
@@ -0,0 +1,6 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ include Verifiable::Associations
4
+ has_many_verifiable :phone_numbers
5
+
6
+ end
@@ -0,0 +1,60 @@
1
+ require "test/test_helper"
2
+
3
+ class VerifiableTest < Test::Unit::TestCase
4
+
5
+ def test_has_many_verifiable
6
+ u = User.create!
7
+ p = u.phone_numbers.create!
8
+ assert_equal [], u.phone_numbers
9
+ assert_equal [p], u.unverified_phone_numbers
10
+ code = p.verification_code_for(u)
11
+ u.verify!(p, code)
12
+ assert_equal [p], u.phone_numbers(true)
13
+ assert_equal [], u.unverified_phone_numbers(true)
14
+ end
15
+
16
+ def test_custom_code
17
+ u = User.create!
18
+ p = PhoneNumber.create!
19
+ v = Verifiable::Verification.create!(:code => "abcd", :object => u, :verifiable => p)
20
+ assert_equal [], u.phone_numbers
21
+ assert_equal [p], u.unverified_phone_numbers
22
+ code = p.verification_code_for(u)
23
+ assert_equal "abcd", code
24
+ u.verify!(p, code)
25
+ assert_equal [p], u.phone_numbers(true)
26
+ assert_equal [], u.unverified_phone_numbers(true)
27
+ end
28
+
29
+ def test_verifiable_for
30
+ u = User.create!
31
+ p = u.phone_numbers.create!
32
+ assert_equal [], p.users
33
+ assert_equal [u], p.unverified_users
34
+ code = u.verification_code_for(p)
35
+ p.verify!(u, code)
36
+ assert_equal [u], p.users(true)
37
+ assert_equal [], p.unverified_users(true)
38
+ end
39
+
40
+ def test_verification_code_for
41
+ u = User.create!
42
+ p = u.phone_numbers.create!
43
+ assert_equal u.verification_code_for(p), p.verification_code_for(u)
44
+ end
45
+
46
+ def test_verify
47
+ u = User.create!
48
+ p = u.phone_numbers.create!
49
+ code = u.verification_code_for(p)
50
+ assert u.verify!(p, code)
51
+ end
52
+
53
+ def test_verify_with_bad_code
54
+ u = User.create!
55
+ p = u.phone_numbers.create!
56
+ code = u.verification_code_for(p)
57
+ assert_equal false, u.verify!(p, "xxxx")
58
+ end
59
+
60
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/verifiable/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "verifiable"
6
+ s.version = Verifiable::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Evan Petrie"]
9
+ s.email = ["ejp@yahoo.com"]
10
+ s.homepage = "http://rubygems.org/gems/verifiable"
11
+ s.summary = "Creates verified associations"
12
+ s.description = "Allows associatons to be create that require verification, i.e. email addresses or phone numbers"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "verifiable"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "rake", "0.8.7"
19
+ s.add_development_dependency "sqlite3", "1.3.3"
20
+ s.add_development_dependency "activerecord", "2.3.11"
21
+ s.add_development_dependency "ruby-debug"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
25
+ s.require_path = 'lib'
26
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verifiable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Evan Petrie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-03 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rake
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 8
44
+ - 7
45
+ version: 0.8.7
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: sqlite3
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 3
58
+ - 3
59
+ version: 1.3.3
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 2
71
+ - 3
72
+ - 11
73
+ version: 2.3.11
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: ruby-debug
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ type: :development
87
+ version_requirements: *id005
88
+ description: Allows associatons to be create that require verification, i.e. email addresses or phone numbers
89
+ email:
90
+ - ejp@yahoo.com
91
+ executables: []
92
+
93
+ extensions: []
94
+
95
+ extra_rdoc_files: []
96
+
97
+ files:
98
+ - .gitignore
99
+ - Gemfile
100
+ - Gemfile.lock
101
+ - LICENSE
102
+ - README.rdoc
103
+ - Rakefile
104
+ - lib/verifiable.rb
105
+ - lib/verifiable/associations.rb
106
+ - lib/verifiable/verification.rb
107
+ - lib/verifiable/version.rb
108
+ - rails_generators/verifiable/templates/verifiable_migration.rb
109
+ - rails_generators/verifiable/verifiable_migration_generator.rb
110
+ - test/phone_number.rb
111
+ - test/schema.rb
112
+ - test/test_helper.rb
113
+ - test/user.rb
114
+ - test/verifiable_test.rb
115
+ - verifiable.gemspec
116
+ has_rdoc: true
117
+ homepage: http://rubygems.org/gems/verifiable
118
+ licenses: []
119
+
120
+ post_install_message:
121
+ rdoc_options: []
122
+
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ segments:
137
+ - 1
138
+ - 3
139
+ - 6
140
+ version: 1.3.6
141
+ requirements: []
142
+
143
+ rubyforge_project: verifiable
144
+ rubygems_version: 1.3.6
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Creates verified associations
148
+ test_files: []
149
+