devise_invitable 0.4.0 → 0.5.0

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 CHANGED
@@ -4,7 +4,7 @@ It adds support to devise[http://github.com/plataformatec/devise] for send invit
4
4
 
5
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 and Devise ~> 1.1
7
+ == Installation for Rails ~> 3.0 and Devise ~> 1.2
8
8
 
9
9
  Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):
10
10
 
@@ -12,8 +12,8 @@ Install DeviseInvitable gem, it will also install dependencies (such as devise a
12
12
 
13
13
  Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):
14
14
 
15
- gem 'devise', '~> 1.1.3'
16
- gem 'devise_invitable', '~> 0.3.4'
15
+ gem 'devise', '~> 1.2.0'
16
+ gem 'devise_invitable', '~> 0.4.0'
17
17
 
18
18
  === Automatic installation
19
19
 
@@ -61,7 +61,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
61
61
 
62
62
  == Model configuration
63
63
 
64
- DeviseInvitable adds three new configuration options:
64
+ DeviseInvitable adds four new configuration options:
65
65
 
66
66
  * 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.
67
67
 
@@ -81,6 +81,8 @@ or directly as parameters to the <tt>devise</tt> method:
81
81
 
82
82
  * 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.
83
83
 
84
+ * validate_on_invite: force a record to be valid before being actually invited.
85
+
84
86
  For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
85
87
 
86
88
  == Configuring views
@@ -126,10 +128,10 @@ To accept an invitation with a token use the <tt>accept_invitation!</tt> class m
126
128
 
127
129
  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.
128
130
  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).
129
- After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
131
+
132
+ After an invitation is created and sent, the inviter 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. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
130
133
 
131
134
  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).
132
- 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.
133
135
 
134
136
  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.
135
137
 
@@ -148,6 +150,13 @@ You would have a User model which is configured as invitable and an Admin model
148
150
  end
149
151
  end
150
152
 
153
+ And include DeviseInvitable::Inviter module into Admin model:
154
+
155
+ class Admin < ActiveRecord::Base
156
+ devise :database_authenticatable, :validatable
157
+ include DeviseInvitable::Inviter
158
+ end
159
+
151
160
  == I18n
152
161
 
153
162
  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
- redirect_to after_sign_in_path_for(resource_name)
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
- sign_in_and_redirect(resource_name, resource)
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,35 @@
1
+ module DeviseInvitable
2
+ module Inviter
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ extend ClassMethods
7
+ attr_accessor :invitation_limit unless respond_to? :invitation_limit
8
+ end
9
+
10
+ # Return true if this user has invitations left to send
11
+ def has_invitations_left?
12
+ if self.class.invitation_limit.present?
13
+ if invitation_limit
14
+ return invitation_limit > 0
15
+ else
16
+ return self.class.invitation_limit > 0
17
+ end
18
+ else
19
+ return true
20
+ end
21
+ end
22
+
23
+ protected
24
+ def decrement_invitation_limit!
25
+ if self.class.invitation_limit.present?
26
+ self.invitation_limit ||= self.class.invitation_limit
27
+ self.update_attribute(:invitation_limit, invitation_limit - 1)
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+ Devise::Models.config(self, :invitation_limit)
33
+ end
34
+ end
35
+ 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.decrement!(:invitation_limit)
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
@@ -4,7 +4,10 @@ module DeviseInvitable
4
4
  ActiveSupport.on_load(:action_controller) { include DeviseInvitable::Controllers::UrlHelpers }
5
5
  ActiveSupport.on_load(:action_view) { include DeviseInvitable::Controllers::UrlHelpers }
6
6
 
7
- config.after_initialize do
7
+ # We use to_prepare instead of after_initialize here because Devise is a Rails engine; its
8
+ # mailer is reloaded like the rest of the user's app. Got to make sure that our mailer methods
9
+ # are included each time Devise::Mailer is (re)loaded.
10
+ config.to_prepare do
8
11
  require 'devise/mailer'
9
12
  Devise::Mailer.send :include, DeviseInvitable::Mailer
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseInvitable
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -1,5 +1,9 @@
1
1
  require 'devise'
2
2
 
3
+ module DeviseInvitable
4
+ autoload :Inviter, 'devise_invitable/inviter'
5
+ end
6
+
3
7
  require 'devise_invitable/mailer'
4
8
  require 'devise_invitable/routes'
5
9
  require 'devise_invitable/schema'
@@ -18,8 +18,8 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
18
18
 
19
19
  def self.down
20
20
  change_table :<%= table_name %> do |t|
21
- remove_references :invited_by, :polymorphic => true
22
- remove :invitation_limit, :invitation_sent_at, :invitation_token
21
+ t.remove_references :invited_by, :polymorphic => true
22
+ t.remove :invitation_limit, :invitation_sent_at, :invitation_token
23
23
  end
24
24
  end
25
25
  end
@@ -34,6 +34,10 @@ module DeviseInvitable
34
34
  # The key to be used to check existing users when sending an invitation
35
35
  # config.invite_key = :email
36
36
 
37
+ # Flag that force a record to be valid before being actually invited
38
+ # Default: false
39
+ # config.validate_on_invite = true
40
+
37
41
  CONTENT
38
42
  end
39
43
  end
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: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
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-03-31 00:00:00 +02:00
18
+ date: 2011-05-09 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,12 +65,12 @@ dependencies:
58
65
  requirements:
59
66
  - - ~>
60
67
  - !ruby/object:Gem::Version
61
- hash: 31
68
+ hash: 25
62
69
  segments:
63
70
  - 1
64
- - 2
65
- - 0
66
- version: 1.2.0
71
+ - 3
72
+ - 1
73
+ version: 1.3.1
67
74
  type: :runtime
68
75
  version_requirements: *id003
69
76
  description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
@@ -90,6 +97,7 @@ files:
90
97
  - lib/devise_invitable/controllers/helpers.rb
91
98
  - lib/devise_invitable/controllers/url_helpers.rb
92
99
  - lib/devise_invitable/version.rb
100
+ - lib/devise_invitable/inviter.rb
93
101
  - lib/generators/active_record/devise_invitable_generator.rb
94
102
  - lib/generators/active_record/templates/migration.rb
95
103
  - lib/generators/devise_invitable/views_generator.rb