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,22 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <head>
5
+ <title><%%= h(yield(:title) || "Untitled") %></title>
6
+ <%%= stylesheet_link_tag '<%= file_name %>' %>
7
+ <%%= yield(:head) %>
8
+ </head>
9
+ <body>
10
+ <div id="container">
11
+ <%%- flash.each do |name, msg| -%>
12
+ <%%= content_tag :div, msg, :id => "flash_#{name}" %>
13
+ <%%- end -%>
14
+
15
+ <%%- if show_title? -%>
16
+ <h1><%%=h yield(:title) %></h1>
17
+ <%%- end -%>
18
+
19
+ <%%= yield %>
20
+ </div>
21
+ </body>
22
+ </html>
@@ -0,0 +1,19 @@
1
+ !!! Strict
2
+ %html{html_attrs}
3
+
4
+ %head
5
+ %title
6
+ = h(yield(:title) || "Untitled")
7
+ %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
8
+ = stylesheet_link_tag '<%= file_name %>'
9
+ = yield(:head)
10
+
11
+ %body
12
+ #container
13
+ - flash.each do |name, msg|
14
+ = content_tag :div, msg, :id => "flash_#{name}"
15
+
16
+ - if show_title?
17
+ %h1=h yield(:title)
18
+
19
+ = yield
@@ -0,0 +1,81 @@
1
+ body {
2
+ background-color: #4B7399;
3
+ font-family: Verdana, Helvetica, Arial;
4
+ font-size: 14px;
5
+ }
6
+
7
+ a img {
8
+ border: none;
9
+ }
10
+
11
+ a {
12
+ color: #0000FF;
13
+ }
14
+
15
+ .clear {
16
+ clear: both;
17
+ height: 0;
18
+ overflow: hidden;
19
+ }
20
+
21
+ #container {
22
+ width: 75%;
23
+ margin: 0 auto;
24
+ background-color: #FFF;
25
+ padding: 20px 40px;
26
+ border: solid 1px black;
27
+ margin-top: 20px;
28
+ }
29
+
30
+ #flash_notice, #flash_error {
31
+ padding: 5px 8px;
32
+ margin: 10px 0;
33
+ }
34
+
35
+ #flash_notice {
36
+ background-color: #CFC;
37
+ border: solid 1px #6C6;
38
+ }
39
+
40
+ #flash_error {
41
+ background-color: #FCC;
42
+ border: solid 1px #C66;
43
+ }
44
+
45
+ .fieldWithErrors {
46
+ display: inline;
47
+ }
48
+
49
+ #errorExplanation {
50
+ width: 400px;
51
+ border: 2px solid #CF0000;
52
+ padding: 0px;
53
+ padding-bottom: 12px;
54
+ margin-bottom: 20px;
55
+ background-color: #f0f0f0;
56
+ }
57
+
58
+ #errorExplanation h2 {
59
+ text-align: left;
60
+ font-weight: bold;
61
+ padding: 5px 5px 5px 15px;
62
+ font-size: 12px;
63
+ margin: 0;
64
+ background-color: #c00;
65
+ color: #fff;
66
+ }
67
+
68
+ #errorExplanation p {
69
+ color: #333;
70
+ margin-bottom: 0;
71
+ padding: 8px;
72
+ }
73
+
74
+ #errorExplanation ul {
75
+ margin: 2px 24px;
76
+ }
77
+
78
+ #errorExplanation ul li {
79
+ font-size: 12px;
80
+ list-style: disc;
81
+ }
@@ -0,0 +1,67 @@
1
+ !primary_color = #4B7399
2
+
3
+ body
4
+ :background-color = !primary_color
5
+ :font
6
+ :family Verdana, Helvetica, Arial
7
+ :size 14px
8
+
9
+ a
10
+ :color #0000FF
11
+ img
12
+ :border none
13
+
14
+ .clear
15
+ :clear both
16
+ :height 0
17
+ :overflow hidden
18
+
19
+ #container
20
+ :width 75%
21
+ :margin 0 auto
22
+ :background #fff
23
+ :padding 20px 40px
24
+ :border solid 1px black
25
+ :margin-top 20px
26
+
27
+ #flash_notice,
28
+ #flash_error
29
+ :padding 5px 8px
30
+ :margin 10px 0
31
+
32
+ #flash_notice
33
+ :background-color #CFC
34
+ :border solid 1px #6C6
35
+
36
+ #flash_error
37
+ :background-color #FCC
38
+ :border solid 1px #C66
39
+
40
+ .fieldWithErrors
41
+ :display inline
42
+
43
+ #errorExplanation
44
+ :width 400px
45
+ :border 2px solid #CF0000
46
+ :padding 0
47
+ :padding-bottom 12px
48
+ :margin-bottom 20px
49
+ :background-color #f0f0f0
50
+ h2
51
+ :text-align left
52
+ :padding 5px 5px 5px 15px
53
+ :margin 0
54
+ :font
55
+ :weight bold
56
+ :size 12px
57
+ :background-color #c00
58
+ :color #fff
59
+ p
60
+ :color #333
61
+ :margin-bottom 0
62
+ :padding 8px
63
+ ul
64
+ :margin 2px 24px
65
+ li
66
+ :font-size 12px
67
+ :list-style disc
@@ -0,0 +1,51 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model and migration to controller and
3
+ views. The resource is ready to use as a starting point for your restful,
4
+ resource-oriented application. Tests or specs are also generated depending
5
+ on if you have a "spec" directory or not.
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
+ Pass the name of the model, either CamelCased or under_scored, as the first
12
+ argument along with an optional list of attribute pairs and controller actions.
13
+
14
+ If no controller actions are specified, they will default to index, show,
15
+ new, create, edit, update, and destroy.
16
+
17
+ IMPORTANT: If no attribute pairs are specified, no model will be generated.
18
+ It will try to determine the attributes from an existing model.
19
+
20
+ Attribute pairs are column_name:sql_type arguments specifying the
21
+ model's attributes. Timestamps are added by default, so you don't have to
22
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
23
+
24
+ For example, `nifty_scaffold post name:string content:text hidden:boolean`
25
+ gives you a model with those three attributes, a controller that handles
26
+ the create/show/update/destroy, forms to create and edit your posts, and
27
+ an index that lists them all, as well as a map.resources :posts
28
+ declaration in config/routes.rb.
29
+
30
+ Adding an "!" in the mix of arguments will invert the passed controller
31
+ actions. This will include all 7 controller actitons except the ones
32
+ mentioned. This option doesn't affect model attributes.
33
+
34
+ Examples:
35
+ script/generate nifty_scaffold post
36
+
37
+ Will create a controller called "posts" it will contain all seven
38
+ CRUD actions along with the views. A model will NOT be created,
39
+ instead it will look for an existing model and use those attributes.
40
+
41
+ script/generate nifty_scaffold post name:string content:text index new edit
42
+
43
+ Will create a Post model and migration file with the name and content
44
+ attributes. It will also create a controller with index, new, create,
45
+ edit, and update actions. Notice the create and update actions are
46
+ added automatically with new and edit.
47
+
48
+ script/generate nifty_scaffold post ! show new
49
+
50
+ Creates a posts controller (no model) with index, edit, update, and
51
+ destroy actions.
@@ -0,0 +1,232 @@
1
+ class NiftyScaffoldGenerator < Rails::Generator::Base
2
+ attr_accessor :name, :attributes, :controller_actions
3
+
4
+ def initialize(runtime_args, runtime_options = {})
5
+ super
6
+ usage if @args.empty?
7
+
8
+ @name = @args.first
9
+ @controller_actions = []
10
+ @attributes = []
11
+
12
+ @args[1..-1].each do |arg|
13
+ if arg == '!'
14
+ options[:invert] = true
15
+ elsif arg.include? ':'
16
+ @attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
17
+ else
18
+ @controller_actions << arg
19
+ @controller_actions << 'create' if arg == 'new'
20
+ @controller_actions << 'update' if arg == 'edit'
21
+ end
22
+ end
23
+
24
+ @controller_actions.uniq!
25
+ @attributes.uniq!
26
+
27
+ if options[:invert] || @controller_actions.empty?
28
+ @controller_actions = all_actions - @controller_actions
29
+ end
30
+
31
+ if @attributes.empty?
32
+ options[:skip_model] = true # default to skipping model if no attributes passed
33
+ if model_exists?
34
+ model_columns_for_attributes.each do |column|
35
+ @attributes << Rails::Generator::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
36
+ end
37
+ else
38
+ @attributes << Rails::Generator::GeneratedAttribute.new('name', 'string')
39
+ end
40
+ end
41
+ end
42
+
43
+ def manifest
44
+ record do |m|
45
+ unless options[:skip_model]
46
+ m.directory "app/models"
47
+ m.template "model.rb", "app/models/#{singular_name}.rb"
48
+ unless options[:skip_migration]
49
+ m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
50
+ end
51
+
52
+ if rspec?
53
+ m.directory "spec/models"
54
+ m.template "tests/#{test_framework}/model.rb", "spec/models/#{singular_name}_spec.rb"
55
+ m.directory "spec/fixtures"
56
+ m.template "fixtures.yml", "spec/fixtures/#{plural_name}.yml"
57
+ else
58
+ m.directory "test/unit"
59
+ m.template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
60
+ m.directory "test/fixtures"
61
+ m.template "fixtures.yml", "test/fixtures/#{plural_name}.yml"
62
+ end
63
+ end
64
+
65
+ unless options[:skip_controller]
66
+ m.directory "app/controllers"
67
+ m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
68
+
69
+ m.directory "app/helpers"
70
+ m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb"
71
+
72
+ m.directory "app/views/#{plural_name}"
73
+ controller_actions.each do |action|
74
+ if File.exist? source_path("views/#{view_language}/#{action}.html.#{view_language}")
75
+ m.template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
76
+ end
77
+ end
78
+
79
+ if form_partial?
80
+ m.template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
81
+ end
82
+
83
+ m.route_resources plural_name
84
+
85
+ if rspec?
86
+ m.directory "spec/controllers"
87
+ m.template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
88
+ else
89
+ m.directory "test/functional"
90
+ m.template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ def form_partial?
97
+ actions? :new, :edit
98
+ end
99
+
100
+ def all_actions
101
+ %w[index show new create edit update destroy]
102
+ end
103
+
104
+ def action?(name)
105
+ controller_actions.include? name.to_s
106
+ end
107
+
108
+ def actions?(*names)
109
+ names.all? { |n| action? n.to_s }
110
+ end
111
+
112
+ def singular_name
113
+ name.underscore
114
+ end
115
+
116
+ def plural_name
117
+ name.underscore.pluralize
118
+ end
119
+
120
+ def class_name
121
+ name.camelize
122
+ end
123
+
124
+ def plural_class_name
125
+ plural_name.camelize
126
+ end
127
+
128
+ def controller_methods(dir_name)
129
+ controller_actions.map do |action|
130
+ read_template("#{dir_name}/#{action}.rb")
131
+ end.join(" \n").strip
132
+ end
133
+
134
+ def render_form
135
+ if form_partial?
136
+ if options[:haml]
137
+ "= render :partial => 'form'"
138
+ else
139
+ "<%= render :partial => 'form' %>"
140
+ end
141
+ else
142
+ read_template("views/#{view_language}/_form.html.#{view_language}")
143
+ end
144
+ end
145
+
146
+ def items_path(suffix = 'path')
147
+ if action? :index
148
+ "#{plural_name}_#{suffix}"
149
+ else
150
+ "root_#{suffix}"
151
+ end
152
+ end
153
+
154
+ def item_path(suffix = 'path')
155
+ if action? :show
156
+ "@#{singular_name}"
157
+ else
158
+ items_path(suffix)
159
+ end
160
+ end
161
+
162
+ def item_path_for_spec(suffix = 'path')
163
+ if action? :show
164
+ "#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
165
+ else
166
+ items_path(suffix)
167
+ end
168
+ end
169
+
170
+ def item_path_for_test(suffix = 'path')
171
+ if action? :show
172
+ "#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
173
+ else
174
+ items_path(suffix)
175
+ end
176
+ end
177
+
178
+ def model_columns_for_attributes
179
+ class_name.constantize.columns.reject do |column|
180
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
181
+ end
182
+ end
183
+
184
+ def rspec?
185
+ test_framework == :rspec
186
+ end
187
+
188
+ protected
189
+
190
+ def view_language
191
+ options[:haml] ? 'haml' : 'erb'
192
+ end
193
+
194
+ def test_framework
195
+ options[:test_framework] ||= default_test_framework
196
+ end
197
+
198
+ def default_test_framework
199
+ File.exist?(destination_path("spec")) ? :rspec : :testunit
200
+ end
201
+
202
+ def add_options!(opt)
203
+ opt.separator ''
204
+ opt.separator 'Options:'
205
+ opt.on("--skip-model", "Don't generate a model or migration file.") { |v| options[:skip_model] = v }
206
+ opt.on("--skip-migration", "Don't generate migration file for model.") { |v| options[:skip_migration] = v }
207
+ opt.on("--skip-timestamps", "Don't add timestamps to migration file.") { |v| options[:skip_timestamps] = v }
208
+ opt.on("--skip-controller", "Don't generate controller, helper, or views.") { |v| options[:skip_controller] = v }
209
+ opt.on("--invert", "Generate all controller actions except these mentioned.") { |v| options[:invert] = v }
210
+ opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = v }
211
+ opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
212
+ opt.on("--rspec", "Use RSpec for test files.") { options[:test_framework] = :rspec }
213
+ opt.on("--shoulda", "Use Shoulda for test files.") { options[:test_framework] = :shoulda }
214
+ end
215
+
216
+ # is there a better way to do this? Perhaps with const_defined?
217
+ def model_exists?
218
+ File.exist? destination_path("app/models/#{singular_name}.rb")
219
+ end
220
+
221
+ def read_template(relative_path)
222
+ ERB.new(File.read(source_path(relative_path)), nil, '-').result(binding)
223
+ end
224
+
225
+ def banner
226
+ <<-EOS
227
+ Creates a controller and optional model given the name, actions, and attributes.
228
+
229
+ USAGE: #{$0} #{spec.name} ModelName [controller_actions and model:attributes] [options]
230
+ EOS
231
+ end
232
+ end
@@ -0,0 +1,9 @@
1
+ def create
2
+ @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
3
+ if @<%= singular_name %>.save
4
+ flash[:notice] = "Successfully created <%= name.underscore.humanize.downcase %>."
5
+ redirect_to <%= item_path('url') %>
6
+ else
7
+ render :action => 'new'
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ def destroy
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= singular_name %>.destroy
4
+ flash[:notice] = "Successfully destroyed <%= name.underscore.humanize.downcase %>."
5
+ redirect_to <%= items_path('url') %>
6
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= plural_name %> = <%= class_name %>.all
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= singular_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,9 @@
1
+ def update
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
4
+ flash[:notice] = "Successfully updated <%= name.underscore.humanize.downcase %>."
5
+ redirect_to <%= item_path('url') %>
6
+ else
7
+ render :action => 'edit'
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ <%= controller_methods :actions %>
3
+ end
@@ -0,0 +1,9 @@
1
+ one:
2
+ <%- for attribute in attributes -%>
3
+ <%= attribute.name %>: <%= attribute.default %>
4
+ <%- end -%>
5
+
6
+ two:
7
+ <%- for attribute in attributes -%>
8
+ <%= attribute.name %>: <%= attribute.default %>
9
+ <%- end -%>
@@ -0,0 +1,2 @@
1
+ module <%= plural_class_name %>Helper
2
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= plural_name %> do |t|
4
+ <%- for attribute in attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %>
6
+ <%- end -%>
7
+ <%- unless options[:skip_timestamps] -%>
8
+ t.timestamps
9
+ <%- end -%>
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :<%= plural_name %>
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ attr_accessible <%= attributes.map { |a| ":#{a.name}" }.join(", ") %>
3
+ end
@@ -0,0 +1,11 @@
1
+ it "create action should render new template when model is invalid" do
2
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
3
+ post :create
4
+ response.should render_template(:new)
5
+ end
6
+
7
+ it "create action should redirect when model is valid" do
8
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
9
+ post :create
10
+ response.should redirect_to(<%= item_path_for_spec('url') %>)
11
+ end
@@ -0,0 +1,6 @@
1
+ it "destroy action should destroy model and redirect to index action" do
2
+ <%= singular_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= singular_name %>
4
+ response.should redirect_to(<%= items_path('url') %>)
5
+ <%= class_name %>.exists?(<%= singular_name %>.id).should be_false
6
+ end
@@ -0,0 +1,4 @@
1
+ it "edit action should render edit template" do
2
+ get :edit, :id => <%= class_name %>.first
3
+ response.should render_template(:edit)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "index action should render index template" do
2
+ get :index
3
+ response.should render_template(:index)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "new action should render new template" do
2
+ get :new
3
+ response.should render_template(:new)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "show action should render show template" do
2
+ get :show, :id => <%= class_name %>.first
3
+ response.should render_template(:show)
4
+ end
@@ -0,0 +1,11 @@
1
+ it "update action should render edit template when model is invalid" do
2
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
3
+ put :update, :id => <%= class_name %>.first
4
+ response.should render_template(:edit)
5
+ end
6
+
7
+ it "update action should redirect when model is valid" do
8
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
9
+ put :update, :id => <%= class_name %>.first
10
+ response.should redirect_to(<%= item_path_for_spec('url') %>)
11
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ <%= controller_methods 'tests/rspec/actions' %>
8
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= class_name %> do
4
+ it "should be valid" do
5
+ <%= class_name %>.new.should be_valid
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ context "create action" do
2
+ should "render new template when model is invalid" do
3
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
4
+ post :create
5
+ assert_template 'new'
6
+ end
7
+
8
+ should "redirect when model is valid" do
9
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
10
+ post :create
11
+ assert_redirected_to <%= item_path_for_test('url') %>
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ context "destroy action" do
2
+ should "destroy model and redirect to index action" do
3
+ <%= singular_name %> = <%= class_name %>.first
4
+ delete :destroy, :id => <%= singular_name %>
5
+ assert_redirected_to <%= items_path('url') %>
6
+ assert !<%= class_name %>.exists?(<%= singular_name %>.id)
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ context "edit action" do
2
+ should "render edit template" do
3
+ get :edit, :id => <%= class_name %>.first
4
+ assert_template 'edit'
5
+ end
6
+ end