diff --git a/README b/README index 37ec8ea..de67c4f 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ == Welcome to Rails -Rails is a web-application framework that includes everything needed to create -database-backed web applications according to the Model-View-Control pattern. +Rails is a web-application framework that includes everything needed to create +database-backed web applications according to the Model-View-Control pattern. This pattern splits the view (also called the presentation) into "dumb" templates that are primarily responsible for inserting pre-built data in between HTML tags. @@ -57,14 +57,14 @@ Options +FollowSymLinks +ExecCGI # If you don't want Rails to look in certain directories, # use the following rewrite rules so that Apache won't rewrite certain requests -# +# # Example: # RewriteCond %{REQUEST_URI} ^/notrails.* # RewriteRule .* - [L] # Redirect all requests not available on the filesystem to Rails # By default the cgi dispatcher is used which is very slow -# +# # For better performance replace the dispatcher with the fastcgi one # # Example: @@ -85,7 +85,7 @@ RewriteRule ^(.*)$ dispatch.cgi [QSA,L] # In case Rails experiences terminal errors # Instead of displaying this message you can supply a file here which will be rendered instead -# +# # Example: # ErrorDocument 500 /500.html @@ -132,7 +132,7 @@ and also on programming in general. Debugger support is available through the debugger command when you start your Mongrel or Webrick server with --debugger. This means that you can break out of execution at any point -in the code, investigate and change the model, AND then resume execution! +in the code, investigate and change the model, AND then resume execution! You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug' Example: diff --git a/app/controllers/picks_controller.rb b/app/controllers/picks_controller.rb index 0b5694a..c8caab4 100644 --- a/app/controllers/picks_controller.rb +++ b/app/controllers/picks_controller.rb @@ -1,46 +1,46 @@ class PicksController < ApplicationController - + def home @pick = Pick.current @end_of_draft = @pick.nil? @previous_picks = Pick.previous_picks end - + def index @picks = Pick.all end - + def show @pick = Pick.find(params[:id]) @players = Player.undrafted end - + def draft_player - + player = Player.find(params[:id]) - + unless player.drafted draft_over = Pick.current.team.draft(player) - + respond_to do |format| if (!draft_over && player.save) flash[:notice] = 'Player was successfully drafted.' format.html { redirect_to(pick_path(Pick.current)) } else - flash[:notice] = 'The Draft is over, cannot pick more players, + flash[:notice] = 'The Draft is over, cannot pick more players, or there was an error drafting the player you selected' format.html { redirect_to(root_path) } end end - + else flash[:notice] = 'That player has already been drafted' redirect_to(pick_path(Pick.current)) end end - + def results @draft_results = Pick.results end - + end diff --git a/app/controllers/players_controller.rb b/app/controllers/players_controller.rb index 1141a7f..e00f42f 100644 --- a/app/controllers/players_controller.rb +++ b/app/controllers/players_controller.rb @@ -2,11 +2,11 @@ class PlayersController < ApplicationController def index @players = Player.all end - + def show @player = Player.find(params[:id]) end - + def undrafted unless params[:position.nil?] @players = Player.filter_position(params[:position]) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 61c0e5e..f73cc8a 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,7 +1,7 @@ class SessionsController < ApplicationController def new end - + def create user = User.authenticate(params[:login], params[:password]) if user @@ -13,7 +13,7 @@ def create redirect_to_target_or_default(root_url) end end - + def destroy session[:user_id] = nil flash[:notice] = "You have been logged out." diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index acebdd4..5087db0 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -2,9 +2,9 @@ class TeamsController < ApplicationController def index @teams = Team.all end - + def show @team = Team.find(params[:id]) end - + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fd3660b..a2ed324 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,7 +2,7 @@ class UsersController < ApplicationController def new @user = User.new end - + def create @user = User.new(params[:user]) if @user.save diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 5ee345e..cb75cb0 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -7,15 +7,15 @@ def title(page_title, show_title = true) @content_for_title = page_title.to_s @show_title = show_title end - + def show_title? @show_title end - + def stylesheet(*args) content_for(:head) { stylesheet_link_tag(*args) } end - + def javascript(*args) content_for(:head) { javascript_include_tag(*args) } end diff --git a/app/models/pick.rb b/app/models/pick.rb index 0f2729f..df58dd8 100644 --- a/app/models/pick.rb +++ b/app/models/pick.rb @@ -1,21 +1,21 @@ class Pick < ActiveRecord::Base has_one :player has_one :team - + validates_presence_of :team_id - + def team Team.find(self.team_id) end - + def self.results Pick.find(:all, :conditions => {:used => true}) end - + def self.current Pick.first(:conditions => {:used => false}) end - + def self.previous pick = Pick.current unless(pick.pick_number == 1) @@ -23,20 +23,20 @@ def self.previous else pick end - + end - + def self.next Pick.find_by_pick_number(Pick.current.pick_number+1) end - - def self.previous_picks + + def self.previous_picks picks = Pick.find(:all, :conditions => {:used => true}) #technically they should already be sorted, but to be robust picks.sort_by {|pick| pick['pick_number']} picks.reverse! picks = picks[0...3] end - - + + end diff --git a/app/models/player.rb b/app/models/player.rb index 4feb655..92b091b 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -1,16 +1,16 @@ class Player < ActiveRecord::Base belongs_to :team belongs_to :pick - - + + def self.kipers_best_available Player.first(:conditions => {:drafted => false}) end - + def self.undrafted Player.find(:all, :conditions => {:drafted => false}) end - + def self.filter_position(position) players = Player.find(:all, :conditions => {:position => position, :drafted => false}) players.sort_by{|player| player['name']} diff --git a/app/models/team.rb b/app/models/team.rb index ebef17b..c9177d2 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -1,12 +1,12 @@ class Team < ActiveRecord::Base belongs_to :pick has_many :players - + def kiper_draft - player = Player.kipers_best_available - do_drafting(player,self) + player = Player.kipers_best_available + do_drafting(player,self) end - + def draft(player) unless Pick.current.pick_number == 255 do_drafting(player,self) @@ -15,16 +15,16 @@ def draft(player) draft_over = true end end - - - - + + + + protected - + def do_drafting(player, team) player.drafted = true player.save - + current_pick = Pick.current current_pick.player = player current_pick.player_id = player.id @@ -33,5 +33,5 @@ def do_drafting(player, team) team.players.push(player) end - + end diff --git a/app/models/user.rb b/app/models/user.rb index 1c645f2..7d1a81c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,10 @@ class User < ActiveRecord::Base # new columns need to be added here to be writable through mass assignment attr_accessible :username, :email, :password, :password_confirmation - + attr_accessor :password before_save :prepare_password - + validates_presence_of :username validates_uniqueness_of :username, :email, :allow_blank => true validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@" @@ -12,26 +12,26 @@ class User < ActiveRecord::Base validates_presence_of :password, :on => :create validates_confirmation_of :password validates_length_of :password, :minimum => 4, :allow_blank => true - + # login can be either username or email address def self.authenticate(login, pass) user = find_by_username(login) || find_by_email(login) return user if user && user.matching_password?(pass) end - + def matching_password?(pass) self.password_hash == encrypt_password(pass) end - + private - + def prepare_password unless password.blank? self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join) self.password_hash = encrypt_password(password) end end - + def encrypt_password(pass) Digest::SHA1.hexdigest([pass, password_salt].join) end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 22972c4..ee33094 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -19,11 +19,11 @@ <%- flash.each do |name, msg| -%> <%= content_tag :div, msg, :id => "flash_#{name}" %> <%- end -%> - + <%- if show_title? -%>