Class: Mdwa::Generators::TransformGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/mdwa/transform/transform_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ TransformGenerator

Returns a new instance of TransformGenerator.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 24

def initialize(*args, &block)
  super
  
  # control if there are any migrations to execute
  @pending_migrations = false
  
  # include files with entities
  inside Rails.root do
    require_all MDWA::DSL::STRUCTURAL_PATH unless Dir.glob("#{MDWA::DSL::STRUCTURAL_PATH}/*.rb").count.zero?
  end
  # select entities that will be generated
  if entities.count.zero?
    @entities = MDWA::DSL.entities.all 
  else
    @entities = entities.collect{ |e| MDWA::DSL.entity(e) }
  end
  
  # entity changes and migrations
  @changes = []
  @random_migration_key = rand.to_s.gsub('.','').to_i
  
  run 'rake db:migrate'
end

Instance Attribute Details

#pending_migrationsObject

Returns the value of attribute pending_migrations.



17
18
19
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 17

def pending_migrations
  @pending_migrations
end

Instance Method Details

#create_menu_itemObject

def



82
83
84
85
86
87
88
89
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 82

def create_menu_item
  @entities.each do |entity|
    generator_model = entity.generator_model
    insert_into_file 'app/views/template/mdwa/_menubar.html.erb', before: '</ul>' do
      "  <%= render '/template/mdwa/menubar/#{generator_model.plural_name}' %>\n"
    end
  end
end

#generate_changes_in_attributesObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 259

def generate_changes_in_attributes
  
  @entities.each do |entity|
    # do not generate migrations for users changes
    next if entity.user?
    # if it's not a resource, ignore
    next unless entity.resource?
    
    # if model does not exist, should generate scaffold
    begin
      model_class = entity.generator_model.model_class
    rescue
      model_class = nil
    end
    
    # if table is not created yet, ignore
    next if model_class.nil? or !model_class.table_exists?
    
    # search for changes in this entity
    model_class.columns.each do |column|
      
      # ignore rails default columns and attributes used in associations
      next if column.name.end_with? '_id'
                  
      entity_attribute = entity.attributes[column.name]
      # model attribute exists, but not in entity -> was erased
      if entity_attribute.nil?
        # atributo não é derivado de file, pode apagar na moral
        if !column.name.ends_with?("_file_name") and !column.name.ends_with?("_content_type") and !column.name.ends_with?("_file_size") and !column.name.ends_with?("_updated_at") 
          @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type}
        else
          # se o atributo é derivado de file e não existe o file na entidade, apaga
          @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type} if column.name.ends_with?("_file_name") and entity.attributes[column.name.gsub("_file_name", '')].nil?
          @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type} if column.name.ends_with?("_content_type") and entity.attributes[column.name.gsub("_content_type", '')].nil?
          @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type} if column.name.ends_with?("_file_size") and entity.attributes[column.name.gsub("_file_size", '')].nil?
          @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type} if column.name.ends_with?("_updated_at") and entity.attributes[column.name.gsub("_updated_at", '')].nil?
        end
      # attribute exists in model and entity, but changed type
      elsif entity_attribute.type.to_sym != column.type.to_sym
        # ignores files, passwords and float, decimal, integer variations
        next if entity_attribute.type.to_sym == :password or (entity_attribute.type.to_sym == :status and column.type.to_sym == :integer) or ((column.type.to_sym == :integer or column.type.to_sym == :decimal) and entity_attribute.type.to_sym == :float)
        @changes << {:entity => entity, :type => 'change_column', :column => column.name, :attr_type => entity_attribute.type, :from => column.type}
      end
    end
    
    # new attributes
    # no column with that name -> column must be added
    entity.attributes.each do |key, attr|
      # se o atributo for file e não existir seus atributos no banco de dados, corrige
      if attr.type.to_sym == :file
        @changes << {:entity => entity, :type => 'add_column', :column => "#{attr.name}_file_name", :attr_type => 'string'} if model_class.columns.select{|c| c.name == "#{attr.name}_file_name"}.count.zero?
        @changes << {:entity => entity, :type => 'add_column', :column => "#{attr.name}_content_type", :attr_type => 'string'} if model_class.columns.select{|c| c.name == "#{attr.name}_content_type"}.count.zero?
        @changes << {:entity => entity, :type => 'add_column', :column => "#{attr.name}_file_size", :attr_type => 'integer'} if model_class.columns.select{|c| c.name == "#{attr.name}_file_size"}.count.zero?
        @changes << {:entity => entity, :type => 'add_column', :column => "#{attr.name}_updated_at", :attr_type => 'datetime'} if model_class.columns.select{|c| c.name == "#{attr.name}_updated_at"}.count.zero?
      
      # nenhuma coluna com esse nome
      elsif model_class.columns.select {|c| c.name == attr.name }.count.zero?
        @changes << {:entity => entity, :type => 'add_column', :column => attr.name, :attr_type => attr.type}
      end
    end
    
    # new foreign keys
    # belongs_to and nested_one associations that are in the entity, but not database
    entity.generator_model.associations.select{|a| a.belongs_to? or a.nested_one?}.each do |assoc|
      if model_class.columns.select{|c| c.name == assoc.model2.singular_name.foreign_key}.count.zero?
        @changes << {:entity => entity, :type => 'add_column', :column => assoc.model2.name.foreign_key, :attr_type => 'integer'}
      end
    end
    
  end # @entities loop
  
  # generate changed code
  unless @changes.empty?
    migration_template 'changes_migration.rb', "db/migrate/alter_#{@changes.collect{|c| c[:entity].file_name}.join('_')}#{@random_migration_key}.rb"
    @pending_migrations = true
  end
  
end

#generate_localesObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 140

def generate_locales

  return nil if options.skip_locales

  locales_file = 'config/locales/mdwa.specific.en.yml'
  # make sure the file exist
  create_file locales_file unless File.exist?(Rails.root + locales_file)
  locales_content = File.read(locales_file)

  # translated
  if I18n.locale.to_sym != :en
    locales_translated_file = "config/locales/mdwa.specific.#{I18n.locale}.yml"
    create_file locales_translated_file unless File.exist?(Rails.root + locales_translated_file)
    locales_translated_content = File.read(locales_translated_file)
  end
  
  @entities.each do |entity|
    model = entity.generator_model

    lines = []
    lines << "\n"
    lines <<  "  #{model.plural_name}:"
    lines <<  "    notice:"
    lines <<  "      create: \"#{model.singular_name.humanize} created.\""
    lines <<  "      update: \"#{model.singular_name.humanize} updated.\""
    lines <<  "      destroy: \"#{model.singular_name.humanize} destroyed.\""
    lines <<  "      batch_update: \"#{model.singular_name.humanize} updated.\""
    lines <<  "    title:"
    lines <<  "      index: \"#{model.plural_name.humanize}\""
    lines <<  "      show: \"#{model.singular_name.humanize}\""
    lines <<  "      new: \"New #{model.singular_name.humanize}\""
    lines <<  "      edit: \"Edit #{model.singular_name.humanize}\""
    lines <<  "    menu:"
    lines <<  "      index: \"#{model.plural_name.humanize}\""
    lines <<  "      new: \"New #{model.singular_name.humanize}\""
    # Status
    if entity.attributes.select{|name, attr| attr.type.to_sym == :status}.count > 0
      entity.attributes.select{|name, attr| attr.type.to_sym == :status}.each do |name, attr|
        lines <<  "    #{name}:"
        lines <<  "      prompt_select: '- Status -'"
        attr.options[:possible_values].each_with_index do |value, index|
          lines <<  "      #{value.to_s.underscore}: '#{value.to_s.humanize}'"
          lines <<  "      #{value.to_s.underscore}_alter: 'Change #{name} to #{value.to_s.humanize}'"
        end
      end
    end 
    # INDEX
    lines <<  "    index:"
    lines <<  "      add: 'Add'"
    lines <<  "      edit: 'Edit'"
    lines <<  "      edit_label: 'Edit'"
    lines <<  "      remove: 'Remove'"
    lines <<  "      remove_label: 'Remove'"
    lines <<  "      confirm_deletion: 'Are you sure?'"
    model.attributes.each do |attr|
      lines <<  "      #{attr.name}: \"#{attr.name.humanize}\""
    end
    model.associations.each do |assoc|
      lines << ((assoc.belongs_to? or assoc.nested_one? or assoc.has_one?) ? "      #{assoc.model2.singular_name}: \"#{assoc.model2.singular_name.humanize}\"" : "      #{assoc.model2.singular_name}: \"#{assoc.model2.plural_name.humanize}\"")
    end
    # EDIT
    lines <<  "    edit:"
    model.attributes.each do |attr|
      lines <<  "      #{attr.name}: \"#{attr.name.humanize}\""
    end
    model.associations.each do |assoc|
      lines << ((assoc.belongs_to? or assoc.nested_one? or assoc.has_one?) ? "      #{assoc.model2.singular_name}: \"#{assoc.model2.singular_name.humanize}\"" : "      #{assoc.model2.singular_name}: \"#{assoc.model2.plural_name.humanize}\"")
    end
    # SHOW
    lines <<  "    show:"
    model.attributes.each do |attr|
      lines <<  "      #{attr.name}: \"#{attr.name.humanize}\""
    end
    model.associations.each do |assoc|
      lines << ((assoc.belongs_to? or assoc.nested_one? or assoc.has_one?) ? "      #{assoc.model2.singular_name}: \"#{assoc.model2.singular_name.humanize}\"" : "      #{assoc.model2.singular_name}: \"#{assoc.model2.plural_name.humanize}\"")
    end
    lines << "\n"

    if !locales_content.include?( "  #{model.plural_name}:" )
      append_file locales_file, lines.join("\n"), :after => "en:"
    end

    if I18n.locale.to_sym != :en and !locales_translated_content.include?( "  #{model.plural_name}:" )
      append_file locales_translated_file, lines.join("\n"), :after => "#{I18n.locale}:"
    end
    
  end # @entities loop

end

#generate_migrationsObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 230

def generate_migrations
  
  @entities.each do |entity|
    # if it's not a resource, ignore
    next unless entity.resource?
  
    # if model does not exist, should generate scaffold
    begin
      model_class = entity.generator_model.model_class
    rescue
      model_class = nil
    end
    
    # if is a new scaffold, generate migration for scaffold
    if (model_class.nil? or !model_class.table_exists?) and !entity.user?
      migration_for_entity(entity) 
      next
    end
    
    # generate new fields for users
    if entity.user?
      generation_string = "#{entity.generate} --only_diff_migration --skip_rake_migrate --skip-questions #{'--force' if options.force}"
      generate generation_string
    end
    
  end # @entities loop
end

#generate_model_controller_helper_viewsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 48

def generate_model_controller_helper_views
  @entities.each do |entity|
    generator_model = entity.generator_model

    #
    # Para cada namespace, gera seu código  
    #
    # Pasta onde o código gerado vai ser colocado
    # Namespace 'frontend' é o público e gera código nas raízes do Rails        
    namespaces = Dir.glob("#{Rails.root}/#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/*").select{|d| File.directory?(d)}.collect {|n| File.basename(n)}
    if !namespaces.count.zero?
      namespaces.each do |namespace|

        namespace_destino = (namespace != 'frontend') ? namespace : ''
        templates_deste_namespace = MDWA::DSL::TEMPLATES_PATH + entity.file_name + '/' + namespace + '/'

        # Gera model, controllers e helper
        mdwa_template "#{entity.file_name}/#{namespace}/model.erb", "app/models/#{namespace_destino}/#{generator_model.singular_name}.rb" if File.exists?(templates_deste_namespace + 'model.erb')
        mdwa_template "#{entity.file_name}/#{namespace}/helper.erb", "app/helpers/#{namespace_destino}/#{generator_model.plural_name}_helper.rb"  if File.exists?(templates_deste_namespace + 'helper.erb')
        mdwa_template "#{entity.file_name}/#{namespace}/controller.erb", "app/controllers/#{namespace_destino}/#{generator_model.plural_name}_controller.rb"  if File.exists?(templates_deste_namespace + 'controller.erb')
        
        # Gera as views
        Dir.glob("#{templates_deste_namespace}/views/*").select{|d| !File.directory?(d)}.each do |file|
          file_name = File.basename(file)
          mdwa_template "#{entity.file_name}/#{namespace}/views/#{file_name}", "app/views/#{namespace_destino}/#{generator_model.plural_name}/#{file_name}"
        end
        # Item de menu
        mdwa_template "#{entity.file_name}/#{namespace}/views/menu/menu.html.erb", "app/views/template/mdwa/menubar/_#{generator_model.plural_name}.html.erb"

      end # nm.each
    end # if nm == 0
  end # each
end

#generate_routesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 91

def generate_routes
  
  route 'mdwa_router(self)'
  path_to_routes = 'app/mdwa/templates/routes.rb'
  insert_into_file 'config/routes.rb', "load File.expand_path('../../#{path_to_routes}', __FILE__)\n\n", :before => /.+::Application\.routes\.draw do(?:\s*\|map\|)?\s*$/
  
  # clear routes file contents
  File.truncate(path_to_routes, 0)
  append_file path_to_routes, "def mdwa_router(router)\n\nend"

  inject_into_file path_to_routes, :after => "def mdwa_router(router)\n" do
  "\n# Software visualization
    namespace :mdwa do
controller :requirements do
  get 'requirements/index' => 'requirements#index', :as => 'requirements'
  get 'requirements/:alias' => 'requirements#show', :as => 'requirement'
end

root :to => 'requirements#index'
    end"
  end
  
  MDWA::DSL.entities.all.each do |entity|
    generator_model = entity.generator_model
    
    # inject scaffold code
    inject_into_file path_to_routes, :after => "def mdwa_router(router)\n" do
      route_str = []
      route_str << "\n  namespace :#{generator_model.space} do" if generator_model.namespace?
      route_str << "    controller :#{generator_model.plural_name} do"
      route_str << "      post '#{generator_model.plural_name}/batch_update' => :batch_update, as: :#{generator_model.plural_name}_batch_update"
      route_str << "    end"
      route_str << "    resources :#{generator_model.plural_name}"
      route_str << "  end\n" if generator_model.namespace?
          
      route_str.join "\n"
    end
  
    # inject specific actions
    inject_into_file path_to_routes, :after => "controller :#{generator_model.plural_name} do" do
      routes = []
      entity.actions.generate_routes.each do |action_name, generation_string|
        routes << "\n\t#{generation_string}"
      end
      routes.join
    end
  end
end

#generate_testsObject



352
353
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 352

def generate_tests
end

#many_to_many_tablesObject

Search for many to many tables.



340
341
342
343
344
345
346
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 340

def many_to_many_tables
  @entities.each do |entity|
    entity.generator_model.associations.select{|a| a.has_and_belongs_to_many?}.each do |association|
      generate "mdwa:association #{association.model1.singular_name} has_and_belongs_to_many #{association.model2.singular_name} --skip-models"
    end
  end
end

#run_rake_migrateObject



348
349
350
# File 'lib/generators/mdwa/transform/transform_generator.rb', line 348

def run_rake_migrate
  rake('db:migrate') if @pending_migrations and yes?('Run rake db:migrate?')
end