flockonus-nifty-generators 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. data/CHANGELOG +194 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +75 -0
  5. data/Rakefile +10 -0
  6. data/features/nifty_authentication.feature +80 -0
  7. data/features/nifty_config.feature +17 -0
  8. data/features/nifty_layout.feature +20 -0
  9. data/features/nifty_scaffold.feature +74 -0
  10. data/features/step_definitions/common_steps.rb +62 -0
  11. data/features/step_definitions/rails_setup_steps.rb +6 -0
  12. data/features/support/env.rb +6 -0
  13. data/features/support/matchers.rb +7 -0
  14. data/lib/generators/nifty/authentication/USAGE +50 -0
  15. data/lib/generators/nifty/authentication/authentication_generator.rb +154 -0
  16. data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
  17. data/lib/generators/nifty/authentication/templates/controller_authentication.rb +60 -0
  18. data/lib/generators/nifty/authentication/templates/fixtures.yml +24 -0
  19. data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
  20. data/lib/generators/nifty/authentication/templates/sessions_controller.rb +41 -0
  21. data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
  22. data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  23. data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
  24. data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +56 -0
  25. data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  26. data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
  27. data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +61 -0
  28. data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  29. data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
  30. data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +53 -0
  31. data/lib/generators/nifty/authentication/templates/user.rb +38 -0
  32. data/lib/generators/nifty/authentication/templates/users_controller.rb +32 -0
  33. data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
  34. data/lib/generators/nifty/authentication/templates/views/erb/_form.html.erb +20 -0
  35. data/lib/generators/nifty/authentication/templates/views/erb/edit.html.erb +3 -0
  36. data/lib/generators/nifty/authentication/templates/views/erb/login.html.erb +30 -0
  37. data/lib/generators/nifty/authentication/templates/views/erb/signup.html.erb +5 -0
  38. data/lib/generators/nifty/authentication/templates/views/haml/_form.html.haml +20 -0
  39. data/lib/generators/nifty/authentication/templates/views/haml/edit.html.haml +3 -0
  40. data/lib/generators/nifty/authentication/templates/views/haml/login.html.haml +30 -0
  41. data/lib/generators/nifty/authentication/templates/views/haml/signup.html.haml +5 -0
  42. data/lib/generators/nifty/config/USAGE +23 -0
  43. data/lib/generators/nifty/config/config_generator.rb +24 -0
  44. data/lib/generators/nifty/config/templates/config.yml +8 -0
  45. data/lib/generators/nifty/config/templates/load_config.rb +2 -0
  46. data/lib/generators/nifty/layout/USAGE +25 -0
  47. data/lib/generators/nifty/layout/layout_generator.rb +30 -0
  48. data/lib/generators/nifty/layout/templates/char_limit_form.js +36 -0
  49. data/lib/generators/nifty/layout/templates/error_messages_helper.rb +23 -0
  50. data/lib/generators/nifty/layout/templates/layout.html.erb +19 -0
  51. data/lib/generators/nifty/layout/templates/layout.html.haml +21 -0
  52. data/lib/generators/nifty/layout/templates/layout_helper.rb +22 -0
  53. data/lib/generators/nifty/layout/templates/stylesheet.css +92 -0
  54. data/lib/generators/nifty/layout/templates/stylesheet.sass +66 -0
  55. data/lib/generators/nifty/scaffold/USAGE +51 -0
  56. data/lib/generators/nifty/scaffold/scaffold_generator.rb +319 -0
  57. data/lib/generators/nifty/scaffold/templates/actions/create.rb +8 -0
  58. data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +5 -0
  59. data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
  60. data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
  61. data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
  62. data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
  63. data/lib/generators/nifty/scaffold/templates/actions/update.rb +8 -0
  64. data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
  65. data/lib/generators/nifty/scaffold/templates/fixtures.yml +9 -0
  66. data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
  67. data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
  68. data/lib/generators/nifty/scaffold/templates/model.rb +4 -0
  69. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  70. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  71. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  72. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  73. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  74. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  75. data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  76. data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
  77. data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
  78. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  79. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  80. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  81. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  82. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  83. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  84. data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  85. data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
  86. data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
  87. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  88. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  89. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  90. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  91. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  92. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  93. data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  94. data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
  95. data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
  96. data/lib/generators/nifty/scaffold/templates/views/erb/_form.html.erb +13 -0
  97. data/lib/generators/nifty/scaffold/templates/views/erb/edit.html.erb +14 -0
  98. data/lib/generators/nifty/scaffold/templates/views/erb/index.html.erb +29 -0
  99. data/lib/generators/nifty/scaffold/templates/views/erb/new.html.erb +7 -0
  100. data/lib/generators/nifty/scaffold/templates/views/erb/show.html.erb +20 -0
  101. data/lib/generators/nifty/scaffold/templates/views/haml/_form.html.haml +10 -0
  102. data/lib/generators/nifty/scaffold/templates/views/haml/edit.html.haml +14 -0
  103. data/lib/generators/nifty/scaffold/templates/views/haml/index.html.haml +25 -0
  104. data/lib/generators/nifty/scaffold/templates/views/haml/new.html.haml +7 -0
  105. data/lib/generators/nifty/scaffold/templates/views/haml/show.html.haml +20 -0
  106. data/lib/generators/nifty.rb +28 -0
  107. data/rails_generators/nifty_authentication/USAGE +50 -0
  108. data/rails_generators/nifty_authentication/lib/insert_commands.rb +74 -0
  109. data/rails_generators/nifty_authentication/nifty_authentication_generator.rb +128 -0
  110. data/rails_generators/nifty_authentication/templates/authentication.rb +61 -0
  111. data/rails_generators/nifty_authentication/templates/authlogic_session.rb +2 -0
  112. data/rails_generators/nifty_authentication/templates/fixtures.yml +24 -0
  113. data/rails_generators/nifty_authentication/templates/migration.rb +20 -0
  114. data/rails_generators/nifty_authentication/templates/sessions_controller.rb +45 -0
  115. data/rails_generators/nifty_authentication/templates/sessions_helper.rb +2 -0
  116. data/rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  117. data/rails_generators/nifty_authentication/templates/tests/rspec/user.rb +83 -0
  118. data/rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb +26 -0
  119. data/rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  120. data/rails_generators/nifty_authentication/templates/tests/shoulda/user.rb +85 -0
  121. data/rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb +27 -0
  122. data/rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  123. data/rails_generators/nifty_authentication/templates/tests/testunit/user.rb +88 -0
  124. data/rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb +23 -0
  125. data/rails_generators/nifty_authentication/templates/user.rb +42 -0
  126. data/rails_generators/nifty_authentication/templates/users_controller.rb +18 -0
  127. data/rails_generators/nifty_authentication/templates/users_helper.rb +2 -0
  128. data/rails_generators/nifty_authentication/templates/views/erb/login.html.erb +30 -0
  129. data/rails_generators/nifty_authentication/templates/views/erb/signup.html.erb +24 -0
  130. data/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +30 -0
  131. data/rails_generators/nifty_authentication/templates/views/haml/signup.html.haml +24 -0
  132. data/rails_generators/nifty_config/USAGE +23 -0
  133. data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
  134. data/rails_generators/nifty_config/templates/config.yml +8 -0
  135. data/rails_generators/nifty_config/templates/load_config.rb +2 -0
  136. data/rails_generators/nifty_layout/USAGE +25 -0
  137. data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
  138. data/rails_generators/nifty_layout/templates/helper.rb +22 -0
  139. data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
  140. data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
  141. data/rails_generators/nifty_layout/templates/stylesheet.css +97 -0
  142. data/rails_generators/nifty_layout/templates/stylesheet.sass +67 -0
  143. data/rails_generators/nifty_scaffold/USAGE +51 -0
  144. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +232 -0
  145. data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
  146. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
  147. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  148. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  149. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  150. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  151. data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
  152. data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
  153. data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
  154. data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
  155. data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
  156. data/rails_generators/nifty_scaffold/templates/model.rb +3 -0
  157. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
  158. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  159. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  160. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
  161. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
  162. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
  163. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
  164. data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
  165. data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
  166. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  167. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  168. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  169. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  170. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  171. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  172. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  173. data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
  174. data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
  175. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
  176. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  177. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  178. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
  179. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
  180. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
  181. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
  182. data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
  183. data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
  184. data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
  185. data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
  186. data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
  187. data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
  188. data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
  189. data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +10 -0
  190. data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +14 -0
  191. data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +25 -0
  192. data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +7 -0
  193. data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +20 -0
  194. data/test/test_helper.rb +119 -0
  195. data/test/test_nifty_authentication_generator.rb +274 -0
  196. data/test/test_nifty_config_generator.rb +37 -0
  197. data/test/test_nifty_layout_generator.rb +42 -0
  198. data/test/test_nifty_scaffold_generator.rb +534 -0
  199. metadata +313 -0
@@ -0,0 +1,92 @@
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, #flash_alert {
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, #flash_alert {
41
+ background-color: #FCC;
42
+ border: solid 1px #C66;
43
+ }
44
+
45
+ .error_messages {
46
+ width: 400px;
47
+ border: 2px solid #CF0000;
48
+ padding: 0px;
49
+ padding-bottom: 12px;
50
+ margin-bottom: 20px;
51
+ background-color: #f0f0f0;
52
+ font-size: 12px;
53
+ }
54
+
55
+ .error_messages h2 {
56
+ text-align: left;
57
+ font-weight: bold;
58
+ padding: 5px 10px;
59
+ font-size: 12px;
60
+ margin: 0;
61
+ background-color: #c00;
62
+ color: #fff;
63
+ }
64
+
65
+ .error_messages p {
66
+ margin: 8px 10px;
67
+ }
68
+
69
+ .error_messages ul {
70
+ margin: 0;
71
+ }
72
+
73
+ /* Flockonus custom 1*/
74
+
75
+ .fieldWithErrors, .field_with_errors {
76
+ display: inline;
77
+ background-color: #F63;
78
+ }
79
+
80
+ .limit_input, .field_desc {
81
+ color: #AAA;
82
+ font-size: 11;
83
+ }
84
+
85
+ input[type="text"]{
86
+ width: 300px;
87
+ }
88
+
89
+ textarea{
90
+ width: 485px;
91
+ }
92
+
@@ -0,0 +1,66 @@
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: blue
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: white
23
+ padding: 20px 40px
24
+ border: solid 1px black
25
+ margin-top: 20px
26
+
27
+ #flash_notice,
28
+ #flash_error,
29
+ #flash_alert
30
+ padding: 5px 8px
31
+ margin: 10px 0
32
+
33
+ #flash_notice
34
+ background-color: #ccffcc
35
+ border: solid 1px #66cc66
36
+
37
+ #flash_error,
38
+ #flash_alert
39
+ background-color: #ffcccc
40
+ border: solid 1px #cc6666
41
+
42
+ .fieldWithErrors
43
+ display: inline
44
+
45
+ .error_messages
46
+ width: 400px
47
+ border: 2px solid #cf0000
48
+ padding: 0
49
+ padding-bottom: 12px
50
+ margin-bottom: 20px
51
+ background-color: #f0f0f0
52
+ font:
53
+ size: 12px
54
+ h2
55
+ text-align: left
56
+ padding: 5px 5px 5px 15px
57
+ margin: 0
58
+ font:
59
+ weight: bold
60
+ size: 12px
61
+ background-color: #cc0000
62
+ color: white
63
+ p
64
+ margin: 8px 10px
65
+ ul
66
+ margin: 0
@@ -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
+ rails 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
+ rails 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
+ rails 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,319 @@
1
+ require 'generators/nifty'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/generated_attribute'
4
+
5
+ module Nifty
6
+ module Generators
7
+ class ScaffoldGenerator < Base
8
+ include Rails::Generators::Migration
9
+ no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
10
+
11
+ argument :scaffold_name, :type => :string, :required => true, :banner => 'ModelName'
12
+ argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'
13
+
14
+ class_option :skip_model, :desc => 'Don\'t generate a model or migration file.', :type => :boolean
15
+ class_option :skip_migration, :desc => 'Dont generate migration file for model.', :type => :boolean
16
+ class_option :skip_timestamps, :desc => 'Don\'t add timestamps to migration file.', :type => :boolean
17
+ class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
18
+ class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
19
+ class_option :namespace_model, :desc => 'If the resource is namespaced, include the model in the namespace.', :type => :boolean
20
+ class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
21
+
22
+ class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
23
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
24
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
25
+
26
+ def initialize(*args, &block)
27
+ super
28
+
29
+ print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/
30
+
31
+ @controller_actions = []
32
+ @model_attributes = []
33
+ @skip_model = options.skip_model?
34
+ @namespace_model = options.namespace_model?
35
+ @invert_actions = options.invert?
36
+
37
+ args_for_c_m.each do |arg|
38
+ if arg == '!'
39
+ @invert_actions = true
40
+ elsif arg.include?(':')
41
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
42
+ else
43
+ @controller_actions << arg
44
+ @controller_actions << 'create' if arg == 'new'
45
+ @controller_actions << 'update' if arg == 'edit'
46
+ end
47
+ end
48
+
49
+ @controller_actions.uniq!
50
+ @model_attributes.uniq!
51
+
52
+ if @invert_actions || @controller_actions.empty?
53
+ @controller_actions = all_actions - @controller_actions
54
+ end
55
+
56
+ if @model_attributes.empty?
57
+ @skip_model = true # skip model if no attributes
58
+ if model_exists?
59
+ model_columns_for_attributes.each do |column|
60
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
61
+ end
62
+ else
63
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
64
+ end
65
+ end
66
+ end
67
+
68
+ def add_gems
69
+ add_gem "mocha", :group => :test
70
+ end
71
+
72
+ def create_model
73
+ unless @skip_model
74
+ template 'model.rb', "app/models/#{model_path}.rb"
75
+ if test_framework == :rspec
76
+ template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
77
+ template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
78
+ else
79
+ template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
80
+ template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
81
+ end
82
+ end
83
+ end
84
+
85
+ def create_migration
86
+ unless @skip_model || options.skip_migration?
87
+ migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
88
+ end
89
+ end
90
+
91
+ def create_controller
92
+ unless options.skip_controller?
93
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
94
+
95
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
96
+
97
+ controller_actions.each do |action|
98
+ if %w[index show new edit].include?(action) # Actions with templates
99
+ template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
100
+ end
101
+ end
102
+
103
+ if form_partial?
104
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
105
+ end
106
+
107
+ namespaces = plural_name.split('/')
108
+ resource = namespaces.pop
109
+ route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
110
+ "namespace(:#{namespace}){ #{acc} }"
111
+ }
112
+
113
+ if test_framework == :rspec
114
+ template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
115
+ else
116
+ template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
117
+ end
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def form_partial?
124
+ actions? :new, :edit
125
+ end
126
+
127
+ def all_actions
128
+ %w[index show new create edit update destroy]
129
+ end
130
+
131
+ def action?(name)
132
+ controller_actions.include? name.to_s
133
+ end
134
+
135
+ def actions?(*names)
136
+ names.all? { |name| action? name }
137
+ end
138
+
139
+ def singular_name
140
+ scaffold_name.underscore
141
+ end
142
+
143
+ def plural_name
144
+ scaffold_name.underscore.pluralize
145
+ end
146
+
147
+ def table_name
148
+ if scaffold_name.include?('::') && @namespace_model
149
+ plural_name.gsub('/', '_')
150
+ end
151
+ end
152
+
153
+ def class_name
154
+ if @namespace_model
155
+ scaffold_name.camelize
156
+ else
157
+ scaffold_name.split('::').last.camelize
158
+ end
159
+ end
160
+
161
+ def model_path
162
+ class_name.underscore
163
+ end
164
+
165
+ def plural_class_name
166
+ plural_name.camelize
167
+ end
168
+
169
+ def instance_name
170
+ if @namespace_model
171
+ singular_name.gsub('/','_')
172
+ else
173
+ singular_name.split('/').last
174
+ end
175
+ end
176
+
177
+ def instances_name
178
+ instance_name.pluralize
179
+ end
180
+
181
+ def controller_methods(dir_name)
182
+ controller_actions.map do |action|
183
+ read_template("#{dir_name}/#{action}.rb")
184
+ end.join("\n").strip
185
+ end
186
+
187
+ def render_form
188
+ if form_partial?
189
+ if options.haml?
190
+ "= render 'form'"
191
+ else
192
+ "<%= render 'form' %>"
193
+ end
194
+ else
195
+ read_template("views/#{view_language}/_form.html.#{view_language}")
196
+ end
197
+ end
198
+
199
+ def item_resource
200
+ scaffold_name.underscore.gsub('/','_')
201
+ end
202
+
203
+ def items_path
204
+ if action? :index
205
+ "#{item_resource.pluralize}_path"
206
+ else
207
+ "root_path"
208
+ end
209
+ end
210
+
211
+ def item_path(options = {})
212
+ if action? :show
213
+ name = options[:instance_variable] ? "@#{instance_name}" : instance_name
214
+ if %w(new edit).include? options[:action].to_s
215
+ "#{options[:action].to_s}_#{item_resource}_path(#{name})"
216
+ else
217
+ if scaffold_name.include?('::') && !@namespace_model
218
+ namespace = singular_name.split('/')[0..-2]
219
+ "[ :#{namespace.join(', :')}, #{name} ]"
220
+ else
221
+ name
222
+ end
223
+ end
224
+ else
225
+ items_path
226
+ end
227
+ end
228
+
229
+ def item_url
230
+ if action? :show
231
+ item_resource + '_url'
232
+ else
233
+ items_url
234
+ end
235
+ end
236
+
237
+ def items_url
238
+ if action? :index
239
+ item_resource.pluralize + '_url'
240
+ else
241
+ "root_url"
242
+ end
243
+ end
244
+
245
+ def item_path_for_spec(suffix = 'path')
246
+ if action? :show
247
+ "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
248
+ else
249
+ if suffix == 'path'
250
+ items_path
251
+ else
252
+ items_url
253
+ end
254
+ end
255
+ end
256
+
257
+ def item_path_for_test(suffix = 'path')
258
+ if action? :show
259
+ "#{item_resource}_#{suffix}(assigns(:#{instance_name}))"
260
+ else
261
+ if suffix == 'path'
262
+ items_path
263
+ else
264
+ items_url
265
+ end
266
+ end
267
+ end
268
+
269
+ def model_columns_for_attributes
270
+ class_name.constantize.columns.reject do |column|
271
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
272
+ end
273
+ end
274
+
275
+ def view_language
276
+ options.haml? ? 'haml' : 'erb'
277
+ end
278
+
279
+ def test_framework
280
+ return @test_framework if defined?(@test_framework)
281
+ if options.testunit?
282
+ return @test_framework = :testunit
283
+ elsif options.rspec?
284
+ return @test_framework = :rspec
285
+ elsif options.shoulda?
286
+ return @test_framework = :shoulda
287
+ else
288
+ return @test_framework = default_test_framework
289
+ end
290
+ end
291
+
292
+ def default_test_framework
293
+ File.exist?(destination_path("spec")) ? :rspec : :testunit
294
+ end
295
+
296
+ def model_exists?
297
+ File.exist? destination_path("app/models/#{singular_name}.rb")
298
+ end
299
+
300
+ def read_template(relative_path)
301
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
302
+ end
303
+
304
+ def destination_path(path)
305
+ File.join(destination_root, path)
306
+ end
307
+
308
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
309
+ # Implement the required interface for Rails::Generators::Migration.
310
+ def self.next_migration_number(dirname) #:nodoc:
311
+ if ActiveRecord::Base.timestamped_migrations
312
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
313
+ else
314
+ "%.3d" % (current_migration_number(dirname) + 1)
315
+ end
316
+ end
317
+ end
318
+ end
319
+ end
@@ -0,0 +1,8 @@
1
+ def create
2
+ @<%= instance_name %> = <%= class_name %>.new(params[:<%= instance_name %>])
3
+ if @<%= instance_name %>.save
4
+ redirect_to <%= item_path :instance_variable => true %>, :notice => "Successfully created <%= class_name.underscore.humanize.downcase %>."
5
+ else
6
+ render :action => 'new'
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ def destroy
2
+ @<%= instance_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= instance_name %>.destroy
4
+ redirect_to <%= items_url %>, :notice => "Successfully destroyed <%= class_name.underscore.humanize.downcase %>."
5
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= instance_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= instances_name %> = <%= class_name %>.all
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= instance_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= instance_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,8 @@
1
+ def update
2
+ @<%= instance_name %> = <%= class_name %>.find(params[:id])
3
+ if @<%= instance_name %>.update_attributes(params[:<%= instance_name %>])
4
+ redirect_to <%= item_url %>, :notice => "Successfully updated <%= class_name.underscore.humanize.downcase %>."
5
+ else
6
+ render :action => 'edit'
7
+ end
8
+ 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 model_attributes -%>
3
+ <%= attribute.name %>: <%= attribute.default %>
4
+ <%- end -%>
5
+
6
+ two:
7
+ <%- for attribute in model_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<%= class_name.pluralize.delete('::') %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name || plural_name.split('/').last %> do |t|
4
+ <%- for attribute in model_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 :<%= table_name || plural_name.split('/').last %>
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <%= "set_table_name :#{table_name}\n" if table_name -%>
3
+ attr_accessible <%= model_attributes.map { |a| ":#{a.name}" }.join(", ") %>
4
+ 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
+ <%= instance_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= instance_name %>
4
+ response.should redirect_to(<%= items_url %>)
5
+ <%= class_name %>.exists?(<%= instance_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
+ render_views
6
+
7
+ <%= controller_methods 'tests/rspec/actions' %>
8
+ end