devise_invitable 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +32 -1
- data/app/controllers/devise/invitations_controller.rb +10 -5
- data/lib/devise_invitable/inviter.rb +39 -0
- data/lib/devise_invitable/model.rb +1 -20
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/devise_invitable.rb +4 -0
- metadata +22 -15
data/README.rdoc
CHANGED
|
@@ -97,6 +97,28 @@ You can also use the generator to generate scoped views:
|
|
|
97
97
|
|
|
98
98
|
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
|
99
99
|
|
|
100
|
+
== Configuring controllers
|
|
101
|
+
|
|
102
|
+
To change the controller's behavior, create a controller that inherits from <tt>Devise::InvitationsController</tt>. The available methods are: new, create, edit, and update. You should read the {original controllers source}[https://raw.github.com/scambra/devise_invitable/master/app/controllers/devise/invitations_controller.rb] before editing any of these actions. Your controller might now look something like this:
|
|
103
|
+
|
|
104
|
+
class Users::InvitationsController < Devise::InvitationsController
|
|
105
|
+
def update
|
|
106
|
+
if this
|
|
107
|
+
redirect_to root_path
|
|
108
|
+
else
|
|
109
|
+
super
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
Now just tell Devise that you want to use your controller, the controller above is 'users/invitations', so our routes.rb would have this line:
|
|
115
|
+
|
|
116
|
+
devise_for :users, :controllers => { :invitations => 'users/invitations' }
|
|
117
|
+
|
|
118
|
+
be sure that you generate the views and put them into the controller that you generated, so for this example it would be:
|
|
119
|
+
|
|
120
|
+
rails generate devise_invitable:views users/invitations
|
|
121
|
+
|
|
100
122
|
== Usage
|
|
101
123
|
|
|
102
124
|
=== Send an invitation
|
|
@@ -128,10 +150,12 @@ To accept an invitation with a token use the <tt>accept_invitation!</tt> class m
|
|
|
128
150
|
|
|
129
151
|
Since the invitations controller take care of all the creation/acceptation of an invitation, in most cases you wouldn't call the <tt>invite!</tt> and <tt>accept_invitation!</tt> methods directly.
|
|
130
152
|
Instead, in your views, put a link to <tt>new_user_invitation_path</tt> or <tt>new_invitation_path(:user)</tt> or even <tt>/users/invitation/new</tt> to prepare and send an invitation (to a user in this example).
|
|
153
|
+
|
|
131
154
|
After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
|
|
132
155
|
|
|
156
|
+
After an invitation is accepted, the invitee will be redirected to after_accept_path_for(resource), which is the same path as after_sign_in_path_for by default. If you want to override the path, override invitations controller and define after_accept_path_for method. This is useful in the common case that a user is invited to a specific location in your application. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
157
|
+
|
|
133
158
|
The invitation email includes a link to accept the invitation that looks like this: <tt>/users/invitation/accept?invitation_token=abcd123</tt>. When clicked, the invited must set a password in order to accept its invitation. Note that if the invitation_token is not present or not valid, the invited is redirected to after_sign_out_path_for(resource_name).
|
|
134
|
-
You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
135
159
|
|
|
136
160
|
The controller sets the invited_by_id attribute for the new user to the current user. This will let you easily keep track of who invited who.
|
|
137
161
|
|
|
@@ -150,6 +174,13 @@ You would have a User model which is configured as invitable and an Admin model
|
|
|
150
174
|
end
|
|
151
175
|
end
|
|
152
176
|
|
|
177
|
+
And include DeviseInvitable::Inviter module into Admin model:
|
|
178
|
+
|
|
179
|
+
class Admin < ActiveRecord::Base
|
|
180
|
+
devise :database_authenticatable, :validatable
|
|
181
|
+
include DeviseInvitable::Inviter
|
|
182
|
+
end
|
|
183
|
+
|
|
153
184
|
== I18n
|
|
154
185
|
|
|
155
186
|
DeviseInvitable uses flash messages with I18n with the flash keys <tt>:send_instructions</tt>, <tt>:invitation_token_invalid</tt> and <tt>:updated</tt>. To customize your app, you can modify the generated locale file:
|
|
@@ -18,9 +18,9 @@ class Devise::InvitationsController < ApplicationController
|
|
|
18
18
|
|
|
19
19
|
if resource.errors.empty?
|
|
20
20
|
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
21
|
-
|
|
21
|
+
respond_with resource, :location => redirect_location(resource_name, resource)
|
|
22
22
|
else
|
|
23
|
-
render_with_scope :new
|
|
23
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
|
|
@@ -40,9 +40,10 @@ class Devise::InvitationsController < ApplicationController
|
|
|
40
40
|
|
|
41
41
|
if resource.errors.empty?
|
|
42
42
|
set_flash_message :notice, :updated
|
|
43
|
-
|
|
43
|
+
sign_in(resource_name, resource)
|
|
44
|
+
respond_with resource, :location => after_accept_path_for(resource)
|
|
44
45
|
else
|
|
45
|
-
render_with_scope :edit
|
|
46
|
+
respond_with_navigational(resource){ render_with_scope :edit }
|
|
46
47
|
end
|
|
47
48
|
end
|
|
48
49
|
|
|
@@ -55,7 +56,11 @@ class Devise::InvitationsController < ApplicationController
|
|
|
55
56
|
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
56
57
|
build_resource
|
|
57
58
|
set_flash_message :alert, :no_invitations_remaining
|
|
58
|
-
render_with_scope :new
|
|
59
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
59
60
|
end
|
|
60
61
|
end
|
|
62
|
+
|
|
63
|
+
def after_accept_path_for(resource)
|
|
64
|
+
after_sign_in_path_for(resource)
|
|
65
|
+
end
|
|
61
66
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module DeviseInvitable
|
|
2
|
+
module Inviter
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
extend ClassMethods
|
|
7
|
+
attr_writer :invitation_limit unless respond_to? :invitation_limit
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def invitation_limit
|
|
11
|
+
self[:invitation_limit] || self.class.invitation_limit
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Return true if this user has invitations left to send
|
|
15
|
+
def has_invitations_left?
|
|
16
|
+
if self.class.invitation_limit.present?
|
|
17
|
+
if invitation_limit
|
|
18
|
+
return invitation_limit > 0
|
|
19
|
+
else
|
|
20
|
+
return self.class.invitation_limit > 0
|
|
21
|
+
end
|
|
22
|
+
else
|
|
23
|
+
return true
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
def decrement_invitation_limit!
|
|
29
|
+
if self.class.invitation_limit.present?
|
|
30
|
+
self.invitation_limit ||= self.class.invitation_limit
|
|
31
|
+
self.update_attribute(:invitation_limit, invitation_limit - 1)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
module ClassMethods
|
|
36
|
+
Devise::Models.config(self, :invitation_limit)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -24,6 +24,7 @@ module Devise
|
|
|
24
24
|
attr_accessor :skip_invitation
|
|
25
25
|
|
|
26
26
|
included do
|
|
27
|
+
include ::DeviseInvitable::Inviter
|
|
27
28
|
belongs_to :invited_by, :polymorphic => true
|
|
28
29
|
end
|
|
29
30
|
|
|
@@ -41,19 +42,6 @@ module Devise
|
|
|
41
42
|
persisted? && invitation_token.present?
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
# Return true if this user has invitations left to send
|
|
45
|
-
def has_invitations_left?
|
|
46
|
-
if self.class.invitation_limit.present?
|
|
47
|
-
if invitation_limit
|
|
48
|
-
return invitation_limit > 0
|
|
49
|
-
else
|
|
50
|
-
return self.class.invitation_limit > 0
|
|
51
|
-
end
|
|
52
|
-
else
|
|
53
|
-
return true
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
45
|
# Reset invitation token and send invitation again
|
|
58
46
|
def invite!
|
|
59
47
|
if new_record? || invited?
|
|
@@ -81,13 +69,6 @@ module Devise
|
|
|
81
69
|
end
|
|
82
70
|
|
|
83
71
|
protected
|
|
84
|
-
def decrement_invitation_limit!
|
|
85
|
-
if self.class.invitation_limit.present?
|
|
86
|
-
self.invitation_limit ||= self.class.invitation_limit
|
|
87
|
-
self.update_attribute(:invitation_limit, invitation_limit - 1)
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
72
|
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
92
73
|
def password_required?
|
|
93
74
|
!@skip_password && super
|
data/lib/devise_invitable.rb
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: devise_invitable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 9
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
version: 0.
|
|
8
|
+
- 5
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.5.1
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Sergio Cambra
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
18
|
+
date: 2011-06-24 00:00:00 +02:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
21
|
- !ruby/object:Gem::Dependency
|
|
@@ -40,7 +40,7 @@ dependencies:
|
|
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
41
|
none: false
|
|
42
42
|
requirements:
|
|
43
|
-
- -
|
|
43
|
+
- - ">="
|
|
44
44
|
- !ruby/object:Gem::Version
|
|
45
45
|
hash: 7
|
|
46
46
|
segments:
|
|
@@ -48,6 +48,13 @@ dependencies:
|
|
|
48
48
|
- 0
|
|
49
49
|
- 0
|
|
50
50
|
version: 3.0.0
|
|
51
|
+
- - <
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
hash: 3
|
|
54
|
+
segments:
|
|
55
|
+
- 3
|
|
56
|
+
- 2
|
|
57
|
+
version: "3.2"
|
|
51
58
|
type: :runtime
|
|
52
59
|
version_requirements: *id002
|
|
53
60
|
- !ruby/object:Gem::Dependency
|
|
@@ -58,20 +65,19 @@ dependencies:
|
|
|
58
65
|
requirements:
|
|
59
66
|
- - ">="
|
|
60
67
|
- !ruby/object:Gem::Version
|
|
61
|
-
hash:
|
|
68
|
+
hash: 25
|
|
62
69
|
segments:
|
|
63
70
|
- 1
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
version: 1.
|
|
67
|
-
- -
|
|
71
|
+
- 3
|
|
72
|
+
- 1
|
|
73
|
+
version: 1.3.1
|
|
74
|
+
- - <
|
|
68
75
|
- !ruby/object:Gem::Version
|
|
69
|
-
hash:
|
|
76
|
+
hash: 5
|
|
70
77
|
segments:
|
|
71
78
|
- 1
|
|
72
|
-
-
|
|
73
|
-
|
|
74
|
-
version: 1.4.0
|
|
79
|
+
- 5
|
|
80
|
+
version: "1.5"
|
|
75
81
|
type: :runtime
|
|
76
82
|
version_requirements: *id003
|
|
77
83
|
description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
|
|
@@ -98,6 +104,7 @@ files:
|
|
|
98
104
|
- lib/devise_invitable/controllers/helpers.rb
|
|
99
105
|
- lib/devise_invitable/controllers/url_helpers.rb
|
|
100
106
|
- lib/devise_invitable/version.rb
|
|
107
|
+
- lib/devise_invitable/inviter.rb
|
|
101
108
|
- lib/generators/active_record/devise_invitable_generator.rb
|
|
102
109
|
- lib/generators/active_record/templates/migration.rb
|
|
103
110
|
- lib/generators/devise_invitable/views_generator.rb
|