app/controllers/erd/erd_controller.rb in erd-0.1.6 vs app/controllers/erd/erd_controller.rb in erd-0.2.0

- old
+ new

@@ -26,33 +26,33 @@ begin action, model, column, from, to = row['action'], row['model'].tableize, row['column'], row['from'], row['to'] case action when 'remove_model' generated_migration_file = Erd::Migrator.execute_generate_migration "drop_#{model}" - gsub_file generated_migration_file, /def up.* end/m, "def change\n drop_table :#{model}\n end" + gsub_file generated_migration_file, /def (up|change).* end/m, "def change\n drop_table :#{model}\n end" Erd::Migrator.run_migrations :up => generated_migration_file executed_migrations << generated_migration_file when 'rename_model' from, to = from.tableize, to.tableize generated_migration_file = Erd::Migrator.execute_generate_migration "rename_#{from}_to_#{to}" - gsub_file generated_migration_file, /def up.* end/m, "def change\n rename_table :#{from}, :#{to}\n end" + gsub_file generated_migration_file, /def (up|change).* end/m, "def change\n rename_table :#{from}, :#{to}\n end" Erd::Migrator.run_migrations :up => generated_migration_file executed_migrations << generated_migration_file when 'add_column' name_and_type = column.scan(/(.*)\((.*?)\)/).first name, type = name_and_type[0], name_and_type[1] generated_migration_file = Erd::Migrator.execute_generate_migration "add_#{name}_to_#{model}", ["#{name}:#{type}"] Erd::Migrator.run_migrations :up => generated_migration_file executed_migrations << generated_migration_file when 'rename_column' generated_migration_file = Erd::Migrator.execute_generate_migration "rename_#{model}_#{from}_to_#{to}" - gsub_file generated_migration_file, /def up.* end/m, "def change\n rename_column :#{model}, :#{from}, :#{to}\n end" + gsub_file generated_migration_file, /def (up|change).* end/m, "def change\n rename_column :#{model}, :#{from}, :#{to}\n end" Erd::Migrator.run_migrations :up => generated_migration_file executed_migrations << generated_migration_file when 'alter_column' generated_migration_file = Erd::Migrator.execute_generate_migration "change_#{model}_#{column}_type_to_#{to}" - gsub_file generated_migration_file, /def up.* end/m, "def change\n change_column :#{model}, :#{column}, :#{to}\n end" + gsub_file generated_migration_file, /def (up|change).* end/m, "def change\n change_column :#{model}, :#{column}, :#{to}\n end" Erd::Migrator.run_migrations :up => generated_migration_file executed_migrations << generated_migration_file when 'move' json_file = Rails.root.join('tmp', 'erd_positions.json') positions = json_file.exist? ? ActiveSupport::JSON.decode(json_file.read) : {} @@ -75,25 +75,26 @@ end private def render_plain(plain, positions) _scale, svg_width, svg_height = plain.scan(/\Agraph ([0-9\.]+) ([0-9\.]+) ([0-9\.]+)$/).first - ratio = [BigDecimal('4800') / BigDecimal(svg_width), BigDecimal('3200') / BigDecimal(svg_height), 180].min # node name x y width height label style shape color fillcolor models = plain.scan(/^node ([^ ]+) ([0-9\.]+) ([0-9\.]+) ([0-9\.]+) ([0-9\.]+) <\{?(<((?!^\}?>).)*)^\}?> [^ ]+ [^ ]+ [^ ]+ [^ ]+\n/m).map {|node_name, x, y, width, height, label| label_doc = Nokogiri::HTML::DocumentFragment.parse(label) - model_name = node_name.sub(/^m_/, '') + model_name = node_name.dup + model_name[0] = model_name[-1] = '' if (model_name.first == '"') && (model_name.last == '"') + model_name = model_name.sub(/^m_/, '') next if model_name == 'ActiveRecord::SchemaMigration' columns = [] if (cols_table = label_doc.search('table')[1]) columns = cols_table.search('tr > td').map {|col| col_name, col_type = col.text.split(' '); {:name => col_name, :type => col_type}} end custom_x, custom_y = positions[model_name.tableize].try(:split, ',') - {:model => model_name, :x => (custom_x || BigDecimal(x) * ratio), :y => (custom_y || BigDecimal(y) * ratio), :width => BigDecimal(width) * ratio, :height => height, :columns => columns} + {:model => model_name, :x => (custom_x || (BigDecimal(x) * 72).round), :y => (custom_y || (BigDecimal(y) * 72).round), :width => (BigDecimal(width) * 72).round, :height => height, :columns => columns} }.compact # edge tail head n x1 y1 .. xn yn [label xl yl] style color edges = plain.scan(/^edge ([^ ]+)+ ([^ ]+)/).map {|from, to| {:from => from.sub(/^m_/, ''), :to => to.sub(/^m_/, '')}} - render_to_string 'erd/erd/erd', :layout => nil, :locals => {:width => BigDecimal(svg_width) * ratio, :height => BigDecimal(svg_height) * ratio, :models => models, :edges => edges} + render_to_string 'erd/erd/erd', :layout => nil, :locals => {:width => (BigDecimal(svg_width) * 72).round, :height => (BigDecimal(svg_height) * 72).round, :models => models, :edges => edges} end def gsub_file(path, flag, *args, &block) path = File.expand_path path, Rails.root