From 171b2e945240bd7a52e9c2d7ebd4665c74361a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Thibout=C3=B4t?= Date: Fri, 22 Apr 2022 05:17:51 -0400 Subject: [PATCH] Add basic column type conversion. --- lib/generators/graphiti/resource_generator.rb | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/generators/graphiti/resource_generator.rb b/lib/generators/graphiti/resource_generator.rb index 7580fd4..dcff131 100644 --- a/lib/generators/graphiti/resource_generator.rb +++ b/lib/generators/graphiti/resource_generator.rb @@ -100,7 +100,10 @@ def default_attributes end if attributes_class.table_exists? return attributes_class.columns.map do |c| - OpenStruct.new({name: c.name.to_sym, type: c.type}) + OpenStruct.new({ + name: c.name.to_sym, + type: convert_column_type_to_graphiti_resource_type(c.type) + }) end else raise "#{attributes_class} table must exist. Please run migrations." @@ -181,5 +184,34 @@ def resource_klass def type model_klass.name.underscore.pluralize end + + def convert_column_type_to_graphiti_resource_type(type) + # TODO: Support database specific types. + case type + when :string, :text + :string + when :float, :decimal + :integer + when :integer, :bigint + :integer + when :datetime, :time + :datetime + when :date + :date + when :boolean + :boolean + when :numeric + # TODO: Return type. + type + when :primary_key + # TODO: Return type. + type + when :binary + # TODO: Return type. + type + else + type + end + end end end