fetty-generators 1.7.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. data/Gemfile +2 -0
  2. data/README.rdoc +42 -36
  3. data/Rakefile +1 -3
  4. data/lib/generators/fetty.rb +160 -10
  5. data/lib/generators/fetty/authentication/USAGE +4 -0
  6. data/lib/generators/fetty/authentication/authentication_generator.rb +171 -0
  7. data/lib/generators/fetty/authentication/templates/controllers/reset_passwords_controller.rb +45 -0
  8. data/lib/generators/fetty/authentication/templates/controllers/sessions_controller.rb +30 -0
  9. data/lib/generators/fetty/authentication/templates/controllers/users_controller.rb +78 -0
  10. data/lib/generators/fetty/authentication/templates/helpers/reset_passwords_helper.rb +2 -0
  11. data/lib/generators/fetty/authentication/templates/helpers/sessions_helper.rb +2 -0
  12. data/lib/generators/fetty/authentication/templates/helpers/users_helper.rb +2 -0
  13. data/lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb +53 -0
  14. data/lib/generators/fetty/authentication/templates/lib/users_authentication.rb +110 -0
  15. data/lib/generators/fetty/authentication/templates/mailers/setup_mail.rb +11 -0
  16. data/lib/generators/fetty/authentication/templates/mailers/user_mailer.rb +14 -0
  17. data/lib/generators/fetty/authentication/templates/models/active_record/create_users.rb +17 -0
  18. data/lib/generators/fetty/authentication/templates/models/active_record/user.rb +15 -0
  19. data/lib/generators/fetty/authentication/templates/models/mongoid/user.rb +23 -0
  20. data/lib/generators/fetty/authentication/templates/spec/controllers/reset_passwords_controller_spec.rb +93 -0
  21. data/lib/generators/fetty/authentication/templates/spec/controllers/sessions_controller_spec.rb +61 -0
  22. data/lib/generators/fetty/authentication/templates/spec/controllers/users_controller_spec.rb +158 -0
  23. data/lib/generators/fetty/authentication/templates/spec/models/user_spec.rb +108 -0
  24. data/lib/generators/fetty/authentication/templates/spec/routing/reset_passwords_routing_spec.rb +21 -0
  25. data/lib/generators/fetty/authentication/templates/spec/routing/sessions_routing_spec.rb +17 -0
  26. data/lib/generators/fetty/authentication/templates/spec/routing/users_routing_spec.rb +37 -0
  27. data/lib/generators/fetty/authentication/templates/spec/support/user_factories.rb +6 -0
  28. data/lib/generators/fetty/authentication/templates/views/layouts/application.html.erb +33 -0
  29. data/lib/generators/fetty/authentication/templates/views/reset_passwords/edit.html.erb +19 -0
  30. data/lib/generators/fetty/authentication/templates/views/reset_passwords/new.html.erb +14 -0
  31. data/lib/generators/fetty/authentication/templates/views/sessions/new.html.erb +22 -0
  32. data/lib/generators/fetty/authentication/templates/views/user_mailer/user_activation.text.erb +4 -0
  33. data/lib/generators/fetty/authentication/templates/views/user_mailer/user_forgot_password.text.erb +7 -0
  34. data/lib/generators/fetty/authentication/templates/views/users/_table.html.erb +23 -0
  35. data/lib/generators/fetty/authentication/templates/views/users/edit.html.erb +21 -0
  36. data/lib/generators/fetty/authentication/templates/views/users/index.html.erb +7 -0
  37. data/lib/generators/fetty/authentication/templates/views/users/index.js.erb +1 -0
  38. data/lib/generators/fetty/authentication/templates/views/users/new.html.erb +23 -0
  39. data/lib/generators/fetty/authentication/templates/views/users/show.html.erb +19 -0
  40. data/lib/generators/fetty/messages/USAGE +4 -0
  41. data/lib/generators/fetty/messages/messages_generator.rb +136 -0
  42. data/lib/generators/fetty/messages/templates/assets/javascripts/jquery.tokeninput.js +718 -0
  43. data/lib/generators/fetty/messages/templates/assets/javascripts/messages.js +19 -0
  44. data/lib/generators/fetty/messages/templates/assets/stylesheets/messages.css +87 -0
  45. data/lib/generators/fetty/messages/templates/assets/stylesheets/token-input-facebook.css +122 -0
  46. data/lib/generators/fetty/messages/templates/controllers/messages_controller.rb +109 -0
  47. data/lib/generators/fetty/messages/templates/helpers/messages_helper.rb +2 -0
  48. data/lib/generators/fetty/messages/templates/lib/users_messages.rb +67 -0
  49. data/lib/generators/fetty/messages/templates/models/active_record/create_messages.rb +24 -0
  50. data/lib/generators/fetty/messages/templates/models/active_record/message.rb +67 -0
  51. data/lib/generators/fetty/messages/templates/models/mongoid/message.rb +81 -0
  52. data/lib/generators/fetty/messages/templates/spec/controllers/messages_controller_spec.rb +78 -0
  53. data/lib/generators/fetty/messages/templates/spec/models/message_spec.rb +53 -0
  54. data/lib/generators/fetty/messages/templates/spec/routing/messages_routing_spec.rb +61 -0
  55. data/lib/generators/fetty/messages/templates/spec/support/message_factories.rb +8 -0
  56. data/lib/generators/fetty/messages/templates/views/_head.html.erb +20 -0
  57. data/lib/generators/fetty/messages/templates/views/_messages.html.erb +56 -0
  58. data/lib/generators/fetty/messages/templates/views/_tabs_panel.html.erb +11 -0
  59. data/lib/generators/fetty/messages/templates/views/index.html.erb +10 -0
  60. data/lib/generators/fetty/messages/templates/views/index.js.erb +1 -0
  61. data/lib/generators/fetty/messages/templates/views/new.html.erb +26 -0
  62. data/lib/generators/fetty/messages/templates/views/show.html.erb +35 -0
  63. data/lib/generators/fetty/scaffold/USAGE +2 -52
  64. data/lib/generators/fetty/scaffold/scaffold_generator.rb +96 -329
  65. data/lib/generators/fetty/scaffold/templates/controllers/active_record/controller.rb +60 -0
  66. data/lib/generators/fetty/scaffold/templates/controllers/mongoid/controller.rb +60 -0
  67. data/lib/generators/fetty/scaffold/templates/helpers/helper.rb +2 -0
  68. data/lib/generators/fetty/scaffold/templates/models/active_record/migration.rb +16 -0
  69. data/lib/generators/fetty/scaffold/templates/models/active_record/model.rb +10 -0
  70. data/lib/generators/fetty/scaffold/templates/models/mongoid/model.rb +16 -0
  71. data/lib/generators/fetty/scaffold/templates/test/rspec/controller.rb +98 -0
  72. data/lib/generators/fetty/scaffold/templates/test/rspec/factories.rb +26 -0
  73. data/lib/generators/fetty/scaffold/templates/test/rspec/helper.rb +15 -0
  74. data/lib/generators/fetty/scaffold/templates/test/rspec/model.rb +9 -0
  75. data/lib/generators/fetty/scaffold/templates/test/rspec/request.rb +11 -0
  76. data/lib/generators/fetty/scaffold/templates/test/rspec/routing.rb +41 -0
  77. data/lib/generators/fetty/scaffold/templates/test/test_unit/controller.rb +62 -0
  78. data/lib/generators/fetty/scaffold/templates/{fixtures.yml → test/test_unit/fixtures.yml} +2 -0
  79. data/lib/generators/fetty/scaffold/templates/test/test_unit/helper.rb +4 -0
  80. data/lib/generators/fetty/scaffold/templates/test/test_unit/model.rb +7 -0
  81. data/lib/generators/fetty/scaffold/templates/views/_form.html.erb +20 -0
  82. data/lib/generators/fetty/scaffold/templates/views/_table.html.erb +48 -0
  83. data/lib/generators/fetty/scaffold/templates/views/edit.html.erb +13 -0
  84. data/lib/generators/fetty/scaffold/templates/views/index.html.erb +15 -0
  85. data/lib/generators/fetty/scaffold/templates/views/{erb/index.js.erb → index.js.erb} +0 -0
  86. data/lib/generators/fetty/scaffold/templates/views/new.html.erb +9 -0
  87. data/lib/generators/fetty/scaffold/templates/views/show.html.erb +28 -0
  88. data/lib/generators/fetty/setup/USAGE +3 -20
  89. data/lib/generators/fetty/setup/setup_generator.rb +122 -61
  90. data/lib/generators/fetty/setup/templates/ability.rb +0 -0
  91. data/lib/generators/fetty/setup/templates/ckeditor.rb +54 -0
  92. data/lib/generators/fetty/setup/templates/ckeditor.tar.gz +0 -0
  93. data/lib/generators/fetty/setup/templates/escape_utils.rb +7 -0
  94. data/lib/generators/fetty/setup/templates/file_uploader.rb +15 -0
  95. data/lib/generators/fetty/setup/templates/image_uploader.rb +48 -0
  96. data/lib/generators/fetty/setup/templates/spec_helper.rb +44 -0
  97. data/lib/generators/fetty/views/USAGE +5 -0
  98. data/lib/generators/fetty/{layout/templates/stylesheet.css → views/templates/application.css} +184 -166
  99. data/lib/generators/fetty/views/templates/application.html.erb +23 -0
  100. data/lib/generators/fetty/{layout → views}/templates/application.js +0 -0
  101. data/lib/generators/fetty/views/templates/application_helper.rb +10 -0
  102. data/lib/generators/fetty/views/templates/down_arrow.gif +0 -0
  103. data/lib/generators/fetty/{layout → views}/templates/error_messages_helper.rb +0 -0
  104. data/lib/generators/fetty/{layout → views}/templates/layout_helper.rb +0 -0
  105. data/lib/generators/fetty/views/templates/up_arrow.gif +0 -0
  106. data/lib/generators/fetty/views/views_generator.rb +51 -0
  107. data/lib/generators/scaffold.rb +204 -0
  108. metadata +126 -111
  109. data/LICENSE +0 -20
  110. data/lib/generators/fetty/layout/USAGE +0 -26
  111. data/lib/generators/fetty/layout/layout_generator.rb +0 -44
  112. data/lib/generators/fetty/layout/templates/layout.html.erb +0 -32
  113. data/lib/generators/fetty/layout/templates/layout.html.haml +0 -34
  114. data/lib/generators/fetty/layout/templates/no-devise-links-layout.html.erb +0 -23
  115. data/lib/generators/fetty/layout/templates/no-devise-links-layout.html.haml +0 -23
  116. data/lib/generators/fetty/layout/templates/stylesheet.sass +0 -163
  117. data/lib/generators/fetty/scaffold/templates/actions/create.rb +0 -8
  118. data/lib/generators/fetty/scaffold/templates/actions/destroy.rb +0 -5
  119. data/lib/generators/fetty/scaffold/templates/actions/edit.rb +0 -3
  120. data/lib/generators/fetty/scaffold/templates/actions/index.rb +0 -10
  121. data/lib/generators/fetty/scaffold/templates/actions/new.rb +0 -3
  122. data/lib/generators/fetty/scaffold/templates/actions/show.rb +0 -3
  123. data/lib/generators/fetty/scaffold/templates/actions/update.rb +0 -8
  124. data/lib/generators/fetty/scaffold/templates/controller.rb +0 -7
  125. data/lib/generators/fetty/scaffold/templates/helper.rb +0 -2
  126. data/lib/generators/fetty/scaffold/templates/migration.rb +0 -16
  127. data/lib/generators/fetty/scaffold/templates/model.rb +0 -7
  128. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/create.rb +0 -11
  129. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/destroy.rb +0 -6
  130. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/edit.rb +0 -4
  131. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/index.rb +0 -4
  132. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/new.rb +0 -4
  133. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/show.rb +0 -4
  134. data/lib/generators/fetty/scaffold/templates/tests/rspec/actions/update.rb +0 -11
  135. data/lib/generators/fetty/scaffold/templates/tests/rspec/controller.rb +0 -8
  136. data/lib/generators/fetty/scaffold/templates/tests/rspec/model.rb +0 -7
  137. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/create.rb +0 -13
  138. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/destroy.rb +0 -8
  139. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/edit.rb +0 -6
  140. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/index.rb +0 -6
  141. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/new.rb +0 -6
  142. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/show.rb +0 -6
  143. data/lib/generators/fetty/scaffold/templates/tests/shoulda/actions/update.rb +0 -13
  144. data/lib/generators/fetty/scaffold/templates/tests/shoulda/controller.rb +0 -5
  145. data/lib/generators/fetty/scaffold/templates/tests/shoulda/model.rb +0 -7
  146. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/create.rb +0 -11
  147. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/destroy.rb +0 -6
  148. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/edit.rb +0 -4
  149. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/index.rb +0 -4
  150. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/new.rb +0 -4
  151. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/show.rb +0 -4
  152. data/lib/generators/fetty/scaffold/templates/tests/testunit/actions/update.rb +0 -11
  153. data/lib/generators/fetty/scaffold/templates/tests/testunit/controller.rb +0 -5
  154. data/lib/generators/fetty/scaffold/templates/tests/testunit/model.rb +0 -7
  155. data/lib/generators/fetty/scaffold/templates/views/erb/_form.html.erb +0 -22
  156. data/lib/generators/fetty/scaffold/templates/views/erb/_table.html.erb +0 -50
  157. data/lib/generators/fetty/scaffold/templates/views/erb/edit.html.erb +0 -12
  158. data/lib/generators/fetty/scaffold/templates/views/erb/index.html.erb +0 -13
  159. data/lib/generators/fetty/scaffold/templates/views/erb/new.html.erb +0 -9
  160. data/lib/generators/fetty/scaffold/templates/views/erb/show.html.erb +0 -26
  161. data/lib/generators/fetty/scaffold/templates/views/haml/_form.html.haml +0 -8
  162. data/lib/generators/fetty/scaffold/templates/views/haml/_table.html.haml +0 -33
  163. data/lib/generators/fetty/scaffold/templates/views/haml/edit.html.haml +0 -14
  164. data/lib/generators/fetty/scaffold/templates/views/haml/index.html.haml +0 -12
  165. data/lib/generators/fetty/scaffold/templates/views/haml/index.js.haml +0 -1
  166. data/lib/generators/fetty/scaffold/templates/views/haml/new.html.haml +0 -8
  167. data/lib/generators/fetty/scaffold/templates/views/haml/show.html.haml +0 -23
@@ -1,53 +1,3 @@
1
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 fetty-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, `fetty: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 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 fetty: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
- this command will also generate the views using simple-form, meta_search
41
- and will_paginate in the index.html.erb/haml as a default.
42
-
43
- rails generate fetty:scaffold post name:string content:text index new edit
44
-
45
- Will create a Post model and migration file with the name and content
46
- attributes. It will also create a controller with index, new, create,
47
- edit, and update actions. Notice the create and update actions are
48
- added automatically with new and edit.
49
-
50
- rails generate fetty:scaffold post ! show new
51
-
52
- Creates a posts controller (no model) with index, edit, update, and
53
- destroy actions.
2
+ The fetty:scaffold generator creates a basic scaffolding
3
+ using many useful gems.
@@ -1,4 +1,6 @@
1
1
  require 'generators/fetty'
2
+ require 'generators/scaffold'
3
+ require 'rails/generators/active_record'
2
4
  require 'rails/generators/migration'
3
5
  require 'rails/generators/generated_attribute'
4
6
 
@@ -6,364 +8,129 @@ module Fetty
6
8
  module Generators
7
9
  class ScaffoldGenerator < Base
8
10
  include Rails::Generators::Migration
9
- no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
10
-
11
+ include Fetty::Generators::Scaffold
12
+
11
13
  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 :with_cancan_check, :desc => 'Generate your view with CanCan Checking abilities in every actions.', :type => :boolean, :default => true
19
- class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
20
- class_option :namespace_model, :desc => 'If the resource is namespaced, include the model in the namespace.', :type => :boolean
21
- class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
22
-
23
- class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
24
- class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
25
- class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
26
-
27
- def initialize(*args, &block)
28
- super
29
-
30
- print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/
31
-
32
- @controller_actions = []
33
- @model_attributes = []
34
- @skip_model = options.skip_model?
35
- @namespace_model = options.namespace_model?
36
- @invert_actions = options.invert?
37
-
38
- @paperclip_name = []
39
- @tinymce_name = []
14
+ argument :attributes, :type => :array, :default => [], :banner => 'attribute:type'
15
+
16
+ class_option :controller, :desc => 'Generate controller, helper, or views.', :type => :boolean, :default => true
17
+ class_option :model, :desc => "Generate a model or migration file.", :type => :boolean, :default => true
18
+ class_option :migration, :desc => "Generate migration file for model.", :type => :boolean, :default => true
19
+ class_option :timestamps, :desc => "Add timestamps to migration file.", :type => :boolean, :default => true
20
+ class_option :test, :desc => "Generate Test files, either test/unit or rspec", :type => :boolean, :default => true
21
+
22
+ class_option :except, :desc => 'Generate all controller actions except these mentioned.', :type => :array, :default => []
23
+
24
+ def generate_scaffold
25
+ print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/ && !attributes.empty?
26
+ print_usage unless attributes.drop_while { |arg| arg.include?(':') }.count == 0
40
27
 
41
- args_for_c_m.each do |arg|
42
- if arg == '!'
43
- @invert_actions = true
44
- elsif arg.include?(':')
45
- if arg.include?(':paperclip')
46
- @paperclip_name << arg.split(':').first
47
- @model_attributes << Rails::Generators::GeneratedAttribute.new("#{arg.split(':').first}_file_name", "string")
48
- @model_attributes << Rails::Generators::GeneratedAttribute.new("#{arg.split(':').first}_content_type", "string")
49
- @model_attributes << Rails::Generators::GeneratedAttribute.new("#{arg.split(':').first}_file_size", "integer")
50
- @model_attributes << Rails::Generators::GeneratedAttribute.new("#{arg.split(':').first}_updated_at", "datetime")
51
- elsif arg.include?(':tinymce')
52
- @tinymce_name << arg.split(':').first
53
- @model_attributes << Rails::Generators::GeneratedAttribute.new(arg.split(':').first, "text")
54
- else
55
- @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
56
- end
57
- else
58
- @controller_actions << arg
59
- @controller_actions << 'create' if arg == 'new'
60
- @controller_actions << 'update' if arg == 'edit'
61
- end
62
- end
63
-
64
- @controller_actions.uniq!
65
- @model_attributes.uniq!
28
+ @orm = using_mongoid? ? 'mongoid' : 'active_record'
66
29
 
67
- if @invert_actions || @controller_actions.empty?
68
- @controller_actions = all_actions - @controller_actions
69
- end
70
-
71
- if @model_attributes.empty?
72
- @skip_model = true # skip model if no attributes
73
- if model_exists?
74
- model_columns_for_attributes.each do |column|
75
- @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
30
+ setting_model_attributes
31
+ if options[:model]
32
+ generate_model
33
+ if options.migration? && !using_mongoid?
34
+ generate_migration
76
35
  end
77
- else
78
- @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
79
- end
80
- end
81
- end
82
-
83
- def create_migration
84
- unless @skip_model || options.skip_migration?
85
- migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
86
- delete_paperclip_field_from_model_attributes
87
36
  end
88
- end
89
-
90
- def create_model
91
- unless @skip_model
92
- template 'model.rb', "app/models/#{model_path}.rb"
93
- if test_framework == :rspec
94
- template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
95
- template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
96
- else
97
- template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
98
- template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
99
- end
37
+
38
+ if options.controller?
39
+ setting_controller_attributes
40
+ generate_controller
41
+ generate_helper
42
+ generate_views
43
+ generate_routes
100
44
  end
101
- end
102
-
103
- def create_controller
104
- unless options.skip_controller?
105
- template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
106
-
107
- template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
108
-
109
- controller_actions.each do |action|
110
- if %w[index show new edit].include?(action) # Actions with templates
111
- template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
112
- if action == 'index'
113
- template "views/#{view_language}/_table.html.#{view_language}", "app/views/#{plural_name}/_table.html.#{view_language}"
114
- template "views/#{view_language}/index.js.#{view_language}", "app/views/#{plural_name}/index.js.#{view_language}"
115
- end
116
- end
117
- end
118
-
119
- if form_partial?
120
- template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
121
- end
122
-
123
- namespaces = plural_name.split('/')
124
- resource = namespaces.pop
125
- route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
126
- "namespace(:#{namespace}){ #{acc} }"
127
- }
128
-
129
- if test_framework == :rspec
130
- template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
131
- else
132
- template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
133
- end
45
+
46
+ if options.test?
47
+ generate_test_unit if using_test_unit?
48
+ generate_specs if using_rspec?
134
49
  end
50
+
51
+ rescue Exception => e
52
+ print_notes(e.message,"error",:red)
135
53
  end
136
-
137
- private
138
54
 
139
- def delete_paperclip_field_from_model_attributes
140
- unless @paperclip_name.empty?
141
- @paperclip_name.each do |name|
142
- @model_attributes.delete_if do |a|
143
- a.name == "#{name}_content_type" || a.name == "#{name}_updated_at" || a.name == "#{name}_file_size"
144
- end
145
- @model_attributes.map do |a|
146
- if a.name == "#{name}_file_name"
147
- a.name = name
148
- end
149
- end
150
- end
151
- end
152
- end
153
-
154
- def generate_action_links(action, object, link_text, link_path)
155
- out = ""
156
- out << "\t<% if can? :#{action}, #{object} %>\n" if options.with_cancan_check?
157
- unless action == :destroy
158
- out << "\t\t<%= link_to '#{link_text}', #{link_path} %>\n"
159
- else
160
- out << "\t\t<%= link_to '#{link_text}', #{link_path}, :confirm => 'Are you sure?', :method => :delete %>\n"
161
- end
162
- out << "\t<% end %>" if options.with_cancan_check?
163
- out.html_safe
164
- end
165
-
166
- def form_partial?
167
- actions? :new, :edit
168
- end
169
-
170
- def all_actions
171
- %w[index show new create edit update destroy]
172
- end
173
-
174
- def action?(name)
175
- controller_actions.include? name.to_s
176
- end
177
-
178
- def actions?(*names)
179
- names.all? { |name| action? name }
180
- end
181
-
182
- def singular_table_name
183
- scaffold_name.pluralize.singularize
184
- end
185
-
186
- def singular_name
187
- scaffold_name.underscore
188
- end
189
-
190
- def plural_name
191
- scaffold_name.underscore.pluralize
192
- end
55
+ private
193
56
 
194
- def table_name
195
- if scaffold_name.include?('::') && @namespace_model
196
- plural_name.gsub('/', '_')
197
- end
198
- end
199
-
200
- def class_name
201
- if @namespace_model
202
- scaffold_name.camelize
203
- else
204
- scaffold_name.split('::').last.camelize
57
+ def generate_model
58
+ begin
59
+ template "models/#{@orm}/model.rb", model_name(:path)
60
+ rescue Exception => e
61
+ raise e
205
62
  end
206
63
  end
207
-
208
- def model_path
209
- class_name.underscore
210
- end
211
-
212
- def plural_class_name
213
- plural_name.camelize
214
- end
215
-
216
- def instance_name
217
- if @namespace_model
218
- singular_name.gsub('/','_')
219
- else
220
- singular_name.split('/').last
64
+
65
+ def generate_migration
66
+ begin
67
+ migration_template "models/#{@orm}/migration.rb", migration_name(:path)
68
+ rescue Exception => e
69
+ raise e
221
70
  end
222
71
  end
223
-
224
- def instances_name
225
- instance_name.pluralize
226
- end
227
-
228
- def controller_methods(dir_name)
229
- controller_actions.map do |action|
230
- read_template("#{dir_name}/#{action}.rb")
231
- end.join("\n").strip
232
- end
233
-
234
- def render_form
235
- if form_partial?
236
- if options.haml?
237
- "= render 'form'"
238
- else
239
- "<%= render 'form' %>"
240
- end
241
- else
242
- read_template("views/#{view_language}/_form.html.#{view_language}")
72
+
73
+ def generate_controller
74
+ begin
75
+ template "controllers/#{@orm}/controller.rb", controller_name(:path)
76
+ rescue Exception => e
77
+ raise e
243
78
  end
244
79
  end
245
-
246
- def render_table
247
- if options.haml?
248
- "= render 'table'"
249
- else
250
- "<%= render 'table' %>"
251
- end
252
- end
253
80
 
254
- def item_resource
255
- scaffold_name.underscore.gsub('/','_')
256
- end
257
-
258
- def items_path
259
- if action? :index
260
- "#{item_resource.pluralize}_path"
261
- else
262
- "root_path"
81
+ def generate_helper
82
+ begin
83
+ template 'helpers/helper.rb', helper_name(:path)
84
+ rescue Exception => e
85
+ raise e
263
86
  end
264
87
  end
265
-
266
- def item_path(options = {})
267
- if action? :show
268
- name = options[:instance_variable] ? "@#{instance_name}" : instance_name
269
- if %w(new edit).include? options[:action].to_s
270
- "#{options[:action].to_s}_#{item_resource}_path(#{name})"
271
- else
272
- if scaffold_name.include?('::') && !@namespace_model
273
- namespace = singular_name.split('/')[0..-2]
274
- "[ :#{namespace.join(', :')}, #{name} ]"
275
- else
276
- name
88
+
89
+ def generate_views
90
+ begin
91
+ controller_actions.each do |action|
92
+ unless action == 'create' || action == 'update' || action == 'destroy'
93
+ template "views/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
94
+ if action == 'index'
95
+ template "views/index.js.erb", "app/views/#{plural_name}/index.js.erb"
96
+ template "views/_table.html.erb", "app/views/#{plural_name}/_table.html.erb"
97
+ end
277
98
  end
278
99
  end
279
- else
280
- items_path
281
- end
282
- end
283
-
284
- def item_url
285
- if action? :show
286
- item_resource + '_url'
287
- else
288
- items_url
289
- end
290
- end
291
-
292
- def items_url
293
- if action? :index
294
- item_resource.pluralize + '_url'
295
- else
296
- "root_url"
297
- end
298
- end
299
-
300
- def item_path_for_spec(suffix = 'path')
301
- if action? :show
302
- "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
303
- else
304
- if suffix == 'path'
305
- items_path
306
- else
307
- items_url
308
- end
309
- end
310
- end
311
-
312
- def item_path_for_test(suffix = 'path')
313
- if action? :show
314
- "#{item_resource}_#{suffix}(assigns(:#{instance_name}))"
315
- else
316
- if suffix == 'path'
317
- items_path
318
- else
319
- items_url
100
+
101
+ if actions? :new, :edit
102
+ template "views/_form.html.erb", "app/views/#{plural_name}/_form.html.erb"
320
103
  end
104
+ rescue Exception => e
105
+ raise e
321
106
  end
322
107
  end
323
-
324
- def model_columns_for_attributes
325
- class_name.constantize.columns.reject do |column|
326
- column.name.to_s =~ /^(id|created_at|updated_at)$/
327
- end
328
- end
329
-
330
- def view_language
331
- options.haml? ? 'haml' : 'erb'
332
- end
333
-
334
- def test_framework
335
- return @test_framework if defined?(@test_framework)
336
- if options.testunit?
337
- return @test_framework = :testunit
338
- elsif options.rspec?
339
- return @test_framework = :rspec
340
- elsif options.shoulda?
341
- return @test_framework = :shoulda
342
- else
343
- return @test_framework = default_test_framework
344
- end
345
- end
346
-
347
- def default_test_framework
348
- File.exist?(destination_path("spec")) ? :rspec : :testunit
349
- end
350
-
351
- def model_exists?
352
- File.exist? destination_path("app/models/#{singular_name}.rb")
108
+
109
+ def generate_test_unit
110
+ template "test/test_unit/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
111
+ template "test/test_unit/fixtures.yml", "test/fixtures/#{plural_name}.yml"
112
+ template "test/test_unit/model.rb", "test/unit/#{singular_name}_test.rb"
113
+ template "test/test_unit/helper.rb", "test/unit/helpers/#{plural_name}_helper_test.rb"
114
+ rescue Exception => e
115
+ raise e
353
116
  end
354
-
355
- def read_template(relative_path)
356
- ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
117
+
118
+ def generate_specs
119
+ template "test/rspec/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
120
+ template "test/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
121
+ template "test/rspec/helper.rb", "spec/helpers/#{plural_name}_helper_test.rb"
122
+ template "test/rspec/request.rb", "spec/requests/#{singular_name}_spec.rb.rb"
123
+ template "test/rspec/routing.rb", "spec/routing/#{plural_name}_routing_spec.rb"
124
+ template "test/rspec/factories.rb", "spec/support/#{singular_name}_factories.rb"
125
+ rescue Exception => e
126
+ raise e
357
127
  end
358
-
128
+
129
+
359
130
  # FIXME: Should be proxied to ActiveRecord::Generators::Base
360
131
  # Implement the required interface for Rails::Generators::Migration.
361
132
  def self.next_migration_number(dirname) #:nodoc:
362
- if ActiveRecord::Base.timestamped_migrations
363
- Time.now.utc.strftime("%Y%m%d%H%M%S")
364
- else
365
- "%.3d" % (current_migration_number(dirname) + 1)
366
- end
133
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
367
134
  end
368
135
  end
369
136
  end