aihs_devise_invitable 0.4.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +27 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +117 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +187 -0
  6. data/Rakefile +57 -0
  7. data/app/controllers/devise/invitations_controller.rb +47 -0
  8. data/app/views/devise/invitations/edit.html.erb +14 -0
  9. data/app/views/devise/invitations/new.html.erb +12 -0
  10. data/app/views/devise/mailer/invitation.html.erb +8 -0
  11. data/app/views/devise/mailer/invitation_instructions.html.erb +8 -0
  12. data/config/locales/en.yml +9 -0
  13. data/devise_invitable.gemspec +143 -0
  14. data/lib/devise_invitable.rb +16 -0
  15. data/lib/devise_invitable/controllers/helpers.rb +7 -0
  16. data/lib/devise_invitable/controllers/url_helpers.rb +24 -0
  17. data/lib/devise_invitable/mailer.rb +14 -0
  18. data/lib/devise_invitable/model.rb +130 -0
  19. data/lib/devise_invitable/rails.rb +13 -0
  20. data/lib/devise_invitable/routes.rb +13 -0
  21. data/lib/devise_invitable/schema.rb +33 -0
  22. data/lib/devise_invitable/version.rb +3 -0
  23. data/lib/generators/active_record/devise_invitable_generator.rb +13 -0
  24. data/lib/generators/active_record/templates/migration.rb +20 -0
  25. data/lib/generators/devise_invitable/devise_invitable_generator.rb +16 -0
  26. data/lib/generators/devise_invitable/install_generator.rb +35 -0
  27. data/lib/generators/devise_invitable/views_generator.rb +10 -0
  28. data/test/generators_test.rb +45 -0
  29. data/test/integration/invitation_test.rb +107 -0
  30. data/test/integration_tests_helper.rb +58 -0
  31. data/test/mailers/invitation_mail_test.rb +63 -0
  32. data/test/model_tests_helper.rb +41 -0
  33. data/test/models/invitable_test.rb +213 -0
  34. data/test/models_test.rb +32 -0
  35. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  36. data/test/rails_app/app/controllers/application_controller.rb +3 -0
  37. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  38. data/test/rails_app/app/controllers/users_controller.rb +12 -0
  39. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  40. data/test/rails_app/app/models/octopussy.rb +11 -0
  41. data/test/rails_app/app/models/user.rb +4 -0
  42. data/test/rails_app/app/views/home/index.html.erb +0 -0
  43. data/test/rails_app/app/views/layouts/application.html.erb +16 -0
  44. data/test/rails_app/app/views/users/invitations/new.html.erb +15 -0
  45. data/test/rails_app/config.ru +4 -0
  46. data/test/rails_app/config/application.rb +46 -0
  47. data/test/rails_app/config/boot.rb +14 -0
  48. data/test/rails_app/config/database.yml +22 -0
  49. data/test/rails_app/config/environment.rb +5 -0
  50. data/test/rails_app/config/environments/development.rb +26 -0
  51. data/test/rails_app/config/environments/production.rb +49 -0
  52. data/test/rails_app/config/environments/test.rb +35 -0
  53. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/rails_app/config/initializers/devise.rb +174 -0
  55. data/test/rails_app/config/initializers/inflections.rb +10 -0
  56. data/test/rails_app/config/initializers/mime_types.rb +5 -0
  57. data/test/rails_app/config/initializers/secret_token.rb +7 -0
  58. data/test/rails_app/config/initializers/session_store.rb +8 -0
  59. data/test/rails_app/config/locales/en.yml +5 -0
  60. data/test/rails_app/config/routes.rb +4 -0
  61. data/test/rails_app/script/rails +6 -0
  62. data/test/routes_test.rb +20 -0
  63. data/test/test_helper.rb +31 -0
  64. metadata +222 -0
data/.gitignore ADDED
@@ -0,0 +1,27 @@
1
+ .bundle
2
+ .document
3
+
4
+ ## MAC OS
5
+ .DS_Store
6
+
7
+ ## TEXTMATE
8
+ *.tmproj
9
+ tmtags
10
+
11
+ ## EMACS
12
+ *~
13
+ \#*
14
+ .\#*
15
+
16
+ ## VIM
17
+ *.swp
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+
24
+ ## PROJECT::SPECIFIC
25
+ test/rails_app/log
26
+ test/rails_app/db/*.sqlite3
27
+ webrat.log
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ #gemspec
4
+ gem 'mocha', '>= 0.9.8'
5
+ gem 'capybara', '>= 0.3.9'
6
+ gem 'rails', '~> 3.0.0'
7
+ gem 'sqlite3-ruby'
8
+ gem 'devise', :path => '../devise'
data/Gemfile.lock ADDED
@@ -0,0 +1,117 @@
1
+ GIT
2
+ remote: git://github.com/plataformatec/devise.git
3
+ revision: 976d77defed17adaca81381222b5b80cdd34f8ac
4
+ specs:
5
+ devise (1.2.rc)
6
+ bcrypt-ruby (~> 2.1.2)
7
+ orm_adapter (~> 0.0.2)
8
+ warden (~> 1.0.0)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ abstract (1.0.0)
14
+ actionmailer (3.0.1)
15
+ actionpack (= 3.0.1)
16
+ mail (~> 2.2.5)
17
+ actionpack (3.0.1)
18
+ activemodel (= 3.0.1)
19
+ activesupport (= 3.0.1)
20
+ builder (~> 2.1.2)
21
+ erubis (~> 2.6.6)
22
+ i18n (~> 0.4.1)
23
+ rack (~> 1.2.1)
24
+ rack-mount (~> 0.6.12)
25
+ rack-test (~> 0.5.4)
26
+ tzinfo (~> 0.3.23)
27
+ activemodel (3.0.1)
28
+ activesupport (= 3.0.1)
29
+ builder (~> 2.1.2)
30
+ i18n (~> 0.4.1)
31
+ activerecord (3.0.1)
32
+ activemodel (= 3.0.1)
33
+ activesupport (= 3.0.1)
34
+ arel (~> 1.0.0)
35
+ tzinfo (~> 0.3.23)
36
+ activeresource (3.0.1)
37
+ activemodel (= 3.0.1)
38
+ activesupport (= 3.0.1)
39
+ activesupport (3.0.1)
40
+ arel (1.0.1)
41
+ activesupport (~> 3.0.0)
42
+ bcrypt-ruby (2.1.2)
43
+ builder (2.1.2)
44
+ capybara (0.4.0)
45
+ celerity (>= 0.7.9)
46
+ culerity (>= 0.2.4)
47
+ mime-types (>= 1.16)
48
+ nokogiri (>= 1.3.3)
49
+ rack (>= 1.0.0)
50
+ rack-test (>= 0.5.4)
51
+ selenium-webdriver (>= 0.0.27)
52
+ xpath (~> 0.1.2)
53
+ celerity (0.8.2)
54
+ childprocess (0.1.3)
55
+ ffi (~> 0.6.3)
56
+ culerity (0.2.12)
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
+ orm_adapter (0.0.3)
73
+ polyglot (0.3.1)
74
+ rack (1.2.1)
75
+ rack-mount (0.6.13)
76
+ rack (>= 1.0.0)
77
+ rack-test (0.5.6)
78
+ rack (>= 1.0)
79
+ rails (3.0.1)
80
+ actionmailer (= 3.0.1)
81
+ actionpack (= 3.0.1)
82
+ activerecord (= 3.0.1)
83
+ activeresource (= 3.0.1)
84
+ activesupport (= 3.0.1)
85
+ bundler (~> 1.0.0)
86
+ railties (= 3.0.1)
87
+ railties (3.0.1)
88
+ actionpack (= 3.0.1)
89
+ activesupport (= 3.0.1)
90
+ rake (>= 0.8.4)
91
+ thor (~> 0.14.0)
92
+ rake (0.8.7)
93
+ rubyzip (0.9.4)
94
+ selenium-webdriver (0.0.29)
95
+ childprocess (>= 0.0.7)
96
+ ffi (~> 0.6.3)
97
+ json_pure
98
+ rubyzip
99
+ sqlite3-ruby (1.3.2)
100
+ thor (0.14.3)
101
+ treetop (1.4.8)
102
+ polyglot (>= 0.3.1)
103
+ tzinfo (0.3.23)
104
+ warden (1.0.1)
105
+ rack (>= 1.0.0)
106
+ xpath (0.1.2)
107
+ nokogiri (~> 1.3)
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ capybara (>= 0.3.9)
114
+ devise!
115
+ mocha (>= 0.9.8)
116
+ rails (~> 3.0.0)
117
+ sqlite3-ruby
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sergio Cambra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,187 @@
1
+ = DeviseInvitable
2
+
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
+
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
+
7
+ == Installation for Rails ~> 3.0 and Devise ~> 1.1
8
+
9
+ Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):
10
+
11
+ gem install devise_invitable
12
+
13
+ Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):
14
+
15
+ gem 'devise', '~> 1.1.3'
16
+ gem 'devise_invitable', '~> 0.3.4'
17
+
18
+ === Automatic installation
19
+
20
+ Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (config/initializers/devise.rb):
21
+
22
+ rails generate devise_invitable:install
23
+
24
+ When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:
25
+
26
+ rails generate devise_invitable MODEL
27
+
28
+ Replace MODEL by the class name you want to add DeviseInvitable, like User, Admin, etc. This will add the :invitable flag to your model's Devise modules. The generator will also create a migration file (if your ORM support them). Continue reading this file to understand exactly what the generator produces and how to use it.
29
+
30
+ === Manual installation
31
+
32
+ Follow the walkthrough for Devise and after it's done, follow this walkthrough.
33
+
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
+
36
+ class User < ActiveRecord::Base
37
+ devise :database_authenticatable, :confirmable, :invitable
38
+ end
39
+
40
+ Add t.invitable to your Devise model migration:
41
+
42
+ create_table :users do
43
+ ...
44
+ t.invitable
45
+ ...
46
+ end
47
+ add_index :users, :invitation_token
48
+
49
+ or for a model that already exists, define a migration to add DeviseInvitable to your model:
50
+
51
+ change_table :users do |t|
52
+ t.string :invitation_token, :limit => 60
53
+ t.datetime :invitation_sent_at
54
+ t.index :invitation_token
55
+ end
56
+
57
+ # Allow null encrypted_password
58
+ change_column_null :users, :encrypted_password, true
59
+ # Allow null password_salt (add it if you are using Devise's encryptable module)
60
+ change_column_null :users, :password_salt, true
61
+
62
+ == Model configuration
63
+
64
+ DeviseInvitable adds a new configuration option:
65
+
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
+
68
+ You can set this configuration option in the Devise initializer as follow:
69
+
70
+ # ==> Configuration for :invitable
71
+ # The period the generated invitation token is valid, after
72
+ # this period, the invited resource won't be able to accept the invitation.
73
+ # When invite_for is 0 (the default), the invitation won't expire.
74
+ # config.invite_for = 2.weeks
75
+
76
+ or directly as parameters to the <tt>devise</tt> method:
77
+
78
+ devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
79
+
80
+ For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
81
+
82
+ == Configuring views
83
+
84
+ 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:
85
+
86
+ rails generate devise_invitable:views
87
+
88
+ You can also use the generator to generate scoped views:
89
+
90
+ rails generate devise_invitable:views users
91
+
92
+ Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
93
+
94
+ == Usage
95
+
96
+ === Send an invitation
97
+
98
+ 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.
99
+
100
+ User.invite!(:email => "new_user@example.com", :name => "John Doe")
101
+ # => an invitation email will be sent to new_user@example.com
102
+
103
+ === Accept an invitation
104
+
105
+ 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.
106
+
107
+ User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
108
+
109
+ == Integration in a Rails application
110
+
111
+ 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.
112
+ 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).
113
+ After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
114
+
115
+ 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).
116
+ 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.
117
+
118
+ == Controller filter
119
+
120
+ InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
121
+
122
+ 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.
123
+
124
+ 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:
125
+
126
+ class ApplicationController < ActionController::Base
127
+ protected
128
+ def authenticate_inviter!
129
+ authenticate_admin!
130
+ end
131
+ end
132
+
133
+ == I18n
134
+
135
+ 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:
136
+
137
+ en:
138
+ devise:
139
+ invitations:
140
+ send_instructions: 'An invitation email has been sent to %{email}.'
141
+ invitation_token_invalid: 'The invitation token provided is not valid!'
142
+ updated: 'Your password was set successfully. You are now signed in.'
143
+
144
+ You can also create distinct messages based on the resource you've configured using the singular name given in routes:
145
+
146
+ en:
147
+ devise:
148
+ invitations:
149
+ user:
150
+ send_instructions: 'A new user invitation has been sent to %{email}.'
151
+ invitation_token_invalid: 'Your invitation token is not valid!'
152
+ updated: 'Welcome on board! You are now signed in.'
153
+
154
+ The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
155
+
156
+ en:
157
+ devise:
158
+ mailer:
159
+ invitation_instructions:
160
+ subject: 'You got an invitation!'
161
+ user_subject: 'You got a user invitation!'
162
+
163
+ Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
164
+
165
+ == Other ORMs
166
+
167
+ DeviseInvitable supports ActiveRecord and Mongoid, like Devise.
168
+
169
+ == Contributors
170
+
171
+ Check them all at:
172
+
173
+ http://github.com/scambra/devise_invitable/contributors
174
+
175
+ Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
176
+
177
+ == Note on Patches/Pull Requests
178
+
179
+ * Fork the project.
180
+ * Make your feature addition or bug fix.
181
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
182
+ * 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)
183
+ * Send me a pull request. Bonus points for topic branches.
184
+
185
+ == Copyright
186
+
187
+ Copyright (c) 2009 Sergio Cambra. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rake'
2
+ require File.join(File.dirname(__FILE__), 'lib', 'devise_invitable', 'version')
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "aihs_devise_invitable"
8
+ gem.version = DeviseInvitable::VERSION.dup
9
+ gem.summary = %Q{An invitation strategy for devise}
10
+ gem.description = %Q{It adds support for send invitations by email (it requires to be authenticated) and accept the invitation setting the password}
11
+ gem.email = "sergio@entrecables.com"
12
+ gem.homepage = "http://github.com/scambra/devise_invitable"
13
+ gem.authors = ["Sergio Cambra"]
14
+ gem.add_development_dependency 'mocha', '>= 0.9.8'
15
+ gem.add_development_dependency 'capybara', '>= 0.3.9'
16
+ gem.add_development_dependency 'rails', '~> 3.0.0'
17
+ gem.add_development_dependency 'sqlite3-ruby'
18
+ #gem.add_dependency 'devise', '~> 1.2.0'
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "devise_invitable #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
@@ -0,0 +1,47 @@
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
+ set_flash_message :notice, :send_instructions, :email => self.resource.email
20
+ redirect_to after_sign_in_path_for(resource_name)
21
+ else
22
+ render_with_scope :new
23
+ end
24
+ end
25
+
26
+ # GET /resource/invitation/accept?invitation_token=abcdef
27
+ def edit
28
+ if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
29
+ render_with_scope :edit
30
+ else
31
+ set_flash_message(:alert, :invitation_token_invalid)
32
+ redirect_to after_sign_out_path_for(resource_name)
33
+ end
34
+ end
35
+
36
+ # PUT /resource/invitation
37
+ def update
38
+ self.resource = resource_class.accept_invitation!(params[resource_name])
39
+
40
+ if resource.errors.empty?
41
+ set_flash_message :notice, :updated
42
+ sign_in_and_redirect(resource_name, resource)
43
+ else
44
+ render_with_scope :edit
45
+ end
46
+ end
47
+ end