devise_referable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +3 -0
- data/README.rdoc +7 -1
- data/VERSION +1 -1
- data/devise_referable.gemspec +6 -3
- data/lib/devise_referable/referral.rb +57 -0
- data/lib/devise_referable.rb +1 -2
- data/rails/init.rb +3 -1
- data/test/models/referral_test.rb +1 -0
- data/test/rails_app/app/models/referral.rb +4 -0
- data/test/test_helper.rb +1 -1
- metadata +7 -4
- data/app/models/referral.rb +0 -38
data/CHANGES
ADDED
data/README.rdoc
CHANGED
@@ -23,7 +23,7 @@ Configure devise_referable inside your app (and warden and devise if you weren't
|
|
23
23
|
|
24
24
|
Follow the walkthrough for devise with the following modifications.
|
25
25
|
|
26
|
-
Create a migration for the referrals table:
|
26
|
+
Create a migration for the referrals table, minimally:
|
27
27
|
|
28
28
|
create_table :referrals, :force => true do |t|
|
29
29
|
t.integer :referrer_id
|
@@ -34,6 +34,12 @@ Create a migration for the referrals table:
|
|
34
34
|
t.timestamps
|
35
35
|
end
|
36
36
|
|
37
|
+
And create a Referral class, minimally:
|
38
|
+
|
39
|
+
class Referral < ActiveRecord::Base
|
40
|
+
acts_as_referral
|
41
|
+
end
|
42
|
+
|
37
43
|
Add a referrer_token column to your referrer classes.
|
38
44
|
|
39
45
|
add_column :table_name1, :referrer_token, :string
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/devise_referable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{devise_referable}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Matt Van Horn, based on work by Sergio Cambra"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-22}
|
13
13
|
s.description = %q{It tracks the referring entity via cookie, and creates a record when user registers.}
|
14
14
|
s.email = %q{mattvanhorn@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,17 +19,18 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"CHANGES",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
25
26
|
"VERSION",
|
26
27
|
"app/controllers/referrals_controller.rb",
|
27
|
-
"app/models/referral.rb",
|
28
28
|
"devise_referable.gemspec",
|
29
29
|
"init.rb",
|
30
30
|
"lib/devise/hooks/referable.rb",
|
31
31
|
"lib/devise_referable.rb",
|
32
32
|
"lib/devise_referable/model.rb",
|
33
|
+
"lib/devise_referable/referral.rb",
|
33
34
|
"lib/devise_referable/routes.rb",
|
34
35
|
"rails/init.rb",
|
35
36
|
"test/integration/referable_test.rb",
|
@@ -43,6 +44,7 @@ Gem::Specification.new do |s|
|
|
43
44
|
"test/rails_app/app/controllers/users_controller.rb",
|
44
45
|
"test/rails_app/app/helpers/application_helper.rb",
|
45
46
|
"test/rails_app/app/models/blog.rb",
|
47
|
+
"test/rails_app/app/models/referral.rb",
|
46
48
|
"test/rails_app/app/models/user.rb",
|
47
49
|
"test/rails_app/app/views/home/index.html.erb",
|
48
50
|
"test/rails_app/config/boot.rb",
|
@@ -78,6 +80,7 @@ Gem::Specification.new do |s|
|
|
78
80
|
"test/rails_app/app/controllers/users_controller.rb",
|
79
81
|
"test/rails_app/app/helpers/application_helper.rb",
|
80
82
|
"test/rails_app/app/models/blog.rb",
|
83
|
+
"test/rails_app/app/models/referral.rb",
|
81
84
|
"test/rails_app/app/models/user.rb",
|
82
85
|
"test/rails_app/config/boot.rb",
|
83
86
|
"test/rails_app/config/environment.rb",
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'devise/hooks/referable'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Referable
|
5
|
+
module Referral
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def generate_token
|
13
|
+
self.referral_token ||= Devise.friendly_token
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def acts_as_referral(options = {})
|
19
|
+
class_eval <<-EOV
|
20
|
+
include Devise::Referable::Referral::InstanceMethods
|
21
|
+
|
22
|
+
def self.referrer_types
|
23
|
+
User.referrer_types
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.lookup_referrer(token)
|
27
|
+
unless token.blank?
|
28
|
+
referrer_types.map{ |type| type.to_s.classify.constantize }.each do |klass|
|
29
|
+
ref = klass.find_by_referrer_token(token)
|
30
|
+
return ref unless ref.nil?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.from_token(token)
|
37
|
+
sender = lookup_referrer(token)
|
38
|
+
if sender
|
39
|
+
self.create do |ref|
|
40
|
+
ref.referrer = sender
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
belongs_to :referrer, :polymorphic => true
|
46
|
+
belongs_to :recipient, :class_name => 'User'
|
47
|
+
before_create :generate_token
|
48
|
+
|
49
|
+
EOV
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
data/lib/devise_referable.rb
CHANGED
@@ -7,6 +7,5 @@ Devise.add_module :referable, :controller => :referrals, :model => 'devise_refer
|
|
7
7
|
|
8
8
|
module DeviseReferable; end
|
9
9
|
|
10
|
-
require File.join(File.dirname(__FILE__), '..', 'app', 'models', 'referral')
|
11
|
-
|
12
10
|
require 'devise_referable/routes'
|
11
|
+
require 'devise_referable/referral'
|
data/rails/init.rb
CHANGED
@@ -19,6 +19,7 @@ class ReferableTest < ActiveSupport::TestCase
|
|
19
19
|
test 'should create referral from token' do
|
20
20
|
Blog.expects(:find_by_referrer_token).with('abc123').returns(create_user) # I just need an AR obj here to avoid massive stubbing.
|
21
21
|
# Should be a blog not a user.
|
22
|
+
|
22
23
|
result = Referral.from_token('abc123')
|
23
24
|
assert result.is_a?( Referral)
|
24
25
|
assert_not_nil result.referral_token
|
data/test/test_helper.rb
CHANGED
@@ -44,7 +44,7 @@ ActionController::Routing::Routes.draw do |map|
|
|
44
44
|
end
|
45
45
|
|
46
46
|
require File.join(File.dirname(__FILE__), '..', 'app', 'controllers', 'referrals_controller')
|
47
|
-
require File.join(File.dirname(__FILE__), '..', 'app', 'models', 'referral')
|
47
|
+
# require File.join(File.dirname(__FILE__), '..', 'app', 'models', 'referral')
|
48
48
|
|
49
49
|
Webrat.configure do |config|
|
50
50
|
config.mode = :rails
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matt Van Horn, based on work by Sergio Cambra
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-22 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -67,17 +67,18 @@ extra_rdoc_files:
|
|
67
67
|
files:
|
68
68
|
- .document
|
69
69
|
- .gitignore
|
70
|
+
- CHANGES
|
70
71
|
- LICENSE
|
71
72
|
- README.rdoc
|
72
73
|
- Rakefile
|
73
74
|
- VERSION
|
74
75
|
- app/controllers/referrals_controller.rb
|
75
|
-
- app/models/referral.rb
|
76
76
|
- devise_referable.gemspec
|
77
77
|
- init.rb
|
78
78
|
- lib/devise/hooks/referable.rb
|
79
79
|
- lib/devise_referable.rb
|
80
80
|
- lib/devise_referable/model.rb
|
81
|
+
- lib/devise_referable/referral.rb
|
81
82
|
- lib/devise_referable/routes.rb
|
82
83
|
- rails/init.rb
|
83
84
|
- test/integration/referable_test.rb
|
@@ -91,6 +92,7 @@ files:
|
|
91
92
|
- test/rails_app/app/controllers/users_controller.rb
|
92
93
|
- test/rails_app/app/helpers/application_helper.rb
|
93
94
|
- test/rails_app/app/models/blog.rb
|
95
|
+
- test/rails_app/app/models/referral.rb
|
94
96
|
- test/rails_app/app/models/user.rb
|
95
97
|
- test/rails_app/app/views/home/index.html.erb
|
96
98
|
- test/rails_app/config/boot.rb
|
@@ -150,6 +152,7 @@ test_files:
|
|
150
152
|
- test/rails_app/app/controllers/users_controller.rb
|
151
153
|
- test/rails_app/app/helpers/application_helper.rb
|
152
154
|
- test/rails_app/app/models/blog.rb
|
155
|
+
- test/rails_app/app/models/referral.rb
|
153
156
|
- test/rails_app/app/models/user.rb
|
154
157
|
- test/rails_app/config/boot.rb
|
155
158
|
- test/rails_app/config/environment.rb
|
data/app/models/referral.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'devise/hooks/referable'
|
2
|
-
|
3
|
-
class Referral < ActiveRecord::Base
|
4
|
-
belongs_to :referrer, :polymorphic => true
|
5
|
-
belongs_to :recipient, :class_name => 'User'
|
6
|
-
|
7
|
-
before_create :generate_token
|
8
|
-
|
9
|
-
class << self
|
10
|
-
def referrer_types
|
11
|
-
User.referrer_types
|
12
|
-
end
|
13
|
-
|
14
|
-
def lookup_referrer(token)
|
15
|
-
unless token.blank?
|
16
|
-
referrer_types.map{ |type| type.to_s.classify.constantize }.each do |klass|
|
17
|
-
ref = klass.find_by_referrer_token(token)
|
18
|
-
return ref unless ref.nil?
|
19
|
-
end
|
20
|
-
end
|
21
|
-
return nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def from_token(token)
|
25
|
-
sender = lookup_referrer(token)
|
26
|
-
if sender
|
27
|
-
Referral.create do |ref|
|
28
|
-
ref.referrer = sender
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def generate_token
|
35
|
-
self.referral_token ||= Devise.friendly_token
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|