preseason 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/CONTRIBUTING.md +18 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +100 -0
  6. data/LICENSE.txt +23 -0
  7. data/README.md +129 -0
  8. data/Rakefile +1 -0
  9. data/bin/preseason +8 -0
  10. data/lib/Gemfile +4 -0
  11. data/lib/Rakefile +1 -0
  12. data/lib/play.rb +2 -0
  13. data/lib/preseason/colorize.rb +57 -0
  14. data/lib/preseason/config/authentication.rb +31 -0
  15. data/lib/preseason/config/database.rb +48 -0
  16. data/lib/preseason/config/factory.rb +19 -0
  17. data/lib/preseason/config/heroku.rb +17 -0
  18. data/lib/preseason/config/ie8.rb +17 -0
  19. data/lib/preseason/config.rb +6 -0
  20. data/lib/preseason/generator_context.rb +20 -0
  21. data/lib/preseason/recipe/active_admin.rb +7 -0
  22. data/lib/preseason/recipe/application.rb +6 -0
  23. data/lib/preseason/recipe/authlogic.rb +50 -0
  24. data/lib/preseason/recipe/bundle.rb +7 -0
  25. data/lib/preseason/recipe/custom_error_pages.rb +30 -0
  26. data/lib/preseason/recipe/database.rb +28 -0
  27. data/lib/preseason/recipe/devise.rb +9 -0
  28. data/lib/preseason/recipe/flash.rb +6 -0
  29. data/lib/preseason/recipe/foreman.rb +5 -0
  30. data/lib/preseason/recipe/gemfile.rb +147 -0
  31. data/lib/preseason/recipe/git.rb +62 -0
  32. data/lib/preseason/recipe/gitignore.rb +22 -0
  33. data/lib/preseason/recipe/guard.rb +7 -0
  34. data/lib/preseason/recipe/heroku.rb +12 -0
  35. data/lib/preseason/recipe/ie8.rb +18 -0
  36. data/lib/preseason/recipe/initializer.rb +20 -0
  37. data/lib/preseason/recipe/playbook.rb +69 -0
  38. data/lib/preseason/recipe/production.rb +36 -0
  39. data/lib/preseason/recipe/routes.rb +21 -0
  40. data/lib/preseason/recipe/rspec.rb +5 -0
  41. data/lib/preseason/recipe/rvm.rb +10 -0
  42. data/lib/preseason/recipe/schedule.rb +5 -0
  43. data/lib/preseason/recipe/spork_rspec.rb +7 -0
  44. data/lib/preseason/recipe/whiskey_disk.rb +5 -0
  45. data/lib/preseason/recipe.rb +38 -0
  46. data/lib/preseason/templates/authlogic/POST_INSTALL +10 -0
  47. data/lib/preseason/templates/authlogic/app/controllers/application_controller.rb +40 -0
  48. data/lib/preseason/templates/authlogic/app/controllers/user_session_controller.rb +58 -0
  49. data/lib/preseason/templates/authlogic/app/mailers/site_mailer.rb +5 -0
  50. data/lib/preseason/templates/authlogic/app/models/user.rb +11 -0
  51. data/lib/preseason/templates/authlogic/app/models/user_session.rb +2 -0
  52. data/lib/preseason/templates/authlogic/app/views/site_mailer/password_reset_instructions.html.erb +22 -0
  53. data/lib/preseason/templates/authlogic/app/views/user_session/acquire_password.html.erb +14 -0
  54. data/lib/preseason/templates/authlogic/app/views/user_session/forgot_password.html.erb +11 -0
  55. data/lib/preseason/templates/authlogic/app/views/user_session/new.html.erb +17 -0
  56. data/lib/preseason/templates/authlogic/config/routes.rb +11 -0
  57. data/lib/preseason/templates/authlogic/spec/factories/users.rb +11 -0
  58. data/lib/preseason/templates/custom_error_pages/app/views/errors/application_error.html.erb +5 -0
  59. data/lib/preseason/templates/custom_error_pages/app/views/errors/not_found.html.erb +5 -0
  60. data/lib/preseason/templates/custom_error_pages/app/views/errors/unprocessable_entity.html.erb +5 -0
  61. data/lib/preseason/templates/custom_error_pages/config/routes.rb +5 -0
  62. data/lib/preseason/templates/database/config/database.yml.dist.erb +19 -0
  63. data/lib/preseason/templates/database/config/database.yml.erb +19 -0
  64. data/lib/preseason/templates/flash/app/views/shared/_flash.html.erb +6 -0
  65. data/lib/preseason/templates/ie8/app/assets/javascripts/ie8.coffee +8 -0
  66. data/lib/preseason/templates/playbook/app/views/layouts/application.html.erb +31 -0
  67. data/lib/preseason/templates/readme/README.md.erb +17 -0
  68. data/lib/preseason/templates/schedule/config/schedule.rb +7 -0
  69. data/lib/preseason/templates/spec/spec_helper.erb +61 -0
  70. data/lib/preseason/templates/sqlite/config/database.yml +17 -0
  71. data/lib/preseason/templates/whiskey_disk/config/deploy.yml +7 -0
  72. data/lib/preseason/version.rb +3 -0
  73. data/lib/preseason.rb +82 -0
  74. data/preseason.gemspec +27 -0
  75. metadata +175 -0
@@ -0,0 +1,9 @@
1
+ class Preseason::Recipe::Devise < Preseason::Recipe
2
+ def prepare
3
+ return unless config.authentication.devise?
4
+
5
+ generate 'devise:install'
6
+ generate 'devise', 'user'
7
+ insert_into_file 'config/environments/development.rb', "\n config.action_mailer.default_url_options = { :host => 'localhost:3000' }\n", :before => /^end$/
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class Preseason::Recipe::Flash < Preseason::Recipe
2
+ def prepare
3
+ empty_directory 'app/views/shared'
4
+ mirror_file 'app/views/shared/_flash.html.erb'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class Preseason::Recipe::Foreman < Preseason::Recipe
2
+ def prepare
3
+ create_file 'Procfile', ''
4
+ end
5
+ end
@@ -0,0 +1,147 @@
1
+ class Preseason::Recipe::Gemfile < Preseason::Recipe
2
+ def prepare
3
+ remove_comments
4
+ remove_unwanted_gems
5
+ remove_empty_lines
6
+ add_ruby_version
7
+ add_database_gem
8
+ add_global_gems
9
+ add_non_heroku_gems
10
+ add_production_gems
11
+ add_development_gems
12
+ add_development_test_gems
13
+ add_test_gems
14
+ add_factory_gem
15
+ add_active_admin_gem
16
+ add_authentication_gem
17
+ add_modernizr_gem
18
+ add_normalize_gem
19
+ add_ie8_gems
20
+ end
21
+
22
+ private
23
+ def remove_comments
24
+ gsub_file 'Gemfile', /^\s*#\s*.*$/, ''
25
+ end
26
+
27
+ def remove_unwanted_gems
28
+ gems_to_remove = %w(jquery-rails) # removing jquery-rails now so we can move it to top of Gemfile
29
+ gems_to_remove << 'sqlite3' unless config.database.sqlite?
30
+ gsub_file 'Gemfile', /^\s*gem '(#{Regexp.union(gems_to_remove)})'.*$/, ''
31
+ end
32
+
33
+ def remove_empty_lines
34
+ gsub_file 'Gemfile', /^\n/, ''
35
+ end
36
+
37
+ def add_ruby_version
38
+ insert_into_file 'Gemfile', "ruby '#{RUBY_VERSION}'\n\n", :after => /source 'https:\/\/rubygems.org'.*\n/
39
+ end
40
+
41
+ def add_database_gem
42
+ insert_into_file 'Gemfile', "gem '#{config.database.gem_name}'\n", :after => /gem 'rails'.*\n/ unless config.database.sqlite?
43
+ end
44
+
45
+ def add_global_gems
46
+ insert_into_file 'Gemfile', :after => /gem '#{config.database.gem_name}'.*\n/ do
47
+ %w(
48
+ whiskey_disk
49
+ jquery-rails
50
+ bourbon
51
+ neat
52
+ ).map { |gem_name| "gem '#{gem_name}'" }.join("\n") << "\n"
53
+ end
54
+ end
55
+
56
+ def add_modernizr_gem
57
+ insert_into_file 'Gemfile', "gem 'modernizr-rails'\n", :after => /gem 'uglifier'.*\n/
58
+ end
59
+
60
+ def add_normalize_gem
61
+ insert_into_file 'Gemfile', "gem 'normalize-rails'\n", :after => /gem 'modernizr-rails'.*\n/
62
+ end
63
+
64
+ def add_ie8_gems
65
+ if config.ie8.enabled?
66
+ insert_into_file 'Gemfile', :after => /gem 'jquery-rails'\n/ do
67
+ "gem 'selectivizr-rails'\ngem 'respond-rails'\n"
68
+ end
69
+ end
70
+ end
71
+
72
+ def add_non_heroku_gems
73
+ unless config.heroku.use?
74
+ insert_into_file 'Gemfile', :after => /gem 'jquery-rails'\n/ do
75
+ "gem 'lograge'\ngem 'whenever', :require => false\n"
76
+ end
77
+ end
78
+ end
79
+
80
+ def add_production_gems
81
+ if config.heroku.use?
82
+ gem_group :production do
83
+ gem 'heroku_rails_deflate'
84
+ end
85
+ end
86
+ end
87
+
88
+ def add_development_gems
89
+ gem_group :development do
90
+ gem 'foreman'
91
+ gem 'guard-bundler'
92
+ gem 'guard-rspec'
93
+ gem 'guard-spork'
94
+ gem 'rb-fsevent', :require => false
95
+ end
96
+ end
97
+
98
+ def add_development_test_gems
99
+ gem_group :development, :test do
100
+ gem 'pry-rails'
101
+ gem 'pry-nav'
102
+ gem 'awesome_print'
103
+ gem 'quiet_assets'
104
+ gem 'rspec-rails'
105
+ end
106
+ end
107
+
108
+ def add_test_gems
109
+ gem_group :test do
110
+ gem 'spork-rails'
111
+ gem 'database_cleaner'
112
+ gem 'shoulda-matchers'
113
+ gem 'capybara-webkit'
114
+ gem 'launchy'
115
+ gem 'fuubar'
116
+ gem 'simplecov'
117
+ end
118
+ end
119
+
120
+ def add_factory_gem
121
+ if config.factory.factory_girl?
122
+ insert_into_file 'Gemfile', :after => "group :development, :test do\n" do
123
+ " gem 'factory_girl_rails'\n"
124
+ end
125
+ end
126
+ end
127
+
128
+ def add_active_admin_gem
129
+ if config.authentication.active_admin?
130
+ insert_into_file 'Gemfile', :after => "gem 'jquery-rails'\n" do
131
+ "gem 'activeadmin', :github => 'gregbell/active_admin'\n"
132
+ end
133
+ end
134
+ end
135
+
136
+ def add_authentication_gem
137
+ if config.authentication.authlogic?
138
+ insert_into_file 'Gemfile', :after => "gem 'jquery-rails'\n" do
139
+ "gem 'authlogic'\n"
140
+ end
141
+ elsif config.authentication.devise?
142
+ insert_into_file 'Gemfile', :after => "gem 'jquery-rails'\n" do
143
+ "gem 'devise'\n"
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,62 @@
1
+ class Preseason::Recipe::Git < Preseason::Recipe
2
+ attr_reader :repo_name, :github_username, :public_repo
3
+
4
+ def prepare
5
+ remove_file 'README.rdoc'
6
+ create_file 'README.md', parse_template('readme/README.md.erb')
7
+
8
+ run 'rake db:migrate'
9
+ run 'git init'
10
+ run 'git add .'
11
+ run 'git commit -m "Initial commit"'
12
+
13
+ publish_to_github if github?
14
+ end
15
+
16
+ private
17
+ def publish_to_github
18
+ ask_repo_details
19
+ ask_repo_owner if repo_organization?
20
+
21
+ run "curl -u '#{github_username}' #{github_url} -d '#{curl_params}' > /dev/null"
22
+ run "git remote add origin git@github.com:#{repo_owner}/#{repo_name}.git"
23
+
24
+ run 'git push -u origin master'
25
+ run 'git checkout -b staging'
26
+ run 'git push -u origin staging'
27
+ end
28
+
29
+ def github?
30
+ yes?("Do you want to create a Github repo? [y/n]")
31
+ end
32
+
33
+ def ask_repo_details
34
+ @repo_name = ask "What do you want to name the Github repo?"
35
+ @github_username = ask "What is your Github username?"
36
+ @public_repo = yes? "Is this a public repo? [y/n]"
37
+ end
38
+
39
+ def ask_repo_owner
40
+ @repo_owner = ask "What is the org's name?"
41
+ end
42
+
43
+ def repo_owner
44
+ @repo_owner || github_username
45
+ end
46
+
47
+ def repo_organization?
48
+ @repo_organization ||= yes? "Does this repo belong to an organization? [y/n]"
49
+ end
50
+
51
+ def github_url
52
+ if repo_organization?
53
+ "https://api.github.com/orgs/#{repo_owner}/repos"
54
+ else
55
+ "https://api.github.com/user/repos"
56
+ end
57
+ end
58
+
59
+ def curl_params
60
+ %Q[{"name":"#{repo_name}","public":"#{public_repo}"}]
61
+ end
62
+ end
@@ -0,0 +1,22 @@
1
+ class Preseason::Recipe::Gitignore < Preseason::Recipe
2
+ def prepare
3
+ append_to_file '.gitignore' do
4
+ "\n" << %w(
5
+ .DS_Store
6
+ *.pid
7
+ .powrc
8
+ .ruby-gemset
9
+ .ruby-version
10
+ *.swp
11
+ config/aws_s3.yml
12
+ config/sunspot.yml
13
+ coverage
14
+ dump.rdb
15
+ erd.pdf
16
+ public/assets
17
+ solr/data
18
+ logfile
19
+ ).join("\n")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ class Preseason::Recipe::Guard < Preseason::Recipe
2
+ def prepare
3
+ run 'bundle exec guard init'
4
+ gsub_file 'Guardfile', ":cucumber_env => { 'RAILS_ENV' => 'test' }, ", ''
5
+ gsub_file 'Guardfile', "guard 'rspec' do", "guard 'rspec', :cli => '--drb --format Fuubar --tag focus', :all_after_pass => false, :all_on_start => false do"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ class Preseason::Recipe::Heroku < Preseason::Recipe
2
+ def prepare
3
+ return unless config.heroku.use?
4
+
5
+ if yes?("Do you want to setup Heroku? [y/n]")
6
+ run "heroku auth:login"
7
+ run "heroku apps:create #{app_name}"
8
+ run "heroku addons:add pgbackups"
9
+ run "git push heroku master -u"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ class Preseason::Recipe::IE8 < Preseason::Recipe
2
+ def prepare
3
+ return unless config.ie8.enabled?
4
+
5
+ copy_js
6
+ insert_into_layout
7
+ end
8
+
9
+ private
10
+
11
+ def copy_js
12
+ mirror_file 'app/assets/javascripts/ie8.coffee'
13
+ end
14
+
15
+ def insert_into_layout
16
+ insert_into_file 'app/views/layouts/application.html.erb', "\n\n <!--[if IE 8]>\n <%= javascript_include_tag 'ie8' %>\n <![endif]-->", :after => /^ \<\%\= yield \%\>$/
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ class Preseason::Recipe::Initializer < Preseason::Recipe
2
+ attr_accessor :assets
3
+
4
+ def prepare
5
+ precompile_assets
6
+ initialize_assets
7
+ end
8
+
9
+ private
10
+ def precompile_assets
11
+ @assets = %w(screen.css)
12
+ @assets << 'ie8.js' if config.ie8.enabled?
13
+ end
14
+
15
+ def initialize_assets
16
+ old_line = '# Rails.application.config.assets.precompile += %w( search.js )'
17
+ new_line = "Rails.application.config.assets.precompile += #{@assets}"
18
+ gsub_file 'config/initializers/assets.rb', old_line, new_line
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ require 'find'
2
+
3
+ class Preseason::Recipe::Playbook < Preseason::Recipe
4
+ def prepare
5
+ download_playbook_repo
6
+ copy_playbook_assets
7
+ clean_playbook_assets
8
+ add_normalize_import
9
+ integrate_playbook
10
+ remove_unwanted_files
11
+ end
12
+
13
+ private
14
+
15
+ def exclude_rules
16
+ # exclude:
17
+ # => files and directories beginning with "."
18
+ # => "README.md" files
19
+ # => "application.coffee" file
20
+ [/^\./, /^(?:readme.md|application\.coffee|syntax\.scss)$/]
21
+ end
22
+
23
+ def download_playbook_repo
24
+ version = '1.1.3.1'
25
+ get "https://github.com/centresource/playbook/archive/v#{version}.tar.gz", '/tmp/playbook.tar.gz'
26
+ remove_dir "/tmp/playbook-#{version}" if Dir.exist? "/tmp/playbook-#{version}"
27
+ remove_dir '/tmp/playbook-css' if Dir.exist? '/tmp/playbook-css'
28
+ `tar -zxvf /tmp/playbook.tar.gz -C /tmp 2> /dev/null`
29
+ `mv /tmp/playbook-#{version}/styles /tmp/playbook-css`
30
+ end
31
+
32
+ def copy_playbook_assets
33
+ Find.find('/tmp/playbook-css/') do |path|
34
+ if exclude_rules.none? { |regex| regex =~ File.basename(path) }
35
+ style_path = path.gsub('/tmp/playbook-css/', 'app/assets/stylesheets/')
36
+ if File.directory? path
37
+ FileUtils.makedirs style_path
38
+ else
39
+ copy_file path, style_path
40
+ end
41
+ else
42
+ Find.prune
43
+ end
44
+ end
45
+ end
46
+
47
+ def clean_playbook_assets
48
+ gsub_file 'app/assets/stylesheets/screen.scss', 'bourbon/app/assets/stylesheets/bourbon', 'bourbon'
49
+ gsub_file 'app/assets/stylesheets/screen.scss', 'neat/app/assets/stylesheets/neat-helpers', 'neat-helpers'
50
+ gsub_file 'app/assets/stylesheets/screen.scss', 'neat/app/assets/stylesheets/neat', 'neat'
51
+ end
52
+
53
+ def add_normalize_import
54
+ insert_into_file 'app/assets/stylesheets/screen.scss', "@import \"normalize-rails/normalize\";\n", :before => '@import "bourbon";'
55
+ end
56
+
57
+ def integrate_playbook
58
+ mirror_file 'app/views/layouts/application.html.erb'
59
+ end
60
+
61
+ def remove_unwanted_files
62
+ remove_file 'app/assets/stylesheets/application.css'
63
+ remove_file 'public/index.html'
64
+ remove_file 'app/assets/images/rails.png'
65
+ remove_dir '/tmp/centresource-generator-playbook*'
66
+ remove_dir '/tmp/playbook-css'
67
+ remove_file '/tmp/playbook.tar.gz'
68
+ end
69
+ end
@@ -0,0 +1,36 @@
1
+ class Preseason::Recipe::Production < Preseason::Recipe
2
+ PRODUCTION = 'config/environments/production.rb'
3
+
4
+ def prepare
5
+ enable_lograge unless config.heroku.use?
6
+ configure_heroku_rails_deflate
7
+ add_precompile_assets
8
+ end
9
+
10
+ private
11
+ def enable_lograge
12
+ insert_into_file PRODUCTION, "\n config.lograge.enabled = true\n", :before => /^end$/
13
+ end
14
+
15
+ def configure_heroku_rails_deflate
16
+ gsub_file PRODUCTION, 'config.serve_static_assets = false', 'config.serve_static_assets = true'
17
+ end
18
+
19
+ def add_precompile_assets
20
+ insert_into_file PRODUCTION, precompile_array, :before => /^end$/
21
+ end
22
+
23
+ def precompile_array
24
+ str = <<-TXT
25
+
26
+ config.assets.precompile += %w(
27
+ screen.css
28
+ ie8.js
29
+ #{'active_admin.js' if config.authentication.active_admin?}
30
+ #{'active_admin.css' if config.authentication.active_admin?}
31
+ )
32
+ TXT
33
+
34
+ str.gsub /^\s{4}\n/, ''
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ class Preseason::Recipe::Routes < Preseason::Recipe
2
+ def prepare
3
+ remove_comments_and_newlines
4
+ add_root_path
5
+ generate_site_controller
6
+ end
7
+
8
+ private
9
+ def remove_comments_and_newlines
10
+ gsub_file 'config/routes.rb', /^\s*#\s*.*$/, ''
11
+ gsub_file 'config/routes.rb', /^\n/, ''
12
+ end
13
+
14
+ def add_root_path
15
+ insert_into_file 'config/routes.rb', " root :to => 'site#index'\n", :before => /^end$/
16
+ end
17
+
18
+ def generate_site_controller
19
+ generate 'controller site index --no-assets --no-helper'
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ class Preseason::Recipe::Rspec < Preseason::Recipe
2
+ def prepare
3
+ generate 'rspec:install'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'rvm'
2
+
3
+ class Preseason::Recipe::Rvm < Preseason::Recipe
4
+ def prepare
5
+ run "rvm gemset create #{app_name}"
6
+ RVM.gemset_use! "#{app_name}" # `run "rvm gemset use #{app_name}"` doesn't work -- rvm still uses the terminal's current gemset
7
+ create_file '.ruby-version', "#{RUBY_VERSION}\n"
8
+ create_file '.ruby-gemset', "#{app_name}\n"
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Preseason::Recipe::Schedule < Preseason::Recipe
2
+ def prepare
3
+ mirror_file 'config/schedule.rb'
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class Preseason::Recipe::SporkRspec < Preseason::Recipe
2
+ def prepare
3
+ remove_file 'spec/spec_helper.rb'
4
+ create_file 'spec/spec_helper.rb', parse_template('spec/spec_helper.erb')
5
+ remove_file 'test'
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class Preseason::Recipe::WhiskeyDisk < Preseason::Recipe
2
+ def prepare
3
+ mirror_file 'config/deploy.yml' unless config.heroku.use?
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require 'erb'
2
+
3
+ class Preseason::Recipe
4
+ include Preseason::GeneratorContext
5
+ include Preseason::Colorize
6
+
7
+ attr_reader :config
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def recipe_root
14
+ "#{template_path}/#{self.class.name.demodulize.underscore}"
15
+ end
16
+
17
+ def post_install_hook
18
+ # implement in child classes
19
+ end
20
+
21
+ private
22
+ def mirror_file(path)
23
+ remove_file(path) if File.exist?(path)
24
+ copy_file "#{recipe_root}/#{path}", path
25
+ end
26
+
27
+ def parse_template(path)
28
+ ERB.new(File.read(File.join(template_path, path))).result(binding)
29
+ end
30
+
31
+ def template_path
32
+ @template_path ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
33
+ end
34
+
35
+ def insert(path, opts = {})
36
+ insert_into_file path, File.read("#{recipe_root}/#{path}"), opts
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+
2
+ Authlogic Post-Install
3
+ ======================
4
+
5
+ Authlogic has been installed and configured. Next steps:
6
+
7
+ - Update the login form with your preferred markup (app/views/user_session/new.html.erb)
8
+ - Update the default "from" address in the site mailer for password reset emails (app/mailers/site_mailer.rb)
9
+ - Update the password reset email to your needs (app/views/site_mailer/password_reset_instructions.html.erb)
10
+ - Update the reset password pages with your preferred markup (app/views/user_session/{acquire_password,forgot_password}.html.erb)
@@ -0,0 +1,40 @@
1
+
2
+
3
+ private
4
+
5
+ def current_user_session
6
+ return @current_user_session if defined?(@current_user_session)
7
+ @current_user_session = UserSession.find
8
+ end
9
+
10
+ def current_user
11
+ return @current_user if defined?(@current_user)
12
+ @current_user = current_user_session && current_user_session.user
13
+ end
14
+
15
+ def require_user
16
+ unless current_user
17
+ store_location
18
+ flash[:notice] = 'You must be logged in to access this page'
19
+ redirect_to login_url
20
+ return false
21
+ end
22
+ end
23
+
24
+ def require_no_user
25
+ if current_user
26
+ store_location
27
+ flash[:notice] = 'You must be logged out to access this page'
28
+ redirect_to root_url
29
+ return false
30
+ end
31
+ end
32
+
33
+ def store_location
34
+ session[:return_to] = request.request_uri
35
+ end
36
+
37
+ def redirect_back_or_default(default)
38
+ redirect_to(session[:return_to] || default)
39
+ session[:return_to] = nil
40
+ end
@@ -0,0 +1,58 @@
1
+ class UserSessionController < ApplicationController
2
+ before_filter :find_user_by_perishable_token, :only => [:acquire_password, :reset_password]
3
+
4
+ def new
5
+ @user_session = UserSession.new
6
+ end
7
+
8
+ def create
9
+ @user_session = UserSession.new(params[:user_session])
10
+
11
+ if @user_session.save
12
+ redirect_back_or_default root_url
13
+ else
14
+ flash.now[:error] = 'Username or password are incorrect.'
15
+ render :action => :new
16
+ end
17
+ end
18
+
19
+ def destroy
20
+ current_user_session.destroy
21
+ redirect_to login_url
22
+ end
23
+
24
+ def send_reset_password_link
25
+ @user = User.where('LOWER(email) = ?', params[:email].downcase).first
26
+ if @user.blank?
27
+ flash.now[:error] = "No user found with that email address."
28
+ render :forgot_password
29
+ else
30
+ begin
31
+ @user.send_password_reset_instructions!
32
+ rescue Net::SMTPFatalError
33
+ end
34
+ flash[:notice] = "An email has been sent with a link to reset your password. Please check your inbox."
35
+ redirect_to root_url
36
+ end
37
+ end
38
+
39
+ def reset_password
40
+ if @user.update_attributes(params[:user])
41
+ flash[:success] = "Your password has been changed. Please login to continue."
42
+ redirect_to root_url
43
+ else
44
+ flash.now[:error] = "There was an error updating your password."
45
+ render :acquire_password
46
+ end
47
+ end
48
+
49
+ private
50
+ def find_user_by_perishable_token
51
+ @user = User.find_by_perishable_token(params[:token])
52
+
53
+ if @user.blank?
54
+ flash[:error] = "There was a problem finding your account. The reset password link may have expired. Please try again."
55
+ redirect_to forgot_password_url
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+
2
+ def password_reset_instructions(user)
3
+ @user = user
4
+ mail(:to => user.email, :subject => 'Password Reset Instructions')
5
+ end
@@ -0,0 +1,11 @@
1
+
2
+ acts_as_authentic do |c|
3
+ # because RSpec has problems with Authlogic's session maintenance
4
+ # see https://github.com/binarylogic/authlogic/issues/262#issuecomment-1804988
5
+ c.maintain_sessions = false
6
+ end
7
+
8
+ def send_password_reset_instructions!
9
+ reset_perishable_token!
10
+ SiteMailer.password_reset_instructions(self).deliver
11
+ end
@@ -0,0 +1,2 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ end