devise_invitable 0.3.4 → 0.6.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 +124 -43
- data/app/controllers/devise/invitations_controller.rb +36 -9
- data/app/views/devise/invitations/new.html.erb +1 -1
- data/config/locales/en.yml +5 -3
- data/lib/devise_invitable/controllers/helpers.rb +1 -6
- data/lib/devise_invitable/inviter.rb +39 -0
- data/lib/devise_invitable/mailer.rb +4 -3
- data/lib/devise_invitable/model.rb +109 -44
- data/lib/devise_invitable/model.rb~ +210 -0
- data/lib/devise_invitable/rails.rb +5 -2
- data/lib/devise_invitable/routes.rb +8 -5
- data/lib/devise_invitable/schema.rb +29 -2
- data/lib/devise_invitable/version.rb +3 -0
- data/lib/devise_invitable.rb +48 -6
- data/lib/generators/active_record/templates/migration.rb +15 -7
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +4 -0
- data/lib/generators/devise_invitable/install_generator.rb +21 -2
- data/lib/generators/devise_invitable/views_generator.rb +11 -2
- data/lib/generators/mongoid/devise_invitable_generator.rb +8 -0
- metadata +62 -148
- data/.document +0 -5
- data/.gitignore +0 -22
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -108
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/devise_invitable.gemspec +0 -146
- data/test/generators_test.rb +0 -45
- data/test/integration/invitable_test.rb +0 -109
- data/test/integration_tests_helper.rb +0 -58
- data/test/mailers/invitation_test.rb +0 -62
- data/test/model_tests_helper.rb +0 -41
- data/test/models/invitable_test.rb +0 -188
- data/test/models_test.rb +0 -31
- data/test/rails_app/app/controllers/admins_controller.rb +0 -6
- data/test/rails_app/app/controllers/application_controller.rb +0 -3
- data/test/rails_app/app/controllers/home_controller.rb +0 -4
- data/test/rails_app/app/controllers/users_controller.rb +0 -12
- data/test/rails_app/app/helpers/application_helper.rb +0 -2
- data/test/rails_app/app/models/octopussy.rb +0 -11
- data/test/rails_app/app/models/user.rb +0 -4
- data/test/rails_app/app/views/home/index.html.erb +0 -0
- data/test/rails_app/app/views/layouts/application.html.erb +0 -15
- data/test/rails_app/app/views/users/invitations/new.html.erb +0 -15
- data/test/rails_app/config/application.rb +0 -46
- data/test/rails_app/config/boot.rb +0 -13
- data/test/rails_app/config/database.yml +0 -22
- data/test/rails_app/config/environment.rb +0 -5
- data/test/rails_app/config/environments/development.rb +0 -26
- data/test/rails_app/config/environments/production.rb +0 -49
- data/test/rails_app/config/environments/test.rb +0 -35
- data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
- data/test/rails_app/config/initializers/devise.rb +0 -144
- data/test/rails_app/config/initializers/inflections.rb +0 -10
- data/test/rails_app/config/initializers/mime_types.rb +0 -5
- data/test/rails_app/config/initializers/secret_token.rb +0 -7
- data/test/rails_app/config/initializers/session_store.rb +0 -8
- data/test/rails_app/config/locales/en.yml +0 -5
- data/test/rails_app/config/routes.rb +0 -4
- data/test/rails_app/config.ru +0 -4
- data/test/rails_app/script/rails +0 -6
- data/test/routes_test.rb +0 -20
- data/test/test_helper.rb +0 -30
- /data/app/views/devise/mailer/{invitation.html.erb → invitation_instructions.html.erb} +0 -0
data/README.rdoc
CHANGED
|
@@ -2,26 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
It adds support to devise[http://github.com/plataformatec/devise] for send invitations by email (it requires to be authenticated) and accept the invitation setting the password.
|
|
4
4
|
|
|
5
|
-
DeviseInvitable currently only support
|
|
5
|
+
DeviseInvitable currently only support Rails 3, if you want to use it with Rails 2.3 you must install version {0.2.3}[http://rubygems.org/gems/devise_invitable/versions/0.2.3]
|
|
6
6
|
|
|
7
|
-
== Installation for Rails ~> 3.0
|
|
7
|
+
== Installation for Rails ~> 3.0 and Devise ~> 1.2
|
|
8
8
|
|
|
9
|
-
Install
|
|
9
|
+
Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
gem install devise_invitable
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):
|
|
14
14
|
|
|
15
|
-
gem 'devise'
|
|
16
|
-
gem 'devise_invitable'
|
|
15
|
+
gem 'devise', '~> 1.2.0'
|
|
16
|
+
gem 'devise_invitable', '~> 0.4.0'
|
|
17
17
|
|
|
18
18
|
=== Automatic installation
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (config/initializers/devise.rb):
|
|
21
21
|
|
|
22
22
|
rails generate devise_invitable:install
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:
|
|
25
25
|
|
|
26
26
|
rails generate devise_invitable MODEL
|
|
27
27
|
|
|
@@ -31,7 +31,7 @@ Replace MODEL by the class name you want to add DeviseInvitable, like User, Admi
|
|
|
31
31
|
|
|
32
32
|
Follow the walkthrough for Devise and after it's done, follow this walkthrough.
|
|
33
33
|
|
|
34
|
-
Add :invitable to the
|
|
34
|
+
Add :invitable to the <tt>devise</tt> call in your model (we’re assuming here you already have a User model with some Devise modules):
|
|
35
35
|
|
|
36
36
|
class User < ActiveRecord::Base
|
|
37
37
|
devise :database_authenticatable, :confirmable, :invitable
|
|
@@ -46,39 +46,47 @@ Add t.invitable to your Devise model migration:
|
|
|
46
46
|
end
|
|
47
47
|
add_index :users, :invitation_token
|
|
48
48
|
|
|
49
|
-
or for a model that
|
|
49
|
+
or for a model that already exists, define a migration to add DeviseInvitable to your model:
|
|
50
50
|
|
|
51
51
|
change_table :users do |t|
|
|
52
|
-
t.string :invitation_token, :limit =>
|
|
52
|
+
t.string :invitation_token, :limit => 60
|
|
53
53
|
t.datetime :invitation_sent_at
|
|
54
|
+
t.datetime :invitation_accepted_at
|
|
54
55
|
t.index :invitation_token
|
|
55
56
|
end
|
|
56
57
|
|
|
57
|
-
# Allow null encrypted_password
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
DeviseInvitable doesn't use _attr_accessible_ or _attr_protected_, so be sure to define attributes as accessible or protected in your model.
|
|
58
|
+
# Allow null encrypted_password
|
|
59
|
+
change_column_null :users, :encrypted_password, true
|
|
60
|
+
# Allow null password_salt (add it if you are using Devise's encryptable module)
|
|
61
|
+
change_column_null :users, :password_salt, true
|
|
62
62
|
|
|
63
63
|
== Model configuration
|
|
64
64
|
|
|
65
|
-
DeviseInvitable adds
|
|
65
|
+
DeviseInvitable adds some new configuration options:
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
* invite_for: The period the generated invitation token is valid, after this period, the invited resource won't be able to accept the invitation. When invite_for is 0 (the default), the invitation won't expire.
|
|
68
68
|
|
|
69
|
-
You can set
|
|
69
|
+
You can set this configuration option in the Devise initializer as follow:
|
|
70
70
|
|
|
71
71
|
# ==> Configuration for :invitable
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
72
|
+
# The period the generated invitation token is valid, after
|
|
73
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
74
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
75
75
|
# config.invite_for = 2.weeks
|
|
76
76
|
|
|
77
|
-
or directly as parameters to the <tt>devise</tt> method
|
|
77
|
+
or directly as parameters to the <tt>devise</tt> method:
|
|
78
78
|
|
|
79
79
|
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
* invitation_limit: The number of invitations users can send. The default value of nil means users can send as many invites as they want. A setting of 0 means they can't send invitations. A setting n > 0 means they can send n invitations.
|
|
82
|
+
|
|
83
|
+
* invite_key: The key to be used to check existing users when sending an invitation. The key must be an unique field. The default value is looking for users by email.
|
|
84
|
+
|
|
85
|
+
* validate_on_invite: force a record to be valid before being actually invited.
|
|
86
|
+
|
|
87
|
+
* resend_invitation: resend invitation if user with invited status is invited again. Enabled by default.
|
|
88
|
+
|
|
89
|
+
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
82
90
|
|
|
83
91
|
== Configuring views
|
|
84
92
|
|
|
@@ -92,51 +100,123 @@ You can also use the generator to generate scoped views:
|
|
|
92
100
|
|
|
93
101
|
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
|
94
102
|
|
|
103
|
+
== Configuring controllers
|
|
104
|
+
|
|
105
|
+
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:
|
|
106
|
+
|
|
107
|
+
class Users::InvitationsController < Devise::InvitationsController
|
|
108
|
+
def update
|
|
109
|
+
if this
|
|
110
|
+
redirect_to root_path
|
|
111
|
+
else
|
|
112
|
+
super
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
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:
|
|
118
|
+
|
|
119
|
+
devise_for :users, :controllers => { :invitations => 'users/invitations' }
|
|
120
|
+
|
|
121
|
+
be sure that you generate the views and put them into the controller that you generated, so for this example it would be:
|
|
122
|
+
|
|
123
|
+
rails generate devise_invitable:views users/invitations
|
|
124
|
+
|
|
95
125
|
== Usage
|
|
96
126
|
|
|
97
127
|
=== Send an invitation
|
|
98
128
|
|
|
99
|
-
To send an invitation to a user, use the <tt>invite!</tt> class method.
|
|
100
|
-
You can also include other attributes in the hash. The record will not be validated.
|
|
129
|
+
To send an invitation to a user, use the <tt>invite!</tt> class method. <tt>:email</tt> must be present in the parameters hash. You can also include other attributes in the hash. The record will not be validated.
|
|
101
130
|
|
|
102
|
-
User.invite(:email => "new_user@example.com", :name => "John Doe")
|
|
131
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
103
132
|
# => an invitation email will be sent to new_user@example.com
|
|
104
133
|
|
|
134
|
+
If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.
|
|
135
|
+
|
|
136
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
|
|
137
|
+
u.skip_invitation = true
|
|
138
|
+
end
|
|
139
|
+
# => the record will be created, but the invitation email will not be sent
|
|
140
|
+
|
|
141
|
+
You can add :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.
|
|
142
|
+
|
|
143
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
|
|
144
|
+
# => the record will be created, but the invitation email will not be sent
|
|
145
|
+
|
|
105
146
|
=== Accept an invitation
|
|
106
147
|
|
|
107
|
-
To accept an invitation with a token use the <tt>accept_invitation
|
|
148
|
+
To accept an invitation with a token use the <tt>accept_invitation!</tt> class method. <tt>:invitation_token</tt> must be present in the parameters hash. You can also include other attributes in the hash.
|
|
149
|
+
|
|
150
|
+
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
108
151
|
|
|
109
|
-
|
|
152
|
+
=== Callbacks
|
|
153
|
+
|
|
154
|
+
A callback event is fired before and after an invitation is accepted (User#accept_invitation!). For example, in your resource model you can add:
|
|
155
|
+
|
|
156
|
+
after_invitation_accepted :email_invited_by
|
|
157
|
+
|
|
158
|
+
def email_invited_by
|
|
159
|
+
# ...
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
The callbacks support all options and arguments available to the standard callbacks provided by AR.
|
|
110
163
|
|
|
111
164
|
== Integration in a Rails application
|
|
112
165
|
|
|
113
|
-
Since the invitations controller take care of all the
|
|
114
|
-
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.
|
|
115
|
-
|
|
166
|
+
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.
|
|
167
|
+
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).
|
|
168
|
+
|
|
169
|
+
After an invitation is created and sent, the inviter will be redirected to after_invite_path_for(resource_name), which is stored path or the same path as after_sign_in_path_for by default.
|
|
170
|
+
|
|
171
|
+
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.
|
|
172
|
+
|
|
173
|
+
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).
|
|
174
|
+
|
|
175
|
+
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.
|
|
116
176
|
|
|
117
177
|
== Controller filter
|
|
118
178
|
|
|
119
179
|
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
120
180
|
|
|
121
|
-
Default behavior requires authentication of the same resource. For example, if your model User is
|
|
181
|
+
Default behavior requires authentication of the same resource as the invited one. For example, if your model User is invitable, it will allow all authenticated users to send invitations to other users.
|
|
122
182
|
|
|
123
|
-
You would have a User model which is configured as invitable and an Admin model which is not. If you
|
|
183
|
+
You would have a User model which is configured as invitable and an Admin model which is not. If you want to allow only admins to send invitations, simply overwrite the authenticate_inviter! method as follow:
|
|
124
184
|
|
|
125
185
|
class ApplicationController < ActionController::Base
|
|
126
186
|
protected
|
|
127
187
|
def authenticate_inviter!
|
|
128
|
-
authenticate_admin!
|
|
188
|
+
authenticate_admin!(:force => true)
|
|
129
189
|
end
|
|
130
190
|
end
|
|
131
191
|
|
|
192
|
+
And include DeviseInvitable::Inviter module into Admin model:
|
|
193
|
+
|
|
194
|
+
class Admin < ActiveRecord::Base
|
|
195
|
+
devise :database_authenticatable, :validatable
|
|
196
|
+
include DeviseInvitable::Inviter
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
== Has many invitations
|
|
200
|
+
|
|
201
|
+
If you want to get all records invited by a resource, you should define has_many association in the model allowed to send invitations.
|
|
202
|
+
|
|
203
|
+
For the default behavior, define it like this:
|
|
204
|
+
|
|
205
|
+
has_many :invitations, :class_name => self.class.to_s, :as => :invited_by
|
|
206
|
+
|
|
207
|
+
For the previous example, where admins send invitations to users, define it like this:
|
|
208
|
+
|
|
209
|
+
has_many :invitations, :class_name => 'User', :as => :invited_by
|
|
210
|
+
|
|
132
211
|
== I18n
|
|
133
212
|
|
|
134
|
-
DeviseInvitable uses flash messages with I18n with the flash keys <tt>:send_instructions</tt> and <tt>:updated</tt>. To customize your app, you can modify the generated locale file:
|
|
213
|
+
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:
|
|
135
214
|
|
|
136
215
|
en:
|
|
137
216
|
devise:
|
|
138
217
|
invitations:
|
|
139
|
-
send_instructions: 'An email
|
|
218
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
219
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
140
220
|
updated: 'Your password was set successfully. You are now signed in.'
|
|
141
221
|
|
|
142
222
|
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
|
|
@@ -145,17 +225,18 @@ You can also create distinct messages based on the resource you've configured us
|
|
|
145
225
|
devise:
|
|
146
226
|
invitations:
|
|
147
227
|
user:
|
|
148
|
-
send_instructions: 'A new user invitation has been sent.'
|
|
228
|
+
send_instructions: 'A new user invitation has been sent to %{email}.'
|
|
229
|
+
invitation_token_invalid: 'Your invitation token is not valid!'
|
|
149
230
|
updated: 'Welcome on board! You are now signed in.'
|
|
150
231
|
|
|
151
|
-
The DeviseInvitable mailer uses the
|
|
232
|
+
The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
|
|
152
233
|
|
|
153
234
|
en:
|
|
154
235
|
devise:
|
|
155
236
|
mailer:
|
|
156
|
-
|
|
237
|
+
invitation_instructions:
|
|
157
238
|
subject: 'You got an invitation!'
|
|
158
|
-
user_subject: 'You got
|
|
239
|
+
user_subject: 'You got a user invitation!'
|
|
159
240
|
|
|
160
241
|
Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
|
|
161
242
|
|
|
@@ -169,7 +250,7 @@ Check them all at:
|
|
|
169
250
|
|
|
170
251
|
http://github.com/scambra/devise_invitable/contributors
|
|
171
252
|
|
|
172
|
-
Special thanks to rymai[http://github.com/rymai] for
|
|
253
|
+
Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
|
|
173
254
|
|
|
174
255
|
== Note on Patches/Pull Requests
|
|
175
256
|
|
|
@@ -2,6 +2,7 @@ class Devise::InvitationsController < ApplicationController
|
|
|
2
2
|
include Devise::Controllers::InternalHelpers
|
|
3
3
|
|
|
4
4
|
before_filter :authenticate_inviter!, :only => [:new, :create]
|
|
5
|
+
before_filter :has_invitations_left?, :only => [:create]
|
|
5
6
|
before_filter :require_no_authentication, :only => [:edit, :update]
|
|
6
7
|
helper_method :after_sign_in_path_for
|
|
7
8
|
|
|
@@ -13,21 +14,24 @@ class Devise::InvitationsController < ApplicationController
|
|
|
13
14
|
|
|
14
15
|
# POST /resource/invitation
|
|
15
16
|
def create
|
|
16
|
-
self.resource = resource_class.invite!(params[resource_name])
|
|
17
|
+
self.resource = resource_class.invite!(params[resource_name], current_inviter)
|
|
17
18
|
|
|
18
19
|
if resource.errors.empty?
|
|
19
|
-
set_flash_message :notice, :send_instructions
|
|
20
|
-
|
|
20
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
21
|
+
respond_with resource, :location => after_invite_path_for(resource)
|
|
21
22
|
else
|
|
22
|
-
render_with_scope :new
|
|
23
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
27
28
|
def edit
|
|
28
|
-
self.resource = resource_class.
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if params[:invitation_token] && self.resource = resource_class.to_adapter.find_first( :invitation_token => params[:invitation_token] )
|
|
30
|
+
render_with_scope :edit
|
|
31
|
+
else
|
|
32
|
+
set_flash_message(:alert, :invitation_token_invalid)
|
|
33
|
+
redirect_to after_sign_out_path_for(resource_name)
|
|
34
|
+
end
|
|
31
35
|
end
|
|
32
36
|
|
|
33
37
|
# PUT /resource/invitation
|
|
@@ -36,9 +40,32 @@ class Devise::InvitationsController < ApplicationController
|
|
|
36
40
|
|
|
37
41
|
if resource.errors.empty?
|
|
38
42
|
set_flash_message :notice, :updated
|
|
39
|
-
|
|
43
|
+
sign_in(resource_name, resource)
|
|
44
|
+
respond_with resource, :location => after_accept_path_for(resource)
|
|
40
45
|
else
|
|
41
|
-
render_with_scope :edit
|
|
46
|
+
respond_with_navigational(resource){ render_with_scope :edit }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
protected
|
|
51
|
+
def current_inviter
|
|
52
|
+
@current_inviter ||= authenticate_inviter!
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def has_invitations_left?
|
|
56
|
+
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
57
|
+
build_resource
|
|
58
|
+
set_flash_message :alert, :no_invitations_remaining
|
|
59
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
42
60
|
end
|
|
43
61
|
end
|
|
62
|
+
|
|
63
|
+
def after_invite_path_for(resource)
|
|
64
|
+
after_sign_in_path_for(resource)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def after_accept_path_for(resource)
|
|
68
|
+
after_sign_in_path_for(resource)
|
|
69
|
+
end
|
|
44
70
|
end
|
|
71
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<h2>Send invitation</h2>
|
|
2
2
|
|
|
3
|
-
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name) do |f| %>
|
|
3
|
+
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
|
|
4
4
|
<%= devise_error_messages! %>
|
|
5
5
|
|
|
6
6
|
<p><%= f.label :email %><br />
|
data/config/locales/en.yml
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
en:
|
|
2
2
|
devise:
|
|
3
3
|
invitations:
|
|
4
|
-
send_instructions: 'An email
|
|
4
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
5
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
5
6
|
updated: 'Your password was set successfully. You are now signed in.'
|
|
7
|
+
no_invitations_remaining: "No invitations remaining"
|
|
6
8
|
mailer:
|
|
7
|
-
|
|
8
|
-
subject: 'Invitation'
|
|
9
|
+
invitation_instructions:
|
|
10
|
+
subject: 'Invitation instructions'
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
module DeviseInvitable::Controllers::Helpers
|
|
2
2
|
protected
|
|
3
|
-
def authenticate_resource!
|
|
4
|
-
ActiveSupport::Deprecation.warn('authenticate_resource! has been renamed to authenticate_inviter!')
|
|
5
|
-
authenticate_inviter!
|
|
6
|
-
end
|
|
7
|
-
|
|
8
3
|
def authenticate_inviter!
|
|
9
|
-
send(:"authenticate_#{resource_name}!")
|
|
4
|
+
send(:"authenticate_#{resource_name}!", :force => true)
|
|
10
5
|
end
|
|
11
6
|
end
|
|
12
7
|
ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
|
|
@@ -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
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
module DeviseInvitable
|
|
2
2
|
module Mailer
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
|
|
4
|
+
# Deliver an invitation email
|
|
5
|
+
def invitation_instructions(record)
|
|
6
|
+
devise_mail(record, :invitation_instructions)
|
|
6
7
|
end
|
|
7
8
|
end
|
|
8
9
|
end
|