devise_invitable 2.0.2 → 2.0.12

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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/README.md +696 -0
  4. data/app/controllers/devise/invitations_controller.rb +3 -3
  5. data/config/locales/ar.yml +23 -0
  6. data/config/locales/ca.yml +32 -0
  7. data/config/locales/da.yml +41 -0
  8. data/config/locales/de.yml +31 -0
  9. data/config/locales/es.yml +31 -0
  10. data/config/locales/et.yml +23 -0
  11. data/config/locales/fa.yml +31 -0
  12. data/config/locales/fr.yml +31 -0
  13. data/config/locales/id.yml +31 -0
  14. data/config/locales/it.yml +31 -0
  15. data/config/locales/ja.yml +31 -0
  16. data/config/locales/ko.yml +24 -0
  17. data/config/locales/nl.yml +32 -0
  18. data/config/locales/no.yml +17 -0
  19. data/config/locales/pl.yml +31 -0
  20. data/config/locales/pt-BR.yml +23 -0
  21. data/config/locales/pt.yml +23 -0
  22. data/config/locales/ru.yml +23 -0
  23. data/config/locales/tr.yml +24 -0
  24. data/config/locales/ua.yml +31 -0
  25. data/config/locales/vi.yml +25 -0
  26. data/config/locales/zh-CN.yml +31 -0
  27. data/config/locales/zh-HK.yml +31 -0
  28. data/config/locales/zh-TW.yml +34 -0
  29. data/lib/devise_invitable/controllers/helpers.rb +4 -0
  30. data/lib/devise_invitable/mapping.rb +4 -2
  31. data/lib/devise_invitable/models/authenticatable.rb +7 -1
  32. data/lib/devise_invitable/models.rb +16 -12
  33. data/lib/devise_invitable/version.rb +1 -1
  34. data/lib/generators/active_record/templates/migration.rb +0 -1
  35. data/lib/generators/devise_invitable/devise_invitable_generator.rb +1 -1
  36. data/test/functional/controller_helpers_test.rb +10 -0
  37. data/test/generators_test.rb +3 -2
  38. data/test/integration/invitation_test.rb +1 -1
  39. data/test/mailers/invitation_mail_test.rb +1 -1
  40. data/test/model_tests_helper.rb +1 -1
  41. data/test/models/invitable_test.rb +80 -11
  42. data/test/orm/active_record.rb +10 -5
  43. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -2
  44. data/test/test_helper.rb +10 -2
  45. metadata +31 -11
  46. data/README.rdoc +0 -439
data/README.rdoc DELETED
@@ -1,439 +0,0 @@
1
- = DeviseInvitable
2
- {<img src="https://badge.fury.io/rb/devise_invitable.svg"/>}[http://badge.fury.io/rb/devise_invitable] {<img src="https://travis-ci.org/scambra/devise_invitable.svg"/>}[https://travis-ci.org/scambra/devise_invitable] {<img src="https://codeclimate.com/github/scambra/devise_invitable/badges/gpa.svg"/>}[https://codeclimate.com/github/scambra/devise_invitable]
3
-
4
- It adds support to Devise[https://github.com/plataformatec/devise] for sending invitations by email (it requires to be authenticated) and accept the invitation setting the password.
5
-
6
- == Requirements
7
-
8
- The latest version of DeviseInvitable works with Devise >= 4.6.
9
-
10
- If you want to use devise_invitable with earlier Devise releases (4.0 <= x < 4.6), use version 1.7.5.
11
-
12
- == Installation
13
-
14
- Install DeviseInvitable gem:
15
-
16
- gem install devise_invitable
17
-
18
- Add DeviseInvitable to your Gemfile:
19
-
20
- gem 'devise_invitable', '~> 2.0.0'
21
-
22
- === Automatic installation
23
-
24
- Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (<tt>config/initializers/devise.rb</tt>):
25
-
26
- rails generate devise_invitable:install
27
-
28
- When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:
29
-
30
- rails generate devise_invitable MODEL
31
-
32
- Replace MODEL by the class name you want to add DeviseInvitable, like <tt>User</tt>, <tt>Admin</tt>, etc. This will add the <tt>:invitable</tt> flag to your model's Devise modules. The generator will also create a migration file (if your ORM supports them).
33
-
34
- === Manual installation
35
-
36
- Follow the walkthrough for Devise and after it's done, follow this walkthrough.
37
-
38
- == Devise Configuration
39
- Add <tt>:invitable</tt> to the <tt>devise</tt> call in your model (we’re assuming here you already have a User model with some Devise modules):
40
-
41
- class User < ActiveRecord::Base
42
- devise :database_authenticatable, :confirmable, :invitable
43
- end
44
-
45
- == ActiveRecord Migration
46
- Add <tt>t.invitable</tt> to your Devise model migration:
47
-
48
- create_table :users do
49
- ...
50
- ## Invitable
51
- t.string :invitation_token
52
- t.datetime :invitation_created_at
53
- t.datetime :invitation_sent_at
54
- t.datetime :invitation_accepted_at
55
- t.integer :invitation_limit
56
- t.integer :invited_by_id
57
- t.string :invited_by_type
58
- ...
59
- end
60
- add_index :users, :invitation_token, unique: true
61
-
62
- or for a model that already exists, define a migration to add DeviseInvitable to your model:
63
-
64
- def change
65
- add_column :users, :invitation_token, :string
66
- add_column :users, :invitation_created_at, :datetime
67
- add_column :users, :invitation_sent_at, :datetime
68
- add_column :users, :invitation_accepted_at, :datetime
69
- add_column :users, :invitation_limit, :integer
70
- add_column :users, :invited_by_id, :integer
71
- add_column :users, :invited_by_type, :string
72
- add_index :users, :invitation_token, unique: true
73
- end
74
-
75
- If you previously used devise_invitable with a <tt>:limit</tt> on <tt>:invitation_token</tt>, remove it:
76
-
77
- def up
78
- change_column :users, :invitation_token, :string, limit: nil
79
- end
80
-
81
- def down
82
- change_column :users, :invitation_token, :string, limit: 60
83
- end
84
-
85
- == Mongoid Field Definitions
86
- If you are using Mongoid, define the following fields and indexes within your invitable model:
87
-
88
- field :invitation_token, type: String
89
- field :invitation_created_at, type: Time
90
- field :invitation_sent_at, type: Time
91
- field :invitation_accepted_at, type: Time
92
- field :invitation_limit, type: Integer
93
-
94
- index( { invitation_token: 1 }, { background: true} )
95
- index( { invitation_by_id: 1 }, { background: true} )
96
-
97
- You do not need to define a <tt>belongs_to</tt> relationship, as DeviseInvitable does this on your behalf:
98
- belongs_to :invited_by, polymorphic: true
99
-
100
- Remember to create indexes within the MongoDB database after deploying your changes.
101
- rake db:mongoid:create_indexes
102
-
103
- == Model configuration
104
-
105
- DeviseInvitable adds some new configuration options:
106
-
107
- * <tt>invite_for</tt>: The period the generated invitation token is valid. After this period, the invited resource won't be able to accept the invitation. When <tt>invite_for</tt> is <tt>0</tt> (the default), the invitation won't expire.
108
-
109
- You can set this configuration option in the Devise initializer as follow:
110
-
111
- # ==> Configuration for :invitable
112
- # The period the generated invitation token is valid.
113
- # After this period, the invited resource won't be able to accept the invitation.
114
- # When invite_for is 0 (the default), the invitation won't expire.
115
- # config.invite_for = 2.weeks
116
-
117
- or directly as parameters to the <tt>devise</tt> method:
118
-
119
- devise :database_authenticatable, :confirmable, :invitable, invite_for: 2.weeks
120
-
121
- * <tt>invitation_limit</tt>: The number of invitations users can send. The default value of <tt>nil</tt> means users can send as many invites as they want, there is no limit for any user, <tt>invitation_limit</tt> column is not used. A setting of <tt>0</tt> means they can't send invitations. A setting <tt>n > 0</tt> means they can send <tt>n</tt> invitations. You can change <tt>invitation_limit</tt> column for some users so they can send more or less invitations, even with global <tt>invitation_limit = 0</tt>.
122
-
123
- * <tt>invite_key</tt>: The key to be used to check existing users when sending an invitation. You can use multiple keys. This value must be a hash with the invite key as hash keys, and values that respond to the <tt>===</tt> operator (including procs and regexes). The default value is looking for users by email and validating with <tt>Devise.email_regexp</tt>.
124
-
125
- * <tt>validate_on_invite</tt>: force a record to be valid before being actually invited.
126
-
127
- * <tt>resend_invitation</tt>: resend invitation if user with invited status is invited again. Enabled by default.
128
-
129
- * <tt>invited_by_class_name</tt>: the class name of the inviting model. If this is <tt>nil</tt>, polymorphic association is used.
130
-
131
- * <tt>invited_by_foreign_key</tt>: the foreign key to the inviting model (only used if <tt>invited_by_class_name</tt> is set, otherwise <tt>:invited_by_id</tt>)
132
-
133
- * <tt>invited_by_counter_cache</tt>: the column name used for counter_cache column. If this is <tt>nil</tt> (default value), the <tt>invited_by</tt> association is declared without <tt>counter_cache</tt>.
134
-
135
- * <tt>allow_insecure_sign_in_after_accept</tt>: automatically sign in the user after they set a password. Enabled by default.
136
-
137
- * <tt>require_password_on_accepting</tt>: require password when user accepts the invitation. Enabled by default. Disable if you don't want to ask or enforce to set password while accepting, because is set when user is invited or it will be set later.
138
-
139
- For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the <tt>devise_invitable:install</tt> generator described above).
140
-
141
- == Configuring views
142
-
143
- All the views are packaged inside the gem. If you'd like to customize the views, invoke the following generator and it will copy all the views to your application:
144
-
145
- rails generate devise_invitable:views
146
-
147
- You can also use the generator to generate scoped views:
148
-
149
- rails generate devise_invitable:views users
150
-
151
- Then turn scoped views on in <tt>config/initializers/devise.rb</tt>:
152
-
153
- config.scoped_views = true
154
-
155
- Please refer to {Devise's README}[https://github.com/plataformatec/devise] for more information about views.
156
-
157
- == Configuring controllers
158
-
159
- To change the controller's behavior, create a controller that inherits from <tt>Devise::InvitationsController</tt>. The available methods are: <tt>new</tt>, <tt>create</tt>, <tt>edit</tt>, and <tt>update</tt>. Refer to the {original controllers source}[https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb] before editing any of these actions. Your controller might now look something like this:
160
-
161
- class Users::InvitationsController < Devise::InvitationsController
162
- def update
163
- if some_condition
164
- redirect_to root_path
165
- else
166
- super
167
- end
168
- end
169
- end
170
-
171
- Now just tell Devise that you want to use your controller, the controller above is <tt>'users/invitations'</tt>, so our routes.rb would have this line:
172
-
173
- devise_for :users, controllers: { invitations: 'users/invitations' }
174
-
175
- be sure that you generate the views and put them into the controller that you generated, so for this example it would be:
176
-
177
- rails generate devise_invitable:views users
178
-
179
- To change behaviour of inviting or accepting users, you can simply override two methods:
180
-
181
- class Users::InvitationsController < Devise::InvitationsController
182
- private
183
-
184
- # this is called when creating invitation
185
- # should return an instance of resource class
186
- def invite_resource
187
- # skip sending emails on invite
188
- super { |user| user.skip_invitation = true }
189
- end
190
-
191
- # this is called when accepting invitation
192
- # should return an instance of resource class
193
- def accept_resource
194
- resource = resource_class.accept_invitation!(update_resource_params)
195
- # Report accepting invitation to analytics
196
- Analytics.report('invite.accept', resource.id)
197
- resource
198
- end
199
- end
200
-
201
- == Strong Parameters
202
-
203
- When you customize your own views, you may end up adding new attributes to forms. Rails 4 moved the parameter sanitization from the model to the controller, causing DeviseInvitable to handle this concern at the controller as well. Read about it in {Devise README}[https://github.com/plataformatec/devise#strong-parameters]
204
-
205
- There are just two actions in DeviseInvitable that allows any set of parameters to be passed down to the model, therefore requiring sanitization. Their names and the permited parameters by default are:
206
-
207
- * <tt>invite</tt> (Devise::InvitationsController#create) - Permits only the authentication keys (like <tt>email</tt>)
208
- * <tt>accept_invitation</tt> (Devise::InvitationsController#update) - Permits <tt>invitation_token</tt> plus <tt>password</tt> and <tt>password_confirmation</tt>.
209
-
210
- Here is an example of what your application controller might need to include in order to add these parameters to the invitation view:
211
-
212
- before_action :configure_permitted_parameters, if: :devise_controller?
213
-
214
- protected
215
-
216
- def configure_permitted_parameters
217
- devise_parameter_sanitizer.permit(:accept_invitation, keys: [:first_name, :last_name, :phone])
218
- end
219
-
220
-
221
- == Usage
222
-
223
- === Send an invitation
224
-
225
- To send an invitation to a user, use the <tt>invite!</tt> class method. <b>Note: This will create a user, and send an email for the invite.</b> <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.
226
-
227
- User.invite!(email: 'new_user@example.com', name: 'John Doe')
228
- # => an invitation email will be sent to new_user@example.com
229
-
230
- If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to <tt>true</tt>.
231
-
232
- user = User.invite!(email: 'new_user@example.com', name: 'John Doe') do |u|
233
- u.skip_invitation = true
234
- end
235
- # => the record will be created, but the invitation email will not be sent
236
-
237
- When generating the <tt>accept_user_invitation_url</tt> yourself, you must use the <tt>raw_invitation_token</tt>.
238
- This value is temporarily available when you invite a user and will be decrypted when received.
239
-
240
- accept_user_invitation_url(invitation_token: user.raw_invitation_token)
241
-
242
- When <tt>skip_invitation</tt> is used, you must also then set the <tt>invitation_sent_at</tt> field when the user is sent their token. Failure to do so will yield "Invalid invitation token" error when the user attempts to accept the invite.
243
- You can set the column, or call <tt>deliver_invitation</tt> to send the invitation and set the column:
244
-
245
- user.deliver_invitation
246
-
247
- You can add <tt>:skip_invitation</tt> to attributes hash if <tt>skip_invitation</tt> is added to <tt>attr_accessible</tt>.
248
-
249
- User.invite!(email: 'new_user@example.com', name: 'John Doe', skip_invitation: true)
250
- # => the record will be created, but the invitation email will not be sent
251
-
252
- <tt>skip_invitation</tt> skips sending the email, but sets <tt>invitation_token</tt>, so <tt>invited_to_sign_up?</tt> on the
253
- resulting user returns <tt>true</tt>.
254
-
255
- To check if a particular user is created by invitation, irrespective to state of invitation one can use <tt>created_by_invite?</tt>
256
-
257
- **Warning**
258
-
259
- When using <tt>skip_invitation</tt> you must send the email with the user object instance that generated the tokens, as <tt>user.raw_invitation_token</tt> is available only to the instance and is not persisted in the database.
260
-
261
- You can also set <tt>invited_by</tt> when using the <tt>invite!</tt> class method:
262
-
263
- User.invite!({ email: 'new_user@example.com' }, current_user) # current_user will be set as invited_by
264
-
265
- === Sending an invitation after user creation
266
-
267
- You can send an invitation to an existing user if your workflow creates them separately:
268
-
269
- user = User.find(42)
270
- user.invite!(current_user) # current user is optional to set the invited_by attribute
271
-
272
- === Find by invitation token
273
-
274
- To find by invitation token use the <tt>find_by_invitation_token</tt> class method.
275
-
276
- user = User.find_by_invitation_token(params[:invitation_token], true)
277
-
278
- === Accept an invitation
279
-
280
- 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.
281
-
282
- User.accept_invitation!(invitation_token: params[:invitation_token], password: 'ad97nwj3o2', name: 'John Doe')
283
-
284
- === Callbacks
285
-
286
- A callback event is fired before and after an invitation is created (User#invite!) or accepted (User#accept_invitation!). For example, in your resource model you can add:
287
-
288
- before_invitation_created :email_admins
289
- after_invitation_accepted :email_invited_by
290
-
291
- def email_admins
292
- # ...
293
- end
294
-
295
- def email_invited_by
296
- # ...
297
- end
298
-
299
- The callbacks support all options and arguments available to the standard callbacks provided by ActiveRecord.
300
-
301
- === Scopes
302
-
303
- A pair of scopes to find those users that have accepted, and those that have not accepted, invitations are defined:
304
-
305
- User.invitation_accepted # => returns all Users for whom the invitation_accepted_at attribute is not nil
306
- User.invitation_not_accepted # => returns all Users for whom the invitation_accepted_at attribute is nil
307
- User.created_by_invite # => returns all Users who are created by invitations, irrespective to invitation status
308
-
309
- == Integration in a Rails application
310
-
311
- Since the invitations controller takes 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.
312
- 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).
313
-
314
- After an invitation is created and sent, the inviter will be redirected to <tt>after_invite_path_for(inviter, invitee)</tt>, which is the same path as <tt>signed_in_root_path</tt> by default.
315
-
316
- After an invitation is accepted, the invitee will be redirected to <tt>after_accept_path_for(resource)</tt>, which is the same path as <tt>signed_in_root_path</tt> by default. If you want to override the path, override invitations controller and define <tt>after_accept_path_for</tt> 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}[https://github.com/plataformatec/devise], "Controller filters and helpers" section.
317
-
318
- 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 <tt>invitation_token</tt> is not present or not valid, the invited is redirected to <tt>after_sign_out_path_for(resource_name)</tt>.
319
-
320
- The controller sets the <tt>invited_by_id</tt> attribute for the new user to the current user. This will let you easily keep track of who invited whom.
321
-
322
- == Controller filter
323
-
324
- InvitationsController uses <tt>authenticate_inviter!</tt> filter to restrict who can send invitations. You can override this method in your <tt>ApplicationController</tt>.
325
-
326
- Default behavior requires authentication of the same resource as the invited one. For example, if your model <tt>User</tt> is invitable, it will allow all authenticated users to send invitations to other users.
327
-
328
- You would have a <tt>User</tt> model which is configured as invitable and an <tt>Admin</tt> model which is not. If you want to allow only admins to send invitations, simply overwrite the <tt>authenticate_inviter!</tt> method as follow:
329
-
330
- class ApplicationController < ActionController::Base
331
- protected
332
-
333
- def authenticate_inviter!
334
- authenticate_admin!(force: true)
335
- end
336
- end
337
-
338
- And include <tt>DeviseInvitable::Inviter</tt> module into <tt>Admin</tt> model:
339
-
340
- class Admin < ActiveRecord::Base
341
- devise :database_authenticatable, :validatable
342
- include DeviseInvitable::Inviter
343
- end
344
-
345
- == Has many invitations
346
-
347
- If you want to get all records invited by a resource, you should define <tt>has_many</tt> association in the model allowed to send invitations.
348
-
349
- For the default behavior, define it like this:
350
-
351
- has_many :invitations, class_name: self.to_s, as: :invited_by
352
-
353
- For the previous example, where admins send invitations to users, define it like this:
354
-
355
- has_many :invitations, class_name: 'User', as: :invited_by
356
-
357
- == I18n
358
-
359
- 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:
360
-
361
- en:
362
- devise:
363
- invitations:
364
- send_instructions: 'An invitation email has been sent to %{email}.'
365
- invitation_token_invalid: 'The invitation token provided is not valid!'
366
- updated: 'Your password was set successfully. You are now signed in.'
367
- updated_not_active: 'Your password was set successfully.'
368
-
369
- You can also create distinct messages based on the resource you've configured using the singular name given in routes:
370
-
371
- en:
372
- devise:
373
- invitations:
374
- user:
375
- send_instructions: 'A new user invitation has been sent to %{email}.'
376
- invitation_token_invalid: 'Your invitation token is not valid!'
377
- updated: 'Welcome on board! You are now signed in.'
378
- updated_not_active: 'Welcome on board! Sign in to continue.'
379
-
380
- The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
381
-
382
- en:
383
- devise:
384
- mailer:
385
- invitation_instructions:
386
- subject: 'You got an invitation!'
387
- user_subject: 'You got a user invitation!'
388
-
389
- Take a look at the {generated locale file}[https://github.com/scambra/devise_invitable/blob/master/config/locales/en.yml] to check all available messages.
390
-
391
- Check out wiki[https://github.com/scambra/devise_invitable/wiki/I18n] for translations.
392
-
393
- === Use with sub schema
394
- If you are using sub schema in you application, you need to make sure that you are prioritizing your sub schema scheme over Warden in Rack.
395
- For instance, if you are using the Apartment gem go inside your <tt>config/application.rb</tt> file, add the following lines:
396
-
397
- module YourSite
398
- class Application < Rails::Application
399
- ...
400
- Rails.application.config.middleware.insert_before Warden::Manager, Apartment::Elevators::Subdomain
401
- end
402
- end
403
-
404
- == Other ORMs
405
-
406
- DeviseInvitable supports ActiveRecord and Mongoid, like Devise.
407
-
408
- == Wiki
409
-
410
- It's possible to find additional information about DeviseInvitable on the Wiki:
411
-
412
- https://github.com/scambra/devise_invitable/wiki
413
-
414
- == Testing
415
-
416
- To run tests:
417
-
418
- bundle install
419
- bundle exec rake test
420
-
421
- == Contributors
422
-
423
- Check them all at:
424
-
425
- https://github.com/scambra/devise_invitable/contributors
426
-
427
- Special thanks to rymai[https://github.com/rymai] for the Rails 3 support, his fork was a great help.
428
-
429
- == Note on Patches/Pull Requests
430
-
431
- * Fork the project.
432
- * Make your feature addition or bug fix.
433
- * Add tests for it. This is important so I don't break it in a future version unintentionally.
434
- * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
435
- * Send me a pull request. Bonus points for topic branches.
436
-
437
- == Copyright
438
-
439
- Copyright (c) 2019 Sergio Cambra. See LICENSE for details.