This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def with_characteristics?(characteristics_hash) | |
author.name == characteristics_hash[:author_name] && | |
publisher.name == characteristics_hash[:publisher_name] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def by_bce_autor? | |
author.bce? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vampire_title? | |
title.to_s.match?(%r'vamp[iy]re'i) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |