corntrace-nifty-generators 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. data/CHANGELOG +128 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +104 -0
  4. data/Rakefile +18 -0
  5. data/features/nifty_authentication.feature +59 -0
  6. data/features/nifty_config.feature +17 -0
  7. data/features/nifty_layout.feature +19 -0
  8. data/features/nifty_scaffold.feature +22 -0
  9. data/features/step_definitions/common_steps.rb +37 -0
  10. data/features/step_definitions/rails_setup_steps.rb +6 -0
  11. data/features/support/env.rb +6 -0
  12. data/features/support/matchers.rb +7 -0
  13. data/lib/generators/nifty/authentication/USAGE +50 -0
  14. data/lib/generators/nifty/authentication/authentication_generator.rb +145 -0
  15. data/lib/generators/nifty/authentication/templates/authentication.rb +60 -0
  16. data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
  17. data/lib/generators/nifty/authentication/templates/fixtures.yml +24 -0
  18. data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
  19. data/lib/generators/nifty/authentication/templates/sessions_controller.rb +45 -0
  20. data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
  21. data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  22. data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
  23. data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +26 -0
  24. data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  25. data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
  26. data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +27 -0
  27. data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  28. data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
  29. data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +23 -0
  30. data/lib/generators/nifty/authentication/templates/user.rb +42 -0
  31. data/lib/generators/nifty/authentication/templates/users_controller.rb +18 -0
  32. data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
  33. data/lib/generators/nifty/authentication/templates/views/erb/login.html.erb +30 -0
  34. data/lib/generators/nifty/authentication/templates/views/erb/signup.html.erb +24 -0
  35. data/lib/generators/nifty/authentication/templates/views/haml/login.html.haml +30 -0
  36. data/lib/generators/nifty/authentication/templates/views/haml/signup.html.haml +24 -0
  37. data/lib/generators/nifty/config/USAGE +23 -0
  38. data/lib/generators/nifty/config/config_generator.rb +24 -0
  39. data/lib/generators/nifty/config/templates/config.yml +8 -0
  40. data/lib/generators/nifty/config/templates/load_config.rb +2 -0
  41. data/lib/generators/nifty/layout/USAGE +25 -0
  42. data/lib/generators/nifty/layout/layout_generator.rb +29 -0
  43. data/lib/generators/nifty/layout/templates/error_messages_helper.rb +23 -0
  44. data/lib/generators/nifty/layout/templates/layout.html.erb +19 -0
  45. data/lib/generators/nifty/layout/templates/layout.html.haml +21 -0
  46. data/lib/generators/nifty/layout/templates/layout_helper.rb +22 -0
  47. data/lib/generators/nifty/layout/templates/stylesheet.css +75 -0
  48. data/lib/generators/nifty/layout/templates/stylesheet.sass +66 -0
  49. data/lib/generators/nifty/scaffold/USAGE +51 -0
  50. data/lib/generators/nifty/scaffold/scaffold_generator.rb +241 -0
  51. data/lib/generators/nifty/scaffold/templates/actions/create.rb +9 -0
  52. data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +6 -0
  53. data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
  54. data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
  55. data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
  56. data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
  57. data/lib/generators/nifty/scaffold/templates/actions/update.rb +9 -0
  58. data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
  59. data/lib/generators/nifty/scaffold/templates/fixtures.yml +9 -0
  60. data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
  61. data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
  62. data/lib/generators/nifty/scaffold/templates/model.rb +3 -0
  63. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  64. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  65. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  66. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  67. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  68. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  69. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  70. data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
  71. data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
  72. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  73. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  74. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  75. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  76. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  77. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  78. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  79. data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
  80. data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
  81. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  82. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  83. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  84. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  85. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  86. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  87. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  88. data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
  89. data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
  90. data/lib/generators/nifty/scaffold/templates/views/erb/_form.html.erb +10 -0
  91. data/lib/generators/nifty/scaffold/templates/views/erb/edit.html.erb +14 -0
  92. data/lib/generators/nifty/scaffold/templates/views/erb/index.html.erb +29 -0
  93. data/lib/generators/nifty/scaffold/templates/views/erb/new.html.erb +7 -0
  94. data/lib/generators/nifty/scaffold/templates/views/erb/show.html.erb +20 -0
  95. data/lib/generators/nifty/scaffold/templates/views/haml/_form.html.haml +10 -0
  96. data/lib/generators/nifty/scaffold/templates/views/haml/edit.html.haml +14 -0
  97. data/lib/generators/nifty/scaffold/templates/views/haml/index.html.haml +25 -0
  98. data/lib/generators/nifty/scaffold/templates/views/haml/new.html.haml +7 -0
  99. data/lib/generators/nifty/scaffold/templates/views/haml/show.html.haml +20 -0
  100. data/lib/generators/nifty.rb +15 -0
  101. data/rails_generators/nifty_authentication/USAGE +50 -0
  102. data/rails_generators/nifty_authentication/lib/insert_commands.rb +74 -0
  103. data/rails_generators/nifty_authentication/nifty_authentication_generator.rb +128 -0
  104. data/rails_generators/nifty_authentication/templates/authentication.rb +61 -0
  105. data/rails_generators/nifty_authentication/templates/authlogic_session.rb +2 -0
  106. data/rails_generators/nifty_authentication/templates/fixtures.yml +24 -0
  107. data/rails_generators/nifty_authentication/templates/migration.rb +20 -0
  108. data/rails_generators/nifty_authentication/templates/sessions_controller.rb +45 -0
  109. data/rails_generators/nifty_authentication/templates/sessions_helper.rb +2 -0
  110. data/rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  111. data/rails_generators/nifty_authentication/templates/tests/rspec/user.rb +83 -0
  112. data/rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb +26 -0
  113. data/rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  114. data/rails_generators/nifty_authentication/templates/tests/shoulda/user.rb +85 -0
  115. data/rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb +27 -0
  116. data/rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  117. data/rails_generators/nifty_authentication/templates/tests/testunit/user.rb +88 -0
  118. data/rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb +23 -0
  119. data/rails_generators/nifty_authentication/templates/user.rb +42 -0
  120. data/rails_generators/nifty_authentication/templates/users_controller.rb +18 -0
  121. data/rails_generators/nifty_authentication/templates/users_helper.rb +2 -0
  122. data/rails_generators/nifty_authentication/templates/views/erb/login.html.erb +30 -0
  123. data/rails_generators/nifty_authentication/templates/views/erb/signup.html.erb +24 -0
  124. data/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +30 -0
  125. data/rails_generators/nifty_authentication/templates/views/haml/signup.html.haml +24 -0
  126. data/rails_generators/nifty_config/USAGE +23 -0
  127. data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
  128. data/rails_generators/nifty_config/templates/config.yml +8 -0
  129. data/rails_generators/nifty_config/templates/load_config.rb +2 -0
  130. data/rails_generators/nifty_layout/USAGE +25 -0
  131. data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
  132. data/rails_generators/nifty_layout/templates/helper.rb +22 -0
  133. data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
  134. data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
  135. data/rails_generators/nifty_layout/templates/stylesheet.css +81 -0
  136. data/rails_generators/nifty_layout/templates/stylesheet.sass +67 -0
  137. data/rails_generators/nifty_scaffold/USAGE +51 -0
  138. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +232 -0
  139. data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
  140. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
  141. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  142. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  143. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  144. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  145. data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
  146. data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
  147. data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
  148. data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
  149. data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
  150. data/rails_generators/nifty_scaffold/templates/model.rb +3 -0
  151. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
  152. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  153. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  154. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
  155. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
  156. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
  157. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
  158. data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
  159. data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
  160. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  161. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  162. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  163. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  164. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  165. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  166. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  167. data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
  168. data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
  169. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
  170. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  171. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  172. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
  173. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
  174. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
  175. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
  176. data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
  177. data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
  178. data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
  179. data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
  180. data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
  181. data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
  182. data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
  183. data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +10 -0
  184. data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +14 -0
  185. data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +25 -0
  186. data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +7 -0
  187. data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +20 -0
  188. data/test/test_helper.rb +119 -0
  189. data/test/test_nifty_authentication_generator.rb +274 -0
  190. data/test/test_nifty_config_generator.rb +37 -0
  191. data/test/test_nifty_layout_generator.rb +42 -0
  192. data/test/test_nifty_scaffold_generator.rb +534 -0
  193. metadata +255 -0
@@ -0,0 +1,20 @@
1
+ <%% title "<%= singular_name.titleize %>" %>
2
+
3
+ <%- for attribute in model_attributes -%>
4
+ <p>
5
+ <strong><%= attribute.human_name.titleize %>:</strong>
6
+ <%%= @<%= singular_name %>.<%= attribute.name %> %>
7
+ </p>
8
+ <%- end -%>
9
+
10
+ <p>
11
+ <%- if action? :edit -%>
12
+ <%%= link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ <%%= link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> |
16
+ <%- end -%>
17
+ <%- if action? :index -%>
18
+ <%%= link_to "View All", <%= plural_name %>_path %>
19
+ <%- end -%>
20
+ </p>
@@ -0,0 +1,10 @@
1
+ - form_for @<%= singular_name %> do |f|
2
+ = f.error_messages
3
+ <%- for attribute in model_attributes -%>
4
+ %p
5
+ = f.label :<%= attribute.name %>
6
+ %br
7
+ = f.<%= attribute.field_type %> :<%= attribute.name %>
8
+ <%- end -%>
9
+ %p
10
+ = f.submit
@@ -0,0 +1,14 @@
1
+ - title "Edit <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if actions? :show, :index -%>
6
+ %p
7
+ <%- if action? :show -%>
8
+ = link_to "Show", <%= singular_name %>_path(@<%= singular_name %>)
9
+ |
10
+ <%- end -%>
11
+ <%- if action? :index -%>
12
+ = link_to "View All", <%= plural_name %>_path
13
+ <%- end -%>
14
+ <%- end -%>
@@ -0,0 +1,25 @@
1
+ - title "<%= plural_name.titleize %>"
2
+
3
+ %table
4
+ %tr
5
+ <%- for attribute in model_attributes -%>
6
+ %th <%= attribute.human_name %>
7
+ <%- end -%>
8
+ - for <%= singular_name %> in @<%= plural_name %>
9
+ %tr
10
+ <%- for attribute in model_attributes -%>
11
+ %td= <%= singular_name %>.<%= attribute.name %>
12
+ <%- end -%>
13
+ <%- if action? :show -%>
14
+ %td= link_to 'Show', <%= singular_name %>
15
+ <%- end -%>
16
+ <%- if action? :edit -%>
17
+ %td= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>)
18
+ <%- end -%>
19
+ <%- if action? :destroy -%>
20
+ %td= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete
21
+ <%- end -%>
22
+
23
+ <%- if actions? :new -%>
24
+ %p= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path
25
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ - title "New <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ %p= link_to "Back to List", <%= plural_name %>_path
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ - title "<%= singular_name.titleize %>"
2
+
3
+ <%- for attribute in model_attributes -%>
4
+ %p
5
+ %strong <%= attribute.human_name.titleize %>:
6
+ = @<%= singular_name %>.<%= attribute.name %>
7
+ <%- end -%>
8
+
9
+ %p
10
+ <%- if action? :edit -%>
11
+ = link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>)
12
+ |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ = link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete
16
+ |
17
+ <%- end -%>
18
+ <%- if action? :index -%>
19
+ = link_to "View All", <%= plural_name %>_path
20
+ <%- end -%>
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Nifty
4
+ module Generators
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+ def self.source_root
7
+ @_nifty_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'nifty', generator_name, 'templates'))
8
+ end
9
+
10
+ def self.banner
11
+ "#{$0} nifty:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,50 @@
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 nifty_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
+ Make sure to setup the authlogic gem if you are using that option.
17
+
18
+ config.gem "authlogic"
19
+
20
+ Examples:
21
+ script/generate nifty_authentication
22
+
23
+ Creates user model, users_controller, and sessions_controller.
24
+
25
+ script/generate nifty_authentication account
26
+
27
+ Creates account model, accounts_controller, and sessions_controller.
28
+
29
+ script/generate nifty_authentication Account UserSession
30
+
31
+ Creates account model, accounts_controller, and user_sessions_controller.
32
+
33
+ Methods:
34
+ There are several methods generated which you can use in your application.
35
+ Here's a common example of what you might add to your layout.
36
+
37
+ <% if logged_in? %>
38
+ Welcome <%= current_user.username %>! Not you?
39
+ <%= link_to "Log out", logout_path %>
40
+ <% else %>
41
+ <%= link_to "Sign up", signup_path %> or
42
+ <%= link_to "log in", login_path %>.
43
+ <% end %>
44
+
45
+ You can also restrict unregistered users from accessing a controller using
46
+ a before filter. For example.
47
+
48
+ before_filter :login_required, :except => [:index, :show]
49
+
50
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,74 @@
1
+ Rails::Generator::Commands::Create.class_eval do
2
+ def route_resource(*resources)
3
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
4
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
5
+
6
+ logger.route "map.resource #{resource_list}"
7
+ unless options[:pretend]
8
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
9
+ "#{match}\n map.resource #{resource_list}\n"
10
+ end
11
+ end
12
+ end
13
+
14
+ def route_name(name, path, route_options = {})
15
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
16
+
17
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
18
+ unless options[:pretend]
19
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
20
+ "#{match}\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
21
+ end
22
+ end
23
+ end
24
+
25
+ def insert_into(file, line)
26
+ logger.insert "#{line} into #{file}"
27
+ unless options[:pretend]
28
+ gsub_file file, /^(class|module) .+$/ do |match|
29
+ "#{match}\n #{line}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ Rails::Generator::Commands::Destroy.class_eval do
36
+ def route_resource(*resources)
37
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
38
+ look_for = "\n map.resource #{resource_list}\n"
39
+ logger.route "map.resource #{resource_list}"
40
+ unless options[:pretend]
41
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
42
+ end
43
+ end
44
+
45
+ def route_name(name, path, route_options = {})
46
+ look_for = "\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
47
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
48
+ unless options[:pretend]
49
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
50
+ end
51
+ end
52
+
53
+ def insert_into(file, line)
54
+ logger.remove "#{line} from #{file}"
55
+ unless options[:pretend]
56
+ gsub_file file, "\n #{line}", ''
57
+ end
58
+ end
59
+ end
60
+
61
+ Rails::Generator::Commands::List.class_eval do
62
+ def route_resource(*resources)
63
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
64
+ logger.route "map.resource #{resource_list}"
65
+ end
66
+
67
+ def route_name(name, path, options = {})
68
+ logger.route "map.#{name} '#{path}', :controller => '{options[:controller]}', :action => '#{options[:action]}'"
69
+ end
70
+
71
+ def insert_into(file, line)
72
+ logger.insert "#{line} into #{file}"
73
+ end
74
+ end
@@ -0,0 +1,128 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
2
+ class NiftyAuthenticationGenerator < Rails::Generator::Base
3
+ attr_accessor :user_name, :session_name
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+
8
+ @user_name = @args[0] || 'user'
9
+ @session_name = @args[1] || (options[:authlogic] ? @user_name + '_session' : 'session')
10
+ end
11
+
12
+ def manifest
13
+ record do |m|
14
+ m.directory "app/models"
15
+ m.directory "app/controllers"
16
+ m.directory "app/helpers"
17
+ m.directory "app/views"
18
+ m.directory "lib"
19
+
20
+ m.directory "app/views/#{user_plural_name}"
21
+ m.template "user.rb", "app/models/#{user_singular_name}.rb"
22
+ m.template "authlogic_session.rb", "app/models/#{session_singular_name}.rb" if options[:authlogic]
23
+ m.template "users_controller.rb", "app/controllers/#{user_plural_name}_controller.rb"
24
+ m.template "users_helper.rb", "app/helpers/#{user_plural_name}_helper.rb"
25
+ m.template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
26
+
27
+ m.directory "app/views/#{session_plural_name}"
28
+ m.template "sessions_controller.rb", "app/controllers/#{session_plural_name}_controller.rb"
29
+ m.template "sessions_helper.rb", "app/helpers/#{session_plural_name}_helper.rb"
30
+ m.template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
31
+
32
+ m.template "authentication.rb", "lib/authentication.rb"
33
+ m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{user_plural_name}"
34
+
35
+ m.route_resources user_plural_name
36
+ m.route_resources session_plural_name
37
+ m.route_name :login, 'login', :controller => session_plural_name, :action => 'new'
38
+ m.route_name :logout, 'logout', :controller => session_plural_name, :action => 'destroy'
39
+ m.route_name :signup, 'signup', :controller => user_plural_name, :action => 'new'
40
+
41
+ m.insert_into "app/controllers/#{application_controller_name}.rb", 'include Authentication'
42
+
43
+ if test_framework == :rspec
44
+ m.directory "spec"
45
+ m.directory "spec/fixtures"
46
+ m.directory "spec/controllers"
47
+ m.directory "spec/models"
48
+ m.template "fixtures.yml", "spec/fixtures/#{user_plural_name}.yml"
49
+ m.template "tests/rspec/user.rb", "spec/models/#{user_singular_name}_spec.rb"
50
+ m.template "tests/rspec/users_controller.rb", "spec/controllers/#{user_plural_name}_controller_spec.rb"
51
+ m.template "tests/rspec/sessions_controller.rb", "spec/controllers/#{session_plural_name}_controller_spec.rb"
52
+ else
53
+ m.directory "test"
54
+ m.directory "test/fixtures"
55
+ m.directory "test/functional"
56
+ m.directory "test/unit"
57
+ m.template "fixtures.yml", "test/fixtures/#{user_plural_name}.yml"
58
+ m.template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
59
+ m.template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
60
+ m.template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{session_plural_name}_controller_test.rb"
61
+ end
62
+ end
63
+ end
64
+
65
+ def user_singular_name
66
+ user_name.underscore
67
+ end
68
+
69
+ def user_plural_name
70
+ user_singular_name.pluralize
71
+ end
72
+
73
+ def user_class_name
74
+ user_name.camelize
75
+ end
76
+
77
+ def user_plural_class_name
78
+ user_plural_name.camelize
79
+ end
80
+
81
+ def session_singular_name
82
+ session_name.underscore
83
+ end
84
+
85
+ def session_plural_name
86
+ session_singular_name.pluralize
87
+ end
88
+
89
+ def session_class_name
90
+ session_name.camelize
91
+ end
92
+
93
+ def session_plural_class_name
94
+ session_plural_name.camelize
95
+ end
96
+
97
+ def application_controller_name
98
+ Rails.version >= '2.3.0' ? 'application_controller' : 'application'
99
+ end
100
+
101
+ protected
102
+
103
+ def view_language
104
+ options[:haml] ? 'haml' : 'erb'
105
+ end
106
+
107
+ def test_framework
108
+ options[:test_framework] ||= File.exist?(destination_path("spec")) ? :rspec : :testunit
109
+ end
110
+
111
+ def add_options!(opt)
112
+ opt.separator ''
113
+ opt.separator 'Options:'
114
+ opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
115
+ opt.on("--rspec", "Use RSpec for test files.") { options[:test_framework] = :rspec }
116
+ opt.on("--shoulda", "Use Shoulda for test files.") { options[:test_framework] = :shoulda }
117
+ opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = true }
118
+ opt.on("--authlogic", "Use Authlogic for authentication.") { |v| options[:authlogic] = true }
119
+ end
120
+
121
+ def banner
122
+ <<-EOS
123
+ Creates user model and controllers to handle registration and authentication.
124
+
125
+ USAGE: #{$0} #{spec.name} [user_name] [sessions_controller_name]
126
+ EOS
127
+ end
128
+ end
@@ -0,0 +1,61 @@
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 <%%=h current_<%= user_singular_name %>.username %>! Not you?
7
+ # <%%= link_to "Log out", logout_path %>
8
+ # <%% else %>
9
+ # <%%= link_to "Sign up", signup_path %> or
10
+ # <%%= link_to "log in", login_path %>.
11
+ # <%% end %>
12
+ #
13
+ # You can also restrict unregistered users from accessing a controller using
14
+ # a before filter. For example.
15
+ #
16
+ # before_filter :login_required, :except => [:index, :show]
17
+ module Authentication
18
+ def self.included(controller)
19
+ controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :redirect_to_target_or_default
20
+ controller.filter_parameter_logging :password
21
+ end
22
+
23
+ <%- if options[:authlogic] -%>
24
+ def current_<%= session_singular_name %>
25
+ return @current_<%= session_singular_name %> if defined?(@current_<%= session_singular_name %>)
26
+ @current_<%= session_singular_name %> = <%= session_class_name %>.find
27
+ end
28
+
29
+ def current_<%= user_singular_name %>
30
+ return @current_<%= user_singular_name %> if defined?(@current_<%= user_singular_name %>)
31
+ @current_<%= user_singular_name %> = current_<%= session_singular_name %> && current_<%= session_singular_name %>.record
32
+ end
33
+ <%- else -%>
34
+ def current_<%= user_singular_name %>
35
+ @current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
36
+ end
37
+ <%- end -%>
38
+
39
+ def logged_in?
40
+ current_<%= user_singular_name %>
41
+ end
42
+
43
+ def login_required
44
+ unless logged_in?
45
+ flash[:error] = "You must first log in or sign up before accessing this page."
46
+ store_target_location
47
+ redirect_to login_url
48
+ end
49
+ end
50
+
51
+ def redirect_to_target_or_default(default)
52
+ redirect_to(session[:return_to] || default)
53
+ session[:return_to] = nil
54
+ end
55
+
56
+ private
57
+
58
+ def store_target_location
59
+ session[:return_to] = request.request_uri
60
+ end
61
+ end
@@ -0,0 +1,2 @@
1
+ class <%= session_class_name %> < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,24 @@
1
+ # password: "secret"
2
+ foo:
3
+ username: foo
4
+ email: foo@example.com
5
+ <%- if options[:authlogic] -%>
6
+ persistence_token: d5ddba13ed4408ea2b0a12ab18ed2d2eda086279736bdc121ca726a11f1e4b99217d9c534c2cc4ebb22729349c8c5fdbe1529e1f2c3c5859c62ef4dd9feea25c
7
+ crypted_password: 3d16c326648cccafe3d4b4cb024475c381dda92f430dfedf6f933e1f61203bacb6bae2437849bdb43b06be335e23790e4aa03902b3c28c3bbbbe27d501e521f3
8
+ password_salt: n6z_wtpWoIsHgQb5IcFd
9
+ <%- else -%>
10
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
11
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
12
+ <%- end -%>
13
+
14
+ bar:
15
+ username: bar
16
+ email: bar@example.com
17
+ <%- if options[:authlogic] -%>
18
+ persistence_token: 19e074bd7cb506ab3e7e53e41f24f0ab3221c8cb68111f4c1aa43965114ad734233979a50a9463537487cdca18c279ac91c4bc83693d589625d446493322394c
19
+ crypted_password: 3bc9f4113ca645a186765df3d31a9352d0067bf2304ba0cdd6b08a7f3d58c6668ab1762fa3e76aef466ea2ff188399d8e6c40244fa59312bb4112292dac9f7f0
20
+ password_salt: UiAh9ejabnKRxqsiK0xO
21
+ <%- else -%>
22
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
23
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
24
+ <%- end -%>
@@ -0,0 +1,20 @@
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
+ <%- if options[:authlogic] -%>
7
+ t.string :persistence_token
8
+ t.string :crypted_password
9
+ <%- else -%>
10
+ t.string :password_hash
11
+ <%- end -%>
12
+ t.string :password_salt
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :<%= user_plural_name %>
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ class <%= session_plural_class_name %>Controller < ApplicationController
2
+ <%- if options[:authlogic] -%>
3
+ def new
4
+ @<%= session_singular_name %> = <%= session_class_name %>.new
5
+ end
6
+
7
+ def create
8
+ @<%= session_singular_name %> = <%= session_class_name %>.new(params[:<%= session_singular_name %>])
9
+ if @<%= session_singular_name %>.save
10
+ flash[:notice] = "Logged in successfully."
11
+ redirect_to_target_or_default(root_url)
12
+ else
13
+ render :action => 'new'
14
+ end
15
+ end
16
+
17
+ def destroy
18
+ @<%= session_singular_name %> = <%= session_class_name %>.find
19
+ @<%= session_singular_name %>.destroy
20
+ flash[:notice] = "You have been logged out."
21
+ redirect_to root_url
22
+ end
23
+ <%- else -%>
24
+ def new
25
+ end
26
+
27
+ def create
28
+ <%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
29
+ if <%= user_singular_name %>
30
+ session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
31
+ flash[:notice] = "Logged in successfully."
32
+ redirect_to_target_or_default(root_url)
33
+ else
34
+ flash.now[:error] = "Invalid login or password."
35
+ render :action => 'new'
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ session[:<%= user_singular_name %>_id] = nil
41
+ flash[:notice] = "You have been logged out."
42
+ redirect_to root_url
43
+ end
44
+ <%- end -%>
45
+ end
@@ -0,0 +1,2 @@
1
+ module <%= session_plural_class_name %>Helper
2
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= session_plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ it "new action should render new template" do
8
+ get :new
9
+ response.should render_template(:new)
10
+ end
11
+
12
+ <%- if options[:authlogic] -%>
13
+ it "create action should render new template when authentication is invalid" do
14
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
15
+ response.should render_template(:new)
16
+ <%= session_class_name %>.find.should be_nil
17
+ end
18
+
19
+ it "create action should redirect when authentication is valid" do
20
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
21
+ response.should redirect_to(root_url)
22
+ <%= session_class_name %>.find.<%= user_singular_name %>.should == <%= user_plural_name %>(:foo)
23
+ end
24
+ <%- else -%>
25
+ it "create action should render new template when authentication is invalid" do
26
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
27
+ post :create
28
+ response.should render_template(:new)
29
+ session['<%= user_singular_name %>_id'].should be_nil
30
+ end
31
+
32
+ it "create action should redirect when authentication is valid" do
33
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
34
+ post :create
35
+ response.should redirect_to(root_url)
36
+ session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
37
+ end
38
+ <%- end -%>
39
+ end
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_class_name %> do
4
+ <%- unless options[:authlogic] -%>
5
+ def new_<%= user_singular_name %>(attributes = {})
6
+ attributes[:username] ||= 'foo'
7
+ attributes[:email] ||= 'foo@example.com'
8
+ attributes[:password] ||= 'abc123'
9
+ attributes[:password_confirmation] ||= attributes[:password]
10
+ <%= user_class_name %>.new(attributes)
11
+ end
12
+
13
+ before(:each) do
14
+ <%= user_class_name %>.delete_all
15
+ end
16
+
17
+ it "should be valid" do
18
+ new_<%= user_singular_name %>.should be_valid
19
+ end
20
+
21
+ it "should require username" do
22
+ new_<%= user_singular_name %>(:username => '').should have(1).error_on(:username)
23
+ end
24
+
25
+ it "should require password" do
26
+ new_<%= user_singular_name %>(:password => '').should have(1).error_on(:password)
27
+ end
28
+
29
+ it "should require well formed email" do
30
+ new_<%= user_singular_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
31
+ end
32
+
33
+ it "should validate uniqueness of email" do
34
+ new_<%= user_singular_name %>(:email => 'bar@example.com').save!
35
+ new_<%= user_singular_name %>(:email => 'bar@example.com').should have(1).error_on(:email)
36
+ end
37
+
38
+ it "should validate uniqueness of username" do
39
+ new_<%= user_singular_name %>(:username => 'uniquename').save!
40
+ new_<%= user_singular_name %>(:username => 'uniquename').should have(1).error_on(:username)
41
+ end
42
+
43
+ it "should not allow odd characters in username" do
44
+ new_<%= user_singular_name %>(:username => 'odd ^&(@)').should have(1).error_on(:username)
45
+ end
46
+
47
+ it "should validate password is longer than 3 characters" do
48
+ new_<%= user_singular_name %>(:password => 'bad').should have(1).error_on(:password)
49
+ end
50
+
51
+ it "should require matching password confirmation" do
52
+ new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
53
+ end
54
+
55
+ it "should generate password hash and salt on create" do
56
+ <%= user_singular_name %> = new_<%= user_singular_name %>
57
+ <%= user_singular_name %>.save!
58
+ <%= user_singular_name %>.password_hash.should_not be_nil
59
+ <%= user_singular_name %>.password_salt.should_not be_nil
60
+ end
61
+
62
+ it "should authenticate by username" do
63
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
64
+ <%= user_singular_name %>.save!
65
+ <%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
66
+ end
67
+
68
+ it "should authenticate by email" do
69
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
70
+ <%= user_singular_name %>.save!
71
+ <%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
72
+ end
73
+
74
+ it "should not authenticate bad username" do
75
+ <%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
76
+ end
77
+
78
+ it "should not authenticate bad password" do
79
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
80
+ <%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
81
+ end
82
+ <%- end -%>
83
+ end