Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Epigene's full-sized avatar

Augusts Bautra Epigene

View GitHub Profile
@Epigene
Epigene / book.rb
Created October 14, 2017 16:57
Matching author and publisher names?
def with_characteristics?(characteristics_hash)
author.name == characteristics_hash[:author_name] &&
publisher.name == characteristics_hash[:publisher_name]
end
@Epigene
Epigene / bout3.rb
Created October 14, 2017 16:53
FC.build_stubbed vs other FC strategies
describe "FactoryGirl.build_stubbed VS [FactoryGirl.build, FactoryGirl.create]" do
subject { book.with_characteristics?(options) }
let(:options) { {author_name: book.author.name, publisher_name: book.publisher.name} }
bout3_number_of = 200
context "when setting up with FactoryGirl.build_stubbed" do
let(:book) { build_stubbed(:book) }
@Epigene
Epigene / bout2.rb
Created October 14, 2017 16:52
#build_stubbed vs FactoryGirl strategies
describe "#instance_double VS FactoryGirl.build_stubbed, FactoryGirl.build, and FactoryGirl.create" do
subject { book.by_bce_autor? }
bout2_number_of = 200
context "when setting up with #instance_double" do
let(:book) { build_stubbed(:book) }
let(:author) { instance_double("Author", bce?: true) }
before { allow(book).to receive(:author).and_return(author) }
@Epigene
Epigene / book.rb
Created October 14, 2017 16:47
Ancient author?
def by_bce_autor?
author.bce?
end
@Epigene
Epigene / bout1.rb
Created October 14, 2017 16:45
#new VS all other setup strategies
describe "#new VS all others when testing Book#vampire_title?" do
subject { book.vampire_title? }
bout1_number_of = 200
context "when setting up with #new" do
bout1_number_of.times do
let(:book) { Book.new(title: "Vampire Literature, a Historical Perspective") }
it { is_expected.to eq(true) }
@Epigene
Epigene / book.rb
Created October 14, 2017 16:43
Book about vampires?
def vampire_title?
title.to_s.match?(%r'vamp[iy]re'i)
end
@Epigene
Epigene / Pure SQL.rb
Last active July 29, 2017 20:35
The WOW
# from https://stackoverflow.com/a/123481/3319298
# Uses LEFT JOIN to join the posts table with itself in a clever manner - on id AND the relevant, :created_at row.
# The check on created_at makes it so that the last records have NULLs in t2 rows, so we use WHERE to select those and done.
scope :users_last_posts, -> {
query = <<~HEREDOC
SELECT t1.id
FROM post t1
LEFT OUTER JOIN posts t2
ON t1.author_id = t2.author_id AND
@Epigene
Epigene / the ok.rb
Created July 29, 2017 19:56
Use SQL!
last_post_ids = Post.select("MAX(id) AS last_post_id").group(:user_id)
last_posts = Post.where(id: last_post_ids).order(created_at: :desc)
last_post_ids = []
User.where(id: Post.all.distinct.pluck(:user_id)).find_each |user|
last_post_ids << user.posts.order(created_at: :desc).limit(1).pluck(:id).last
end
last_posts = Post.where(id: last_post_ids).order(created_at: :desc)
@Epigene
Epigene / Loop over users.rb
Last active July 29, 2017 19:39
The ugly
last_posts = []
User.all.each |user|
last_posts << user.posts.order(created_at: :asc).last
end
# oops, some users have no posts and the array has nils
last_posts = last_posts.compact
# now to have them in correct order
last_posts = last_posts.sort{ |x, y| y.created_at <=> x.created_at }