Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Make homepage smarter about finding random submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
chadoh committed Nov 1, 2013
1 parent 823f079 commit 2dc19ba
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/homepage.rb
@@ -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

0 comments on commit 2dc19ba

Please sign in to comment.