Last active
January 31, 2024 16:10
-
-
Save thdaraujo/7d9aa5ffd1b61bf57604ded14535e357 to your computer and use it in GitHub Desktop.
Rails `in_order_of` with pg enum columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# how to run: | |
# $ ruby enum_in_order_of_test.rb | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "pg" | |
gem "rails", "7.1" | |
gem "byebug" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
ActiveRecord::Base.logger = Logger.new($stdout) | |
def db_setup! | |
config = {adapter: "postgresql", | |
host: "localhost", | |
port: "5432", | |
username: "postgres", | |
password: "postgres", | |
database: "example_rails_order_db"} | |
ActiveRecord::Base.establish_connection(config.except(:database)) | |
ActiveRecord::Base.connection.drop_database(config[:database]) | |
ActiveRecord::Base.connection.create_database(config[:database]) | |
ActiveRecord::Base.clear_active_connections! | |
# connect to new db | |
ActiveRecord::Base.establish_connection(config) | |
end | |
db_setup! | |
ActiveRecord::Schema.define do | |
create_enum :post_status, ["draft", "published", "archived"] | |
create_table :posts do |t| | |
t.enum :status, enum_type: "post_status", default: "draft", null: false | |
t.timestamps | |
end | |
create_table :conversations do |t| | |
t.integer "status" | |
t.timestamps | |
end | |
end | |
class Post < ActiveRecord::Base | |
enum status: { | |
draft: "draft", | |
published: "published", | |
archived: "archived" | |
} | |
validates :status, presence: true, inclusion: {in: statuses.keys} | |
end | |
class Conversation < ActiveRecord::Base | |
enum status: [:active, :archived] | |
end | |
class OrderByEnumTest < Minitest::Test | |
def test_in_order_of | |
Post.create!(status: :draft) | |
Post.create!(status: :published) | |
Post.create!(status: :archived) | |
Post.create!(status: :archived) | |
expected = ["archived", "archived", "draft", "published"] | |
assert_equal expected, | |
Post.in_order_of(:status, [:archived, :draft, :published]).map(&:status), | |
"should respect the given order" | |
end | |
def test_in_order_of_integer_enum | |
Conversation.create!(status: :active) | |
Conversation.create!(status: :archived) | |
Conversation.create!(status: nil) | |
assert_equal ["archived", "active"], | |
Conversation.in_order_of(:status, [:archived, :active]).map(&:status), | |
"should be in order of [archived, active]" | |
assert_equal [nil, "archived", "active"], | |
Conversation.in_order_of(:status, [nil, :archived, :active]).map(&:status), | |
"should be in order of [nil, archived, active]" | |
assert_equal [nil, "archived"], | |
Conversation.in_order_of(:status, [nil, :archived]).map(&:status), | |
"should be in order of [nil, archived]" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment