Skip to content

Instantly share code, notes, and snippets.

Augusts Bautra Epigene

Block or report user

Report or block Epigene

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Epigene
Epigene / book.rb
Created Oct 14, 2017
Matching author and publisher names?
View book.rb
def with_characteristics?(characteristics_hash)
author.name == characteristics_hash[:author_name] &&
publisher.name == characteristics_hash[:publisher_name]
end
@Epigene
Epigene / bout3.rb
Created Oct 14, 2017
FC.build_stubbed vs other FC strategies
View bout3.rb
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 Oct 14, 2017
#build_stubbed vs FactoryGirl strategies
View bout2.rb
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 Oct 14, 2017
Ancient author?
View book.rb
def by_bce_autor?
author.bce?
end
@Epigene
Epigene / bout1.rb
Created Oct 14, 2017
#new VS all other setup strategies
View bout1.rb
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 Oct 14, 2017
Book about vampires?
View book.rb
def vampire_title?
title.to_s.match?(%r'vamp[iy]re'i)
end
View Pure SQL.rb
# 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
View the ok.rb
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)
View get a query object.rb
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)
View Loop over users.rb
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 }
You can’t perform that action at this time.