Skip to content

Instantly share code, notes, and snippets.

@shirts
Last active January 24, 2020 16:39
Show Gist options
  • Save shirts/226da54d61a47f560cf19b18c4f0f5f0 to your computer and use it in GitHub Desktop.
Save shirts/226da54d61a47f560cf19b18c4f0f5f0 to your computer and use it in GitHub Desktop.
Sequel Playground

ruby migrate.rb create ruby migrate.rb drop

$ irb

require_relative 'sequel'

user = User.first

user.send_welcome_email

user.notifications

require 'sequel'
DB = Sequel.connect('postgres://postgres@localhost:5432/graveyard')
DB.extension :pg_enum
def drop_tables
[:users, :comments, :posts, :notifications].each do |table|
puts "Dropping table #{table}"
DB.drop_table table if DB.table_exists?(table)
end
end
def create_tables
unless DB.table_exists?(:users)
puts 'Creating users'
DB.create_table :users do
primary_key :id
String :first_name, null: false
String :last_name, null: false
end
end
unless DB.table_exists?(:comments)
puts 'Creating comments'
DB.create_table :comments do
primary_key :id
foreign_key :user_id, null: false
foreign_key :post_id, null: false
String :text, null: false
end
end
unless DB.table_exists?(:posts)
puts 'Creating posts'
DB.create_table :posts do
primary_key :id
foreign_key :user_id, null: false
String :title, null: false
String :body, null: false
end
end
unless DB.table_exists?(:notifications)
puts 'Creating notifications'
DB.drop_enum(:type)
DB.create_enum(:type, %w[password_reset rent_payment welcome_email])
DB.create_table :notifications do
primary_key :id
foreign_key :user_id
type :type
end
end
end
drop_tables if ARGV[0] == 'drop'
create_tables if ARGV[0] == 'create'
require 'sequel'
DATABASE_NAME = 'sequel_playground'
DB = Sequel.connect("postgres://postgres@localhost:5432/#{DATABASE_NAME}")
class User < Sequel::Model
one_to_many :comments
one_to_many :posts
one_to_many :notifications
def send_welcome_email
return if received_notification?(:welcome)
create_notification('welcome')
puts 'Welcome to the app!'
end
def rent_reminder
return if received_notification?(:rent_reminder)
create_notification('rent_reminder')
puts 'Time to pay your rent!'
end
def password_reset
create_notification('password_reset')
puts 'Someone has requested a password reset'
end
def received_notification?(type)
self.reload.notifications.any? do |notification|
notification.type == Notification::TYPES[type.to_sym]
end
end
def create_notification(type)
Notification.create(user_id: self.id, type: type.to_s)
end
end
class Post < Sequel::Model
many_to_one :user
one_to_many :comments
end
class Comment < Sequel::Model
many_to_one :post
many_to_one :user
end
class Notification < Sequel::Model
one_to_one :user
TYPES = {
welcome: 'welcome',
rent_reminder: 'rent_reminder',
password_reset: 'password_reset'
}
end
jane = User.create(
first_name: 'Jane',
last_name: 'Doe'
)
john = User.create(
first_name: 'John',
last_name: 'Brownstein'
)
Post.create(
user_id: jane.id,
title: 'How to Lose Weight',
body: 'blah blah blah'
)
['this post sucks', 'great post!', 'lol'].each do |text|
Comment.create(post_id: Post.first.id, text: text, user: john)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment