Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix challenges order #101

Merged
merged 2 commits into from Nov 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/challenges_controller.rb
Expand Up @@ -3,7 +3,7 @@ class ChallengesController < ApplicationController

def index
@challenges = if admin?
Challenge.in_reverse_chronological_order
Challenge.in_chronological_order
else
Challenge.visible
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/challenge.rb
Expand Up @@ -5,12 +5,12 @@ class Challenge < ActiveRecord::Base
has_many :solutions, class_name: 'ChallengeSolution'

class << self
def in_reverse_chronological_order
order('created_at DESC')
def in_chronological_order
order('created_at ASC')
end

def visible
where(hidden: false).in_reverse_chronological_order
where(hidden: false).in_chronological_order
end

def find_with_solutions_and_users(challenge_id)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/challenges_controller_spec.rb
Expand Up @@ -22,7 +22,7 @@

it "assigns all challenges for admins" do
current_user.stub admin?: true
Challenge.stub in_reverse_chronological_order: 'challenges'
Challenge.stub in_chronological_order: 'challenges'

get :index

Expand Down
14 changes: 7 additions & 7 deletions spec/models/challenge_spec.rb
Expand Up @@ -9,17 +9,17 @@
create(:challenge, closes_at: 1.day.ago).should be_closed
end

it "can fetch all records, sorted in reverse chronological order" do
second = create :challenge, created_at: 2.days.ago
first = create :challenge, created_at: 1.day.ago
it "can fetch all records, sorted in chronological order" do
first = create :challenge, created_at: 2.days.ago
second = create :challenge, created_at: 1.day.ago

Challenge.in_reverse_chronological_order.should eq [first, second]
Challenge.in_chronological_order.should eq [first, second]
end

it "can fetch all visible records, sorted in reverse chronological order" do
second = create :visible_challenge, created_at: 3.days.ago
it "can fetch all visible records, sorted in chronological order" do
first = create :visible_challenge, created_at: 3.days.ago
create :hidden_challenge, created_at: 2.days.ago
first = create :visible_challenge, created_at: 1.day.ago
second = create :visible_challenge, created_at: 1.day.ago

Challenge.visible.should eq [first, second]
end
Expand Down