medea-generators 0.1.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 (59) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +77 -0
  4. data/Rakefile +2 -0
  5. data/lib/generators/medea/init/init_generator.rb +42 -0
  6. data/lib/generators/medea/init/templates/database.yml +14 -0
  7. data/lib/generators/medea/scaffold/USAGE +35 -0
  8. data/lib/generators/medea/scaffold/scaffold_generator.rb +297 -0
  9. data/lib/generators/medea/scaffold/templates/actions/create.rb +13 -0
  10. data/lib/generators/medea/scaffold/templates/actions/destroy.rb +6 -0
  11. data/lib/generators/medea/scaffold/templates/actions/edit.rb +3 -0
  12. data/lib/generators/medea/scaffold/templates/actions/index.rb +3 -0
  13. data/lib/generators/medea/scaffold/templates/actions/new.rb +3 -0
  14. data/lib/generators/medea/scaffold/templates/actions/show.rb +3 -0
  15. data/lib/generators/medea/scaffold/templates/actions/update.rb +9 -0
  16. data/lib/generators/medea/scaffold/templates/controller.rb +3 -0
  17. data/lib/generators/medea/scaffold/templates/fixtures.yml +9 -0
  18. data/lib/generators/medea/scaffold/templates/helper.rb +2 -0
  19. data/lib/generators/medea/scaffold/templates/model.rb +3 -0
  20. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  21. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  22. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  23. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  24. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  25. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  26. data/lib/generators/medea/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  27. data/lib/generators/medea/scaffold/templates/tests/rspec/controller.rb +8 -0
  28. data/lib/generators/medea/scaffold/templates/tests/rspec/model.rb +7 -0
  29. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  30. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  31. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  32. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  33. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  34. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  35. data/lib/generators/medea/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  36. data/lib/generators/medea/scaffold/templates/tests/shoulda/controller.rb +5 -0
  37. data/lib/generators/medea/scaffold/templates/tests/shoulda/model.rb +7 -0
  38. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  39. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  40. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  41. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  42. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  43. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  44. data/lib/generators/medea/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  45. data/lib/generators/medea/scaffold/templates/tests/testunit/controller.rb +5 -0
  46. data/lib/generators/medea/scaffold/templates/tests/testunit/model.rb +7 -0
  47. data/lib/generators/medea/scaffold/templates/views/erb/_form.html.erb +10 -0
  48. data/lib/generators/medea/scaffold/templates/views/erb/edit.html.erb +14 -0
  49. data/lib/generators/medea/scaffold/templates/views/erb/index.html.erb +29 -0
  50. data/lib/generators/medea/scaffold/templates/views/erb/new.html.erb +7 -0
  51. data/lib/generators/medea/scaffold/templates/views/erb/show.html.erb +20 -0
  52. data/lib/generators/medea/scaffold/templates/views/haml/_form.html.haml +10 -0
  53. data/lib/generators/medea/scaffold/templates/views/haml/edit.html.haml +14 -0
  54. data/lib/generators/medea/scaffold/templates/views/haml/index.html.haml +25 -0
  55. data/lib/generators/medea/scaffold/templates/views/haml/new.html.haml +7 -0
  56. data/lib/generators/medea/scaffold/templates/views/haml/show.html.haml +20 -0
  57. data/lib/generators/medea.rb +28 -0
  58. data/medea-generators.gemspec +22 -0
  59. metadata +151 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in medea-generators.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ medea-generators (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.3)
11
+ actionpack (= 3.0.3)
12
+ mail (~> 2.2.9)
13
+ actionpack (3.0.3)
14
+ activemodel (= 3.0.3)
15
+ activesupport (= 3.0.3)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.13)
21
+ rack-test (~> 0.5.6)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.3)
24
+ activesupport (= 3.0.3)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4)
27
+ activerecord (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ arel (~> 2.0.2)
31
+ tzinfo (~> 0.3.23)
32
+ activeresource (3.0.3)
33
+ activemodel (= 3.0.3)
34
+ activesupport (= 3.0.3)
35
+ activesupport (3.0.3)
36
+ arel (2.0.7)
37
+ builder (2.1.2)
38
+ erubis (2.6.6)
39
+ abstract (>= 1.0.0)
40
+ i18n (0.5.0)
41
+ mail (2.2.14)
42
+ activesupport (>= 2.3.6)
43
+ i18n (>= 0.4.0)
44
+ mime-types (~> 1.16)
45
+ treetop (~> 1.4.8)
46
+ mime-types (1.16)
47
+ polyglot (0.3.1)
48
+ rack (1.2.1)
49
+ rack-mount (0.6.13)
50
+ rack (>= 1.0.0)
51
+ rack-test (0.5.7)
52
+ rack (>= 1.0)
53
+ rails (3.0.3)
54
+ actionmailer (= 3.0.3)
55
+ actionpack (= 3.0.3)
56
+ activerecord (= 3.0.3)
57
+ activeresource (= 3.0.3)
58
+ activesupport (= 3.0.3)
59
+ bundler (~> 1.0)
60
+ railties (= 3.0.3)
61
+ railties (3.0.3)
62
+ actionpack (= 3.0.3)
63
+ activesupport (= 3.0.3)
64
+ rake (>= 0.8.7)
65
+ thor (~> 0.14.4)
66
+ rake (0.8.7)
67
+ thor (0.14.6)
68
+ treetop (1.4.9)
69
+ polyglot (>= 0.3.1)
70
+ tzinfo (0.3.24)
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ medea-generators!
77
+ rails (~> 3.0.0)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,42 @@
1
+ require 'generators/medea'
2
+ require 'rails/generators/generated_attribute'
3
+ require 'medea'
4
+
5
+ module Medea
6
+ module Generators
7
+ class InitGenerator < Base
8
+ no_tasks {
9
+ attr_accessor :topic, :user, :pass
10
+
11
+ def random_string length = 25
12
+ rand(32**length).to_s(32)
13
+ end
14
+ }
15
+ argument :jason_topic, :type => :string, :required => true, :banner => "Topic"
16
+ argument :username, :type => :string, :required => false, :banner => "username"
17
+ argument :password, :type => :string, :required => false, :banner => "s3cretp@ssword"
18
+
19
+ def initialize(*args, &block)
20
+ super
21
+
22
+ @topic = jason_topic
23
+ @user = username
24
+ @user ||= random_string
25
+ @pass = password
26
+ @pass ||= random_string
27
+ end
28
+
29
+ def init_jasondb
30
+ #we can't create the topic, but we can create database.yml
31
+ template "database.yml", "config/database.yml"
32
+
33
+ # #we will also deploy templates at this point.
34
+ # jason_base = "http://#{@user}:#{@pass}@rest.jason.com/"
35
+ # topics = [@topic, "#{@topic}-dev", "#{@topic}-test"]
36
+ # for t in topics
37
+ # Medea::setup_templates "#{jason_base}#{t}/"
38
+ # end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ development:
2
+ jason_topic: <%= @topic %>-dev
3
+ jason_user: <%= @user %>
4
+ jason_password: <%= @pass %>
5
+
6
+ test:
7
+ jason_topic: <%= @topic %>-test
8
+ jason_user: <%= @user %>
9
+ jason_password: <%= @pass %>
10
+
11
+ production:
12
+ jason_topic: <%= @topic %>
13
+ jason_user: <%= @user %>
14
+ jason_password: <%= @pass %>
@@ -0,0 +1,35 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model 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 nifty_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
+ For example, `medea:scaffold post` gives you a model, a controller that handles
18
+ the create/show/update/destroy, forms to create and edit your posts, and
19
+ an index that lists them all, as well as a map.resources :posts
20
+ declaration in config/routes.rb.
21
+
22
+ Adding an "!" in the mix of arguments will invert the passed controller
23
+ actions. This will include all 7 controller actitons except the ones
24
+ mentioned. This option doesn't affect model attributes.
25
+
26
+ Examples:
27
+ rails generate medea:scaffold post
28
+
29
+ Will create a controller called "posts" it will contain all seven
30
+ CRUD actions along with the views. A model will also be created.
31
+
32
+ rails generate medea:scaffold post ! show new
33
+
34
+ Creates a posts model and controller with index, edit, update, and
35
+ destroy actions.
@@ -0,0 +1,297 @@
1
+ require 'generators/medea'
2
+ require 'rails/generators/generated_attribute'
3
+
4
+ module Medea
5
+ module Generators
6
+ class ScaffoldGenerator < Base
7
+ no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
8
+
9
+ argument :scaffold_name, :type => :string, :required => true, :banner => 'ModelName'
10
+ argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'
11
+
12
+ class_option :skip_model, :desc => 'Don\'t generate a model.', :type => :boolean
13
+ class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
14
+ class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
15
+ class_option :namespace_model, :desc => 'If the resource is namespaced, include the model in the namespace.', :type => :boolean
16
+ class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
17
+
18
+ class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
19
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
20
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
21
+
22
+ def initialize(*args, &block)
23
+ super
24
+
25
+ print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/
26
+
27
+ @controller_actions = []
28
+ @model_attributes = []
29
+ @skip_model = options.skip_model?
30
+ @namespace_model = options.namespace_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
+ if model_exists?
54
+ model_columns_for_attributes.each do |column|
55
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
56
+ end
57
+ else
58
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
59
+ end
60
+ end
61
+ end
62
+
63
+ def add_gems
64
+ add_gem "medea"
65
+ add_gem "mocha", :group => :test
66
+ end
67
+
68
+ def create_model
69
+ template 'model.rb', "app/models/#{model_path}.rb"
70
+ if test_framework == :rspec
71
+ template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
72
+ template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
73
+ else
74
+ template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
75
+ template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
76
+ end
77
+ end
78
+
79
+ def create_controller
80
+ unless options.skip_controller?
81
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
82
+
83
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
84
+
85
+ controller_actions.each do |action|
86
+ if %w[index show new edit].include?(action) # Actions with templates
87
+ template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
88
+ end
89
+ end
90
+
91
+ if form_partial?
92
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
93
+ end
94
+
95
+ namespaces = plural_name.split('/')
96
+ resource = namespaces.pop
97
+ route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
98
+ "namespace(:#{namespace}){ #{acc} }"
99
+ }
100
+
101
+ if test_framework == :rspec
102
+ template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
103
+ else
104
+ template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
105
+ end
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def form_partial?
112
+ actions? :new, :edit
113
+ end
114
+
115
+ def all_actions
116
+ %w[index show new create edit update destroy]
117
+ end
118
+
119
+ def action?(name)
120
+ controller_actions.include? name.to_s
121
+ end
122
+
123
+ def actions?(*names)
124
+ names.all? { |name| action? name }
125
+ end
126
+
127
+ def singular_name
128
+ scaffold_name.underscore
129
+ end
130
+
131
+ def plural_name
132
+ scaffold_name.underscore.pluralize
133
+ end
134
+
135
+ def table_name
136
+ if scaffold_name.include?('::') && @namespace_model
137
+ plural_name.gsub('/', '_')
138
+ end
139
+ end
140
+
141
+ def class_name
142
+ if @namespace_model
143
+ scaffold_name.camelize
144
+ else
145
+ scaffold_name.split('::').last.camelize
146
+ end
147
+ end
148
+
149
+ def model_path
150
+ class_name.underscore
151
+ end
152
+
153
+ def plural_class_name
154
+ plural_name.camelize
155
+ end
156
+
157
+ def instance_name
158
+ if @namespace_model
159
+ singular_name.gsub('/','_')
160
+ else
161
+ singular_name.split('/').last
162
+ end
163
+ end
164
+
165
+ def instances_name
166
+ instance_name.pluralize
167
+ end
168
+
169
+ def controller_methods(dir_name)
170
+ controller_actions.map do |action|
171
+ read_template("#{dir_name}/#{action}.rb")
172
+ end.join("\n").strip
173
+ end
174
+
175
+ def render_form
176
+ if form_partial?
177
+ if options.haml?
178
+ "= render 'form'"
179
+ else
180
+ "<%= render 'form' %>"
181
+ end
182
+ else
183
+ read_template("views/#{view_language}/_form.html.#{view_language}")
184
+ end
185
+ end
186
+
187
+ def item_resource
188
+ scaffold_name.underscore.gsub('/','_')
189
+ end
190
+
191
+ def items_path
192
+ if action? :index
193
+ "#{item_resource.pluralize}_path"
194
+ else
195
+ "root_path"
196
+ end
197
+ end
198
+
199
+ def item_path(options = {})
200
+ if action? :show
201
+ name = options[:instance_variable] ? "@#{instance_name}" : instance_name
202
+ if %w(new edit).include? options[:action].to_s
203
+ "#{options[:action].to_s}_#{item_resource}_path(#{name})"
204
+ else
205
+ if scaffold_name.include?('::') && !@namespace_model
206
+ namespace = singular_name.split('/')[0..-2]
207
+ "[ :#{namespace.join(', :')}, #{name} ]"
208
+ else
209
+ name
210
+ end
211
+ end
212
+ else
213
+ items_path
214
+ end
215
+ end
216
+
217
+ def item_url
218
+ if action? :show
219
+ item_resource + '_url'
220
+ else
221
+ items_url
222
+ end
223
+ end
224
+
225
+ def items_url
226
+ if action? :index
227
+ item_resource.pluralize + '_url'
228
+ else
229
+ "root_url"
230
+ end
231
+ end
232
+
233
+ def item_path_for_spec(suffix = 'path')
234
+ if action? :show
235
+ "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
236
+ else
237
+ if suffix == 'path'
238
+ items_path
239
+ else
240
+ items_url
241
+ end
242
+ end
243
+ end
244
+
245
+ def item_path_for_test(suffix = 'path')
246
+ if action? :show
247
+ "#{item_resource}_#{suffix}(assigns(:#{instance_name}))"
248
+ else
249
+ if suffix == 'path'
250
+ items_path
251
+ else
252
+ items_url
253
+ end
254
+ end
255
+ end
256
+
257
+ def model_columns_for_attributes
258
+ class_name.constantize.columns.reject do |column|
259
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
260
+ end
261
+ end
262
+
263
+ def view_language
264
+ options.haml? ? 'haml' : 'erb'
265
+ end
266
+
267
+ def test_framework
268
+ return @test_framework if defined?(@test_framework)
269
+ if options.testunit?
270
+ return @test_framework = :testunit
271
+ elsif options.rspec?
272
+ return @test_framework = :rspec
273
+ elsif options.shoulda?
274
+ return @test_framework = :shoulda
275
+ else
276
+ return @test_framework = default_test_framework
277
+ end
278
+ end
279
+
280
+ def default_test_framework
281
+ File.exist?(destination_path("spec")) ? :rspec : :testunit
282
+ end
283
+
284
+ def model_exists?
285
+ File.exist? destination_path("app/models/#{singular_name}.rb")
286
+ end
287
+
288
+ def read_template(relative_path)
289
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
290
+ end
291
+
292
+ def destination_path(path)
293
+ File.join(destination_root, path)
294
+ end
295
+ end
296
+ end
297
+ end
@@ -0,0 +1,13 @@
1
+ def create
2
+ @<%= instance_name %> = <%= class_name %>.new(params[:<%= instance_name %>])
3
+ begin
4
+ if @<%= instance_name %>.save!
5
+ flash[:notice] = "Successfully created <%= class_name.underscore.humanize.downcase %>."
6
+ redirect_to <%= item_path :instance_variable => true %>
7
+ else
8
+ render :action => 'new'
9
+ end
10
+ rescue
11
+ render :action => 'new'
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ def destroy
2
+ @<%= instance_name %> = <%= class_name %>.get_by_key(params[:id])
3
+ @<%= instance_name %>.delete!
4
+ flash[:notice] = "Successfully destroyed <%= class_name.underscore.humanize.downcase %>."
5
+ redirect_to <%= items_url %>
6
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= instance_name %> = <%= class_name %>.get_by_key(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= instances_name %> = <%= class_name %>.all
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= instance_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= instance_name %> = <%= class_name %>.get_by_key(params[:id])
3
+ end
@@ -0,0 +1,9 @@
1
+ def update
2
+ @<%= instance_name %> = <%= class_name %>.get_by_key(params[:id])
3
+ if @<%= instance_name %>.update_attributes(params[:<%= instance_name %>])
4
+ flash[:notice] = "Successfully updated <%= class_name.underscore.humanize.downcase %>."
5
+ redirect_to <%= item_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,3 @@
1
+ class <%= class_name %> < Medea::JasonObject
2
+
3
+ end
@@ -0,0 +1,11 @@
1
+ it "create action should render new templates 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
+ <%= instance_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= instance_name %>
4
+ response.should redirect_to(<%= items_url %>)
5
+ <%= class_name %>.exists?(<%= instance_name %>.id).should be_false
6
+ end
@@ -0,0 +1,4 @@
1
+ it "edit action should render edit templates" 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 templates" do
2
+ get :index
3
+ response.should render_template(:index)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "new action should render new templates" do
2
+ get :new
3
+ response.should render_template(:new)
4
+ end
@@ -0,0 +1,4 @@
1
+ it "show action should render show templates" 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 templates 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,8 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= plural_class_name %>Controller do
4
+ fixtures :all
5
+ render_views
6
+
7
+ <%= controller_methods 'tests/rspec/actions' %>
8
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= class_name %> do
4
+ it "should be valid" do
5
+ <%= class_name %>.new.should be_valid
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ context "create action" do
2
+ should "render new templates 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
+ <%= instance_name %> = <%= class_name %>.first
4
+ delete :destroy, :id => <%= instance_name %>
5
+ assert_redirected_to <%= items_url %>
6
+ assert !<%= class_name %>.exists?(<%= instance_name %>.id)
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ context "edit action" do
2
+ should "render edit templates" do
3
+ get :edit, :id => <%= class_name %>.first
4
+ assert_template 'edit'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ context "index action" do
2
+ should "render index templates" do
3
+ get :index
4
+ assert_template 'index'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ context "new action" do
2
+ should "render new templates" do
3
+ get :new
4
+ assert_template 'new'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ context "show action" do
2
+ should "render show templates" do
3
+ get :show, :id => <%= class_name %>.first
4
+ assert_template 'show'
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ context "update action" do
2
+ should "render edit templates 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,5 @@
1
+ require 'test_helper'
2
+
3
+ class <%= plural_class_name %>ControllerTest < ActionController::TestCase
4
+ <%= controller_methods 'tests/shoulda/actions' %>
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>Test < ActiveSupport::TestCase
4
+ should "be valid" do
5
+ assert <%= class_name %>.new.valid?
6
+ end
7
+ 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
+ <%= instance_name %> = <%= class_name %>.first
3
+ delete :destroy, :id => <%= instance_name %>
4
+ assert_redirected_to <%= items_url %>
5
+ assert !<%= class_name %>.exists?(<%= instance_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,5 @@
1
+ require 'test_helper'
2
+
3
+ class <%= plural_class_name %>ControllerTest < ActionController::TestCase
4
+ <%= controller_methods 'tests/testunit/actions' %>
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>Test < ActiveSupport::TestCase
4
+ def test_should_be_valid
5
+ assert <%= class_name %>.new.valid?
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ <%%= form_for <%= item_path :instance_variable => true %> 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", <%= item_path :instance_variable => true %> %> |
9
+ <%- end -%>
10
+ <%- if action? :index -%>
11
+ <%%= link_to "View All", <%= items_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 <%= instance_name %> in @<%= instances_name %> %>
10
+ <tr>
11
+ <%- for attribute in model_attributes -%>
12
+ <td><%%= <%= instance_name %>.<%= attribute.name %> %></td>
13
+ <%- end -%>
14
+ <%- if action? :show -%>
15
+ <td><%%= link_to "Show", <%= item_path %> %></td>
16
+ <%- end -%>
17
+ <%- if action? :edit -%>
18
+ <td><%%= link_to "Edit", <%= item_path :action => :edit %> %></td>
19
+ <%- end -%>
20
+ <%- if action? :destroy -%>
21
+ <td><%%= link_to "Destroy", <%= item_path %>, :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 %>", <%= item_path :action => :new %> %></p>
29
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ <%% title "New <%= singular_name.titleize %>" %>
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ <p><%%= link_to "Back to List", <%= items_path %> %></p>
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ <%% title "<%= singular_name.titleize %>" %>
2
+
3
+ <%- for attribute in model_attributes -%>
4
+ <p>
5
+ <strong><%= attribute.human_name.titleize %>:</strong>
6
+ <%%= @<%= instance_name %>.<%= attribute.name %> %>
7
+ </p>
8
+ <%- end -%>
9
+
10
+ <p>
11
+ <%- if action? :edit -%>
12
+ <%%= link_to "Edit", <%= item_path :action => :edit, :instance_variable => true %> %> |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ <%%= link_to "Destroy", <%= item_path :instance_variable => true %>, :confirm => 'Are you sure?', :method => :delete %> |
16
+ <%- end -%>
17
+ <%- if action? :index -%>
18
+ <%%= link_to "View All", <%= items_path %> %>
19
+ <%- end -%>
20
+ </p>
@@ -0,0 +1,10 @@
1
+ = form_for <%= item_path %> do |f|
2
+ = f.error_messages
3
+ <%- for attribute in model_attributes -%>
4
+ %p
5
+ = f.label :<%= attribute.name %>
6
+ %br
7
+ = f.<%= attribute.field_type %> :<%= attribute.name %>
8
+ <%- end -%>
9
+ %p
10
+ = f.submit
@@ -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", <%= item_path %>
9
+ |
10
+ <%- end -%>
11
+ <%- if action? :index -%>
12
+ = link_to "View All", <%= items_path %>
13
+ <%- end -%>
14
+ <%- end -%>
@@ -0,0 +1,25 @@
1
+ - title "<%= plural_name.titleize %>"
2
+
3
+ %table
4
+ %tr
5
+ <%- for attribute in model_attributes -%>
6
+ %th <%= attribute.human_name %>
7
+ <%- end -%>
8
+ - for <%= instance_name %> in @<%= instances_name %>
9
+ %tr
10
+ <%- for attribute in model_attributes -%>
11
+ %td= <%= instance_name %>.<%= attribute.name %>
12
+ <%- end -%>
13
+ <%- if action? :show -%>
14
+ %td= link_to 'Show', <%= item_path %>
15
+ <%- end -%>
16
+ <%- if action? :edit -%>
17
+ %td= link_to 'Edit', <%= item_path :action => :edit %>
18
+ <%- end -%>
19
+ <%- if action? :destroy -%>
20
+ %td= link_to 'Destroy', <%= item_path %>, :confirm => 'Are you sure?', :method => :delete
21
+ <%- end -%>
22
+
23
+ <%- if actions? :new -%>
24
+ %p= link_to "New <%= singular_name.titleize %>", <%= item_path :action => :new %>
25
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ - title "New <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ %p= link_to "Back to List", <%= items_path %>
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ - title "<%= singular_name.titleize %>"
2
+
3
+ <%- for attribute in model_attributes -%>
4
+ %p
5
+ %strong <%= attribute.human_name.titleize %>:
6
+ = @<%= instance_name %>.<%= attribute.name %>
7
+ <%- end -%>
8
+
9
+ %p
10
+ <%- if action? :edit -%>
11
+ = link_to "Edit", <%= item_path :action => :edit, :instance_variable => true %>
12
+ |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ = link_to "Destroy", <%= item_path :instance_variable => true %>, :confirm => 'Are you sure?', :method => :delete
16
+ |
17
+ <%- end -%>
18
+ <%- if action? :index -%>
19
+ = link_to "View All", <%= items_path %>
20
+ <%- end -%>
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Medea
4
+ module Generators
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+ def self.source_root
7
+ @_medea_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'medea', generator_name, 'templates'))
8
+ end
9
+
10
+ def self.banner
11
+ "rails generate medea:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
12
+ end
13
+
14
+ private
15
+
16
+ def add_gem(name, options = {})
17
+ gemfile_content = File.read(destination_path("Gemfile"))
18
+ File.open(destination_path("Gemfile"), 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
19
+ gem name, options unless gemfile_content.include? name
20
+ end
21
+
22
+ def print_usage
23
+ self.class.help(Thor::Base.shell.new)
24
+ exit
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "medea-generators"
6
+ s.version = "0.1.0"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Michael Jensen"]
9
+ s.email = ["michaelj@jasondb.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Medea Generators provides scaffolding for Rails projects using Medea to access JasonDB}
12
+ s.description = %q{Medea Generators provides scaffolding for Rails projects using Medea to access JasonDB.
13
+ This is very closely modelled on Ryan Bates' Nifty Generators.}
14
+
15
+ s.add_development_dependency 'rails', '~> 3.0.0'
16
+ s.add_dependency 'medea'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: medea-generators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Jensen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-18 00:00:00 +11:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: medea
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: |-
49
+ Medea Generators provides scaffolding for Rails projects using Medea to access JasonDB.
50
+ This is very closely modelled on Ryan Bates' Nifty Generators.
51
+ email:
52
+ - michaelj@jasondb.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - Rakefile
64
+ - lib/generators/medea.rb
65
+ - lib/generators/medea/init/init_generator.rb
66
+ - lib/generators/medea/init/templates/database.yml
67
+ - lib/generators/medea/scaffold/USAGE
68
+ - lib/generators/medea/scaffold/scaffold_generator.rb
69
+ - lib/generators/medea/scaffold/templates/actions/create.rb
70
+ - lib/generators/medea/scaffold/templates/actions/destroy.rb
71
+ - lib/generators/medea/scaffold/templates/actions/edit.rb
72
+ - lib/generators/medea/scaffold/templates/actions/index.rb
73
+ - lib/generators/medea/scaffold/templates/actions/new.rb
74
+ - lib/generators/medea/scaffold/templates/actions/show.rb
75
+ - lib/generators/medea/scaffold/templates/actions/update.rb
76
+ - lib/generators/medea/scaffold/templates/controller.rb
77
+ - lib/generators/medea/scaffold/templates/fixtures.yml
78
+ - lib/generators/medea/scaffold/templates/helper.rb
79
+ - lib/generators/medea/scaffold/templates/model.rb
80
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/create.rb
81
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/destroy.rb
82
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/edit.rb
83
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/index.rb
84
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/new.rb
85
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/show.rb
86
+ - lib/generators/medea/scaffold/templates/tests/rspec/actions/update.rb
87
+ - lib/generators/medea/scaffold/templates/tests/rspec/controller.rb
88
+ - lib/generators/medea/scaffold/templates/tests/rspec/model.rb
89
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/create.rb
90
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/destroy.rb
91
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/edit.rb
92
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/index.rb
93
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/new.rb
94
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/show.rb
95
+ - lib/generators/medea/scaffold/templates/tests/shoulda/actions/update.rb
96
+ - lib/generators/medea/scaffold/templates/tests/shoulda/controller.rb
97
+ - lib/generators/medea/scaffold/templates/tests/shoulda/model.rb
98
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/create.rb
99
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/destroy.rb
100
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/edit.rb
101
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/index.rb
102
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/new.rb
103
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/show.rb
104
+ - lib/generators/medea/scaffold/templates/tests/testunit/actions/update.rb
105
+ - lib/generators/medea/scaffold/templates/tests/testunit/controller.rb
106
+ - lib/generators/medea/scaffold/templates/tests/testunit/model.rb
107
+ - lib/generators/medea/scaffold/templates/views/erb/_form.html.erb
108
+ - lib/generators/medea/scaffold/templates/views/erb/edit.html.erb
109
+ - lib/generators/medea/scaffold/templates/views/erb/index.html.erb
110
+ - lib/generators/medea/scaffold/templates/views/erb/new.html.erb
111
+ - lib/generators/medea/scaffold/templates/views/erb/show.html.erb
112
+ - lib/generators/medea/scaffold/templates/views/haml/_form.html.haml
113
+ - lib/generators/medea/scaffold/templates/views/haml/edit.html.haml
114
+ - lib/generators/medea/scaffold/templates/views/haml/index.html.haml
115
+ - lib/generators/medea/scaffold/templates/views/haml/new.html.haml
116
+ - lib/generators/medea/scaffold/templates/views/haml/show.html.haml
117
+ - medea-generators.gemspec
118
+ has_rdoc: true
119
+ homepage: ""
120
+ licenses: []
121
+
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.3.7
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Medea Generators provides scaffolding for Rails projects using Medea to access JasonDB
150
+ test_files: []
151
+