This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make homepage smarter about finding random submissions
- Loading branch information
Showing
1 changed file
with
17 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
class Homepage | ||
def initialize(publication) | ||
magazine = publication.magazines.published.order("random()").first | ||
magazine = magazine_for(publication) | ||
@hook = nil unless magazine | ||
defined?(@hook) or @hook = loop do | ||
hook = magazine.submissions.published.order("random()").first | ||
break hook unless hook.photo? | ||
end | ||
defined?(@hook) or @hook = submission_for(magazine) | ||
end | ||
|
||
def hook(&block) | ||
yield(@hook) if @hook | ||
end | ||
|
||
private | ||
|
||
def magazine_for(publication) | ||
publication.magazines. | ||
published. | ||
order("random()").first | ||
end | ||
|
||
def submission_for(magazine) | ||
magazine.submissions. | ||
published. | ||
where("photo_file_name IS NULL"). | ||
order("random()").first | ||
end | ||
end |