devise_invitable 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/app/controllers/devise/invitations_controller.rb~ +48 -0
  2. data/config/locales/en.yml +1 -1
  3. data/lib/devise_invitable/model.rb +5 -0
  4. data/lib/devise_invitable/model.rb~ +129 -0
  5. data/lib/devise_invitable/version.rb +3 -0
  6. metadata +42 -104
  7. data/.document +0 -5
  8. data/.gitignore +0 -22
  9. data/Gemfile +0 -3
  10. data/Gemfile.lock +0 -117
  11. data/Rakefile +0 -55
  12. data/VERSION +0 -1
  13. data/devise_invitable.gemspec +0 -146
  14. data/test/generators_test.rb +0 -45
  15. data/test/integration/invitable_test.rb +0 -109
  16. data/test/integration_tests_helper.rb +0 -58
  17. data/test/mailers/invitation_test.rb +0 -62
  18. data/test/model_tests_helper.rb +0 -41
  19. data/test/models/invitable_test.rb +0 -188
  20. data/test/models_test.rb +0 -31
  21. data/test/rails_app/app/controllers/admins_controller.rb +0 -6
  22. data/test/rails_app/app/controllers/application_controller.rb +0 -3
  23. data/test/rails_app/app/controllers/home_controller.rb +0 -4
  24. data/test/rails_app/app/controllers/users_controller.rb +0 -12
  25. data/test/rails_app/app/helpers/application_helper.rb +0 -2
  26. data/test/rails_app/app/models/octopussy.rb +0 -11
  27. data/test/rails_app/app/models/user.rb +0 -4
  28. data/test/rails_app/app/views/home/index.html.erb +0 -0
  29. data/test/rails_app/app/views/layouts/application.html.erb +0 -15
  30. data/test/rails_app/app/views/users/invitations/new.html.erb +0 -15
  31. data/test/rails_app/config.ru +0 -4
  32. data/test/rails_app/config/application.rb +0 -46
  33. data/test/rails_app/config/boot.rb +0 -13
  34. data/test/rails_app/config/database.yml +0 -22
  35. data/test/rails_app/config/environment.rb +0 -5
  36. data/test/rails_app/config/environments/development.rb +0 -26
  37. data/test/rails_app/config/environments/production.rb +0 -49
  38. data/test/rails_app/config/environments/test.rb +0 -35
  39. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  40. data/test/rails_app/config/initializers/devise.rb +0 -144
  41. data/test/rails_app/config/initializers/inflections.rb +0 -10
  42. data/test/rails_app/config/initializers/mime_types.rb +0 -5
  43. data/test/rails_app/config/initializers/secret_token.rb +0 -7
  44. data/test/rails_app/config/initializers/session_store.rb +0 -8
  45. data/test/rails_app/config/locales/en.yml +0 -5
  46. data/test/rails_app/config/routes.rb +0 -4
  47. data/test/rails_app/script/rails +0 -6
  48. data/test/routes_test.rb +0 -20
  49. data/test/test_helper.rb +0 -30
@@ -0,0 +1,48 @@
1
+ class Devise::InvitationsController < ApplicationController
2
+ include Devise::Controllers::InternalHelpers
3
+
4
+ before_filter :authenticate_inviter!, :only => [:new, :create]
5
+ before_filter :require_no_authentication, :only => [:edit, :update]
6
+ helper_method :after_sign_in_path_for
7
+
8
+ # GET /resource/invitation/new
9
+ def new
10
+ build_resource
11
+ render_with_scope :new
12
+ end
13
+
14
+ # POST /resource/invitation
15
+ def create
16
+ self.resource = resource_class.invite!(params[resource_name])
17
+
18
+ if resource.errors.empty?
19
+ puts params.inspect
20
+ set_flash_message :notice, :send_instructions, :email => self.resource.email
21
+ redirect_to after_sign_in_path_for(resource_name)
22
+ else
23
+ render_with_scope :new
24
+ end
25
+ end
26
+
27
+ # GET /resource/invitation/accept?invitation_token=abcdef
28
+ def edit
29
+ if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :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
35
+ end
36
+
37
+ # PUT /resource/invitation
38
+ def update
39
+ self.resource = resource_class.accept_invitation!(params[resource_name])
40
+
41
+ if resource.errors.empty?
42
+ set_flash_message :notice, :updated
43
+ sign_in_and_redirect(resource_name, resource)
44
+ else
45
+ render_with_scope :edit
46
+ end
47
+ end
48
+ end
@@ -4,5 +4,5 @@ en:
4
4
  send_instructions: 'An email with instructions about how to set the password has been sent.'
5
5
  updated: 'Your password was set successfully. You are now signed in.'
6
6
  mailer:
7
- invitiation:
7
+ invitation:
8
8
  subject: 'Invitation'
@@ -61,6 +61,11 @@ module Devise
61
61
  invited? && invitation_period_valid?
62
62
  end
63
63
 
64
+ # Only verify password when is not invited
65
+ def valid_password?(password)
66
+ super unless invited?
67
+ end
68
+
64
69
  protected
65
70
 
66
71
  # Checks if the invitation for the user is within the limit time.
@@ -0,0 +1,129 @@
1
+ module Devise
2
+ module Models
3
+ # Invitable is responsible to send emails with invitations.
4
+ # When an invitation is sent to an email, an account is created for it.
5
+ # An invitation has a link to set the password, as reset password from recoverable.
6
+ #
7
+ # Configuration:
8
+ #
9
+ # invite_for: the time you want the user will have to confirm the account after
10
+ # is invited. When invite_for is zero, the invitation won't expire.
11
+ # By default invite_for is 0.
12
+ #
13
+ # Examples:
14
+ #
15
+ # User.find(1).invited? # true/false
16
+ # User.invite!(:email => 'someone@example.com') # send invitation
17
+ # User.accept_invitation!(:invitation_token => '...') # accept invitation with a token
18
+ # User.find(1).accept_invitation! # accept invitation
19
+ # User.find(1).invite! # reset invitation status and send invitation again
20
+ module Invitable
21
+ extend ActiveSupport::Concern
22
+
23
+ # Accept an invitation by clearing invitation token and confirming it if model
24
+ # is confirmable
25
+ def accept_invitation!
26
+ if self.invited?
27
+ self.invitation_token = nil
28
+ self.save
29
+ end
30
+ end
31
+
32
+ # Verifies whether a user has been invited or not
33
+ def invited?
34
+ persisted? && invitation_token.present?
35
+ end
36
+
37
+ # Send invitation by email
38
+ def send_invitation
39
+ ::Devise.mailer.invitation(self).deliver
40
+ end
41
+
42
+ # Reset invitation token and send invitation again
43
+ def invite!
44
+ if new_record? || invited?
45
+ self.skip_confirmation! if self.new_record? and self.respond_to? :skip_confirmation!
46
+ generate_invitation_token
47
+ save(:validate=>false)
48
+ send_invitation
49
+ end
50
+ end
51
+
52
+ # Verify whether a invitation is active or not. If the user has been
53
+ # invited, we need to calculate if the invitation time has not expired
54
+ # for this user, in other words, if the invitation is still valid.
55
+ def valid_invitation?
56
+ invited? && invitation_period_valid?
57
+ end
58
+
59
+ protected
60
+
61
+ # Checks if the invitation for the user is within the limit time.
62
+ # We do this by calculating if the difference between today and the
63
+ # invitation sent date does not exceed the invite for time configured.
64
+ # Invite_for is a model configuration, must always be an integer value.
65
+ #
66
+ # Example:
67
+ #
68
+ # # invite_for = 1.day and invitation_sent_at = today
69
+ # invitation_period_valid? # returns true
70
+ #
71
+ # # invite_for = 5.days and invitation_sent_at = 4.days.ago
72
+ # invitation_period_valid? # returns true
73
+ #
74
+ # # invite_for = 5.days and invitation_sent_at = 5.days.ago
75
+ # invitation_period_valid? # returns false
76
+ #
77
+ # # invite_for = nil
78
+ # invitation_period_valid? # will always return true
79
+ #
80
+ def invitation_period_valid?
81
+ invitation_sent_at && (self.class.invite_for.to_i.zero? || invitation_sent_at.utc >= self.class.invite_for.ago)
82
+ end
83
+
84
+ # Generates a new random token for invitation, and stores the time
85
+ # this token is being generated
86
+ def generate_invitation_token
87
+ self.invitation_token = Devise.friendly_token
88
+ self.invitation_sent_at = Time.now.utc
89
+ end
90
+
91
+ module ClassMethods
92
+ # Attempt to find a user by it's email. If a record is not found, create a new
93
+ # user and send invitation to it. If user is found, returns the user with an
94
+ # email already exists error.
95
+ # Attributes must contain the user email, other attributes will be set in the record
96
+ def invite!(attributes={})
97
+ invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
98
+ invitable.attributes = attributes
99
+
100
+ if invitable.new_record?
101
+ invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
102
+ else
103
+ invitable.errors.add(:email, :taken) unless invitable.invited?
104
+ end
105
+
106
+ invitable.invite! if invitable.errors.empty?
107
+ invitable
108
+ end
109
+
110
+ # Attempt to find a user by it's invitation_token to set it's password.
111
+ # If a user is found, reset it's password and automatically try saving
112
+ # the record. If not user is found, returns a new user containing an
113
+ # error in invitation_token attribute.
114
+ # Attributes must contain invitation_token, password and confirmation
115
+ def accept_invitation!(attributes={})
116
+ invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
117
+ invitable.errors.add(:invitation_token, :invalid) if attributes[:invitation_token] && !invitable.new_record? && !invitable.valid_invitation?
118
+ if invitable.errors.empty?
119
+ invitable.attributes = attributes
120
+ invitable.accept_invitation!
121
+ end
122
+ invitable
123
+ end
124
+
125
+ Devise::Models.config(self, :invite_for)
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseInvitable
2
+ VERSION = '0.3.6'
3
+ 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: 25
5
- prerelease: false
4
+ hash: 31
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 5
10
- version: 0.3.5
9
+ - 6
10
+ version: 0.3.6
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: 2010-11-02 00:00:00 +01:00
18
+ date: 2011-02-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -96,85 +96,47 @@ dependencies:
96
96
  version: 1.1.0
97
97
  type: :runtime
98
98
  version_requirements: *id005
99
- description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation setting the password
100
- email: sergio@entrecables.com
99
+ description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
100
+ email:
101
+ - sergio@entrecables.com
101
102
  executables: []
102
103
 
103
104
  extensions: []
104
105
 
105
- extra_rdoc_files:
106
- - LICENSE
107
- - README.rdoc
106
+ extra_rdoc_files: []
107
+
108
108
  files:
109
- - .document
110
- - .gitignore
111
- - Gemfile
112
- - Gemfile.lock
113
- - LICENSE
114
- - README.rdoc
115
- - Rakefile
116
- - VERSION
117
- - app/controllers/devise/invitations_controller.rb
118
109
  - app/views/devise/invitations/edit.html.erb
119
110
  - app/views/devise/invitations/new.html.erb
120
111
  - app/views/devise/mailer/invitation.html.erb
112
+ - app/controllers/devise/invitations_controller.rb~
113
+ - app/controllers/devise/invitations_controller.rb
121
114
  - config/locales/en.yml
122
- - devise_invitable.gemspec
123
- - lib/devise_invitable.rb
124
- - lib/devise_invitable/controllers/helpers.rb
125
- - lib/devise_invitable/controllers/url_helpers.rb
126
- - lib/devise_invitable/mailer.rb
127
- - lib/devise_invitable/model.rb
128
115
  - lib/devise_invitable/rails.rb
129
- - lib/devise_invitable/routes.rb
130
116
  - lib/devise_invitable/schema.rb
131
- - lib/generators/active_record/devise_invitable_generator.rb
132
- - lib/generators/active_record/templates/migration.rb
117
+ - lib/devise_invitable/mailer.rb
118
+ - lib/devise_invitable/routes.rb
119
+ - lib/devise_invitable/version.rb
120
+ - lib/devise_invitable/model.rb
121
+ - lib/devise_invitable/model.rb~
122
+ - lib/devise_invitable/controllers/helpers.rb
123
+ - lib/devise_invitable/controllers/url_helpers.rb
124
+ - lib/devise_invitable.rb
125
+ - lib/generators/devise_invitable/views_generator.rb
133
126
  - lib/generators/devise_invitable/devise_invitable_generator.rb
134
127
  - lib/generators/devise_invitable/install_generator.rb
135
- - lib/generators/devise_invitable/views_generator.rb
136
- - test/generators_test.rb
137
- - test/integration/invitable_test.rb
138
- - test/integration_tests_helper.rb
139
- - test/mailers/invitation_test.rb
140
- - test/model_tests_helper.rb
141
- - test/models/invitable_test.rb
142
- - test/models_test.rb
143
- - test/rails_app/app/controllers/admins_controller.rb
144
- - test/rails_app/app/controllers/application_controller.rb
145
- - test/rails_app/app/controllers/home_controller.rb
146
- - test/rails_app/app/controllers/users_controller.rb
147
- - test/rails_app/app/helpers/application_helper.rb
148
- - test/rails_app/app/models/octopussy.rb
149
- - test/rails_app/app/models/user.rb
150
- - test/rails_app/app/views/home/index.html.erb
151
- - test/rails_app/app/views/layouts/application.html.erb
152
- - test/rails_app/app/views/users/invitations/new.html.erb
153
- - test/rails_app/config.ru
154
- - test/rails_app/config/application.rb
155
- - test/rails_app/config/boot.rb
156
- - test/rails_app/config/database.yml
157
- - test/rails_app/config/environment.rb
158
- - test/rails_app/config/environments/development.rb
159
- - test/rails_app/config/environments/production.rb
160
- - test/rails_app/config/environments/test.rb
161
- - test/rails_app/config/initializers/backtrace_silencers.rb
162
- - test/rails_app/config/initializers/devise.rb
163
- - test/rails_app/config/initializers/inflections.rb
164
- - test/rails_app/config/initializers/mime_types.rb
165
- - test/rails_app/config/initializers/secret_token.rb
166
- - test/rails_app/config/initializers/session_store.rb
167
- - test/rails_app/config/locales/en.yml
168
- - test/rails_app/config/routes.rb
169
- - test/rails_app/script/rails
170
- - test/routes_test.rb
171
- - test/test_helper.rb
128
+ - lib/generators/active_record/devise_invitable_generator.rb
129
+ - lib/generators/active_record/templates/migration.rb
130
+ - LICENSE
131
+ - README.rdoc
172
132
  has_rdoc: true
173
- homepage: http://github.com/scambra/devise_invitable
133
+ homepage: https://github.com/scambra/devise_invitable
174
134
  licenses: []
175
135
 
176
136
  post_install_message:
177
137
  rdoc_options:
138
+ - --main
139
+ - README.rdoc
178
140
  - --charset=UTF-8
179
141
  require_paths:
180
142
  - lib
@@ -183,53 +145,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
145
  requirements:
184
146
  - - ">="
185
147
  - !ruby/object:Gem::Version
186
- hash: 3
148
+ hash: 59
187
149
  segments:
188
- - 0
189
- version: "0"
150
+ - 1
151
+ - 8
152
+ - 6
153
+ version: 1.8.6
190
154
  required_rubygems_version: !ruby/object:Gem::Requirement
191
155
  none: false
192
156
  requirements:
193
157
  - - ">="
194
158
  - !ruby/object:Gem::Version
195
- hash: 3
159
+ hash: 23
196
160
  segments:
197
- - 0
198
- version: "0"
161
+ - 1
162
+ - 3
163
+ - 6
164
+ version: 1.3.6
199
165
  requirements: []
200
166
 
201
167
  rubyforge_project:
202
- rubygems_version: 1.3.7
168
+ rubygems_version: 1.5.2
203
169
  signing_key:
204
170
  specification_version: 3
205
- summary: An invitation strategy for devise
206
- test_files:
207
- - test/integration/invitable_test.rb
208
- - test/integration_tests_helper.rb
209
- - test/mailers/invitation_test.rb
210
- - test/model_tests_helper.rb
211
- - test/models/invitable_test.rb
212
- - test/models_test.rb
213
- - test/rails_app/app/controllers/admins_controller.rb
214
- - test/rails_app/app/controllers/application_controller.rb
215
- - test/rails_app/app/controllers/home_controller.rb
216
- - test/rails_app/app/controllers/users_controller.rb
217
- - test/rails_app/app/helpers/application_helper.rb
218
- - test/rails_app/app/models/user.rb
219
- - test/rails_app/app/models/octopussy.rb
220
- - test/rails_app/config/boot.rb
221
- - test/rails_app/config/environment.rb
222
- - test/rails_app/config/environments/development.rb
223
- - test/rails_app/config/environments/production.rb
224
- - test/rails_app/config/environments/test.rb
225
- - test/rails_app/config/initializers/backtrace_silencers.rb
226
- - test/rails_app/config/initializers/inflections.rb
227
- - test/rails_app/config/initializers/devise.rb
228
- - test/rails_app/config/initializers/session_store.rb
229
- - test/rails_app/config/initializers/mime_types.rb
230
- - test/rails_app/config/initializers/secret_token.rb
231
- - test/rails_app/config/routes.rb
232
- - test/rails_app/config/application.rb
233
- - test/routes_test.rb
234
- - test/test_helper.rb
235
- - test/generators_test.rb
171
+ summary: An invitation strategy for Devise
172
+ test_files: []
173
+
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,22 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
22
- test/rails_app/log
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source :rubygems
2
-
3
- gemspec
@@ -1,117 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- devise_invitable (0.3.2)
5
- devise (~> 1.1.0)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- abstract (1.0.0)
11
- actionmailer (3.0.1)
12
- actionpack (= 3.0.1)
13
- mail (~> 2.2.5)
14
- actionpack (3.0.1)
15
- activemodel (= 3.0.1)
16
- activesupport (= 3.0.1)
17
- builder (~> 2.1.2)
18
- erubis (~> 2.6.6)
19
- i18n (~> 0.4.1)
20
- rack (~> 1.2.1)
21
- rack-mount (~> 0.6.12)
22
- rack-test (~> 0.5.4)
23
- tzinfo (~> 0.3.23)
24
- activemodel (3.0.1)
25
- activesupport (= 3.0.1)
26
- builder (~> 2.1.2)
27
- i18n (~> 0.4.1)
28
- activerecord (3.0.1)
29
- activemodel (= 3.0.1)
30
- activesupport (= 3.0.1)
31
- arel (~> 1.0.0)
32
- tzinfo (~> 0.3.23)
33
- activeresource (3.0.1)
34
- activemodel (= 3.0.1)
35
- activesupport (= 3.0.1)
36
- activesupport (3.0.1)
37
- arel (1.0.1)
38
- activesupport (~> 3.0.0)
39
- bcrypt-ruby (2.1.2)
40
- builder (2.1.2)
41
- capybara (0.4.0)
42
- celerity (>= 0.7.9)
43
- culerity (>= 0.2.4)
44
- mime-types (>= 1.16)
45
- nokogiri (>= 1.3.3)
46
- rack (>= 1.0.0)
47
- rack-test (>= 0.5.4)
48
- selenium-webdriver (>= 0.0.27)
49
- xpath (~> 0.1.2)
50
- celerity (0.8.2)
51
- childprocess (0.1.3)
52
- ffi (~> 0.6.3)
53
- culerity (0.2.12)
54
- devise (1.1.3)
55
- bcrypt-ruby (~> 2.1.2)
56
- warden (~> 0.10.7)
57
- erubis (2.6.6)
58
- abstract (>= 1.0.0)
59
- ffi (0.6.3)
60
- rake (>= 0.8.7)
61
- i18n (0.4.2)
62
- json_pure (1.4.6)
63
- mail (2.2.9)
64
- activesupport (>= 2.3.6)
65
- i18n (~> 0.4.1)
66
- mime-types (~> 1.16)
67
- treetop (~> 1.4.8)
68
- mime-types (1.16)
69
- mocha (0.9.9)
70
- rake
71
- nokogiri (1.4.3.1)
72
- polyglot (0.3.1)
73
- rack (1.2.1)
74
- rack-mount (0.6.13)
75
- rack (>= 1.0.0)
76
- rack-test (0.5.6)
77
- rack (>= 1.0)
78
- rails (3.0.1)
79
- actionmailer (= 3.0.1)
80
- actionpack (= 3.0.1)
81
- activerecord (= 3.0.1)
82
- activeresource (= 3.0.1)
83
- activesupport (= 3.0.1)
84
- bundler (~> 1.0.0)
85
- railties (= 3.0.1)
86
- railties (3.0.1)
87
- actionpack (= 3.0.1)
88
- activesupport (= 3.0.1)
89
- rake (>= 0.8.4)
90
- thor (~> 0.14.0)
91
- rake (0.8.7)
92
- rubyzip (0.9.4)
93
- selenium-webdriver (0.0.29)
94
- childprocess (>= 0.0.7)
95
- ffi (~> 0.6.3)
96
- json_pure
97
- rubyzip
98
- sqlite3-ruby (1.3.2)
99
- thor (0.14.3)
100
- treetop (1.4.8)
101
- polyglot (>= 0.3.1)
102
- tzinfo (0.3.23)
103
- warden (0.10.7)
104
- rack (>= 1.0.0)
105
- xpath (0.1.2)
106
- nokogiri (~> 1.3)
107
-
108
- PLATFORMS
109
- ruby
110
-
111
- DEPENDENCIES
112
- capybara (>= 0.3.9)
113
- devise (~> 1.1.0)
114
- devise_invitable!
115
- mocha (>= 0.9.8)
116
- rails (~> 3.0.0)
117
- sqlite3-ruby