mdd 3.0.0 → 3.0.1

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 (35) hide show
  1. data/lib/generators/mdwa/association/association_generator.rb +11 -10
  2. data/lib/generators/mdwa/association/templates/migrate/many_to_many.rb +3 -3
  3. data/lib/generators/mdwa/code/USAGE +5 -0
  4. data/lib/generators/mdwa/code/code_generator.rb +166 -0
  5. data/lib/generators/mdwa/code/templates/migration.rb +15 -0
  6. data/lib/generators/mdwa/entity/USAGE +5 -0
  7. data/lib/generators/mdwa/entity/entity_generator.rb +25 -0
  8. data/lib/generators/mdwa/entity/templates/entity.rb +50 -0
  9. data/lib/generators/mdwa/sandbox/sandbox_generator.rb +3 -1
  10. data/lib/generators/mdwa/sandbox/templates/app/controllers/a/home_controller.rb +4 -0
  11. data/lib/generators/mdwa/sandbox/templates/app/views/a/administrators/_form.html.erb +6 -38
  12. data/lib/generators/mdwa/sandbox/templates/app/views/a/administrators/_form_fields.html.erb +25 -0
  13. data/lib/generators/mdwa/sandbox/templates/db/seeds/site.rb +2 -1
  14. data/lib/generators/mdwa/scaffold/scaffold_generator.rb +5 -3
  15. data/lib/generators/mdwa/scaffold/templates/views/_list.html.erb +1 -1
  16. data/lib/generators/mdwa/user/USAGE +3 -0
  17. data/lib/generators/mdwa/user/templates/controllers/ajax_controller.rb +78 -0
  18. data/lib/generators/mdwa/user/templates/controllers/controller.rb +77 -0
  19. data/lib/generators/mdwa/user/templates/migrate.rb +15 -0
  20. data/lib/generators/mdwa/user/templates/model.rb +11 -0
  21. data/lib/generators/mdwa/user/templates/views/update.js.erb +12 -0
  22. data/lib/generators/mdwa/user/user_generator.rb +113 -0
  23. data/lib/mdwa/dsl.rb +7 -1
  24. data/lib/mdwa/dsl/entities.rb +14 -2
  25. data/lib/mdwa/dsl/entity.rb +94 -9
  26. data/lib/mdwa/dsl/entity_association.rb +59 -0
  27. data/lib/mdwa/dsl/entity_attribute.rb +46 -0
  28. data/lib/mdwa/dsl/generator.rb +10 -0
  29. data/lib/mdwa/generators/model.rb +13 -4
  30. data/lib/mdwa/generators/model_association.rb +1 -1
  31. data/lib/mdwa/generators/model_attribute.rb +10 -9
  32. data/lib/mdwa/version.rb +1 -1
  33. data/mdd.gemspec +1 -0
  34. data/test/entity_test.rb +188 -13
  35. metadata +35 -2
@@ -1,38 +1,123 @@
1
1
  # -*- encoding : utf-8 -*-
2
- require 'ostruct'
2
+ require 'mdwa/generators'
3
3
 
4
4
  module MDWA
5
5
  module DSL
6
6
 
7
7
  class Entity
8
8
 
9
- attr_accessor :name, :resource, :purpose, :model_name, :ajax, :generated, :force
10
- attr_accessor :attributes, :associations
9
+ attr_accessor :name, :resource, :user, :purpose, :scaffold_name, :model_name, :ajax, :generated, :force
10
+ attr_accessor :attributes, :associations, :code_generations
11
11
 
12
12
  def initialize( name )
13
13
  # set the entity name
14
14
  self.name = name
15
15
 
16
16
  # fixed attributes
17
- self.resource = false
18
- self.model_name = self.name
17
+ self.resource = true
18
+ self.user = false
19
19
  self.ajax = false
20
20
  self.generated = false
21
21
  self.force = false
22
22
 
23
23
  # arrays
24
- self.attributes = []
25
- self.associations = []
24
+ self.attributes = {}
25
+ self.associations = {}
26
+ self.code_generations = []
26
27
  end
27
28
 
29
+ def name=(value)
30
+ @name = value
31
+ end
32
+
33
+ def scaffold_name
34
+ return (@scaffold_name.blank? ? name: @scaffold_name)
35
+ end
36
+
37
+ def model_name
38
+ return (@model_name.blank? ? scaffold_name: @model_name)
39
+ end
40
+
41
+ def resource?
42
+ self.resource
43
+ end
44
+
45
+ def force?
46
+ self.force
47
+ end
48
+
49
+ def user?
50
+ self.user
51
+ end
52
+
53
+ #
54
+ # Declares one attribute of the list using the block given.
55
+ #
28
56
  def attribute
29
- attr = OpenStruct.new
57
+ attr = EntityAttribute.new(self)
30
58
  yield( attr ) if block_given?
31
- self.attributes << attr
59
+ attr.raise_errors_if_invalid!
60
+ self.attributes[attr.name] = attr
32
61
  end
33
62
 
63
+ #
64
+ # Selects the default attribute of the entity
65
+ #
66
+ def default_attribute
67
+ default_attr = self.attributes.first.last # first element value
68
+ self.attributes.each do |key, attr|
69
+ default_attr = attr if attr.default?
70
+ end
71
+ return default_attr
72
+ end
73
+
74
+ #
75
+ # Defines one association
76
+ #
77
+ def association
78
+ assoc = EntityAssociation.new(self)
79
+ yield( assoc ) if block_given?
80
+ # assoc.raise_errors_if_invalid!
81
+ self.associations[assoc.name] = assoc
82
+ end
83
+
84
+ #
85
+ # Returns the associated model in app/models folder
86
+ #
87
+ def model_class
88
+ Generators::Model.new(self.model_name).model_class
89
+ end
90
+
91
+ #
92
+ # Entity file name
93
+ #
94
+ def file_name
95
+ self.name.singularize.underscore
96
+ end
97
+
98
+ #
99
+ # Generate MDWA scaffold code for structural schema.
100
+ #
34
101
  def generate
102
+ # generates nothing if is not a resource
103
+ return nil unless self.resource
104
+
105
+ gen = []
106
+ gen << scaffold_name
107
+
108
+ attributes.each do |key, attr|
109
+ gen << attr.generate
110
+ end
111
+
112
+ associations.each do |key, assoc|
113
+ gen << assoc.generate
114
+ end
115
+
116
+ gen << "--ajax" if ajax
117
+ gen << "--force" if force
118
+ gen << "--model='#{model_name}'" if model_name != scaffold_name
35
119
 
120
+ "mdwa:#{user? ? 'user': 'scaffold'} #{gen.join(' ')}"
36
121
  end
37
122
 
38
123
  end
@@ -0,0 +1,59 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module MDWA
3
+ module DSL
4
+
5
+ class EntityAssociation
6
+
7
+ attr_accessor :source, :destination, :destination_view
8
+ attr_accessor :name, :type, :composition, :description
9
+
10
+ ACCEPTED_TYPES = [:one_to_many, :many_to_one, :one_to_one, :one_to_one_not_navigable, :many_to_many]
11
+
12
+ def initialize(source)
13
+ self.source = source
14
+ self.composition = false
15
+ end
16
+
17
+ #
18
+ # Sets the destination entity.
19
+ # Sets the name if not set yet.
20
+ #
21
+ def destination=(value)
22
+ self.name = value.downcase if self.name.blank?
23
+ @destination = value
24
+ end
25
+
26
+ #
27
+ # Return the mapped type for the code generation.
28
+ #
29
+ def generator_type
30
+ case self.type.to_sym
31
+ when :one_to_many
32
+ return (self.composition ? 'nested_many' : 'has_many' )
33
+ when :one_to_one
34
+ return (self.composition ? 'nested_one' : 'belongs_to' )
35
+ when :one_to_one_not_navigable
36
+ 'has_one'
37
+ when :many_to_one
38
+ 'belongs_to'
39
+ when :many_to_many
40
+ 'has_and_belongs_to_many'
41
+ end
42
+ end
43
+
44
+ def generate
45
+ destination = DSL.entity(self.destination.camelize)
46
+
47
+ gen = []
48
+ gen << self.name
49
+ gen << destination.scaffold_name + (destination.model_name != destination.scaffold_name ? ",#{destination.model_name}" : '')
50
+ gen << (self.destination_view || destination.default_attribute.name)
51
+ gen << generator_type
52
+
53
+ gen.join(':')
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,46 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'mdwa/generators'
3
+
4
+ module MDWA
5
+ module DSL
6
+
7
+ class EntityAttribute
8
+
9
+ attr_accessor :entity, :name, :type, :default
10
+
11
+ ACCEPTED_TYPES = MDWA::Generators::ModelAttribute::STATIC_TYPES
12
+
13
+ def initialize(entity)
14
+ self.entity = entity
15
+ self.default = false
16
+ end
17
+
18
+ def raise_errors_if_invalid!
19
+ if !valid?
20
+ raise "Invalid entity attribute: name is blank" if self.name.blank?
21
+ raise "Invalid entity attribute: '#{name}' - type is blank" if self.type.blank?
22
+ raise "Invalid entity attribute: '#{name}' - type '#{type}' is invalid" if self.type_invalid?
23
+ raise "Invalid entity attribute: '#{name}' - entity is nil" if self.entity.blank?
24
+ end
25
+ end
26
+
27
+ def valid?
28
+ !entity.nil? and !name.blank? and !type.blank? and !type_invalid?
29
+ end
30
+
31
+ def type_invalid?
32
+ !ACCEPTED_TYPES.include?( self.type.to_sym )
33
+ end
34
+
35
+ def default?
36
+ self.default
37
+ end
38
+
39
+ def generate
40
+ "#{name}:#{type}"
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module MDWA
3
+ module DSL
4
+
5
+ module Generator
6
+
7
+ end # generator
8
+
9
+ end # DSL
10
+ end # MDWA
@@ -4,9 +4,11 @@ module MDWA
4
4
  class Model
5
5
 
6
6
  attr_accessor :name, :namespace, :attributes, :specific_model_name
7
-
7
+
8
+ #
8
9
  # Sets the variables by the string
9
10
  # Format: <namespace>/<model>, the namespace is optional.
11
+ #
10
12
  def initialize( arg )
11
13
 
12
14
  self.namespace = '' # prevents unitialized variable errors
@@ -36,6 +38,13 @@ module MDWA
36
38
  return specific_model.klass
37
39
  end
38
40
  end
41
+
42
+ #
43
+ # Returns the associated model in app/models folder
44
+ #
45
+ def model_class
46
+ self.klass.classify.constantize
47
+ end
39
48
 
40
49
  def controller_name
41
50
  namespace_scope + name.pluralize
@@ -89,9 +98,9 @@ module MDWA
89
98
 
90
99
  private
91
100
  def namespace_scope
92
- return "#{namespace}::" if namespace?
93
- return ''
94
- end
101
+ return "#{namespace}::" if namespace?
102
+ return ''
103
+ end
95
104
 
96
105
  end
97
106
  end
@@ -68,7 +68,7 @@ module MDWA
68
68
  end
69
69
 
70
70
  def has_and_belongs_to_many?
71
- return (relation == 'has_and_belongs_and_to_many')
71
+ return (relation == 'has_and_belongs_to_many')
72
72
  end
73
73
 
74
74
  end
@@ -6,7 +6,7 @@ module MDWA
6
6
  class ModelAttribute
7
7
  attr_accessor :name, :type, :reference, :reference_type, :model
8
8
 
9
- STATIC_TYPES = [:boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp, :file]
9
+ STATIC_TYPES = [:boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp, :file, :password]
10
10
 
11
11
  # Sets the attributes variables
12
12
  # Format: <name>:<type>,<model>:<reference>:<reference_type>
@@ -51,14 +51,14 @@ module MDWA
51
51
 
52
52
  def migration_field
53
53
  @migration_field ||= case self.type.to_s.to_sym
54
- when :string, :file then 'string'
55
- when :boolean then 'boolean'
56
- when :date then 'date'
57
- when :datetime then 'datetime'
58
- when :decimal, :float then 'decimal'
59
- when :text then 'text'
60
- when :time then 'time'
61
- when :timestamp then 'timestamp'
54
+ when :string, :file, :password then 'string'
55
+ when :boolean then 'boolean'
56
+ when :date then 'date'
57
+ when :datetime then 'datetime'
58
+ when :decimal, :float then 'decimal'
59
+ when :text then 'text'
60
+ when :time then 'time'
61
+ when :timestamp then 'timestamp'
62
62
  else
63
63
  'integer'
64
64
  end
@@ -74,6 +74,7 @@ module MDWA
74
74
  when :date then 'date_select'
75
75
  when :text then 'text_area'
76
76
  when :boolean then 'check_box'
77
+ when :password then 'password_field'
77
78
  else
78
79
  'text_field'
79
80
  end
data/lib/mdwa/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module MDWA
3
- VERSION = "3.0.0"
3
+ VERSION = "3.0.1"
4
4
  end
data/mdd.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency 'cancan'
20
20
  s.add_dependency 'will_paginate'
21
21
  s.add_dependency 'nested_form'
22
+ s.add_dependency 'require_all'
22
23
  s.add_development_dependency 'minitest'
23
24
 
24
25
  s.files = `git ls-files`.split("\n")
data/test/entity_test.rb CHANGED
@@ -9,18 +9,84 @@ describe MDWA::DSL::Entities do
9
9
  before do
10
10
  # create product entity
11
11
  MDWA::DSL.entities.register "Product" do |e|
12
+ e.purpose = 'Store the company products.'
12
13
  e.resource = true
13
14
  e.ajax = true
14
- e.generated = false
15
+
16
+ ## attributes
15
17
  e.attribute do |attr|
16
- attr.name = 'test'
18
+ attr.name = 'name'
19
+ attr.type = 'string'
17
20
  end
21
+ e.attribute do |attr|
22
+ attr.name = 'category'
23
+ attr.type = 'integer'
24
+ end
25
+ e.attribute do |attr|
26
+ attr.name = 'stock'
27
+ attr.type = 'integer'
28
+ end
29
+ e.attribute do |attr|
30
+ attr.name = 'published'
31
+ attr.type = 'boolean'
32
+ end
33
+
34
+ ## associations
35
+ e.association do |a|
36
+ a.destination = 'Category'
37
+ a.type = 'many_to_one'
38
+ a.description = 'Holds the category'
39
+ end
40
+
41
+ e.association do |a|
42
+ a.name = 'marca'
43
+ a.destination = 'Brand'
44
+ a.type = 'one_to_many'
45
+ a.composition = true
46
+ a.description = 'Product`s brand'
47
+ end
48
+
49
+ e.association do |a|
50
+ a.destination = 'Category2'
51
+ a.type = 'one_to_one'
52
+ end
53
+
54
+ e.association do |a|
55
+ a.destination = 'Category3'
56
+ a.type = 'one_to_one'
57
+ a.composition = true
58
+ end
59
+
60
+ e.association do |a|
61
+ a.destination = 'Category4'
62
+ a.type = 'one_to_many'
63
+ end
64
+
65
+ e.association do |a|
66
+ a.destination = 'Category5'
67
+ a.type = 'one_to_one_not_navigable'
68
+ end
69
+
70
+ e.association do |a|
71
+ a.destination = 'Category6'
72
+ a.type = 'many_to_many'
73
+ end
74
+
75
+ e.association do |a|
76
+ a.destination = 'Category7'
77
+ a.type = 'many_to_one'
78
+ end
79
+
18
80
  end
19
81
  MDWA::DSL.entity("Product").must_be_instance_of MDWA::DSL::Entity
20
82
 
21
83
  # create category entity
22
84
  MDWA::DSL.entities.register "Category" do |e|
23
85
  e.resource = false
86
+ e.attribute do |attr|
87
+ attr.name = 'name'
88
+ attr.type = 'string'
89
+ end
24
90
  end
25
91
  MDWA::DSL.entity("Category").must_be_instance_of MDWA::DSL::Entity
26
92
  end
@@ -37,31 +103,140 @@ describe MDWA::DSL::Entities do
37
103
  product.must_be_instance_of MDWA::DSL::Entity
38
104
  product.resource.must_equal true
39
105
  product.ajax.must_equal true
40
- product.generated.must_equal false
41
- product.associations.must_be_instance_of Array
42
- product.attributes.must_be_instance_of Array
106
+ product.associations.must_be_instance_of Hash
107
+ product.attributes.must_be_instance_of Hash
43
108
  product.model_name.must_equal( "Product" )
44
109
 
45
110
  category = MDWA::DSL.entity("Category")
46
111
  category.must_be_instance_of MDWA::DSL::Entity
47
112
  category.resource.must_equal false
48
- category.associations.must_be_instance_of Array
49
- category.attributes.must_be_instance_of Array
113
+ category.associations.must_be_instance_of Hash
114
+ category.attributes.must_be_instance_of Hash
50
115
  end
51
116
 
117
+ it "must override elements" do
118
+ MDWA::DSL.entities.register "Product" do |e|
119
+ e.resource = false
120
+ end
121
+ MDWA::DSL.entity("Product").resource.must_equal false
122
+ end
52
123
 
53
- it "must not store other stuff" do
54
- MDWA::DSL.entities.nodes.size.must_equal 2
124
+ it "must store attributes" do
125
+ product = MDWA::DSL.entity("Product")
126
+
127
+ product.attributes.count.must_equal 4
128
+ product.attributes['name'].type.must_equal 'string'
129
+ product.attributes['name'].name.must_equal 'name'
130
+ product.attributes['category'].type.must_equal 'integer'
131
+ product.attributes['category'].name.must_equal 'category'
132
+ product.attributes['published'].type.must_equal 'boolean'
133
+ product.attributes['published'].name.must_equal 'published'
134
+ product.attributes['stock'].type.must_equal 'integer'
135
+ product.attributes['stock'].name.must_equal 'stock'
136
+
137
+ product.default_attribute.name.must_equal 'name'
138
+
139
+ category = MDWA::DSL.entity("Category")
140
+ category.attributes.count.must_equal 1
141
+ category.default_attribute.name.must_equal 'name'
55
142
  end
56
143
 
144
+ it "must keep associations" do
145
+
146
+ product = MDWA::DSL.entity("Product")
147
+ product.associations.count.must_equal 8
148
+
149
+ product.associations['category'].type.must_equal 'many_to_one'
150
+ product.associations['category'].destination.must_equal 'Category'
151
+ product.associations['category'].generator_type.must_equal 'belongs_to'
152
+
153
+ product.associations['brand'].must_equal nil
154
+ product.associations['marca'].type.must_equal 'one_to_many'
155
+ product.associations['marca'].destination.must_equal 'Brand'
156
+ product.associations['marca'].generator_type.must_equal 'nested_many'
157
+
158
+ product.associations['category2'].generator_type.must_equal 'belongs_to'
159
+ product.associations['category3'].generator_type.must_equal 'nested_one'
160
+ product.associations['category4'].generator_type.must_equal 'has_many'
161
+ product.associations['category5'].generator_type.must_equal 'has_one'
162
+ product.associations['category6'].generator_type.must_equal 'has_and_belongs_to_many'
163
+ product.associations['category7'].generator_type.must_equal 'belongs_to'
164
+
165
+ end
57
166
 
58
- it "must override elements" do
167
+ it "must generated correct code" do
59
168
 
60
- MDWA::DSL.entities.register "Product" do |e|
61
- e.resource = false
169
+ MDWA::DSL.entities.register "Project" do |p|
170
+ p.resource = true
171
+ p.scaffold_name = 'a/project'
172
+ p.ajax = true
173
+
174
+ p.attribute do |a|
175
+ a.name = 'nome'
176
+ a.type = 'string'
177
+ end
178
+ p.attribute do |a|
179
+ a.name = 'ativo'
180
+ a.type = 'boolean'
181
+ end
182
+ p.attribute do |a|
183
+ a.name = 'situacao_atual'
184
+ a.type = 'text'
185
+ end
186
+
187
+ p.association do |assoc|
188
+ assoc.destination = 'Group'
189
+ assoc.type = 'many_to_one'
190
+ end
191
+ end
192
+
193
+ MDWA::DSL.entities.register "Group" do |p|
194
+ p.resource = true
195
+ p.scaffold_name = 'a/group'
196
+ p.ajax = true
197
+ p.force = true
198
+
199
+ p.attribute do |a|
200
+ a.name = 'nome'
201
+ a.type = 'string'
202
+ end
203
+
204
+ p.attribute do |a|
205
+ a.name = 'ativo'
206
+ a.type = 'boolean'
207
+ end
208
+ end
209
+
210
+ project = MDWA::DSL.entity("Project")
211
+ group = MDWA::DSL.entity("Group")
212
+
213
+ project.scaffold_name.must_equal 'a/project'
214
+ project.model_name.must_equal 'a/project'
215
+
216
+ project.generate.must_equal "mdwa:scaffold a/project nome:string ativo:boolean situacao_atual:text group:a/group:nome:belongs_to --ajax"
217
+ group.generate.must_equal "mdwa:scaffold a/group nome:string ativo:boolean --ajax --force"
218
+ end
219
+
220
+ it "should not generate non-resource entities" do
221
+ MDWA::DSL.entities.register "Task" do |p|
222
+ p.resource = false
223
+ p.scaffold_name = 'a/task'
224
+ p.ajax = true
225
+ p.force = true
226
+
227
+ p.attribute do |a|
228
+ a.name = 'name'
229
+ a.type = 'string'
230
+ end
231
+
232
+ p.attribute do |a|
233
+ a.name = 'active'
234
+ a.type = 'boolean'
235
+ end
62
236
  end
63
- MDWA::DSL.entity("Product").resource.must_equal false
64
237
 
238
+ task = MDWA::DSL.entity('Task')
239
+ task.generate.must_equal nil
65
240
  end
66
241
 
67
242
  end