zoo-generators 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/CHANGELOG +198 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +96 -0
  5. data/Rakefile +10 -0
  6. data/features/step_definitions/common_steps.rb +62 -0
  7. data/features/step_definitions/rails_setup_steps.rb +6 -0
  8. data/features/support/env.rb +6 -0
  9. data/features/support/matchers.rb +7 -0
  10. data/features/zoo_authentication.feature +83 -0
  11. data/features/zoo_config.feature +17 -0
  12. data/features/zoo_layout.feature +13 -0
  13. data/features/zoo_scaffold.feature +84 -0
  14. data/lib/generators/zoo.rb +28 -0
  15. data/lib/generators/zoo/authentication/USAGE +46 -0
  16. data/lib/generators/zoo/authentication/authentication_generator.rb +151 -0
  17. data/lib/generators/zoo/authentication/templates/controller_authentication.rb +48 -0
  18. data/lib/generators/zoo/authentication/templates/factory.rb +6 -0
  19. data/lib/generators/zoo/authentication/templates/fixtures.yml +12 -0
  20. data/lib/generators/zoo/authentication/templates/migration.rb +15 -0
  21. data/lib/generators/zoo/authentication/templates/sessions_controller.rb +20 -0
  22. data/lib/generators/zoo/authentication/templates/sessions_helper.rb +2 -0
  23. data/lib/generators/zoo/authentication/templates/tests/rspec/sessions_controller.rb +26 -0
  24. data/lib/generators/zoo/authentication/templates/tests/rspec/user.rb +81 -0
  25. data/lib/generators/zoo/authentication/templates/tests/rspec/users_controller.rb +54 -0
  26. data/lib/generators/zoo/authentication/templates/tests/shoulda/sessions_controller.rb +26 -0
  27. data/lib/generators/zoo/authentication/templates/tests/shoulda/user.rb +83 -0
  28. data/lib/generators/zoo/authentication/templates/tests/shoulda/users_controller.rb +59 -0
  29. data/lib/generators/zoo/authentication/templates/tests/testunit/sessions_controller.rb +22 -0
  30. data/lib/generators/zoo/authentication/templates/tests/testunit/user.rb +87 -0
  31. data/lib/generators/zoo/authentication/templates/tests/testunit/users_controller.rb +51 -0
  32. data/lib/generators/zoo/authentication/templates/user.rb +35 -0
  33. data/lib/generators/zoo/authentication/templates/users_controller.rb +30 -0
  34. data/lib/generators/zoo/authentication/templates/users_helper.rb +2 -0
  35. data/lib/generators/zoo/authentication/templates/views/haml/_form.html.haml +16 -0
  36. data/lib/generators/zoo/authentication/templates/views/haml/edit.html.haml +3 -0
  37. data/lib/generators/zoo/authentication/templates/views/haml/login.html.haml +13 -0
  38. data/lib/generators/zoo/authentication/templates/views/haml/signup.html.haml +5 -0
  39. data/lib/generators/zoo/config/USAGE +23 -0
  40. data/lib/generators/zoo/config/config_generator.rb +24 -0
  41. data/lib/generators/zoo/config/templates/config.yml +8 -0
  42. data/lib/generators/zoo/config/templates/load_config.rb +2 -0
  43. data/lib/generators/zoo/layout/USAGE +25 -0
  44. data/lib/generators/zoo/layout/layout_generator.rb +26 -0
  45. data/lib/generators/zoo/layout/templates/error_messages_helper.rb +23 -0
  46. data/lib/generators/zoo/layout/templates/layout.html.erb +19 -0
  47. data/lib/generators/zoo/layout/templates/layout.html.haml +21 -0
  48. data/lib/generators/zoo/layout/templates/layout_helper.rb +22 -0
  49. data/lib/generators/zoo/layout/templates/stylesheet.css +83 -0
  50. data/lib/generators/zoo/layout/templates/stylesheet.sass +73 -0
  51. data/lib/generators/zoo/scaffold/USAGE +51 -0
  52. data/lib/generators/zoo/scaffold/scaffold_generator.rb +316 -0
  53. data/lib/generators/zoo/scaffold/templates/actions/create.rb +8 -0
  54. data/lib/generators/zoo/scaffold/templates/actions/destroy.rb +5 -0
  55. data/lib/generators/zoo/scaffold/templates/actions/edit.rb +3 -0
  56. data/lib/generators/zoo/scaffold/templates/actions/index.rb +3 -0
  57. data/lib/generators/zoo/scaffold/templates/actions/new.rb +3 -0
  58. data/lib/generators/zoo/scaffold/templates/actions/show.rb +3 -0
  59. data/lib/generators/zoo/scaffold/templates/actions/update.rb +8 -0
  60. data/lib/generators/zoo/scaffold/templates/controller.rb +3 -0
  61. data/lib/generators/zoo/scaffold/templates/factory.rb +5 -0
  62. data/lib/generators/zoo/scaffold/templates/fixtures.yml +9 -0
  63. data/lib/generators/zoo/scaffold/templates/helper.rb +2 -0
  64. data/lib/generators/zoo/scaffold/templates/migration.rb +16 -0
  65. data/lib/generators/zoo/scaffold/templates/model.rb +4 -0
  66. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  67. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  68. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  69. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  70. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  71. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  72. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  73. data/lib/generators/zoo/scaffold/templates/tests/rspec/controller.rb +8 -0
  74. data/lib/generators/zoo/scaffold/templates/tests/rspec/model.rb +7 -0
  75. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  76. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  77. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  78. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  79. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  80. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  81. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  82. data/lib/generators/zoo/scaffold/templates/tests/shoulda/controller.rb +5 -0
  83. data/lib/generators/zoo/scaffold/templates/tests/shoulda/model.rb +7 -0
  84. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  85. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  86. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  87. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  88. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  89. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  90. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  91. data/lib/generators/zoo/scaffold/templates/tests/testunit/controller.rb +5 -0
  92. data/lib/generators/zoo/scaffold/templates/tests/testunit/model.rb +7 -0
  93. data/lib/generators/zoo/scaffold/templates/views/haml/_form.html.haml +9 -0
  94. data/lib/generators/zoo/scaffold/templates/views/haml/edit.html.haml +14 -0
  95. data/lib/generators/zoo/scaffold/templates/views/haml/index.html.haml +25 -0
  96. data/lib/generators/zoo/scaffold/templates/views/haml/new.html.haml +7 -0
  97. data/lib/generators/zoo/scaffold/templates/views/haml/show.html.haml +20 -0
  98. metadata +243 -0
@@ -0,0 +1,17 @@
1
+ Feature: Zoo Config Generator
2
+ In order to have configure an app
3
+ As a rails developer
4
+ I want to generate a config file and manager
5
+
6
+ Scenario: Generate normal config
7
+ Given a new Rails app
8
+ When I run "rails g zoo:config"
9
+ Then I should see file "config/app_config.yml"
10
+ And I should see file "config/initializers/load_app_config.rb"
11
+
12
+ Scenario: Generate named config
13
+ Given a new Rails app
14
+ When I run "rails g zoo:config FooBar"
15
+ Then I should see "FOO_BAR_CONFIG" in file "config/initializers/load_foo_bar_config.rb"
16
+ And I should see "config/foo_bar_config.yml" in file "config/initializers/load_foo_bar_config.rb"
17
+ And I should see file "config/foo_bar_config.yml"
@@ -0,0 +1,13 @@
1
+ Feature: Zoo Layout Generator
2
+ In order to have a layout
3
+ As a rails developer
4
+ I want to generate a simple layout
5
+
6
+ Scenario: Generate normal application layout
7
+ Given a new Rails app
8
+ When I run "rails g zoo:layout -f"
9
+ Then I should see "stylesheet_link_tag "application"" in file "app/views/layouts/application.html.haml"
10
+ Then I should see "h(page_title" in file "app/helpers/layout_helper.rb"
11
+ And I should see file "app/helpers/layout_helper.rb"
12
+ And I should see file "app/helpers/error_messages_helper.rb"
13
+ And I should see file "public/stylesheets/sass/application.sass"
@@ -0,0 +1,84 @@
1
+ Feature: Zoo Scaffold Generator
2
+ In order to manage a resource
3
+ As a rails developer
4
+ I want to generate a model, controller, and views for that resource
5
+
6
+ Scenario: Generate scaffold for simple resource
7
+ Given a new Rails app
8
+ When I run "rails g zoo:scaffold Project name:string"
9
+ Then I should see the following files
10
+ | app/models/project.rb |
11
+ | app/controllers/projects_controller.rb |
12
+ | app/helpers/projects_helper.rb |
13
+ | app/views/projects/index.html.haml |
14
+ | app/views/projects/show.html.haml |
15
+ | app/views/projects/new.html.haml |
16
+ | app/views/projects/edit.html.haml |
17
+ | db/migrate |
18
+ And I should see "resources :projects" in file "config/routes.rb"
19
+ And I should see "gem "mocha", :group => :test" in file "Gemfile"
20
+ When I run "rails g zoo:layout -f"
21
+ And I run "rake db:migrate"
22
+ And I should successfully run "rails g zoo:scaffold Project -f"
23
+ And I run "bundle install"
24
+ Then I should successfully run "rake test"
25
+
26
+ Scenario: Generate scaffold with rspec tests
27
+ Given a new Rails app
28
+ When I run "rails g zoo:scaffold Project name:string --rspec"
29
+ Then I should see the following files
30
+ | spec/models/project_spec.rb |
31
+ | spec/controllers/projects_controller_spec.rb |
32
+ When I run "rails g zoo:scaffold Task project_id:integer"
33
+ Then I should see the following files
34
+ | spec/models/task_spec.rb |
35
+ | spec/controllers/tasks_controller_spec.rb |
36
+ When I run "rails g zoo:layout -f"
37
+ And I run "rake db:migrate"
38
+ And I add "gem 'rspec-rails', '>= 2.0.0.beta.19'" to file "Gemfile"
39
+ And I run "rails g rspec:install"
40
+ And I replace "mock_with :rspec" with "mock_with :mocha" in file "spec/spec_helper.rb"
41
+ And I run "bundle install"
42
+ Then I should successfully run "rake spec"
43
+
44
+ Scenario: Report error when invalid model name
45
+ Given a new Rails app
46
+ Then I should see "Usage:" when running "rails g zoo:scaffold name:string parent_id:integer"
47
+
48
+ Scenario: Generate scaffold for namespaced resource
49
+ Given a new Rails app
50
+ When I run "rails g zoo:scaffold Admin::User name:string"
51
+ Then I should see the following files
52
+ | app/models/user.rb |
53
+ | app/controllers/admin/users_controller.rb |
54
+ | app/helpers/admin/users_helper.rb |
55
+ | app/views/admin/users/index.html.haml |
56
+ | app/views/admin/users/show.html.haml |
57
+ | app/views/admin/users/new.html.haml |
58
+ | app/views/admin/users/edit.html.haml |
59
+ | db/migrate |
60
+ And I should see "class User" in file "app/models/user.rb"
61
+ And I should not see "set_table_name :" in file "app/models/user.rb"
62
+ And I should see "namespace(:admin){ resources :users }" in file "config/routes.rb"
63
+ When I run "rails g zoo:layout -f"
64
+ And I run "rake db:migrate"
65
+ And I should successfully run "rails g zoo:scaffold Admin::User -f"
66
+ And I run "bundle install"
67
+ Then I should successfully run "rake test"
68
+
69
+ Scenario: Generate scaffold with a namespaced model
70
+ Given a new Rails app
71
+ When I run "rails g zoo:scaffold Admin::User name:string --namespace_model"
72
+ Then I should see "class Admin::User" in file "app/models/admin/user.rb"
73
+ And I should see "set_table_name :admin_users" in file "app/models/admin/user.rb"
74
+ When I run "rails g zoo:layout -f"
75
+ And I run "rake db:migrate"
76
+ And I should successfully run "rails g zoo:scaffold Admin::User -f --namespace_model"
77
+ And I run "bundle install"
78
+ Then I should successfully run "rake test"
79
+
80
+ Scenario: Given scaffold with a new and index action
81
+ Given a new Rails app
82
+ When I run "rails g zoo:scaffold Project name:string index new"
83
+ Then I should see "class Project" in file "app/models/project.rb"
84
+ And I should see "= semantic_form_for @project do |f|" in file "app/views/projects/new.html.haml"
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Zoo
4
+ module Generators
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+ def self.source_root
7
+ @_haml_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'zoo', generator_name, 'templates'))
8
+ end
9
+
10
+ def self.banner
11
+ "rails generate zoo:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
12
+ end
13
+
14
+ private
15
+
16
+ def add_gem(name, options = {})
17
+ gemfile_content = File.read(destination_path("Gemfile"))
18
+ File.open(destination_path("Gemfile"), 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
19
+ gem name, options unless gemfile_content.include? name
20
+ end
21
+
22
+ def print_usage
23
+ self.class.help(Thor::Base.shell.new)
24
+ exit
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ Description:
2
+ Generates a user model, users controller, and sessions controller. The
3
+ users controller handles the registration and the sessions controller
4
+ handles authentication. This is similar to restful_authentication, but
5
+ simpler.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the haml_layout generator. You may want to run that generator first.
9
+
10
+ Usage:
11
+ If you do not pass any arguments, the model name will default to "user", and
12
+ the authentication controller will default to "session". You can override
13
+ each of these respectively by passing one or two arguments. Either name can
14
+ be CamelCased or under_scored.
15
+
16
+ Examples:
17
+ rails generate zoo:authentication
18
+
19
+ Creates user model, users_controller, and sessions_controller.
20
+
21
+ rails generate zoo:authentication account
22
+
23
+ Creates account model, accounts_controller, and sessions_controller.
24
+
25
+ rails generate zoo:authentication Account UserSession
26
+
27
+ Creates account model, accounts_controller, and user_sessions_controller.
28
+
29
+ Methods:
30
+ There are several methods generated which you can use in your application.
31
+ Here's a common example of what you might add to your layout.
32
+
33
+ <% if logged_in? %>
34
+ Welcome <%= current_user.username %>! Not you?
35
+ <%= link_to "Log out", logout_path %>
36
+ <% else %>
37
+ <%= link_to "Sign up", signup_path %> or
38
+ <%= link_to "log in", login_path %>.
39
+ <% end %>
40
+
41
+ You can also restrict unregistered users from accessing a controller using
42
+ a before filter. For example.
43
+
44
+ before_filter :login_required, :except => [:index, :show]
45
+
46
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,151 @@
1
+ require 'generators/zoo'
2
+ require 'rails/generators/migration'
3
+
4
+ module Zoo
5
+ module Generators
6
+ class AuthenticationGenerator < Base
7
+ include Rails::Generators::Migration
8
+
9
+ argument :user_name, :type => :string, :default => 'user', :banner => 'user_name'
10
+ argument :session_name, :type => :string, :default => 'session', :banner => 'sessions_controller_name'
11
+
12
+ class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
13
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
14
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
15
+
16
+ def add_gems
17
+ add_gem "haml-rails"
18
+ add_gem "bcrypt-ruby", :require => "bcrypt"
19
+ add_gem "mocha", :group => :test
20
+ end
21
+
22
+ def create_model_files
23
+ template 'user.rb', "app/models/#{user_singular_name}.rb"
24
+ end
25
+
26
+ def create_controller_files
27
+ template 'users_controller.rb', "app/controllers/#{user_plural_name}_controller.rb"
28
+ template 'sessions_controller.rb', "app/controllers/#{session_plural_name}_controller.rb"
29
+ end
30
+
31
+ def create_helper_files
32
+ template 'users_helper.rb', "app/helpers/#{user_plural_name}_helper.rb"
33
+ template 'sessions_helper.rb', "app/helpers/#{session_plural_name}_helper.rb"
34
+ end
35
+
36
+ def create_view_files
37
+ template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
38
+ template "views/#{view_language}/edit.html.#{view_language}", "app/views/#{user_plural_name}/edit.html.#{view_language}"
39
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{user_plural_name}/_form.html.#{view_language}"
40
+ template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
41
+ end
42
+
43
+ def create_lib_files
44
+ template 'controller_authentication.rb', 'lib/controller_authentication.rb'
45
+ end
46
+
47
+ def create_routes
48
+ route "resources #{user_plural_name.to_sym.inspect}"
49
+ route "resources #{session_plural_name.to_sym.inspect}"
50
+ route "match 'login' => '#{session_plural_name}#new', :as => :login"
51
+ route "match 'logout' => '#{session_plural_name}#destroy', :as => :logout"
52
+ route "match 'signup' => '#{user_plural_name}#new', :as => :signup"
53
+ route "match '#{user_singular_name}/edit' => '#{user_plural_name}#edit', :as => :edit_current_#{user_singular_name}"
54
+ end
55
+
56
+ def create_migration
57
+ migration_template 'migration.rb', "db/migrate/create_#{user_plural_name}.rb"
58
+ end
59
+
60
+ def load_and_include_authentication
61
+ inject_into_class "config/application.rb", "Application", " config.autoload_paths << \"\#{config.root}/lib\""
62
+ inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include ControllerAuthentication\n"
63
+ end
64
+
65
+ def create_test_files
66
+ if test_framework == :rspec
67
+ template 'factory.rb', "spec/factories/#{user_plural_name}.rb"
68
+ template 'fixtures.yml', "spec/fixtures/#{user_plural_name}.yml"
69
+ template 'tests/rspec/user.rb', "spec/models/#{user_singular_name}_spec.rb"
70
+ template 'tests/rspec/users_controller.rb', "spec/controllers/#{user_plural_name}_controller_spec.rb"
71
+ template 'tests/rspec/sessions_controller.rb', "spec/controllers/#{session_plural_name}_controller_spec.rb"
72
+ else
73
+ template 'factory.rb', "test/factories/#{user_plural_name}.rb"
74
+ template 'fixtures.yml', "test/fixtures/#{user_plural_name}.yml"
75
+ template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
76
+ template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
77
+ template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{session_plural_name}_controller_test.rb"
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def session_name
84
+ @_session_name ||= @session_name
85
+ end
86
+
87
+ def user_singular_name
88
+ user_name.underscore
89
+ end
90
+
91
+ def user_plural_name
92
+ user_singular_name.pluralize
93
+ end
94
+
95
+ def user_class_name
96
+ user_name.camelize
97
+ end
98
+
99
+ def user_plural_class_name
100
+ user_plural_name.camelize
101
+ end
102
+
103
+ def session_singular_name
104
+ session_name.underscore
105
+ end
106
+
107
+ def session_plural_name
108
+ session_singular_name.pluralize
109
+ end
110
+
111
+ def session_class_name
112
+ session_name.camelize
113
+ end
114
+
115
+ def session_plural_class_name
116
+ session_plural_name.camelize
117
+ end
118
+
119
+ def view_language
120
+ 'haml'
121
+ end
122
+
123
+ def test_framework
124
+ return @test_framework if defined?(@test_framework)
125
+ if options.testunit?
126
+ return @test_framework = :testunit
127
+ elsif options.rspec?
128
+ return @test_framework = :rspec
129
+ elsif options.shoulda?
130
+ return @test_framework = :shoulda
131
+ else
132
+ return @test_framework = File.exist?(destination_path('spec')) ? :rspec : :testunit
133
+ end
134
+ end
135
+
136
+ def destination_path(path)
137
+ File.join(destination_root, path)
138
+ end
139
+
140
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
141
+ # Implement the required interface for Rails::Generators::Migration.
142
+ def self.next_migration_number(dirname) #:nodoc:
143
+ if ActiveRecord::Base.timestamped_migrations
144
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
145
+ else
146
+ "%.3d" % (current_migration_number(dirname) + 1)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,48 @@
1
+ # This module is included in your application controller which makes
2
+ # several methods available to all controllers and views. Here's a
3
+ # common example you might add to your application layout file.
4
+ #
5
+ # <%% if logged_in? %>
6
+ # Welcome <%%= current_<%= user_singular_name %>.username %>.
7
+ # <%%= link_to "Edit profile", edit_current_<%= user_singular_name %>_path %> or
8
+ # <%%= link_to "Log out", logout_path %>
9
+ # <%% else %>
10
+ # <%%= link_to "Sign up", signup_path %> or
11
+ # <%%= link_to "log in", login_path %>.
12
+ # <%% end %>
13
+ #
14
+ # You can also restrict unregistered users from accessing a controller using
15
+ # a before filter. For example.
16
+ #
17
+ # before_filter :login_required, :except => [:index, :show]
18
+ module ControllerAuthentication
19
+ def self.included(controller)
20
+ controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :redirect_to_target_or_default
21
+ end
22
+
23
+ def current_<%= user_singular_name %>
24
+ @current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
25
+ end
26
+
27
+ def logged_in?
28
+ current_<%= user_singular_name %>
29
+ end
30
+
31
+ def login_required
32
+ unless logged_in?
33
+ store_target_location
34
+ redirect_to login_url, :alert => "You must first log in or sign up before accessing this page."
35
+ end
36
+ end
37
+
38
+ def redirect_to_target_or_default(default, *args)
39
+ redirect_to(session[:return_to] || default, *args)
40
+ session[:return_to] = nil
41
+ end
42
+
43
+ private
44
+
45
+ def store_target_location
46
+ session[:return_to] = request.url
47
+ end
48
+ end
@@ -0,0 +1,6 @@
1
+ Factory.define :<%= user_singular_name %> do |<%= user_singular_name %>|
2
+ <%= user_singular_name %>.username 'bar'
3
+ <%= user_singular_name %>.email 'bar@example.com'
4
+ <%= user_singular_name %>.password_hash '3488f5f7efecab14b91eb96169e5e1ee518a569f''
5
+ <%= user_singular_name %>.password_salt 'bef65e058905c379436d80d1a32e7374b139e7b0'
6
+ end
@@ -0,0 +1,12 @@
1
+ # password: "secret"
2
+ foo:
3
+ username: foo
4
+ email: foo@example.com
5
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
6
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
7
+
8
+ bar:
9
+ username: bar
10
+ email: bar@example.com
11
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
12
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
@@ -0,0 +1,15 @@
1
+ class Create<%= user_plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= user_plural_name %> do |t|
4
+ t.string :username
5
+ t.string :email
6
+ t.string :password_hash
7
+ t.string :password_salt
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :<%= user_plural_name %>
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ class <%= session_plural_class_name %>Controller < ApplicationController
2
+ def new
3
+ end
4
+
5
+ def create
6
+ <%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
7
+ if <%= user_singular_name %>
8
+ session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
9
+ redirect_to_target_or_default root_url, :notice => "Logged in successfully."
10
+ else
11
+ flash.now[:alert] = "Invalid login or password."
12
+ render :new
13
+ end
14
+ end
15
+
16
+ def destroy
17
+ session[:<%= user_singular_name %>_id] = nil
18
+ redirect_to root_url, :notice => "You have been logged out."
19
+ end
20
+ end