Method: Mdwa::Generators::CodeGenerator#entities_and_changes
- Defined in:
- lib/generators/mdwa/code/code_generator.rb
#entities_and_changes ⇒ Object
Generate code for entities or entity changes. Generate migration for field changes.
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 81 82 83 84 85 86 87 88 89 90 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 |
# File 'lib/generators/mdwa/code/code_generator.rb', line 53 def entities_and_changes return false if .only_interface @all_entities.each do |entity| # if it's not a resource, ignore next unless entity.resource? # if model has not a database yet, run the generate command begin # if model does not exist, should generate scaffold model_class = entity.generator_model.model_class rescue model_class = nil end if entity.force? or model_class.nil? or !model_class.table_exists? puts "====================================================" puts "Generating code for '#{entity.name}'" puts "====================================================" generation_string = "#{entity.generate} #{'--skip_rake_migrate' unless options.run_migrations} #{'--force' if options.force} --skip-questions" generate generation_string # append generated code to entity append_to_file "#{MDWA::DSL::STRUCTURAL_PATH}#{entity.file_name}.rb", "\n\nMDWA::DSL.entity('#{entity.name}').code_generations << '#{generation_string}'" next # nothing's changed, go to the next entity end next if entity.user? # check what changed model_class.columns.each do |column| # ignore rails default columns and attributes used in associations next if column.name == 'id' or column.name == 'created_at' or column.name == 'updated_at' or column.name.end_with? '_id' entity_attribute = entity.attributes[column.name] # model attribute exists, but not in entity -> was erased if entity_attribute.nil? @changes << {:entity => entity, :type => 'remove_column', :column => column.name, :attr_type => column.type} # attribute exists in model and entity, but changed type elsif entity_attribute.type.to_sym != column.type.to_sym next if entity_attribute.type.to_sym == :file or entity_attribute.type.to_sym == :password @changes << {:entity => entity, :type => 'change_column', :column => column.name, :attr_type => entity_attribute.type, :from => column.type} end end # new attributes entity.attributes.each do |key, attr| # no column with that name -> column must be added if 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 # generate changed code unless @changes.empty? migration_template 'migration.rb', "db/migrate/alter_#{@changes.collect{|c| c[:entity].file_name}.join('_')}#{@random_migration_key}.rb" end end |