lib/infer_model/to/migration.rb in infer_model-0.1.1 vs lib/infer_model/to/migration.rb in infer_model-0.1.2

- old
+ new

@@ -8,13 +8,15 @@ extend InferModel::Callable TIMESTAMP_FORMAT = "%Y%m%d%H%M%S" param :model - option :target_dir, default: -> { "db/migrate" } - option :table_name, optional: true + option :attributes_map, optional: true + option :filter_on_attribute_mapping, default: -> { true } option :rails_version, default: -> { "7.0" } + option :table_name, optional: true + option :target_dir, default: -> { "db/migrate" } def call FileUtils.mkdir_p(target_dir) File.write(migration_filename, migration_content) end @@ -28,10 +30,20 @@ def given_or_inferred_tablename table_name || model.source_name.pluralize end + def given_or_mapped_attributes + return model.attributes unless attributes_map + + if filter_on_attribute_mapping + attributes_map.to_h { |old_key, new_key| [new_key, model.attributes[old_key]] } + else + model.attributes.transform_keys { |key| attributes_map.fetch(key, key) } + end + end + def migration_content <<~RUBY # frozen_string_literal: true class Create#{given_or_inferred_tablename.camelize} < ActiveRecord::Migration[#{rails_version}] @@ -47,16 +59,16 @@ end COLUMN_DDL_LINES_WITH_INDENTATION_JOINER = "\n#{" " * 3}".freeze def column_ddl_lines - column_definitions = model.attributes.map do |key, common_type| + column_definitions = given_or_mapped_attributes.map do |key, common_type| attribute_and_name = %(t.#{common_type.detected_type} "#{key}") non_null_constraint = common_type.non_null_constraint_possible ? "null: false" : nil [attribute_and_name, non_null_constraint].compact.join(", ") end - index_definitions = model.attributes.filter_map do |key, common_type| + index_definitions = given_or_mapped_attributes.filter_map do |key, common_type| next unless common_type.unique_constraint_possible %(t.index ["#{key}"], unique: true) end