From 8bffcab1bb3d039873582ffac11d6f25d73efd63 Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Tue, 19 Nov 2024 21:21:52 +0900 Subject: [PATCH] Define underlying type for enums to be always string --- lib/typelizer/model_plugins/active_record.rb | 1 + spec/app/app/models/post.rb | 2 +- spec/app/db/migrate/20240707052907_create_posts.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/typelizer/model_plugins/active_record.rb b/lib/typelizer/model_plugins/active_record.rb index 3f03996..69b4377 100644 --- a/lib/typelizer/model_plugins/active_record.rb +++ b/lib/typelizer/model_plugins/active_record.rb @@ -28,6 +28,7 @@ def infer_types(prop) prop.type = @config.type_mapping[column.type] prop.comment = comment_for(prop) prop.enum = enum_for(prop) + prop.type = :string if prop.enum # Ignore underlying column type for enums prop end diff --git a/spec/app/app/models/post.rb b/spec/app/app/models/post.rb index 952858d..012bf50 100644 --- a/spec/app/app/models/post.rb +++ b/spec/app/app/models/post.rb @@ -1,5 +1,5 @@ class Post < ApplicationRecord belongs_to :user - enum category: [:news, :article, :blog].index_by(&:itself) + enum category: { news: 1, article: 2, blog: 3 } end diff --git a/spec/app/db/migrate/20240707052907_create_posts.rb b/spec/app/db/migrate/20240707052907_create_posts.rb index 1617ecb..db84a9c 100644 --- a/spec/app/db/migrate/20240707052907_create_posts.rb +++ b/spec/app/db/migrate/20240707052907_create_posts.rb @@ -2,7 +2,7 @@ class CreatePosts < ActiveRecord::Migration[7.1] def change create_table :posts do |t| t.string :title - t.string :category + t.integer :category t.text :body t.datetime :published_at t.references :user, null: false, foreign_key: true