Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Last active January 31, 2024 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thdaraujo/7d9aa5ffd1b61bf57604ded14535e357 to your computer and use it in GitHub Desktop.
Save thdaraujo/7d9aa5ffd1b61bf57604ded14535e357 to your computer and use it in GitHub Desktop.
Rails `in_order_of` with pg enum columns
# 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