devise_invitable 0.5.4 → 0.6.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
@@ -51,6 +51,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
51
51
  change_table :users do |t|
52
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
 
@@ -148,6 +149,18 @@ To accept an invitation with a token use the <tt>accept_invitation!</tt> class m
148
149
 
149
150
  User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
150
151
 
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.
163
+
151
164
  == Integration in a Rails application
152
165
 
153
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.
@@ -172,7 +185,7 @@ You would have a User model which is configured as invitable and an Admin model
172
185
  class ApplicationController < ActionController::Base
173
186
  protected
174
187
  def authenticate_inviter!
175
- authenticate_admin!(true)
188
+ authenticate_admin!(:force => true)
176
189
  end
177
190
  end
178
191
 
@@ -183,6 +196,18 @@ And include DeviseInvitable::Inviter module into Admin model:
183
196
  include DeviseInvitable::Inviter
184
197
  end
185
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
+
186
211
  == I18n
187
212
 
188
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:
@@ -26,7 +26,7 @@ class Devise::InvitationsController < ApplicationController
26
26
 
27
27
  # GET /resource/invitation/accept?invitation_token=abcdef
28
28
  def edit
29
- if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
29
+ if params[:invitation_token] && self.resource = resource_class.to_adapter.find_first( :invitation_token => params[:invitation_token] )
30
30
  render_with_scope :edit
31
31
  else
32
32
  set_flash_message(:alert, :invitation_token_invalid)
@@ -55,7 +55,7 @@ class Devise::InvitationsController < ApplicationController
55
55
  def has_invitations_left?
56
56
  unless current_inviter.nil? || current_inviter.has_invitations_left?
57
57
  build_resource
58
- set_flash_message :alert, :no_invitations_remaining
58
+ set_flash_message :alert, :no_invitations_remaining
59
59
  respond_with_navigational(resource) { render_with_scope :new }
60
60
  end
61
61
  end
@@ -68,3 +68,4 @@ class Devise::InvitationsController < ApplicationController
68
68
  after_sign_in_path_for(resource)
69
69
  end
70
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 />
@@ -1,7 +1,7 @@
1
1
  module DeviseInvitable::Controllers::Helpers
2
2
  protected
3
3
  def authenticate_inviter!
4
- send(:"authenticate_#{resource_name}!", true)
4
+ send(:"authenticate_#{resource_name}!", :force => true)
5
5
  end
6
6
  end
7
7
  ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
@@ -5,10 +5,5 @@ module DeviseInvitable
5
5
  def invitation_instructions(record)
6
6
  devise_mail(record, :invitation_instructions)
7
7
  end
8
-
9
- def invitation(record)
10
- ActiveSupport::Deprecation.warn('invitation has been renamed to invitation_instructions')
11
- invitation_instructions(record)
12
- end
13
8
  end
14
9
  end
@@ -24,16 +24,22 @@ module Devise
24
24
  attr_accessor :skip_invitation
25
25
 
26
26
  included do
27
- include ::DeviseInvitable::Inviter
27
+ include ::DeviseInvitable::Inviter
28
28
  belongs_to :invited_by, :polymorphic => true
29
+
30
+ include ActiveSupport::Callbacks
31
+ define_callbacks :invitation_accepted
29
32
  end
30
33
 
31
34
  # Accept an invitation by clearing invitation token and confirming it if model
32
35
  # is confirmable
33
36
  def accept_invitation!
34
37
  if self.invited? && self.valid?
35
- self.invitation_token = nil
36
- self.save
38
+ run_callbacks :invitation_accepted do
39
+ self.invitation_token = nil
40
+ self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
41
+ self.save(:validate => false)
42
+ end
37
43
  end
38
44
  end
39
45
 
@@ -44,15 +50,14 @@ module Devise
44
50
 
45
51
  # Reset invitation token and send invitation again
46
52
  def invite!
47
- if new_record? || invited?
48
- @skip_password = true
49
- self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
50
- generate_invitation_token if self.invitation_token.nil?
51
- self.invitation_sent_at = Time.now.utc
52
- if save(:validate => self.class.validate_on_invite)
53
- self.invited_by.decrement_invitation_limit! if self.invited_by
54
- !!deliver_invitation unless @skip_invitation
55
- end
53
+ @skip_password = true
54
+ was_invited = invited?
55
+ self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
56
+ generate_invitation_token if self.invitation_token.nil?
57
+ self.invitation_sent_at = Time.now.utc
58
+ if save(:validate => false)
59
+ self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
60
+ deliver_invitation unless @skip_invitation
56
61
  end
57
62
  end
58
63
 
@@ -121,24 +126,35 @@ module Devise
121
126
  # If user is found and still have pending invitation, email is resend unless
122
127
  # resend_invitation is set to false
123
128
  # Attributes must contain the user email, other attributes will be set in the record
124
- def invite!(attributes={}, invited_by=nil, &block)
129
+ def _invite(attributes={}, invited_by=nil, &block)
125
130
  invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
126
131
  invitable.attributes = attributes
127
132
  invitable.invited_by = invited_by
128
133
 
134
+ invitable.valid? if self.validate_on_invite
129
135
  if invitable.new_record?
130
- invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
136
+ invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
131
137
  else
132
138
  invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
133
139
  end
134
140
 
135
141
  if invitable.errors.empty?
136
142
  yield invitable if block_given?
137
- invitable.invite!
143
+ mail = invitable.invite!
138
144
  end
145
+ [invitable, mail]
146
+ end
147
+
148
+ def invite!(attributes={}, invited_by=nil, &block)
149
+ invitable, mail = _invite(attributes, invited_by, &block)
139
150
  invitable
140
151
  end
141
152
 
153
+ def invite_mail!(attributes={}, invited_by=nil, &block)
154
+ invitable, mail = _invite(attributes, invited_by, &block)
155
+ mail
156
+ end
157
+
142
158
  # Attempt to find a user by it's invitation_token to set it's password.
143
159
  # If a user is found, reset it's password and automatically try saving
144
160
  # the record. If not user is found, returns a new user containing an
@@ -158,6 +174,16 @@ module Devise
158
174
  def invitation_token
159
175
  generate_token(:invitation_token)
160
176
  end
177
+
178
+ # Callback convenience methods
179
+ def before_invitation_accepted(*args, &blk)
180
+ set_callback(:invitation_accepted, :before, *args, &blk)
181
+ end
182
+
183
+ def after_invitation_accepted(*args, &blk)
184
+ set_callback(:invitation_accepted, :after, *args, &blk)
185
+ end
186
+
161
187
 
162
188
  Devise::Models.config(self, :invite_for)
163
189
  Devise::Models.config(self, :validate_on_invite)
@@ -168,3 +194,4 @@ module Devise
168
194
  end
169
195
  end
170
196
  end
197
+
@@ -16,6 +16,7 @@ module DeviseInvitable
16
16
  # change_table :the_resources do |t|
17
17
  # t.string :invitation_token, :limit => 60
18
18
  # t.datetime :invitation_sent_at
19
+ # t.datetime :invitation_accepted_at
19
20
  # t.index :invitation_token # for invitable
20
21
  # end
21
22
  #
@@ -26,6 +27,7 @@ module DeviseInvitable
26
27
  def invitable
27
28
  apply_devise_schema :invitation_token, String, :limit => 60
28
29
  apply_devise_schema :invitation_sent_at, DateTime
30
+ apply_devise_schema :invitation_accepted_at, DateTime
29
31
  apply_devise_schema :invitation_limit, Integer
30
32
  apply_devise_schema :invited_by_id, Integer
31
33
  apply_devise_schema :invited_by_type, String
@@ -1,3 +1,3 @@
1
1
  module DeviseInvitable
2
- VERSION = '0.5.4'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -3,6 +3,7 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
3
3
  change_table :<%= table_name %> do |t|
4
4
  t.string :invitation_token, :limit => 60
5
5
  t.datetime :invitation_sent_at
6
+ t.datetime :invitation_accepted_at
6
7
  t.integer :invitation_limit
7
8
  t.references :invited_by, :polymorphic => true
8
9
  t.index :invitation_token # for invitable
@@ -19,7 +20,7 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
19
20
  def self.down
20
21
  change_table :<%= table_name %> do |t|
21
22
  t.remove_references :invited_by, :polymorphic => true
22
- t.remove :invitation_limit, :invitation_sent_at, :invitation_token
23
+ t.remove :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token
23
24
  end
24
25
  end
25
26
  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: 3
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 4
10
- version: 0.5.4
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergio Cambra
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-27 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-11-15 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: bundler
@@ -63,14 +62,21 @@ dependencies:
63
62
  requirement: &id003 !ruby/object:Gem::Requirement
64
63
  none: false
65
64
  requirements:
66
- - - ~>
65
+ - - ">="
67
66
  - !ruby/object:Gem::Version
68
- hash: 5
67
+ hash: 11
69
68
  segments:
70
69
  - 1
71
70
  - 4
71
+ - 6
72
+ version: 1.4.6
73
+ - - <
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
72
77
  - 1
73
- version: 1.4.1
78
+ - 6
79
+ version: "1.6"
74
80
  type: :runtime
75
81
  version_requirements: *id003
76
82
  description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
@@ -106,7 +112,6 @@ files:
106
112
  - lib/generators/mongoid/devise_invitable_generator.rb
107
113
  - LICENSE
108
114
  - README.rdoc
109
- has_rdoc: true
110
115
  homepage: https://github.com/scambra/devise_invitable
111
116
  licenses: []
112
117
 
@@ -142,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
147
  requirements: []
143
148
 
144
149
  rubyforge_project:
145
- rubygems_version: 1.5.2
150
+ rubygems_version: 1.8.10
146
151
  signing_key:
147
152
  specification_version: 3
148
153
  summary: An invitation strategy for Devise