Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created May 16, 2014 08:30
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 pixeltrix/e485b81fb8a7398ac3f4 to your computer and use it in GitHub Desktop.
Save pixeltrix/e485b81fb8a7398ac3f4 to your computer and use it in GitHub Desktop.
Response to James Coglan's 'An ActiveRecord conumdrum': https://gist.github.com/jcoglan/4ccf434cd2e1305d1c5c
require 'active_record'
require 'logger'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :texts do |t|
t.string :title
t.text :body
end
create_table :quotes do |t|
t.text :body
t.string :source
t.string :url
end
create_table :links do |t|
t.string :title
t.string :url
end
create_table :users do |t|
t.string :username
end
create_table :blogs do |t|
t.belongs_to :user
t.string :title
end
create_table :posts do |t|
t.timestamps
t.belongs_to :blog
t.belongs_to :content, polymorphic: true
end
create_table :comments do |t|
t.timestamps
t.belongs_to :user
t.belongs_to :post
t.text :body
t.boolean :accepted
end
end
class User < ActiveRecord::Base
has_many :blogs
has_many :comments
has_many :blog_comments, through: :blogs, source: :comments
end
class Blog < ActiveRecord::Base
belongs_to :user
has_many :posts
has_many :comments, through: :posts
end
class Post < ActiveRecord::Base
belongs_to :blog
belongs_to :content, polymorphic: true
has_many :comments
end
module Content
extend ActiveSupport::Concern
included do
has_many :posts, as: :content
has_many :comments, through: :posts
end
end
class Text < ActiveRecord::Base; include Content; end
class Quote < ActiveRecord::Base; include Content; end
class Link < ActiveRecord::Base; include Content; end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
has_one :blog, through: :post
def self.for_blog_author(author)
joins(:blog).where(blogs: { user_id: author.id })
end
def self.for_moderation
where(accepted: nil)
end
end
alice = User.create(username: 'alice')
bob = User.create(username: 'bob')
blog = alice.blogs.create(title: 'Not even once')
text_entry = Text.create(
title: 'How to bulk update a MySQL table',
body: 'First, delete all your indexes.'
)
quote_entry = Quote.create(
body: 'There are two hard things in computer science: MySQL consistency.',
source: 'CSS Perverts',
url: 'https://medium.com/could-you-not'
)
link_entry = Link.create(
title: 'Web development that does not always hurt',
url: 'http://rubyonrails.org/'
)
text_post = Post.create(blog: blog, content: text_entry)
quote_post = Post.create(blog: blog, content: quote_entry)
link_post = Post.create(blog: blog, content: link_entry)
Comment.create(
user: bob,
post: text_post,
body: 'This sounds like a lot of work.'
)
Comment.create(
user: bob,
post: text_post,
body: '4 hours later and I am still at my computer. Help!'
)
Comment.create(
user: bob,
post: quote_post,
body: 'Never were truer words spoken. Such thought leadership.'
)
class TestMe < MiniTest::Unit::TestCase
def setup
@alice = User.find_by_username!('alice')
end
def test_awaiting_moderation_count
assert_equal 3, Comment.for_blog_author(@alice).for_moderation.count
assert_equal 3, @alice.blog_comments.for_moderation.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment