Skip to content
Browse files

Merge pull request #43 from codebrawl/feature/refactoring

refactoring
  • Loading branch information...
2 parents 6f87969 + f66642c commit 693f189dfbce2abf6b50652e6bd28ee523da307b @jeffkreeftmeijer jeffkreeftmeijer committed Jan 28, 2012
View
3 Gemfile.lock
@@ -52,7 +52,8 @@ GEM
arel (3.0.0)
blankslate (2.1.2.4)
bson (1.5.2)
- bson_ext (1.3.1)
+ bson_ext (1.5.2)
+ bson (= 1.5.2)
builder (3.0.0)
capybara (1.1.2)
mime-types (>= 1.16)
View
2 README.markdown
@@ -22,7 +22,7 @@ Use [Bundler](http://gembundler.com/) to install the dependencies:
Now, run the tests and make sure they all pass:
- bundle exec rspec spec
+ bundle exec rake
If you get a spec failure, please create an [issue](https://github.com/codebrawl/codebrawl/issues) explaining what went wrong.
View
2 app/controllers/application_controller.rb
@@ -76,7 +76,7 @@ def sitemap
protected
def current_user
- @current_user ||= User.find(session[:user_id]) rescue nil if session[:user_id]
+ @current_user ||= User.first(:conditions => {:id => session[:user_id]}) if session[:user_id]
end
def logged_in?
View
10 app/controllers/entries_controller.rb
@@ -1,15 +1,15 @@
class EntriesController < ApplicationController
before_filter :authenticate_user!, :only => :new
+ before_filter :find_contest
def new
- @contest = Contest.by_slug(params[:contest_id]).if_open
+ @contest = @contest.if_open
@entry = @contest.entries.new
@contest.not_found if @contest.has_entry_from?(current_user)
end
def create
- @contest = Contest.find_by_slug(params[:contest_id])
@entry = @contest.entries.new(params[:entry].merge({:user_id => current_user.id}))
if @entry.save
redirect_to @contest, :notice => 'Thank you for entering!'
@@ -19,10 +19,14 @@ def create
end
def destroy
- @contest = Contest.find_by_slug(params[:contest_id])
@entry = @contest.entries.find(params[:id])
@entry.destroy
redirect_to @contest, :notice => 'Your entry has been deleted.'
end
+ protected
+
+ def find_contest
+ @contest = Contest.find_by_slug(params[:contest_id])
+ end
end
View
4 app/controllers/users_controller.rb
@@ -24,10 +24,10 @@ def show
]).map(&:id)
not_found unless @user = User.where(:login => params[:id]).first
- @entered_contests = Contest.all.where('entries.user_id' => @user.id).select do |contest|
+ @entered_contests = Contest.where('entries.user_id' => @user.id).select do |contest|
contest.state == 'finished'
end
- @submitted_contests = Contest.all.where('user_id' => @user.id)
+ @submitted_contests = Contest.where(:user_id => @user.id)
@medals = { 1 => 'medal_gold', 2 => 'medal_silver', 3 => 'medal_bronze'}
@medals.default = 'rosette'
end
View
6 app/models/contest.rb
@@ -10,9 +10,9 @@ class Contest
include Mongoid::Timestamps
include Mongoid::Slug
- field :name, :type => String
- field :tagline, :type => String
- field :description, :type => String
+ field :name
+ field :tagline
+ field :description
field :starting_on, :type => Date
field :voting_on, :type => Date
View
2 app/models/entry.rb
@@ -2,7 +2,7 @@ class Entry
include Mongoid::Document
include Mongoid::Timestamps
- field :gist_id, :type => String
+ field :gist_id
field :files, :type => Hash, :default => {}
field :score, :type => Float, :default => 0.0
View
21 app/models/user.rb
@@ -19,16 +19,17 @@ class User
extend Bang
extend AttributeFallback
- field 'login', :type => String
- field 'email', :type => String
- field 'token', :type => String
- field 'name', :type => String
- field 'github_id', :type => Integer
- field 'participations', :type => Array, :default => []
- field 'points', :type => Integer, :default => 0
- field 'average_score', :type => Float, :default => 0.0
- field 'urls', :type => Hash, :default => {}
- field 'contributions', :type => Integer
+ field :login
+ field :email
+ field :token
+ field :name
+
+ field :github_id, :type => Integer
+ field :participations, :type => Array, :default => []
+ field :points, :type => Integer, :default => 0
+ field :average_score, :type => Float, :default => 0.0
+ field :urls, :type => Hash, :default => {}
+ field :contributions, :type => Integer
validates :login, :presence => true
View
2 app/views/entries/_voting.haml
@@ -11,7 +11,7 @@
= f.text_area :comment
%fieldset
- - (1..5).to_a.each do |score|
+ - (1..5).each do |score|
= f.label "score_#{score}", score
= f.radio_button :score, score
= f.submit 'Vote'
View
2 app/views/homepage/index.haml
@@ -1,6 +1,6 @@
- content_for(:description){ 'Codebrawl, a code contest website focussed on the Ruby programming language and open source software.' }
-- unless @contests.map(&:state).include? 'open'
+- if @contests.map(&:state).exclude? 'open'
%section.tip
%h1 Get ready for the next season of Codebrawl!
%p
View
2 app/views/users/show.haml
@@ -27,7 +27,7 @@
%td Score
%td Points
- @user.participations.each do |participation|
- - if @entered_contests.map {|p| p.id }.include? participation['contest_id']
+ - if @entered_contests.map(&:id).include? participation['contest_id']
%tr
%td.narrow= image_tag("#{@medals[participation['position']]}.png")
%td= link_to participation['contest_name'], "/contests/#{participation['contest_slug']}"

0 comments on commit 693f189

Please sign in to comment.
Something went wrong with that request. Please try again.