mdd 3.0.3 → 3.0.4

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.
@@ -62,7 +62,7 @@ module Mdwa
62
62
  # if model has not a database yet, run the generate command
63
63
  begin
64
64
  # if model does not exist, should generate scaffold
65
- model_class = entity.model_class
65
+ model_class = entity.generator_model.model_class
66
66
  rescue
67
67
  model_class = nil
68
68
  end
@@ -130,7 +130,7 @@ module Mdwa
130
130
  # if model has not a database yet, run the generate command
131
131
  begin
132
132
  # if model does not exist, should generate scaffold
133
- model_class = entity.model_class
133
+ model_class = entity.generator_model.model_class
134
134
  rescue
135
135
  model_class = nil
136
136
  end
@@ -138,7 +138,8 @@ module Mdwa
138
138
  puts "===================================================="
139
139
  puts "Generating code for '#{entity.name}'"
140
140
  puts "===================================================="
141
- generate "#{entity.generate} --only_interface #{'--force' if options.force}"
141
+ generation_string = "#{entity.generate} --only_interface #{'--force' if options.force}"
142
+ generate generation_string
142
143
 
143
144
  # append generated code to entity
144
145
  append_to_file "#{MDWA::DSL::STRUCTURAL_PATH}#{entity.file_name}.rb", "\n\nMDWA::DSL.entity('#{entity.name}').code_generations << '#{generation_string}'"
@@ -151,14 +152,114 @@ module Mdwa
151
152
 
152
153
  #
153
154
  # Generate actions for entities.
155
+ # Generate controller actions and routes
154
156
  #
155
157
  def entities_actions
156
- end
158
+
159
+ @all_entities.each do |entity|
160
+ # next iteration if entity doesn't have specifications
161
+ next if entity.actions.actions.count.zero?
162
+
163
+ model = entity.generator_model
164
+
165
+ path_to_controller = "app/controllers/#{model.space}/#{model.plural_name}_controller.rb"
166
+ controller_string = File.read("#{Rails.root}/#{path_to_controller}")
167
+ path_to_routes = 'config/routes.rb'
168
+
169
+ #
170
+ # inject methods in the controller
171
+ # decide if code is included after class declaration or after cancan load code.
172
+ cancan_load = "load_and_authorize_resource :class => \"#{model.klass}\""
173
+ if controller_string.include? cancan_load
174
+ after = cancan_load
175
+ else
176
+ inherit_controller = 'A::BackendController' if model.space == 'a'
177
+ after = "class #{model.controller_name}Controller < #{inherit_controller || 'ApplicationController'}"
178
+ end
179
+
180
+ # insert in controller
181
+ insert_into_file path_to_controller, :after => after do
182
+ actions = []
183
+ entity.actions.generate_controller.each do |action_name, generation_string|
184
+ # write the generated code only if it is not declared in the controller
185
+ actions << "\n\n#{generation_string}" unless controller_string.include? "def #{action_name}"
186
+ end
187
+ actions.join
188
+ end
189
+
190
+ # inject routes declarations
191
+ insert_into_file path_to_routes, :after => "controller :#{model.plural_name} do" do
192
+ routes = []
193
+ entity.actions.generate_routes.each do |action_name, generation_string|
194
+ routes << "\n\t\t\t#{generation_string}"
195
+ end
196
+ routes.join
197
+ end
198
+
199
+ # inject routes testing
200
+ insert_into_file "spec/routing/#{model.space}/#{model.plural_name}_routing_spec.rb", :after => 'describe "routing" do' do
201
+ routes = []
202
+ entity.actions.actions.values.select {|a| !a.resource}.each do |action|
203
+ routes << "\n\n\t\tit 'routes to ##{action.name}' do"
204
+ routes << "\n\t\t\t#{action.method.to_s}('#{action.entity.generator_model.to_route_url}/#{'1/' if action.member?}#{action.name}').should route_to('#{action.entity.generator_model.to_route_url}##{action.name}' #{', :id => "1"' if action.member?})"
205
+ routes << "\n\t\tend"
206
+ end
207
+ routes.join
208
+ end
209
+
210
+ # generate the corresponding files
211
+ entity.actions.actions.values.select{ |a| !a.resource? }.each do |action|
212
+ action.template_names.each do |request, file_name|
213
+ case request.to_sym
214
+ when :modalbox, :html
215
+ template 'views/view.html.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}")
216
+ when :ajax
217
+ template 'views/view.js.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}")
218
+ when :ajax_js
219
+ template 'views/view.json.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}")
220
+ else
221
+ template 'views/view.custom.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}")
222
+ end
223
+ end
224
+ end
225
+
226
+ end # iteration over entities
227
+
228
+ end # method
157
229
 
158
230
  #
159
231
  # Generate code for entities specify.
232
+ # Generate unit testing code for models.
160
233
  #
161
234
  def entites_specifications
235
+
236
+ @all_entities.each do |entity|
237
+ # next iteration if entity doesn't have specifications
238
+ next if entity.specifications.count.zero?
239
+
240
+ model = entity.generator_model
241
+
242
+ path_to_spec = "spec/models/#{model.space}/#{model.singular_name}_spec.rb"
243
+ insert_into_file path_to_spec, :after => "describe #{model.klass} do" do
244
+ specs = []
245
+ file_string = File.read("#{Rails.root}/#{path_to_spec}")
246
+ entity.specifications.each do |specification|
247
+ unless file_string.include? specification.description
248
+ specs << "\n\n\tdescribe '#{specification.description}' do"
249
+ specification.details.each do |detail|
250
+ unless file_string.include? detail
251
+ specs << "\t\tit '#{detail}' do"
252
+ specs << "\t\tend"
253
+ end
254
+ end
255
+ specs << "\tend"
256
+ end
257
+ end
258
+ specs.join("\n")
259
+ end
260
+
261
+ end
262
+
162
263
  end
163
264
 
164
265
 
@@ -173,8 +274,6 @@ module Mdwa
173
274
 
174
275
 
175
276
 
176
-
177
-
178
277
  private
179
278
 
180
279
  # Implement the required interface for Rails::Generators::Migration.
@@ -0,0 +1,2 @@
1
+ Custom format.
2
+ Be sure to include this in /config/initializers/mime_types.rb
@@ -0,0 +1,2 @@
1
+ <h1>HTML generated code</h1>
2
+ <p>Replace this with your own code</p>
@@ -0,0 +1 @@
1
+ alert('js.erb response');
@@ -0,0 +1 @@
1
+ <%%= {json_response: 'ok'}.to_json %>
@@ -46,6 +46,25 @@ MDWA::DSL.entities.register "<%= name.singularize.camelize %>" do |e|
46
46
  # a.composition = true
47
47
  # a.description = 'This entity has a composite address.'
48
48
  # end
49
+
50
+ ##
51
+ ## Entity specifications.
52
+ ## Define restrictions and rules so this entity will work properly.
53
+ ##
54
+ # e.specify "fields should not be invalid" do |s|
55
+ # s.such_as "date should be valid"
56
+ # s.such_as "administrator must not be empty"
57
+ # s.such_as "description must not be empty"
58
+ # end
59
+ # e.specify "date should not be in the past"
60
+
61
+ ##
62
+ ## Entity actions. Define controller and routes for new operations with this entity.
63
+ ##
64
+ # e.member_action :publish, :get, :html
65
+ # e.collection_action :export, :post, [:csv, :xml]
66
+ # e.collection_action :report, :get, [:ajax]
67
+
49
68
  <%- end -%>
50
69
 
51
70
  end
@@ -28,6 +28,7 @@ module Mdwa
28
28
  gem 'will_paginate'
29
29
  gem 'nested_form'
30
30
  gem 'require_all'
31
+ gem 'rspec-rails', :group => [:test, :development]
31
32
 
32
33
  inside Rails.root do
33
34
  run "bundle install"
@@ -167,6 +168,11 @@ module Mdwa
167
168
 
168
169
  rake "db:seed" if ask_question( "Run rake db:seeds?" )
169
170
  end
171
+
172
+ def testes
173
+ # Install Rspec as the default testing framework
174
+ generate 'rspec:install'
175
+ end
170
176
 
171
177
  private
172
178
 
@@ -24,6 +24,7 @@ module Mdwa
24
24
  class_option :skip_questions, :desc => 'Answer no for all questions by default.', :type => :boolean, :default => false
25
25
  class_option :skip_interface, :desc => 'Cretes only models, migrations and associations.', :type => :boolean, :default => false
26
26
  class_option :only_interface, :desc => 'Skips models, associations and migrations.', :type => :boolean, :default => false
27
+ class_option :skip_tests, :desc => 'Skip Rspec tests generation', :type => :boolean, :default => false
27
28
 
28
29
  def initialize(*args, &block)
29
30
 
@@ -111,10 +112,10 @@ module Mdwa
111
112
  unless options.skip_interface
112
113
  route_str = []
113
114
  route_str << "namespace :#{@model.space} do" if @model.namespace?
114
- route_str << "\tcontroller :#{@model.plural_name} do"
115
- route_str << "\tend"
116
- route_str << "\tresources :#{@model.plural_name}"
117
- route_str << "end" if @model.namespace?
115
+ route_str << "\t\tcontroller :#{@model.plural_name} do"
116
+ route_str << "\t\tend"
117
+ route_str << "\t\tresources :#{@model.plural_name}"
118
+ route_str << "\tend" if @model.namespace?
118
119
 
119
120
  route route_str.join("\n")
120
121
  end
@@ -139,6 +140,11 @@ module Mdwa
139
140
  rake('db:migrate') if !options.skip_questions and yes? 'Run rake db:migrate?'
140
141
  end
141
142
  end
143
+
144
+ def tests_and_specify
145
+ template 'specs/model.rb', "spec/models/#{@model.space}/#{@model.singular_name}_spec.rb"
146
+ template 'specs/routing.rb', "spec/routing/#{@model.space}/#{@model.plural_name}_routing_spec.rb"
147
+ end
142
148
 
143
149
  private
144
150
 
@@ -0,0 +1,7 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe <%= @model.klass %> do
5
+
6
+
7
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe <%= @model.controller_name %>Controller do
5
+ describe "routing" do
6
+
7
+ it "routes to #index" do
8
+ get("/<%= @model.to_route_url %>").should route_to("<%= @model.to_route_url %>#index")
9
+ end
10
+
11
+ it "routes to #new" do
12
+ get("/<%= @model.to_route_url %>/new").should route_to("<%= @model.to_route_url %>#new")
13
+ end
14
+
15
+ it "routes to #show" do
16
+ get("/<%= @model.to_route_url %>/1").should route_to("<%= @model.to_route_url %>#show", :id => "1")
17
+ end
18
+
19
+ it "routes to #edit" do
20
+ get("/<%= @model.to_route_url %>/1/edit").should route_to("<%= @model.to_route_url %>#edit", :id => "1")
21
+ end
22
+
23
+ it "routes to #create" do
24
+ post("/<%= @model.to_route_url %>").should route_to("<%= @model.to_route_url %>#create")
25
+ end
26
+
27
+ it "routes to #update" do
28
+ put("/<%= @model.to_route_url %>/1").should route_to("<%= @model.to_route_url %>#update", :id => "1")
29
+ end
30
+
31
+ it "routes to #destroy" do
32
+ delete("/<%= @model.to_route_url %>/1").should route_to("<%= @model.to_route_url %>#destroy", :id => "1")
33
+ end
34
+
35
+ end
36
+ end
@@ -1,4 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
+ require 'mdwa/generators'
3
+
2
4
  module MDWA
3
5
  module DSL
4
6
 
@@ -8,11 +10,12 @@ module MDWA
8
10
  ALLOWED_METHODS = [:get, :post, :put, :delete]
9
11
  PREDEFINED_REQUEST_TYPES = [:html, :ajax, :ajax_js, :modalbox]
10
12
 
11
- attr_accessor :name, :type, :method, :request_type, :response, :resource
13
+ attr_accessor :name, :type, :entity, :method, :request_type, :response, :resource
12
14
 
13
- def initialize(name, type, options)
15
+ def initialize(entity, name, type, options = {})
14
16
  self.name = name.to_sym
15
17
  self.type = type.to_sym
18
+ self.entity = entity
16
19
  self.method = options[:method] || :get
17
20
  self.request_type = options[:request_type] || :html
18
21
  self.response = options[:response] || {}
@@ -47,15 +50,26 @@ module MDWA
47
50
  resource
48
51
  end
49
52
 
53
+ def generate_route
54
+ "#{self.method.to_s} '#{self.entity.name.underscore.pluralize}#{'/:id' if member?}/#{self.name.to_s}' => :#{self.name.to_sym}"
55
+ end
56
+
50
57
  def generate_controller
51
58
  action_str = []
59
+ action_str << "\t# #{entity.generator_model.plural_name}##{self.name.to_s}"
60
+ action_str << "\t# Route: #{generate_route}"
52
61
  action_str << "\tdef #{self.name.to_s}"
53
- action_str << ""
62
+
63
+ if member?
64
+ action_str << "\t\t@#{entity.generator_model.singular_name} = #{entity.generator_model.klass}.find(params[:id])"
65
+ action_str << ""
66
+ end
67
+
54
68
  action_str << "\t\trespond_to do |format|"
55
69
  self.request_type.each do |request|
56
70
  case request.to_sym
57
71
  when :modalbox
58
- # action_str << "\t\t\tformat.html{render :layout => false}"
72
+ action_str << "\t\t\tformat.html{render :layout => false}"
59
73
  when :html
60
74
  action_str << "\t\t\tformat.html #{"{#{self.response[:html]}}" unless self.response[:html].blank?}"
61
75
  when :ajax
@@ -69,7 +83,28 @@ module MDWA
69
83
  action_str << "\t\tend"
70
84
 
71
85
  action_str << "\tend"
72
- # action_str.join("\n")
86
+ action_str.join("\n")
87
+ end
88
+
89
+
90
+ def template_names
91
+ names = {}
92
+ self.request_type.each do |request|
93
+ case request.to_sym
94
+ when :modalbox
95
+ names[:modalbox] = "#{self.name}.html.erb"
96
+ when :html
97
+ names[:html] = "#{self.name}.html.erb" if self.response[:html].blank?
98
+ when :ajax
99
+ names[:ajax] = "#{self.name}.js.erb" if self.response[:ajax].blank?
100
+ when :ajax_js
101
+ names[:ajax_js] = "#{self.name}.json.erb" if self.response[:ajax_js].blank?
102
+ else
103
+ names[request.to_sym] = "#{self.name}.#{request.to_s}.erb" if self.response[request.to_sym].blank?
104
+ end
105
+ end
106
+
107
+ return names
73
108
  end
74
109
 
75
110
  end
@@ -107,10 +107,10 @@ module MDWA
107
107
  end
108
108
 
109
109
  #
110
- # Returns the associated model in app/models folder
110
+ # Return an instance of Generators::Model
111
111
  #
112
- def model_class
113
- Generators::Model.new(self.model_name).model_class
112
+ def generator_model
113
+ Generators::Model.new(self.model_name)
114
114
  end
115
115
 
116
116
  #
@@ -12,14 +12,14 @@ module MDWA
12
12
  end
13
13
 
14
14
  def member_action(name, method, request_type)
15
- self.actions[name.to_sym] = Action.new(name.to_sym, :member, :method => method, :request_type => request_type)
15
+ self.actions[name.to_sym] = Action.new(self.entity, name.to_sym, :member, :method => method, :request_type => request_type)
16
16
  end
17
17
 
18
18
  #
19
19
  # Include a collection action
20
20
  # Params: name, method = get, request_type = html
21
21
  def collection_action(name, method, request_type)
22
- self.actions[name.to_sym] = Action.new(name.to_sym, :collection, :method => method, :request_type => request_type)
22
+ self.actions[name.to_sym] = Action.new(self.entity, name.to_sym, :collection, :method => method, :request_type => request_type)
23
23
  end
24
24
 
25
25
  def member_actions
@@ -31,30 +31,30 @@ module MDWA
31
31
  end
32
32
 
33
33
  def generate_routes
34
- routes = []
35
- self.actions.values do |action|
36
- routes << "#{action.method.to_s} '#{entity.name.underscore.pluralize}/#{action.name.to_s}' => #{action.name.to_sym}"
34
+ routes = {}
35
+ self.actions.values.select {|a| !a.resource?}.each do |action|
36
+ routes[action.name] = action.generate_route
37
37
  end
38
38
  return routes
39
39
  end
40
40
 
41
41
  def generate_controller
42
- controller = []
43
- self.actions.value.select {|a| !a.resource?}.each do |action|
44
- controller << action.generate_controller
42
+ controller = {}
43
+ self.actions.values.select {|a| !a.resource?}.each do |action|
44
+ controller[action.name] = action.generate_controller
45
45
  end
46
- # controller.join("\n\n")
46
+ return controller
47
47
  end
48
48
 
49
49
  # Returns all resource actions
50
50
  def set_resource_actions
51
- self.actions[:index] = Action.new(:index, :collection, :resource => true)
52
- self.actions[:new] = Action.new(:new, :collection, :resource => true)
53
- self.actions[:edit] = Action.new(:edit, :member, :resource => true)
54
- self.actions[:show] = Action.new(:show, :member, :resource => true)
55
- self.actions[:create] = Action.new(:create, :collection, :method => :post, :resource => true)
56
- self.actions[:update] = Action.new(:update, :member, :method => :put, :resource => true)
57
- self.actions[:delete] = Action.new(:delete, :member, :method => :delete, :resource => true)
51
+ self.actions[:index] = Action.new(self.entity, :index, :collection, :resource => true)
52
+ self.actions[:new] = Action.new(self.entity, :new, :collection, :resource => true)
53
+ self.actions[:edit] = Action.new(self.entity, :edit, :member, :resource => true)
54
+ self.actions[:show] = Action.new(self.entity, :show, :member, :resource => true)
55
+ self.actions[:create] = Action.new(self.entity, :create, :collection, :method => :post, :resource => true)
56
+ self.actions[:update] = Action.new(self.entity, :update, :member, :method => :put, :resource => true)
57
+ self.actions[:delete] = Action.new(self.entity, :delete, :member, :method => :delete, :resource => true)
58
58
  end
59
59
 
60
60
  def clear_resource_actions
@@ -13,4 +13,4 @@ module MDWA
13
13
  end
14
14
 
15
15
  end # dsl
16
- end # mdwa
16
+ end # mdwa
@@ -63,6 +63,13 @@ module MDWA
63
63
  return singular_name
64
64
  end
65
65
  end
66
+
67
+ def to_route_url
68
+ url = []
69
+ url << space if namespace?
70
+ url << plural_name
71
+ url.join('/')
72
+ end
66
73
 
67
74
  def to_route_object(prefix = '')
68
75
  return "#{prefix}#{singular_name}" if !specific? or !namespace?
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module MDWA
3
- VERSION = "3.0.3"
3
+ VERSION = "3.0.4"
4
4
  end
@@ -19,7 +19,8 @@ describe MDWA::DSL::EntityActions do
19
19
  end
20
20
 
21
21
  it "should create action correctly" do
22
- action = MDWA::DSL::Action.new :publish, :member, :method => :get, :request_type => [:html, :ajax_js]
22
+ product_entity = MDWA::DSL.entity('Product')
23
+ action = MDWA::DSL::Action.new( product_entity, :publish, :member, :method => :get, :request_type => [:html, :ajax_js] )
23
24
  action.name.must_equal :publish
24
25
  action.member?.must_equal true
25
26
  action.method.must_equal :get
@@ -93,9 +94,17 @@ describe MDWA::DSL::EntityActions do
93
94
 
94
95
  end
95
96
 
96
- it "should generate correct code" do
97
+ it 'should generate view files correctly' do
97
98
  product = MDWA::DSL.entity('Product')
98
- puts product.actions.generate_routes
99
+ # p product.actions.actions[:publish].template_names
99
100
  end
100
101
 
102
+ it 'should generate correct code' do
103
+
104
+ product = MDWA::DSL.entity('Product')
105
+ # p product.actions.generate_controller
106
+
107
+ end
108
+
109
+
101
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdd
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-24 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -169,6 +169,10 @@ files:
169
169
  - lib/generators/mdwa/code/USAGE
170
170
  - lib/generators/mdwa/code/code_generator.rb
171
171
  - lib/generators/mdwa/code/templates/migration.rb
172
+ - lib/generators/mdwa/code/templates/views/view.custom.erb
173
+ - lib/generators/mdwa/code/templates/views/view.html.erb
174
+ - lib/generators/mdwa/code/templates/views/view.js.erb
175
+ - lib/generators/mdwa/code/templates/views/view.json.erb
172
176
  - lib/generators/mdwa/entity/USAGE
173
177
  - lib/generators/mdwa/entity/entity_generator.rb
174
178
  - lib/generators/mdwa/entity/templates/entity.rb
@@ -303,6 +307,8 @@ files:
303
307
  - lib/generators/mdwa/scaffold/templates/db_migrate/migrate.rb
304
308
  - lib/generators/mdwa/scaffold/templates/models/model.rb
305
309
  - lib/generators/mdwa/scaffold/templates/models/module.rb
310
+ - lib/generators/mdwa/scaffold/templates/specs/model.rb
311
+ - lib/generators/mdwa/scaffold/templates/specs/routing.rb
306
312
  - lib/generators/mdwa/scaffold/templates/views/_form.html.erb
307
313
  - lib/generators/mdwa/scaffold/templates/views/_form_fields.html.erb
308
314
  - lib/generators/mdwa/scaffold/templates/views/_list.html.erb