corntrace-nifty-generators 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +128 -0
- data/LICENSE +20 -0
- data/README.rdoc +104 -0
- data/Rakefile +18 -0
- data/features/nifty_authentication.feature +59 -0
- data/features/nifty_config.feature +17 -0
- data/features/nifty_layout.feature +19 -0
- data/features/nifty_scaffold.feature +22 -0
- data/features/step_definitions/common_steps.rb +37 -0
- data/features/step_definitions/rails_setup_steps.rb +6 -0
- data/features/support/env.rb +6 -0
- data/features/support/matchers.rb +7 -0
- data/lib/generators/nifty/authentication/USAGE +50 -0
- data/lib/generators/nifty/authentication/authentication_generator.rb +145 -0
- data/lib/generators/nifty/authentication/templates/authentication.rb +60 -0
- data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
- data/lib/generators/nifty/authentication/templates/fixtures.yml +24 -0
- data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
- data/lib/generators/nifty/authentication/templates/sessions_controller.rb +45 -0
- data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +26 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +27 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +23 -0
- data/lib/generators/nifty/authentication/templates/user.rb +42 -0
- data/lib/generators/nifty/authentication/templates/users_controller.rb +18 -0
- data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/views/erb/login.html.erb +30 -0
- data/lib/generators/nifty/authentication/templates/views/erb/signup.html.erb +24 -0
- data/lib/generators/nifty/authentication/templates/views/haml/login.html.haml +30 -0
- data/lib/generators/nifty/authentication/templates/views/haml/signup.html.haml +24 -0
- data/lib/generators/nifty/config/USAGE +23 -0
- data/lib/generators/nifty/config/config_generator.rb +24 -0
- data/lib/generators/nifty/config/templates/config.yml +8 -0
- data/lib/generators/nifty/config/templates/load_config.rb +2 -0
- data/lib/generators/nifty/layout/USAGE +25 -0
- data/lib/generators/nifty/layout/layout_generator.rb +29 -0
- data/lib/generators/nifty/layout/templates/error_messages_helper.rb +23 -0
- data/lib/generators/nifty/layout/templates/layout.html.erb +19 -0
- data/lib/generators/nifty/layout/templates/layout.html.haml +21 -0
- data/lib/generators/nifty/layout/templates/layout_helper.rb +22 -0
- data/lib/generators/nifty/layout/templates/stylesheet.css +75 -0
- data/lib/generators/nifty/layout/templates/stylesheet.sass +66 -0
- data/lib/generators/nifty/scaffold/USAGE +51 -0
- data/lib/generators/nifty/scaffold/scaffold_generator.rb +241 -0
- data/lib/generators/nifty/scaffold/templates/actions/create.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/update.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/fixtures.yml +9 -0
- data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
- data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
- data/lib/generators/nifty/scaffold/templates/model.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/_form.html.erb +10 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/edit.html.erb +14 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/index.html.erb +29 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/new.html.erb +7 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/show.html.erb +20 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/_form.html.haml +10 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/edit.html.haml +14 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/index.html.haml +25 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/new.html.haml +7 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/show.html.haml +20 -0
- data/lib/generators/nifty.rb +15 -0
- data/rails_generators/nifty_authentication/USAGE +50 -0
- data/rails_generators/nifty_authentication/lib/insert_commands.rb +74 -0
- data/rails_generators/nifty_authentication/nifty_authentication_generator.rb +128 -0
- data/rails_generators/nifty_authentication/templates/authentication.rb +61 -0
- data/rails_generators/nifty_authentication/templates/authlogic_session.rb +2 -0
- data/rails_generators/nifty_authentication/templates/fixtures.yml +24 -0
- data/rails_generators/nifty_authentication/templates/migration.rb +20 -0
- data/rails_generators/nifty_authentication/templates/sessions_controller.rb +45 -0
- data/rails_generators/nifty_authentication/templates/sessions_helper.rb +2 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb +39 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/user.rb +83 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb +26 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/user.rb +85 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb +27 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb +36 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/user.rb +88 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb +23 -0
- data/rails_generators/nifty_authentication/templates/user.rb +42 -0
- data/rails_generators/nifty_authentication/templates/users_controller.rb +18 -0
- data/rails_generators/nifty_authentication/templates/users_helper.rb +2 -0
- data/rails_generators/nifty_authentication/templates/views/erb/login.html.erb +30 -0
- data/rails_generators/nifty_authentication/templates/views/erb/signup.html.erb +24 -0
- data/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +30 -0
- data/rails_generators/nifty_authentication/templates/views/haml/signup.html.haml +24 -0
- data/rails_generators/nifty_config/USAGE +23 -0
- data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
- data/rails_generators/nifty_config/templates/config.yml +8 -0
- data/rails_generators/nifty_config/templates/load_config.rb +2 -0
- data/rails_generators/nifty_layout/USAGE +25 -0
- data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
- data/rails_generators/nifty_layout/templates/helper.rb +22 -0
- data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
- data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
- data/rails_generators/nifty_layout/templates/stylesheet.css +81 -0
- data/rails_generators/nifty_layout/templates/stylesheet.sass +67 -0
- data/rails_generators/nifty_scaffold/USAGE +51 -0
- data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +232 -0
- data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
- data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
- data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
- data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
- data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
- data/rails_generators/nifty_scaffold/templates/model.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +10 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +14 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +25 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +7 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +20 -0
- data/test/test_helper.rb +119 -0
- data/test/test_nifty_authentication_generator.rb +274 -0
- data/test/test_nifty_config_generator.rb +37 -0
- data/test/test_nifty_layout_generator.rb +42 -0
- data/test/test_nifty_scaffold_generator.rb +534 -0
- metadata +255 -0
@@ -0,0 +1,241 @@
|
|
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 :model_name, :model_attributes, :controller_actions }
|
10
|
+
|
11
|
+
argument :model_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 :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
|
20
|
+
|
21
|
+
class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
|
22
|
+
class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
|
23
|
+
class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
|
24
|
+
|
25
|
+
def initialize(*args, &block)
|
26
|
+
super
|
27
|
+
|
28
|
+
@controller_actions = []
|
29
|
+
@model_attributes = []
|
30
|
+
|
31
|
+
args_for_c_m.each do |arg|
|
32
|
+
if arg == '!'
|
33
|
+
options[:invert] = true
|
34
|
+
elsif arg.include?(':')
|
35
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
|
36
|
+
else
|
37
|
+
@controller_actions << arg
|
38
|
+
@controller_actions << 'create' if arg == 'new'
|
39
|
+
@controller_actions << 'update' if arg == 'edit'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
@controller_actions.uniq!
|
44
|
+
@model_attributes.uniq!
|
45
|
+
|
46
|
+
if options.invert? || @controller_actions.empty?
|
47
|
+
@controller_actions = all_actions - @controller_actions
|
48
|
+
end
|
49
|
+
|
50
|
+
if @model_attributes.empty?
|
51
|
+
options[:skip_model] = true # default to skipping model if no attributes passed
|
52
|
+
if model_exists?
|
53
|
+
model_columns_for_attributes.each do |column|
|
54
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_model
|
63
|
+
unless options.skip_model?
|
64
|
+
template 'model.rb', "app/models/#{singular_name}.rb"
|
65
|
+
if options.rspec?
|
66
|
+
template "tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
|
67
|
+
template 'fixtures.yml', "spec/fixtures/#{plural_name}.yml"
|
68
|
+
else
|
69
|
+
template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
|
70
|
+
template 'fixtures.yml', "test/fixtures/#{plural_name}.yml"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_migration
|
76
|
+
unless options.skip_model? || options.skip_migration?
|
77
|
+
migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def create_controller
|
82
|
+
unless options.skip_controller?
|
83
|
+
template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
|
84
|
+
|
85
|
+
template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
|
86
|
+
|
87
|
+
controller_actions.each do |action|
|
88
|
+
if %w[index show new edit].include?(action) # Actions with templates
|
89
|
+
template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
if form_partial?
|
94
|
+
template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
|
95
|
+
end
|
96
|
+
|
97
|
+
route "resources #{plural_name.to_sym.inspect}"
|
98
|
+
|
99
|
+
if options.rspec?
|
100
|
+
template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
|
101
|
+
else
|
102
|
+
template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def form_partial?
|
110
|
+
actions? :new, :edit
|
111
|
+
end
|
112
|
+
|
113
|
+
def all_actions
|
114
|
+
%w[index show new create edit update destroy]
|
115
|
+
end
|
116
|
+
|
117
|
+
def action?(name)
|
118
|
+
controller_actions.include? name.to_s
|
119
|
+
end
|
120
|
+
|
121
|
+
def actions?(*names)
|
122
|
+
names.all? { |name| action? name }
|
123
|
+
end
|
124
|
+
|
125
|
+
def singular_name
|
126
|
+
model_name.underscore
|
127
|
+
end
|
128
|
+
|
129
|
+
def plural_name
|
130
|
+
model_name.underscore.pluralize
|
131
|
+
end
|
132
|
+
|
133
|
+
def class_name
|
134
|
+
model_name.camelize
|
135
|
+
end
|
136
|
+
|
137
|
+
def plural_class_name
|
138
|
+
plural_name.camelize
|
139
|
+
end
|
140
|
+
|
141
|
+
def controller_methods(dir_name)
|
142
|
+
controller_actions.map do |action|
|
143
|
+
read_template("#{dir_name}/#{action}.rb")
|
144
|
+
end.join(" \n").strip
|
145
|
+
end
|
146
|
+
|
147
|
+
def render_form
|
148
|
+
if form_partial?
|
149
|
+
if options.haml?
|
150
|
+
"= render 'form'"
|
151
|
+
else
|
152
|
+
"<%= render 'form' %>"
|
153
|
+
end
|
154
|
+
else
|
155
|
+
read_template("views/#{view_language}/_form.html.#{view_language}")
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def items_path(suffix = 'path')
|
160
|
+
if action? :index
|
161
|
+
"#{plural_name}_#{suffix}"
|
162
|
+
else
|
163
|
+
"root_#{suffix}"
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def item_path(suffix = 'path')
|
168
|
+
if action? :show
|
169
|
+
"@#{singular_name}"
|
170
|
+
else
|
171
|
+
items_path(suffix)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def item_path_for_spec(suffix = 'path')
|
176
|
+
if action? :show
|
177
|
+
"#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
|
178
|
+
else
|
179
|
+
items_path(suffix)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def item_path_for_test(suffix = 'path')
|
184
|
+
if action? :show
|
185
|
+
"#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
|
186
|
+
else
|
187
|
+
items_path(suffix)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def model_columns_for_attributes
|
192
|
+
class_name.constantize.columns.reject do |column|
|
193
|
+
column.name.to_s =~ /^(id|created_at|updated_at)$/
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def view_language
|
198
|
+
options.haml? ? 'haml' : 'erb'
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_framework
|
202
|
+
return @test_framework if defined?(@test_framework)
|
203
|
+
if options.testunit?
|
204
|
+
return @test_framework = :testunit
|
205
|
+
elsif options.rspec?
|
206
|
+
return @test_framework = :rspec
|
207
|
+
elsif options.shoulda?
|
208
|
+
return @test_framework = :shoulda
|
209
|
+
else
|
210
|
+
return @test_framework = default_test_framework
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def default_test_framework
|
215
|
+
File.exist?(destination_path("spec")) ? :rspec : :testunit
|
216
|
+
end
|
217
|
+
|
218
|
+
def model_exists?
|
219
|
+
File.exist? destination_path("app/models/#{singular_name}.rb")
|
220
|
+
end
|
221
|
+
|
222
|
+
def read_template(relative_path)
|
223
|
+
ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
|
224
|
+
end
|
225
|
+
|
226
|
+
def destination_path(path)
|
227
|
+
File.join(destination_root, path)
|
228
|
+
end
|
229
|
+
|
230
|
+
# FIXME: Should be proxied to ActiveRecord::Generators::Base
|
231
|
+
# Implement the required interface for Rails::Generators::Migration.
|
232
|
+
def self.next_migration_number(dirname) #:nodoc:
|
233
|
+
if ActiveRecord::Base.timestamped_migrations
|
234
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
235
|
+
else
|
236
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
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 <%= model_name.underscore.humanize.downcase %>."
|
5
|
+
redirect_to <%= item_path('url') %>
|
6
|
+
else
|
7
|
+
render :action => 'new'
|
8
|
+
end
|
9
|
+
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 <%= model_name.underscore.humanize.downcase %>."
|
5
|
+
redirect_to <%= item_path('url') %>
|
6
|
+
else
|
7
|
+
render :action => 'edit'
|
8
|
+
end
|
9
|
+
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 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 :<%= plural_name %>
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
it "create action should render new template when model is invalid" do
|
2
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
3
|
+
post :create
|
4
|
+
response.should render_template(:new)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "create action should redirect when model is valid" do
|
8
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
9
|
+
post :create
|
10
|
+
response.should redirect_to(<%= item_path_for_spec('url') %>)
|
11
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
it "destroy action should destroy model and redirect to index action" do
|
2
|
+
<%= singular_name %> = <%= class_name %>.first
|
3
|
+
delete :destroy, :id => <%= singular_name %>
|
4
|
+
response.should redirect_to(<%= items_path('url') %>)
|
5
|
+
<%= class_name %>.exists?(<%= singular_name %>.id).should be_false
|
6
|
+
end
|
@@ -0,0 +1,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,13 @@
|
|
1
|
+
context "create action" do
|
2
|
+
should "render new template when model is invalid" do
|
3
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
4
|
+
post :create
|
5
|
+
assert_template 'new'
|
6
|
+
end
|
7
|
+
|
8
|
+
should "redirect when model is valid" do
|
9
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
10
|
+
post :create
|
11
|
+
assert_redirected_to <%= item_path_for_test('url') %>
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
context "destroy action" do
|
2
|
+
should "destroy model and redirect to index action" do
|
3
|
+
<%= singular_name %> = <%= class_name %>.first
|
4
|
+
delete :destroy, :id => <%= singular_name %>
|
5
|
+
assert_redirected_to <%= items_path('url') %>
|
6
|
+
assert !<%= class_name %>.exists?(<%= singular_name %>.id)
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
context "update action" do
|
2
|
+
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
|
+
assert_template 'edit'
|
6
|
+
end
|
7
|
+
|
8
|
+
should "redirect when model is valid" do
|
9
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
10
|
+
put :update, :id => <%= class_name %>.first
|
11
|
+
assert_redirected_to <%= item_path_for_test('url') %>
|
12
|
+
end
|
13
|
+
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,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,10 @@
|
|
1
|
+
<%%= form_for @<%= singular_name %> do |f| %>
|
2
|
+
<%%= f.error_messages %>
|
3
|
+
<%- for attribute in model_attributes -%>
|
4
|
+
<p>
|
5
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
</p>
|
8
|
+
<%- end -%>
|
9
|
+
<p><%%= f.submit %></p>
|
10
|
+
<%% end %>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<%% title "Edit <%= singular_name.titleize %>" %>
|
2
|
+
|
3
|
+
<%= render_form %>
|
4
|
+
|
5
|
+
<%- if actions? :show, :index -%>
|
6
|
+
<p>
|
7
|
+
<%- if action? :show -%>
|
8
|
+
<%%= link_to "Show", @<%= singular_name %> %> |
|
9
|
+
<%- end -%>
|
10
|
+
<%- if action? :index -%>
|
11
|
+
<%%= link_to "View All", <%= plural_name %>_path %>
|
12
|
+
<%- end -%>
|
13
|
+
</p>
|
14
|
+
<%- end -%>
|
@@ -0,0 +1,29 @@
|
|
1
|
+
<%% title "<%= plural_name.titleize %>" %>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<%- for attribute in model_attributes -%>
|
6
|
+
<th><%= attribute.human_name.titleize %></th>
|
7
|
+
<%- end -%>
|
8
|
+
</tr>
|
9
|
+
<%% for <%= singular_name %> in @<%= plural_name %> %>
|
10
|
+
<tr>
|
11
|
+
<%- for attribute in model_attributes -%>
|
12
|
+
<td><%%= <%= singular_name %>.<%= attribute.name %> %></td>
|
13
|
+
<%- end -%>
|
14
|
+
<%- if action? :show -%>
|
15
|
+
<td><%%= link_to "Show", <%= singular_name %> %></td>
|
16
|
+
<%- end -%>
|
17
|
+
<%- if action? :edit -%>
|
18
|
+
<td><%%= link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
19
|
+
<%- end -%>
|
20
|
+
<%- if action? :destroy -%>
|
21
|
+
<td><%%= link_to "Destroy", <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
22
|
+
<%- end -%>
|
23
|
+
</tr>
|
24
|
+
<%% end %>
|
25
|
+
</table>
|
26
|
+
|
27
|
+
<%- if action? :new -%>
|
28
|
+
<p><%%= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path %></p>
|
29
|
+
<%- end -%>
|