Files
has_aux_table/demo_functionality.rb
2025-07-15 03:50:34 +00:00

135 lines
3.5 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require "bundler/setup"
require_relative "lib/has_aux_table"
Bundler.require(:default, :development)
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: ":memory:"
)
# ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
# Create base table and aux table in the same block
create_base_table :users do |t|
t.create_aux :fa do |t|
t.string :username, index: true
t.string :url_name, index: true
end
t.timestamps
end
# Change base table later to add an aux table
change_base_table :users do |t|
t.create_aux :e621 do |t|
t.integer :e621_id, index: true
t.string :username, index: true
end
end
create_base_table :posts do |t|
t.timestamp :posted_at, index: true
t.timestamps
end
# Directly create aux tables
create_aux_table :posts, :fa do |t|
t.references :creator,
foreign_key: {
to_table: :users_fa_aux,
primary_key: :base_table_id
}
t.integer :fa_id, index: true
t.string :title
t.string :species
end
create_aux_table :posts, :e621 do |t|
t.integer :e621_id, index: true
t.string :md5, index: true
end
create_table :user_post_fav_joins, id: false do |t|
t.references :user, null: false, foreign_key: { to_table: :users }
t.references :post, null: false, foreign_key: { to_table: :posts }
t.timestamps
end
end
class UserPostFavJoin < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
class User < ActiveRecord::Base
include HasAuxTable
has_many :user_post_fav_joins, dependent: :destroy
has_many :favorite_posts, through: :user_post_fav_joins, source: :post
has_many :created_posts, class_name: "Post", inverse_of: :creator
end
class FaUser < User
aux_table :fa
validates :url_name, presence: true
has_many :created_posts, class_name: "FaPost", inverse_of: :creator
end
class E621User < User
aux_table :e621
validates :username, presence: true
has_many :created_posts, class_name: "E621Post", inverse_of: :creator
end
class Post < ActiveRecord::Base
include HasAuxTable
has_many :user_post_fav_joins, dependent: :destroy
has_many :favoriting_users, through: :user_post_fav_joins, source: :user
validates_presence_of :posted_at
end
class FaPost < Post
aux_table :fa
belongs_to :creator, class_name: "FaUser", inverse_of: :created_posts
validates :fa_id, presence: true, uniqueness: true
validates :title, presence: true
end
class E621Post < Post
aux_table :e621
validates :e621_id, presence: true
belongs_to :creator, class_name: "E621User", inverse_of: :created_posts
end
# ActiveRecord::Base.logger = Logger.new(STDOUT)
fa_user = FaUser.create!(username: "Alice", url_name: "alice")
# puts "Does the post exist? #{FaPost.exists?(fa_id: 1)}"
attrs = {
# creator_id: fa_user.id,
fa_id: 12_345,
title: "Test Post",
species: "Cat",
posted_at: 1.day.ago
}
fa_post = fa_user.created_posts.create!(attrs)
raise unless fa_post.persisted?
raise unless fa_post.creator == fa_user
raise unless fa_post.creator_id == fa_user.id
fa_posts_all = FaPost.all.to_a
raise unless fa_posts_all.size == 1
raise unless fa_posts_all.first.creator == fa_user
raise unless fa_posts_all.first.creator_id == fa_user.id
# e621_user = E621User.create!(username: "bob", e621_id: 67_890)
# e621_post =
# E621Post.create!(
# e621_id: 102_938,
# md5: "DEADBEEF" * 4,
# posted_at: 2.weeks.ago
# )
# e621_user.favorite_posts << e621_post