stager 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/README.md +58 -20
  2. data/lib/generators/stager/controller/USAGE +17 -0
  3. data/lib/generators/stager/controller/controller_generator.rb +234 -0
  4. data/lib/generators/stager/controller/templates/spec/actions/create.rb +13 -0
  5. data/lib/generators/stager/controller/templates/spec/actions/destroy.rb +8 -0
  6. data/lib/generators/stager/controller/templates/spec/actions/edit.rb +6 -0
  7. data/lib/generators/stager/controller/templates/spec/actions/index.rb +6 -0
  8. data/lib/generators/stager/controller/templates/spec/actions/new.rb +6 -0
  9. data/lib/generators/stager/controller/templates/spec/actions/show.rb +6 -0
  10. data/lib/generators/stager/controller/templates/spec/actions/update.rb +13 -0
  11. data/lib/generators/stager/model/USAGE +10 -0
  12. data/lib/generators/stager/model/model_generator.rb +71 -0
  13. data/lib/generators/stager/scaffold/USAGE +2 -0
  14. data/lib/generators/stager/scaffold/scaffold_generator.rb +14 -230
  15. data/lib/stager/version.rb +1 -1
  16. metadata +33 -29
  17. data/lib/generators/stager/scaffold/templates/spec/actions/create.rb +0 -11
  18. data/lib/generators/stager/scaffold/templates/spec/actions/destroy.rb +0 -6
  19. data/lib/generators/stager/scaffold/templates/spec/actions/edit.rb +0 -4
  20. data/lib/generators/stager/scaffold/templates/spec/actions/index.rb +0 -4
  21. data/lib/generators/stager/scaffold/templates/spec/actions/new.rb +0 -4
  22. data/lib/generators/stager/scaffold/templates/spec/actions/show.rb +0 -4
  23. data/lib/generators/stager/scaffold/templates/spec/actions/update.rb +0 -11
  24. /data/lib/generators/stager/{scaffold → controller}/templates/actions/create.rb +0 -0
  25. /data/lib/generators/stager/{scaffold → controller}/templates/actions/destroy.rb +0 -0
  26. /data/lib/generators/stager/{scaffold → controller}/templates/actions/edit.rb +0 -0
  27. /data/lib/generators/stager/{scaffold → controller}/templates/actions/index.rb +0 -0
  28. /data/lib/generators/stager/{scaffold → controller}/templates/actions/new.rb +0 -0
  29. /data/lib/generators/stager/{scaffold → controller}/templates/actions/show.rb +0 -0
  30. /data/lib/generators/stager/{scaffold → controller}/templates/actions/update.rb +0 -0
  31. /data/lib/generators/stager/{scaffold → controller}/templates/controller.rb +0 -0
  32. /data/lib/generators/stager/{scaffold → controller}/templates/helper.rb +0 -0
  33. /data/lib/generators/stager/{scaffold → controller}/templates/spec/controller.rb +0 -0
  34. /data/lib/generators/stager/{scaffold → controller}/templates/views/_form.html.haml +0 -0
  35. /data/lib/generators/stager/{scaffold → controller}/templates/views/edit.html.haml +0 -0
  36. /data/lib/generators/stager/{scaffold → controller}/templates/views/index.html.haml +0 -0
  37. /data/lib/generators/stager/{scaffold → controller}/templates/views/new.html.haml +0 -0
  38. /data/lib/generators/stager/{scaffold → controller}/templates/views/show.html.haml +0 -0
  39. /data/lib/generators/stager/{scaffold → model}/templates/migration.rb +0 -0
  40. /data/lib/generators/stager/{scaffold → model}/templates/model.rb +0 -0
  41. /data/lib/generators/stager/{scaffold → model}/templates/spec/fixtures.yml +0 -0
  42. /data/lib/generators/stager/{scaffold → model}/templates/spec/model.rb +0 -0
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Stager
2
2
 
3
- Stager is a Rails 3.1 gem to create scaffolds. Scaffolds created are specific to 45north's needs. This is a webdevelopment agency in Amsterdam, The Netherlands.
3
+ Stager is a Rails 3.1 gem to create scaffolds, controllers and models. The files created are specific to 45north's needs. This is a webdevelopment agency in Amsterdam, The Netherlands.
4
4
 
5
- version 0.1.1
5
+ version 0.2.0
6
6
  Robin Brouwer
7
7
  45north
8
8
 
@@ -14,31 +14,69 @@ Stager only works with Rails 3.1. Add the following to your Gemfile:
14
14
 
15
15
  Now just run `bundle install` and you're ready.
16
16
 
17
- ## Usage
17
+ ## Scaffold generator
18
18
 
19
- Use the User Model to create all actions.
19
+ Usage:
20
20
 
21
- rails g stager:scaffold User
21
+ # Use the User Model to create all actions.
22
+ rails g stager:scaffold User
23
+
24
+ # Use the User Model to create two actions.
25
+ rails g stager:scaffold User index show
26
+
27
+ # Use the User Model to create all actions, except one action.
28
+ rails g stager:scaffold User ! show
29
+
30
+ # Create a new User Model with all actions.
31
+ rails g stager:scaffold User name:string email:string
32
+
33
+ # Create a new User Model and three actions.
34
+ rails g stager:scaffold User name:string email:string index new create
22
35
 
23
- Use the User Model to create two actions.
36
+ Options:
24
37
 
25
- rails g stager:scaffold User index show
38
+ --namespaced_model # Create a namespaced Model.
39
+ --skip_migration # Skip the migration file.
40
+ --skip_timestamps # Skip the timestamps.
41
+ --skip_presenter # Skip the presenter.
42
+ --skip_helper # Skip the helper.
43
+ --skip_views # Skip the views.
26
44
 
27
- Use the User Model to create all actions, except one action.
28
-
29
- rails g stager:scaffold User ! show
30
45
 
31
- Create a new User Model with all actions.
46
+ ## Controller generator
32
47
 
33
- rails g stager:scaffold User name:string email:string
48
+ Usage:
34
49
 
35
- Create a new User Model and three actions.
50
+ # Create UsersController and use the User Model to create all actions.
51
+ rails g stager:controller Users
52
+
53
+ # Create UsersController and use the User Model to create two actions.
54
+ rails g stager:controller Users index show
55
+
56
+ # Create UsersController and use the User Model to create all actions, except one action.
57
+ rails g stager:controller Users ! show
58
+
59
+ # Create UsersController with all actions and use these two columns inside the views.
60
+ rails g stager:controller User name:string email:string
36
61
 
37
- rails g stager:scaffold User name:string email:string index new create
38
-
39
- Options.
62
+ Options:
40
63
 
41
- --namespaced_model # Create a namespaced Model.
42
- --skip_migration # Skip the migration file.
43
- --skip_timestamps # Skip the timestamps.
44
- --skip_presenter # Skip the presenter.
64
+ --namespaced_model # The Model is namespaced inside the Controller.
65
+ --skip_helper # Skip the helper.
66
+ --skip_views # Skip the views.
67
+
68
+
69
+ ## Model generator
70
+
71
+ Usage:
72
+
73
+ # Create a new Model.
74
+ rails g stager:model User
75
+
76
+ # Create a new Model with columns.
77
+ rails g stager:model User name:string email:string
78
+
79
+ Options:
80
+
81
+ --skip_migration # Skip the migration file.
82
+ --skip_timestamps # Skip the timestamps.
@@ -0,0 +1,17 @@
1
+ Usage:
2
+ # Create UsersController and use the User Model to create all actions.
3
+ rails g stager:controller Users
4
+
5
+ # Create UsersController and use the User Model to create two actions.
6
+ rails g stager:controller Users index show
7
+
8
+ # Create UsersController and use the User Model to create all actions, except one action.
9
+ rails g stager:controller Users ! show
10
+
11
+ # Create UsersController with all actions and use these two columns inside the views.
12
+ rails g stager:controller User name:string email:string
13
+
14
+ Options:
15
+ --namespaced_model # The Model is namespaced inside the Controller.
16
+ --skip_helper # Skip the helper.
17
+ --skip_views # Skip the views.
@@ -0,0 +1,234 @@
1
+ require 'rails/generators/generated_attribute'
2
+
3
+ module Stager
4
+ class ControllerGenerator < Rails::Generators::Base
5
+ no_tasks { attr_accessor :controller_name, :model_attributes, :controller_actions }
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+ argument :controller_name, :type => :string, :required => true
9
+ argument :model_and_controller_args, :type => :array, :default => []
10
+
11
+ class_option :namespaced_model, :type => :boolean, :default => false
12
+ class_option :skip_views, :type => :boolean, :default => false
13
+ class_option :skip_helper, :type => :boolean, :default => false
14
+
15
+ def set_options
16
+ @controller_actions = []
17
+ @model_attributes = []
18
+ @invert_actions = false
19
+ @namespaced_model = false
20
+
21
+ model_and_controller_args.each do |arg|
22
+ if arg == '!'
23
+ @invert_actions = true
24
+ elsif arg.include?(':')
25
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
26
+ else
27
+ @controller_actions << arg
28
+ @controller_actions << 'create' if arg == 'new'
29
+ @controller_actions << 'update' if arg == 'edit'
30
+ end
31
+ end
32
+
33
+ if options[:namespaced_model]
34
+ @namespaced_model = true
35
+ end
36
+
37
+ if @invert_actions || @controller_actions.empty?
38
+ @controller_actions = all_actions - @controller_actions
39
+ end
40
+
41
+ if @model_attributes.empty?
42
+ if model_exists?
43
+ model_columns_for_attributes.each do |column|
44
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def create_controller
51
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
52
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb" unless options[:skip_helper]
53
+
54
+ namespaces = plural_name.split('/')
55
+ resource = namespaces.pop
56
+ if namespaces.present?
57
+ route "namespace(:#{namespaces.first}) { resources :#{resource} }"
58
+ else
59
+ route "resources :#{resource}"
60
+ end
61
+
62
+ template "spec/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
63
+ end
64
+
65
+ def create_views
66
+ unless options[:skip_views]
67
+ controller_actions.each do |action|
68
+ if %w[index show new edit].include?(action)
69
+ template "views/#{action}.html.haml", "app/views/#{plural_name}/#{action}.html.haml"
70
+ end
71
+ end
72
+
73
+ if form_partial?
74
+ template "views/_form.html.haml", "app/views/#{plural_name}/_form.html.haml"
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def model_columns_for_attributes
82
+ class_name.constantize.columns.reject do |column|
83
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
84
+ end
85
+ end
86
+
87
+ def model_exists?
88
+ File.exist?(destination_path("app/models/#{singular_name}.rb")) && class_name.constantize.table_exists?
89
+ end
90
+
91
+ def destination_path(path)
92
+ File.join(destination_root, path)
93
+ end
94
+
95
+ def all_actions
96
+ %w[index show new create edit update destroy]
97
+ end
98
+
99
+ def action?(name)
100
+ controller_actions.include?(name.to_s)
101
+ end
102
+
103
+ def actions?(*names)
104
+ names.all? { |name| action? name }
105
+ end
106
+
107
+ def form_partial?
108
+ actions?(:new, :edit)
109
+ end
110
+
111
+ def render_form
112
+ if form_partial?
113
+ "= render(\"form\")"
114
+ else
115
+ read_template("views/_form.html.haml")
116
+ end
117
+ end
118
+
119
+ def controller_methods(dir_name)
120
+ controller_actions.map do |action|
121
+ read_template("#{dir_name}/#{action}.rb")
122
+ end.join("\n").strip
123
+ end
124
+
125
+ def before_filter_actions
126
+ controller_actions.map { |action| %w(show edit update destroy).include?(action) ? ":#{action}" : nil }.compact.join(", ")
127
+ end
128
+
129
+ def before_filter_actions?
130
+ available = false
131
+ %w(show edit update destroy).each do |name|
132
+ available = true if action?(name)
133
+ end
134
+ available
135
+ end
136
+
137
+ def item_resource
138
+ controller_name.singularize.underscore.gsub('/','_')
139
+ end
140
+
141
+ def items_path
142
+ if action? :index
143
+ "#{item_resource.pluralize}_path"
144
+ else
145
+ "root_path"
146
+ end
147
+ end
148
+
149
+ def item_path(options = {})
150
+ name = options[:instance_variable] ? "@#{instance_name}" : instance_name
151
+ suffix = options[:full_url] ? "url" : "path"
152
+ if options[:action].to_s == "new"
153
+ "new_#{item_resource}_#{suffix}"
154
+ elsif options[:action].to_s == "edit"
155
+ "edit_#{item_resource}_#{suffix}(#{name})"
156
+ else
157
+ if controller_name.include?('::') && !@namespaced_model
158
+ namespace = singular_name.split('/')[0..-2]
159
+ "[:#{namespace.join(', :')}, #{name}]"
160
+ else
161
+ name
162
+ end
163
+ end
164
+ end
165
+
166
+ def item_url
167
+ if action? :show
168
+ item_path(:full_url => true, :instance_variable => true)
169
+ else
170
+ items_url
171
+ end
172
+ end
173
+
174
+ def items_url
175
+ if action? :index
176
+ item_resource.pluralize + '_url'
177
+ else
178
+ "root_url"
179
+ end
180
+ end
181
+
182
+ def item_path_for_spec(suffix = 'path')
183
+ if action? :show
184
+ "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
185
+ else
186
+ if suffix == 'path'
187
+ items_path
188
+ else
189
+ items_url
190
+ end
191
+ end
192
+ end
193
+
194
+ def singular_name
195
+ controller_name.underscore.singularize
196
+ end
197
+
198
+ def plural_name
199
+ controller_name.underscore
200
+ end
201
+
202
+ def class_name
203
+ if @namespaced_model
204
+ singular_name.camelize
205
+ else
206
+ singular_name.split('/').last.camelize
207
+ end
208
+ end
209
+
210
+ def plural_class_name
211
+ plural_name.camelize
212
+ end
213
+
214
+ def instance_name
215
+ if @namespaced_model
216
+ singular_name.gsub('/', '_')
217
+ else
218
+ singular_name.split('/').last
219
+ end
220
+ end
221
+
222
+ def instances_name
223
+ instance_name.pluralize
224
+ end
225
+
226
+ def titleized_name
227
+ singular_name.split('/').last.titleize
228
+ end
229
+
230
+ def read_template(relative_path)
231
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,13 @@
1
+ describe :create do
2
+ it "should render new template when model is invalid" do
3
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
4
+ post :create
5
+ response.should render_template(:new)
6
+ end
7
+
8
+ it "should redirect when model is valid" do
9
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
10
+ post :create
11
+ response.should redirect_to(<%= item_path_for_spec('url') %>)
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ describe :destroy do
2
+ it "should destroy model and redirect to index action" do
3
+ <%= singular_name %> = <%= class_name %>.first
4
+ delete :destroy, :id => <%= singular_name %>
5
+ response.should redirect_to(<%= items_path %>)
6
+ <%= class_name %>.exists?(<%= singular_name %>.id).should be_false
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ describe :edit do
2
+ it "should render edit template" do
3
+ get :edit, :id => <%= class_name %>.first
4
+ response.should render_template(:edit)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ describe :index do
2
+ it "should render index template" do
3
+ get :index
4
+ response.should render_template(:index)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ describe :new do
2
+ it "should render new template" do
3
+ get :new
4
+ response.should render_template(:new)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ describe :show do
2
+ it "should render show template" do
3
+ get :show, :id => <%= class_name %>.first
4
+ response.should render_template(:show)
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ describe :update do
2
+ it "should render edit template when model is invalid" do
3
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
4
+ put :update, :id => <%= class_name %>.first
5
+ response.should render_template(:edit)
6
+ end
7
+
8
+ it "should redirect when model is valid" do
9
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
10
+ put :update, :id => <%= class_name %>.first
11
+ response.should redirect_to(<%= item_path_for_spec('url') %>)
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ Usage:
2
+ # Create a new Model.
3
+ rails g stager:model User
4
+
5
+ # Create a new Model with columns.
6
+ rails g stager:model User name:string email:string
7
+
8
+ Options:
9
+ --skip_migration # Skip the migration file.
10
+ --skip_timestamps # Skip the timestamps.
@@ -0,0 +1,71 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/generated_attribute'
3
+
4
+ module Stager
5
+ class ModelGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ no_tasks { attr_accessor :model_name, :model_attributes }
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+ argument :model_name, :type => :string, :required => true
11
+ argument :model_args, :type => :array, :default => []
12
+
13
+ class_option :skip_migration, :type => :boolean, :default => false
14
+ class_option :skip_timestamps, :type => :boolean, :default => false
15
+
16
+ def set_options
17
+ @model_attributes = []
18
+
19
+ model_args.each do |arg|
20
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
21
+ end
22
+ end
23
+
24
+ def create_model
25
+ template 'model.rb', "app/models/#{model_path}.rb"
26
+
27
+ template "spec/model.rb", "spec/models/#{model_path}_spec.rb"
28
+ template 'spec/fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
29
+ end
30
+
31
+ def create_migration
32
+ unless options[:skip_migration]
33
+ migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def model_path
40
+ class_name.underscore
41
+ end
42
+
43
+ def table_name
44
+ plural_name.gsub('/', '_')
45
+ end
46
+
47
+ def singular_name
48
+ model_name.underscore
49
+ end
50
+
51
+ def plural_name
52
+ model_name.underscore.pluralize
53
+ end
54
+
55
+ def class_name
56
+ singular_name.camelize
57
+ end
58
+
59
+ def plural_class_name
60
+ plural_name.camelize
61
+ end
62
+
63
+ def self.next_migration_number(dirname)
64
+ if ActiveRecord::Base.timestamped_migrations
65
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
66
+ else
67
+ "%.3d" % (current_migration_number(dirname) + 1)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -19,3 +19,5 @@ Options:
19
19
  --skip_migration # Skip the migration file.
20
20
  --skip_timestamps # Skip the timestamps.
21
21
  --skip_presenter # Skip the presenter.
22
+ --skip_helper # Skip the helper.
23
+ --skip_views # Skip the views.
@@ -1,10 +1,6 @@
1
- require 'rails/generators/migration'
2
- require 'rails/generators/generated_attribute'
3
-
4
1
  module Stager
5
2
  class ScaffoldGenerator < Rails::Generators::Base
6
- include Rails::Generators::Migration
7
- no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
3
+ no_tasks { attr_accessor :scaffold_name, :model_attributes }
8
4
 
9
5
  source_root File.expand_path('../templates', __FILE__)
10
6
  argument :scaffold_name, :type => :string, :required => true
@@ -14,265 +10,53 @@ module Stager
14
10
  class_option :skip_migration, :type => :boolean, :default => false
15
11
  class_option :skip_timestamps, :type => :boolean, :default => false
16
12
  class_option :skip_presenter, :type => :boolean, :default => false
13
+ class_option :skip_views, :type => :boolean, :default => false
14
+ class_option :skip_helper, :type => :boolean, :default => false
17
15
 
18
16
  def set_options
19
- @controller_actions = []
20
17
  @model_attributes = []
21
18
  @invert_actions = false
22
19
  @skip_model = false
23
- @namespaced_model = false
24
20
 
25
21
  model_and_controller_args.each do |arg|
26
- if arg == '!'
27
- @invert_actions = true
28
- elsif arg.include?(':')
29
- @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
30
- else
31
- @controller_actions << arg
32
- @controller_actions << 'create' if arg == 'new'
33
- @controller_actions << 'update' if arg == 'edit'
22
+ if arg.include?(':')
23
+ @model_attributes << arg
34
24
  end
35
25
  end
36
26
 
37
- if options[:namespaced_model]
38
- @namespaced_model = true
39
- end
40
-
41
- if @invert_actions || @controller_actions.empty?
42
- @controller_actions = all_actions - @controller_actions
43
- end
44
-
45
27
  if @model_attributes.empty?
46
28
  @skip_model = true
47
- if model_exists?
48
- model_columns_for_attributes.each do |column|
49
- @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
50
- end
51
- else
52
- @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
53
- end
54
29
  end
55
30
  end
56
31
 
57
32
  def create_model
58
33
  unless @skip_model
59
- template 'model.rb', "app/models/#{model_path}.rb"
60
-
61
- template "spec/model.rb", "spec/models/#{model_path}_spec.rb"
62
- template 'spec/fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
63
- end
64
- end
65
-
66
- def create_migration
67
- unless @skip_model || options[:skip_migration]
68
- migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
34
+ invoke "stager:model", [namespaced_scaffold_name?, model_attributes, option?(:skip_migration), option?(:skip_timestamps)]
69
35
  end
70
36
  end
71
37
 
72
38
  def create_controller
73
- template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
74
- template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
75
-
76
- namespaces = plural_name.split('/')
77
- resource = namespaces.pop
78
- if namespaces.present?
79
- route "namespace(:#{namespaces.first}) { resources :#{resource} }"
80
- else
81
- route "resources :#{resource}"
82
- end
83
-
84
- template "spec/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
85
- end
86
-
87
- def create_views
88
- controller_actions.each do |action|
89
- if %w[index show new edit].include?(action)
90
- template "views/#{action}.html.haml", "app/views/#{plural_name}/#{action}.html.haml"
91
- end
92
- end
93
-
94
- if form_partial?
95
- template "views/_form.html.haml", "app/views/#{plural_name}/_form.html.haml"
96
- end
39
+ invoke "stager:controller", [scaffold_name.pluralize, model_and_controller_args, option?(:namespaced_model), option?(:skip_views), option?(:skip_helper)]
97
40
  end
98
41
 
99
42
  def create_presenter
100
43
  unless options[:skip_presenter]
101
- invoke "exhibit:presenter", [scaffold_name, @namespaced_model ? nil : "--no_namespace"]
44
+ invoke "exhibit:presenter", [scaffold_name, options[:namespaced_model] ? nil : "--no_namespace"]
102
45
  end
103
46
  end
104
47
 
105
48
  private
106
49
 
107
- def model_columns_for_attributes
108
- class_name.constantize.columns.reject do |column|
109
- column.name.to_s =~ /^(id|created_at|updated_at)$/
110
- end
111
- end
112
-
113
- def model_exists?
114
- File.exist?(destination_path("app/models/#{singular_name}.rb"))
115
- end
116
-
117
- def model_path
118
- class_name.underscore
119
- end
120
-
121
- def destination_path(path)
122
- File.join(destination_root, path)
123
- end
124
-
125
- def all_actions
126
- %w[index show new create edit update destroy]
127
- end
128
-
129
- def action?(name)
130
- controller_actions.include?(name.to_s)
131
- end
132
-
133
- def actions?(*names)
134
- names.all? { |name| action? name }
135
- end
136
-
137
- def form_partial?
138
- actions?(:new, :edit)
139
- end
140
-
141
- def render_form
142
- if form_partial?
143
- "= render(\"form\")"
50
+ def namespaced_scaffold_name?
51
+ if option[:namespaced_model]
52
+ scaffold_name
144
53
  else
145
- read_template("views/_form.html.haml")
146
- end
147
- end
148
-
149
- def controller_methods(dir_name)
150
- controller_actions.map do |action|
151
- read_template("#{dir_name}/#{action}.rb")
152
- end.join("\n").strip
153
- end
154
-
155
- def before_filter_actions
156
- controller_actions.map { |action| %w(show edit update destroy).include?(action) ? ":#{action}" : nil }.compact.join(", ")
157
- end
158
-
159
- def before_filter_actions?
160
- available = false
161
- %w(show edit update destroy).each do |name|
162
- available = true if action?(name)
54
+ scaffold_name.underscore.split("/").last
163
55
  end
164
- available
165
- end
166
-
167
- def item_resource
168
- scaffold_name.underscore.gsub('/','_')
169
56
  end
170
57
 
171
- def items_path
172
- if action? :index
173
- "#{item_resource.pluralize}_path"
174
- else
175
- "root_path"
176
- end
177
- end
178
-
179
- def item_path(options = {})
180
- name = options[:instance_variable] ? "@#{instance_name}" : instance_name
181
- suffix = options[:full_url] ? "url" : "path"
182
- if options[:action].to_s == "new"
183
- "new_#{item_resource}_#{suffix}"
184
- elsif options[:action].to_s == "edit"
185
- "edit_#{item_resource}_#{suffix}(#{name})"
186
- else
187
- if scaffold_name.include?('::') && !@namespaced_model
188
- namespace = singular_name.split('/')[0..-2]
189
- "[:#{namespace.join(', :')}, #{name}]"
190
- else
191
- name
192
- end
193
- end
194
- end
195
-
196
- def item_url
197
- if action? :show
198
- item_path(:full_url => true, :instance_variable => true)
199
- else
200
- items_url
201
- end
202
- end
203
-
204
- def items_url
205
- if action? :index
206
- item_resource.pluralize + '_url'
207
- else
208
- "root_url"
209
- end
210
- end
211
-
212
- def item_path_for_spec(suffix = 'path')
213
- if action? :show
214
- "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
215
- else
216
- if suffix == 'path'
217
- items_path
218
- else
219
- items_url
220
- end
221
- end
222
- end
223
-
224
- def table_name
225
- if @namespaced_model
226
- plural_name.gsub('/', '_')
227
- end
228
- end
229
-
230
- def singular_name
231
- scaffold_name.underscore
232
- end
233
-
234
- def plural_name
235
- scaffold_name.underscore.pluralize
236
- end
237
-
238
- def class_name
239
- if @namespaced_model
240
- singular_name.camelize
241
- else
242
- singular_name.split('/').last.camelize
243
- end
244
- end
245
-
246
- def plural_class_name
247
- plural_name.camelize
248
- end
249
-
250
- def instance_name
251
- if @namespaced_model
252
- singular_name.gsub('/', '_')
253
- else
254
- singular_name.split('/').last
255
- end
256
- end
257
-
258
- def instances_name
259
- instance_name.pluralize
260
- end
261
-
262
- def titleized_name
263
- singular_name.split('/').last.titleize
264
- end
265
-
266
- def read_template(relative_path)
267
- ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
268
- end
269
-
270
- def self.next_migration_number(dirname)
271
- if ActiveRecord::Base.timestamped_migrations
272
- Time.now.utc.strftime("%Y%m%d%H%M%S")
273
- else
274
- "%.3d" % (current_migration_number(dirname) + 1)
275
- end
58
+ def option?(value)
59
+ options[value] ? "--#{value}" : nil
276
60
  end
277
61
  end
278
62
  end
@@ -1,3 +1,3 @@
1
1
  module Stager
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stager
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Robin Brouwer
@@ -56,33 +56,37 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
 
58
58
  files:
59
+ - lib/generators/stager/controller/controller_generator.rb
60
+ - lib/generators/stager/controller/templates/actions/create.rb
61
+ - lib/generators/stager/controller/templates/actions/destroy.rb
62
+ - lib/generators/stager/controller/templates/actions/edit.rb
63
+ - lib/generators/stager/controller/templates/actions/index.rb
64
+ - lib/generators/stager/controller/templates/actions/new.rb
65
+ - lib/generators/stager/controller/templates/actions/show.rb
66
+ - lib/generators/stager/controller/templates/actions/update.rb
67
+ - lib/generators/stager/controller/templates/controller.rb
68
+ - lib/generators/stager/controller/templates/helper.rb
69
+ - lib/generators/stager/controller/templates/spec/actions/create.rb
70
+ - lib/generators/stager/controller/templates/spec/actions/destroy.rb
71
+ - lib/generators/stager/controller/templates/spec/actions/edit.rb
72
+ - lib/generators/stager/controller/templates/spec/actions/index.rb
73
+ - lib/generators/stager/controller/templates/spec/actions/new.rb
74
+ - lib/generators/stager/controller/templates/spec/actions/show.rb
75
+ - lib/generators/stager/controller/templates/spec/actions/update.rb
76
+ - lib/generators/stager/controller/templates/spec/controller.rb
77
+ - lib/generators/stager/controller/templates/views/_form.html.haml
78
+ - lib/generators/stager/controller/templates/views/edit.html.haml
79
+ - lib/generators/stager/controller/templates/views/index.html.haml
80
+ - lib/generators/stager/controller/templates/views/new.html.haml
81
+ - lib/generators/stager/controller/templates/views/show.html.haml
82
+ - lib/generators/stager/controller/USAGE
83
+ - lib/generators/stager/model/model_generator.rb
84
+ - lib/generators/stager/model/templates/migration.rb
85
+ - lib/generators/stager/model/templates/model.rb
86
+ - lib/generators/stager/model/templates/spec/fixtures.yml
87
+ - lib/generators/stager/model/templates/spec/model.rb
88
+ - lib/generators/stager/model/USAGE
59
89
  - lib/generators/stager/scaffold/scaffold_generator.rb
60
- - lib/generators/stager/scaffold/templates/actions/create.rb
61
- - lib/generators/stager/scaffold/templates/actions/destroy.rb
62
- - lib/generators/stager/scaffold/templates/actions/edit.rb
63
- - lib/generators/stager/scaffold/templates/actions/index.rb
64
- - lib/generators/stager/scaffold/templates/actions/new.rb
65
- - lib/generators/stager/scaffold/templates/actions/show.rb
66
- - lib/generators/stager/scaffold/templates/actions/update.rb
67
- - lib/generators/stager/scaffold/templates/controller.rb
68
- - lib/generators/stager/scaffold/templates/helper.rb
69
- - lib/generators/stager/scaffold/templates/migration.rb
70
- - lib/generators/stager/scaffold/templates/model.rb
71
- - lib/generators/stager/scaffold/templates/spec/actions/create.rb
72
- - lib/generators/stager/scaffold/templates/spec/actions/destroy.rb
73
- - lib/generators/stager/scaffold/templates/spec/actions/edit.rb
74
- - lib/generators/stager/scaffold/templates/spec/actions/index.rb
75
- - lib/generators/stager/scaffold/templates/spec/actions/new.rb
76
- - lib/generators/stager/scaffold/templates/spec/actions/show.rb
77
- - lib/generators/stager/scaffold/templates/spec/actions/update.rb
78
- - lib/generators/stager/scaffold/templates/spec/controller.rb
79
- - lib/generators/stager/scaffold/templates/spec/fixtures.yml
80
- - lib/generators/stager/scaffold/templates/spec/model.rb
81
- - lib/generators/stager/scaffold/templates/views/_form.html.haml
82
- - lib/generators/stager/scaffold/templates/views/edit.html.haml
83
- - lib/generators/stager/scaffold/templates/views/index.html.haml
84
- - lib/generators/stager/scaffold/templates/views/new.html.haml
85
- - lib/generators/stager/scaffold/templates/views/show.html.haml
86
90
  - lib/generators/stager/scaffold/USAGE
87
91
  - lib/stager/version.rb
88
92
  - lib/stager.rb
@@ -1,11 +0,0 @@
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
@@ -1,6 +0,0 @@
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 %>)
5
- <%= class_name %>.exists?(<%= singular_name %>.id).should be_false
6
- end
@@ -1,4 +0,0 @@
1
- it "edit action should render edit template" do
2
- get :edit, :id => <%= class_name %>.first
3
- response.should render_template(:edit)
4
- end
@@ -1,4 +0,0 @@
1
- it "index action should render index template" do
2
- get :index
3
- response.should render_template(:index)
4
- end
@@ -1,4 +0,0 @@
1
- it "new action should render new template" do
2
- get :new
3
- response.should render_template(:new)
4
- end
@@ -1,4 +0,0 @@
1
- it "show action should render show template" do
2
- get :show, :id => <%= class_name %>.first
3
- response.should render_template(:show)
4
- end
@@ -1,11 +0,0 @@
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