dust-generators 0.0.1.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/Gemfile +13 -0
  2. data/Gemfile.lock +38 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.rdoc +19 -0
  5. data/Rakefile +54 -0
  6. data/lib/dust/version.rb +10 -0
  7. data/lib/generators/dust/config/USAGE +23 -0
  8. data/lib/generators/dust/config/config_generator.rb +24 -0
  9. data/lib/generators/dust/config/templates/config.yml +8 -0
  10. data/lib/generators/dust/config/templates/load_config.rb +2 -0
  11. data/lib/generators/dust/scaffold/USAGE +51 -0
  12. data/lib/generators/dust/scaffold/scaffold_generator.rb +249 -0
  13. data/lib/generators/dust/scaffold/templates/actions/create.rb +9 -0
  14. data/lib/generators/dust/scaffold/templates/actions/destroy.rb +6 -0
  15. data/lib/generators/dust/scaffold/templates/actions/edit.rb +3 -0
  16. data/lib/generators/dust/scaffold/templates/actions/index.rb +3 -0
  17. data/lib/generators/dust/scaffold/templates/actions/new.rb +3 -0
  18. data/lib/generators/dust/scaffold/templates/actions/show.rb +3 -0
  19. data/lib/generators/dust/scaffold/templates/actions/update.rb +9 -0
  20. data/lib/generators/dust/scaffold/templates/controller.rb +3 -0
  21. data/lib/generators/dust/scaffold/templates/fixtures.yml +9 -0
  22. data/lib/generators/dust/scaffold/templates/helper.rb +2 -0
  23. data/lib/generators/dust/scaffold/templates/migration.rb +16 -0
  24. data/lib/generators/dust/scaffold/templates/model.rb +15 -0
  25. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  26. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  27. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  28. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  29. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  30. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  31. data/lib/generators/dust/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  32. data/lib/generators/dust/scaffold/templates/tests/rspec/controller.rb +8 -0
  33. data/lib/generators/dust/scaffold/templates/tests/rspec/model.rb +7 -0
  34. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  35. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  36. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  37. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  38. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  39. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  40. data/lib/generators/dust/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  41. data/lib/generators/dust/scaffold/templates/tests/shoulda/controller.rb +5 -0
  42. data/lib/generators/dust/scaffold/templates/tests/shoulda/model.rb +7 -0
  43. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  44. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  45. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  46. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  47. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  48. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  49. data/lib/generators/dust/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  50. data/lib/generators/dust/scaffold/templates/tests/testunit/controller.rb +5 -0
  51. data/lib/generators/dust/scaffold/templates/tests/testunit/model.rb +7 -0
  52. data/lib/generators/dust/scaffold/templates/views/erb/_form.html.erb +16 -0
  53. data/lib/generators/dust/scaffold/templates/views/erb/_search.html.erb +6 -0
  54. data/lib/generators/dust/scaffold/templates/views/erb/edit.html.erb +4 -0
  55. data/lib/generators/dust/scaffold/templates/views/erb/index.html.erb +24 -0
  56. data/lib/generators/dust/scaffold/templates/views/erb/new.html.erb +5 -0
  57. data/lib/generators/dust/scaffold/templates/views/erb/show.html.erb +22 -0
  58. data/lib/generators/dust/scaffold/templates/views/haml/_form.html.haml +10 -0
  59. data/lib/generators/dust/scaffold/templates/views/haml/edit.html.haml +14 -0
  60. data/lib/generators/dust/scaffold/templates/views/haml/index.html.haml +25 -0
  61. data/lib/generators/dust/scaffold/templates/views/haml/new.html.haml +7 -0
  62. data/lib/generators/dust/scaffold/templates/views/haml/show.html.haml +20 -0
  63. data/lib/generators/dust.rb +15 -0
  64. data/test/helper.rb +18 -0
  65. data/test/test_dust-generators.rb +7 -0
  66. metadata +202 -0
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'paperclip'
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.4)
5
+ activesupport (= 3.0.4)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activerecord (3.0.4)
9
+ activemodel (= 3.0.4)
10
+ activesupport (= 3.0.4)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.4)
14
+ arel (2.0.8)
15
+ builder (2.1.2)
16
+ git (1.2.5)
17
+ i18n (0.5.0)
18
+ jeweler (1.5.2)
19
+ bundler (~> 1.0.0)
20
+ git (>= 1.2.5)
21
+ rake
22
+ paperclip (2.3.8)
23
+ activerecord
24
+ activesupport
25
+ rake (0.8.7)
26
+ rcov (0.9.9)
27
+ shoulda (2.11.3)
28
+ tzinfo (0.3.24)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.0.0)
35
+ jeweler (~> 1.5.2)
36
+ paperclip
37
+ rcov
38
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 rossnelson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = dust-generators
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to dust-generators
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 rossnelson. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require './lib/dust/version.rb'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |gem|
17
+ gem.name = "dust-generators"
18
+ gem.version = Dust::Version::STRING
19
+ gem.homepage = "http://github.com/rossnelson/dust-generators"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Generators for DustCMS}
22
+ gem.description = %Q{Generators such as Uploadify Albums and Photos, and Location or facilities}
23
+ gem.email = "axcess1@me.com"
24
+ gem.authors = ["rossnelson"]
25
+ gem.files = FileList["[A-Z]*", "{lib}/**/*"]
26
+ end
27
+
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "dust-generators #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
@@ -0,0 +1,10 @@
1
+ class Dust
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+ BUILD = 'pre2'
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ Description:
2
+ The dust_config generator creates YAML file in your config
3
+ directory and an initializer to load this config. The config has a
4
+ separate section for each environment. This is a great place to put
5
+ any config settings you don't want in your app.
6
+
7
+ The config is loaded into a constant called APP_CONFIG by default,
8
+ this changes depending on the name you choose to pass the generator.
9
+ Use this constant to access the config settings like this.
10
+
11
+ APP_CONFIG[:some_setting]
12
+
13
+
14
+ Examples:
15
+ rails generate dust:config
16
+
17
+ Config: config/app_config.yml
18
+ Initializer: config/initializers/load_app_config.rb
19
+
20
+ rails generate dust:config passwords
21
+
22
+ Config: config/passwords_config.yml
23
+ Initializer: config/initializers/load_passwords_config.rb
@@ -0,0 +1,24 @@
1
+ require 'generators/dust'
2
+
3
+ module Dust
4
+ module Generators
5
+ class ConfigGenerator < Base
6
+ argument :config_name, :type => :string, :default => 'app', :banner => 'config_name'
7
+
8
+ def create_config
9
+ template "load_config.rb", "config/initializers/load_#{file_name}_config.rb"
10
+ copy_file "config.yml", "config/#{file_name}_config.yml"
11
+ end
12
+
13
+ private
14
+
15
+ def file_name
16
+ config_name.underscore
17
+ end
18
+
19
+ def constant_name
20
+ config_name.underscore.upcase
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ development:
2
+ domain: localhost:3000
3
+
4
+ test:
5
+ domain: test.host
6
+
7
+ production:
8
+ domain: example.com
@@ -0,0 +1,2 @@
1
+ raw_config = File.read("#{Rails.root}/config/<%= file_name %>_config.yml")
2
+ <%= constant_name %>_CONFIG = YAML.load(raw_config)[Rails.env].symbolize_keys
@@ -0,0 +1,51 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model and migration to controller and
3
+ views. The resource is ready to use as a starting point for your restful,
4
+ resource-oriented application. Tests or specs are also generated depending
5
+ on if you have a "spec" directory or not.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the dust_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, `dust_scaffold post name:string content:text hidden:boolean`
25
+ gives you a model with those three attributes, a controller that handles
26
+ the create/show/update/destroy, forms to create and edit your posts, and
27
+ an index that lists them all, as well as a map.resources :posts
28
+ declaration in config/routes.rb.
29
+
30
+ Adding an "!" in the mix of arguments will invert the passed controller
31
+ actions. This will include all 7 controller actitons except the ones
32
+ mentioned. This option doesn't affect model attributes.
33
+
34
+ Examples:
35
+ rails generate dust:scaffold post
36
+
37
+ Will create a controller called "posts" it will contain all seven
38
+ CRUD actions along with the views. A model will NOT be created,
39
+ instead it will look for an existing model and use those attributes.
40
+
41
+ rails generate dust:scaffold post name:string content:text index new edit
42
+
43
+ Will create a Post model and migration file with the name and content
44
+ attributes. It will also create a controller with index, new, create,
45
+ edit, and update actions. Notice the create and update actions are
46
+ added automatically with new and edit.
47
+
48
+ rails generate dust:scaffold post ! show new
49
+
50
+ Creates a posts controller (no model) with index, edit, update, and
51
+ destroy actions.
@@ -0,0 +1,249 @@
1
+ require 'generators/dust'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/generated_attribute'
4
+
5
+ module Dust
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
+ @skip_model = options.skip_model?
31
+ @invert_actions = options.invert?
32
+
33
+ args_for_c_m.each do |arg|
34
+ if arg == '!'
35
+ @invert_actions = true
36
+ elsif arg.include?(':')
37
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
38
+ else
39
+ @controller_actions << arg
40
+ @controller_actions << 'create' if arg == 'new'
41
+ @controller_actions << 'update' if arg == 'edit'
42
+ end
43
+ end
44
+
45
+ @controller_actions.uniq!
46
+ @model_attributes.uniq!
47
+
48
+ if @invert_actions || @controller_actions.empty?
49
+ @controller_actions = all_actions - @controller_actions
50
+ end
51
+
52
+ if @model_attributes.empty?
53
+ @skip_model = true # skip model if no attributes
54
+ if model_exists?
55
+ model_columns_for_attributes.each do |column|
56
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
57
+ end
58
+ else
59
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
60
+ end
61
+ end
62
+ end
63
+
64
+ def add_gems
65
+ gem "mocha", :group => :test unless File.read(destination_path("Gemfile")).include? "mocha"
66
+ end
67
+
68
+ def create_model
69
+ unless @skip_model
70
+ template 'model.rb', "app/models/#{singular_name}.rb"
71
+ if test_framework == :rspec
72
+ template "tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
73
+ template 'fixtures.yml', "spec/fixtures/#{plural_name}.yml"
74
+ else
75
+ template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
76
+ template 'fixtures.yml', "test/fixtures/#{plural_name}.yml"
77
+ end
78
+ end
79
+ end
80
+
81
+ def create_migration
82
+ unless @skip_model || options.skip_migration?
83
+ migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
84
+ end
85
+ end
86
+
87
+ def create_controller
88
+ unless options.skip_controller?
89
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
90
+
91
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
92
+
93
+ controller_actions.each do |action|
94
+ if %w[index show new edit].include?(action) # Actions with templates
95
+ template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
96
+ end
97
+ end
98
+
99
+ template "views/#{view_language}/_search.html.erb", "app/views/#{plural_name}/_search.html.erb"
100
+
101
+ if form_partial?
102
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
103
+ end
104
+
105
+ route "resources #{plural_name.to_sym.inspect}"
106
+
107
+ if test_framework == :rspec
108
+ template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
109
+ else
110
+ template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
111
+ end
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def form_partial?
118
+ actions? :new, :edit
119
+ end
120
+
121
+ def all_actions
122
+ %w[index show new create edit update destroy]
123
+ end
124
+
125
+ def action?(name)
126
+ controller_actions.include? name.to_s
127
+ end
128
+
129
+ def actions?(*names)
130
+ names.all? { |name| action? name }
131
+ end
132
+
133
+ def singular_name
134
+ model_name.underscore
135
+ end
136
+
137
+ def plural_name
138
+ model_name.underscore.pluralize
139
+ end
140
+
141
+ def class_name
142
+ model_name.camelize
143
+ end
144
+
145
+ def plural_class_name
146
+ plural_name.camelize
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 render_form
156
+ if form_partial?
157
+ if options.haml?
158
+ "= render 'form'"
159
+ else
160
+ "<%= render 'form' %>"
161
+ end
162
+ else
163
+ read_template("views/#{view_language}/_form.html.#{view_language}")
164
+ end
165
+ end
166
+
167
+ def items_path(suffix = 'path')
168
+ if action? :index
169
+ "#{plural_name}_#{suffix}"
170
+ else
171
+ "root_#{suffix}"
172
+ end
173
+ end
174
+
175
+ def item_path(suffix = 'path')
176
+ if action? :show
177
+ "@#{singular_name}"
178
+ else
179
+ items_path(suffix)
180
+ end
181
+ end
182
+
183
+ def item_path_for_spec(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 item_path_for_test(suffix = 'path')
192
+ if action? :show
193
+ "#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
194
+ else
195
+ items_path(suffix)
196
+ end
197
+ end
198
+
199
+ def model_columns_for_attributes
200
+ class_name.constantize.columns.reject do |column|
201
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
202
+ end
203
+ end
204
+
205
+ def view_language
206
+ options.haml? ? 'haml' : 'erb'
207
+ end
208
+
209
+ def test_framework
210
+ return @test_framework if defined?(@test_framework)
211
+ if options.testunit?
212
+ return @test_framework = :testunit
213
+ elsif options.rspec?
214
+ return @test_framework = :rspec
215
+ elsif options.shoulda?
216
+ return @test_framework = :shoulda
217
+ else
218
+ return @test_framework = default_test_framework
219
+ end
220
+ end
221
+
222
+ def default_test_framework
223
+ File.exist?(destination_path("spec")) ? :rspec : :testunit
224
+ end
225
+
226
+ def model_exists?
227
+ File.exist? destination_path("app/models/#{singular_name}.rb")
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
+
234
+ def destination_path(path)
235
+ File.join(destination_root, path)
236
+ end
237
+
238
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
239
+ # Implement the required interface for Rails::Generators::Migration.
240
+ def self.next_migration_number(dirname) #:nodoc:
241
+ if ActiveRecord::Base.timestamped_migrations
242
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
243
+ else
244
+ "%.3d" % (current_migration_number(dirname) + 1)
245
+ end
246
+ end
247
+ end
248
+ end
249
+ 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,6 @@
1
+ def destroy
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= singular_name %>.destroy
4
+ flash[:notice] = "Successfully destroyed <%= model_name.underscore.humanize.downcase %>."
5
+ redirect_to <%= items_path('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 %>.page(params[:search], params[:page])
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 <%= model_name.underscore.humanize.downcase %>."
5
+ redirect_to <%= item_path('url') %>
6
+ else
7
+ render :action => 'edit'
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ <%= controller_methods :actions %>
3
+ end
@@ -0,0 +1,9 @@
1
+ one:
2
+ <%- for attribute in model_attributes -%>
3
+ <%= attribute.name %>: <%= attribute.default %>
4
+ <%- end -%>
5
+
6
+ two:
7
+ <%- for attribute in model_attributes -%>
8
+ <%= attribute.name %>: <%= attribute.default %>
9
+ <%- end -%>
@@ -0,0 +1,2 @@
1
+ module <%= plural_class_name %>Helper
2
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= 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,15 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ attr_accessible <%= model_attributes.map { |a| ":#{a.name}" }.join(", ") %>
3
+
4
+ def self.page(search, page)
5
+ with_permissions_to(:manage).search(search).order("<%= model_attributes.first %>").paginate(:per_page => 12, :page => page)
6
+ end
7
+
8
+ def self.search(search)
9
+ if search
10
+ where("<%= model_attributes.first %> LIKE ?", "%#{search}%")
11
+ else
12
+ scoped
13
+ end
14
+ end
15
+ 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,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