bpokosh-nifty-generators 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGELOG +19 -0
  2. data/LICENSE +20 -0
  3. data/Manifest +57 -0
  4. data/README +73 -0
  5. data/TODO +6 -0
  6. data/lib/nifty_generators.rb +3 -0
  7. data/nifty-generators.gemspec +51 -0
  8. data/rails_generators/nifty_config/USAGE +23 -0
  9. data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
  10. data/rails_generators/nifty_config/templates/config.yml +8 -0
  11. data/rails_generators/nifty_config/templates/load_config.rb +2 -0
  12. data/rails_generators/nifty_layout/USAGE +25 -0
  13. data/rails_generators/nifty_layout/nifty_layout_generator.rb +31 -0
  14. data/rails_generators/nifty_layout/templates/helper.rb +23 -0
  15. data/rails_generators/nifty_scaffold/USAGE +51 -0
  16. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +205 -0
  17. data/rails_generators/nifty_scaffold/templates/action_specs/create.rb +11 -0
  18. data/rails_generators/nifty_scaffold/templates/action_specs/destroy.rb +6 -0
  19. data/rails_generators/nifty_scaffold/templates/action_specs/edit.rb +4 -0
  20. data/rails_generators/nifty_scaffold/templates/action_specs/index.rb +4 -0
  21. data/rails_generators/nifty_scaffold/templates/action_specs/new.rb +4 -0
  22. data/rails_generators/nifty_scaffold/templates/action_specs/show.rb +4 -0
  23. data/rails_generators/nifty_scaffold/templates/action_specs/update.rb +11 -0
  24. data/rails_generators/nifty_scaffold/templates/action_tests/create.rb +11 -0
  25. data/rails_generators/nifty_scaffold/templates/action_tests/destroy.rb +6 -0
  26. data/rails_generators/nifty_scaffold/templates/action_tests/edit.rb +4 -0
  27. data/rails_generators/nifty_scaffold/templates/action_tests/index.rb +4 -0
  28. data/rails_generators/nifty_scaffold/templates/action_tests/new.rb +4 -0
  29. data/rails_generators/nifty_scaffold/templates/action_tests/show.rb +4 -0
  30. data/rails_generators/nifty_scaffold/templates/action_tests/update.rb +11 -0
  31. data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
  32. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
  33. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  34. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  35. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  36. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  37. data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
  38. data/rails_generators/nifty_scaffold/templates/controller.rb +14 -0
  39. data/rails_generators/nifty_scaffold/templates/controller_spec.rb +8 -0
  40. data/rails_generators/nifty_scaffold/templates/controller_test.rb +5 -0
  41. data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
  42. data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
  43. data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
  44. data/rails_generators/nifty_scaffold/templates/model.rb +2 -0
  45. data/rails_generators/nifty_scaffold/templates/model_spec.rb +11 -0
  46. data/rails_generators/nifty_scaffold/templates/model_test.rb +8 -0
  47. data/tasks/deployment.rake +2 -0
  48. data/test/test_helper.rb +117 -0
  49. data/test/test_nifty_config_generator.rb +37 -0
  50. data/test/test_nifty_layout_generator.rb +36 -0
  51. data/test/test_nifty_scaffold_generator.rb +490 -0
  52. metadata +123 -0
@@ -0,0 +1,205 @@
1
+ class NiftyScaffoldGenerator < Rails::Generator::Base
2
+ attr_accessor :name, :attributes, :controller_actions
3
+
4
+ def initialize(runtime_args, runtime_options = {})
5
+ super
6
+ usage if @args.empty?
7
+
8
+
9
+ @name = @args.first
10
+ @controller_actions = []
11
+ @attributes = []
12
+
13
+ @args[1..-1].each do |arg|
14
+ if arg == '!'
15
+ options[:invert] = true
16
+ elsif arg.include? ':'
17
+ @attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
18
+ else
19
+ @controller_actions << arg
20
+ @controller_actions << 'create' if arg == 'new'
21
+ @controller_actions << 'update' if arg == 'edit'
22
+ end
23
+ end
24
+
25
+ @controller_actions.uniq!
26
+ @attributes.uniq!
27
+
28
+ if options[:invert] || @controller_actions.empty?
29
+ @controller_actions = all_actions - @controller_actions
30
+ end
31
+
32
+ if @attributes.empty?
33
+ options[:skip_model] = true # default to skipping model if no attributes passed
34
+ if model_exists?
35
+ model_columns_for_attributes.each do |column|
36
+ @attributes << Rails::Generator::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
37
+ end
38
+ else
39
+ @attributes << Rails::Generator::GeneratedAttribute.new('name', 'string')
40
+ end
41
+ end
42
+ end
43
+
44
+ def manifest
45
+ record do |m|
46
+ unless options[:skip_model]
47
+ m.directory "app/models"
48
+ m.template "model.rb", "app/models/#{singular_name}.rb"
49
+ unless options[:skip_migration]
50
+ m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
51
+ end
52
+
53
+ if spec_dir?
54
+ m.directory "spec/models"
55
+ m.template "model_spec.rb", "spec/models/#{singular_name}_spec.rb"
56
+ m.directory "spec/fixtures"
57
+ m.template "fixtures.yml", "spec/fixtures/#{plural_name}.yml"
58
+ else
59
+ m.directory "test/unit"
60
+ m.template "model_test.rb", "test/unit/#{singular_name}_test.rb"
61
+ m.directory "test/fixtures"
62
+ m.template "fixtures.yml", "test/fixtures/#{plural_name}.yml"
63
+ end
64
+ end
65
+
66
+ unless options[:skip_controller]
67
+ m.directory "app/controllers"
68
+ m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
69
+
70
+ m.directory "app/helpers"
71
+ m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb"
72
+
73
+ m.directory "app/views/#{plural_name}"
74
+ controller_actions.each do |action|
75
+ if File.exist? source_path("views/#{action}.html.haml")
76
+ m.template "views/#{action}.html.haml", "app/views/#{plural_name}/#{action}.html.haml"
77
+ end
78
+ end
79
+
80
+ if form_partial?
81
+ m.template "views/_form.html.haml", "app/views/#{plural_name}/_form.html.haml"
82
+ end
83
+
84
+ m.route_resources plural_name
85
+
86
+ if spec_dir?
87
+ m.directory "spec/controllers"
88
+ m.template "controller_spec.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
89
+ else
90
+ m.directory "test/functional"
91
+ m.template "controller_test.rb", "test/functional/#{plural_name}_controller_test.rb"
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ def form_partial?
98
+ actions? :new, :edit
99
+ end
100
+
101
+ def all_actions
102
+ %w[index show new create edit update destroy]
103
+ end
104
+
105
+ def action?(name)
106
+ controller_actions.include? name.to_s
107
+ end
108
+
109
+ def actions?(*names)
110
+ names.all? { |n| action? n.to_s }
111
+ end
112
+
113
+ def singular_name
114
+ name.underscore
115
+ end
116
+
117
+ def plural_name
118
+ name.underscore.pluralize
119
+ end
120
+
121
+ def class_name
122
+ name.camelize
123
+ end
124
+
125
+ def plural_class_name
126
+ plural_name.camelize
127
+ end
128
+
129
+ def controller_methods(dir_name)
130
+ controller_actions.map do |action|
131
+ read_template("#{dir_name}/#{action}.rb")
132
+ end.join(" \n").strip
133
+ end
134
+
135
+ def render_form
136
+ if form_partial?
137
+ "= render :partial => 'form'"
138
+ else
139
+ read_template("views/_form.html.haml")
140
+ end
141
+ end
142
+
143
+ def item_path(suffix = 'path')
144
+ if action? :show
145
+ "@#{singular_name}"
146
+ else
147
+ "#{plural_name}_#{suffix}"
148
+ end
149
+ end
150
+
151
+ def item_path_for_spec(suffix = 'path')
152
+ if action? :show
153
+ "#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
154
+ else
155
+ "#{plural_name}_#{suffix}"
156
+ end
157
+ end
158
+
159
+ def item_path_for_test(suffix = 'path')
160
+ if action? :show
161
+ "#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
162
+ else
163
+ "#{plural_name}_#{suffix}"
164
+ end
165
+ end
166
+
167
+ def model_columns_for_attributes
168
+ class_name.constantize.columns.reject do |column|
169
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
170
+ end
171
+ end
172
+
173
+ def spec_dir?
174
+ File.exist? destination_path("spec")
175
+ end
176
+
177
+ protected
178
+
179
+ def add_options!(opt)
180
+ opt.separator ''
181
+ opt.separator 'Options:'
182
+ opt.on("--skip-model", "Don't generate a model or migration file.") { |v| options[:skip_model] = v }
183
+ opt.on("--skip-migration", "Don't generate migration file for model.") { |v| options[:skip_migration] = v }
184
+ opt.on("--skip-timestamps", "Don't add timestamps to migration file.") { |v| options[:skip_timestamps] = v }
185
+ opt.on("--skip-controller", "Don't generate controller, helper, or views.") { |v| options[:skip_controller] = v }
186
+ opt.on("--invert", "Generate all controller actions except these mentioned.") { |v| options[:invert] = v }
187
+ end
188
+
189
+ # is there a better way to do this? Perhaps with const_defined?
190
+ def model_exists?
191
+ File.exist? destination_path("app/models/#{singular_name}.rb")
192
+ end
193
+
194
+ def read_template(relative_path)
195
+ ERB.new(File.read(source_path(relative_path)), nil, '-').result(binding)
196
+ end
197
+
198
+ def banner
199
+ <<-EOS
200
+ Creates a controller and optional model given the name, actions, and attributes.
201
+
202
+ USAGE: #{$0} #{spec.name} ModelName [controller_actions and model:attributes] [options]
203
+ EOS
204
+ end
205
+ 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 to index action when model is valid" do
8
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
9
+ post :create
10
+ response.should redirect_to(<%= item_path_for_spec('url') %>)
11
+ end
@@ -0,0 +1,6 @@
1
+ it "destroy action should destroy model and redirect to index action" do
2
+ <%= singular_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= singular_name %>
4
+ response.should redirect_to(<%= plural_name %>_url)
5
+ <%= class_name %>.exists?(<%= singular_name %>.id).should be_false
6
+ end
@@ -0,0 +1,4 @@
1
+ it "edit action should render edit template" do
2
+ get :edit, :id => <%= class_name %>.first
3
+ response.should render_template(:edit)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "index action should render index template" do
2
+ get :index
3
+ response.should render_template(:index)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "new action should render new template" do
2
+ get :new
3
+ response.should render_template(:new)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "show action should render show template" do
2
+ get :show, :id => <%= class_name %>.first
3
+ response.should render_template(:show)
4
+ end
@@ -0,0 +1,11 @@
1
+ it "update action should render edit template when model is invalid" do
2
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
3
+ put :update, :id => <%= class_name %>.first
4
+ response.should render_template(:edit)
5
+ end
6
+
7
+ it "update action should redirect to show action 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,11 @@
1
+ def test_create_invalid
2
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
3
+ post :create
4
+ assert_template 'new'
5
+ end
6
+
7
+ def test_create_valid
8
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
9
+ post :create
10
+ assert_redirected_to <%= item_path_for_test('url') %>
11
+ end
@@ -0,0 +1,6 @@
1
+ def test_destroy
2
+ <%= singular_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= singular_name %>
4
+ assert_redirected_to <%= plural_name %>_url
5
+ assert !<%= class_name %>.exists?(<%= singular_name %>.id)
6
+ end
@@ -0,0 +1,4 @@
1
+ def test_edit
2
+ get :edit, :id => <%= class_name %>.first
3
+ assert_template 'edit'
4
+ end
@@ -0,0 +1,4 @@
1
+ def test_index
2
+ get :index
3
+ assert_template 'index'
4
+ end
@@ -0,0 +1,4 @@
1
+ def test_new
2
+ get :new
3
+ assert_template 'new'
4
+ end
@@ -0,0 +1,4 @@
1
+ def test_show
2
+ get :show, :id => <%= class_name %>.first
3
+ assert_template 'show'
4
+ end
@@ -0,0 +1,11 @@
1
+ def test_update_invalid
2
+ <%= class_name %>.any_instance.stubs(:valid?).returns(false)
3
+ put :update, :id => <%= class_name %>.first
4
+ assert_template 'edit'
5
+ end
6
+
7
+ def test_update_valid
8
+ <%= class_name %>.any_instance.stubs(:valid?).returns(true)
9
+ put :update, :id => <%= class_name %>.first
10
+ assert_redirected_to <%= item_path_for_test('url') %>
11
+ end
@@ -0,0 +1,9 @@
1
+ def create
2
+ @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
3
+ if @<%= singular_name %>.save
4
+ flash[:notice] = "Successfully created <%= name.humanize.downcase %>."
5
+ redirect_to <%= item_path('url') %>
6
+ else
7
+ render :action => 'new'
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ def destroy
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= singular_name %>.destroy
4
+ flash[:notice] = "Successfully destroyed <%= name.humanize.downcase %>."
5
+ redirect_to <%= plural_name %>_url
6
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= plural_name %> = <%= class_name %>.find(:all)
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= singular_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,9 @@
1
+ def update
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
4
+ flash[:notice] = "Successfully updated <%= name.humanize.downcase %>."
5
+ redirect_to <%= item_path('url') %>
6
+ else
7
+ render :action => 'edit'
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ make_resourceful do
3
+ actions :all
4
+ end
5
+
6
+ def current_objects
7
+ @current_objects = current_model.paginate(:all,
8
+ :order => ‘orders.created_at’,
9
+ :page => params[:page],
10
+ :conditions => search_conditions,
11
+ :joins => :user)
12
+ end
13
+
14
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ <%= controller_methods :action_specs %>
8
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class <%= plural_class_name %>ControllerTest < ActionController::TestCase
4
+ <%= controller_methods :action_tests %>
5
+ end
@@ -0,0 +1,9 @@
1
+ one:
2
+ <%- for attribute in attributes -%>
3
+ <%= attribute.name %>: <%= attribute.default %>
4
+ <%- end -%>
5
+
6
+ two:
7
+ <%- for attribute in attributes -%>
8
+ <%= attribute.name %>: <%= attribute.default %>
9
+ <%- end -%>
@@ -0,0 +1,2 @@
1
+ module <%= plural_class_name %>Helper
2
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= plural_name %> do |t|
4
+ <%- for attribute in attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %>
6
+ <%- end -%>
7
+ <%- unless options[:skip_timestamps] -%>
8
+ t.timestamps
9
+ <%- end -%>
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :<%= plural_name %>
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ end