Skip to content

Commit

Permalink
I'm feeling lucky feature (#53)
Browse files Browse the repository at this point in the history
* feat: Add GET /announcements/random endpoint

* test: Add controller spec for GET /announcements/random

* New line

* refact: Fixes based on feedback
  • Loading branch information
hugoabonizio authored and veelenga committed Oct 4, 2017
1 parent 62c7052 commit 976c56b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Amber::Server.configure do |app|

routes :web do
resources "/announcements", AnnouncementController
get "/announcements/random", AnnouncementController, :random
get "/=:hashid", AnnouncementController, :expand
get "/rss", RSSController, :show
get "/sessions/new", SessionsController, :new
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/announcement_controller_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,14 @@ describe AnnouncementController do
end
end
end

describe "GET random" do
before { announcement }

it "redirects to a random announcement" do
get "/announcements/random"
expect(response.status_code).to eq 302
expect(response).to redirect_to "/announcements/#{announcement.id}"
end
end
end
13 changes: 13 additions & 0 deletions spec/models/announcement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,17 @@ describe Announcement do
expect(hashid).to eq "D49Nz"
end
end

describe ".random" do
let!(:ann) { announcement(user.tap &.save).tap &.save }

before do
Announcement.clear
User.clear
end

it "returns random announcements" do
expect(Announcement.random.not_nil!.id).to eq ann.id
end
end
end
8 changes: 8 additions & 0 deletions src/controllers/announcement_controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class AnnouncementController < ApplicationController
end
end

def random
if announcement = Announcement.random
redirect_to "/announcements/#{announcement.id}"
else
redirect_to "/"
end
end

private def announcement_params
params.to_h.select %w(title description type)
end
Expand Down
4 changes: 4 additions & 0 deletions src/models/announcement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ class Announcement < Granite::ORM::Base
def content
Autolink.auto_link(Markdown.to_html(description.not_nil!))
end

def self.random
Announcement.all("ORDER BY RANDOM() LIMIT 1").first?
end
end
2 changes: 2 additions & 0 deletions src/views/layouts/_search.slang
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ aside.widget.search
input type="hidden" name="user" value="#{user}"
- if type
input type="hidden" name="type" value="#{type}"
a href="/announcements/random"
| I'm feeling lucky!

0 comments on commit 976c56b

Please sign in to comment.