Created
December 16, 2011 21:15
-
-
Save hardboiled/1488018 to your computer and use it in GitHub Desktop.
Destroy Game without score when Team is destroyed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# On branch master | |
# Changes to be committed: | |
# (use "git reset HEAD <file>..." to unstage) | |
# | |
# modified: app/controllers/memberships_controller.rb | |
# modified: app/controllers/teams_controller.rb | |
# modified: app/models/game.rb | |
# modified: app/models/team.rb | |
# modified: spec/requests/results_spec.rb | |
# | |
diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb | |
index d9c4088..4426ee0 100644 | |
--- a/app/controllers/memberships_controller.rb | |
+++ b/app/controllers/memberships_controller.rb | |
@@ -7,14 +7,14 @@ class MembershipsController < ApplicationController | |
redirect_to @membership.team | |
else | |
#TODO: | |
- # Currently throws an error: still haven't | |
- # figured out the best way to handle this. | |
- render '/teams' | |
+ # Still haven't figured out the best way to | |
+ # handle this. | |
+ redirect_to '/teams' | |
end | |
end | |
def destroy | |
@membership = Membership.find(params[:id]) | |
- if @membership.delete | |
+ if @membership.destroy | |
flash[:success] = "Member removed!" | |
redirect_to @membership.team | |
else | |
diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb | |
index 3942c47..a5eb913 100644 | |
--- a/app/controllers/teams_controller.rb | |
+++ b/app/controllers/teams_controller.rb | |
@@ -11,7 +11,7 @@ class TeamsController < ApplicationController | |
def destroy | |
@team = Team.find(params[:id]) | |
- if @team.delete | |
+ if @team.destroy | |
flash[:success] = "Team deleted" | |
redirect_to '/teams' | |
else | |
diff --git a/app/models/game.rb b/app/models/game.rb | |
index aecb01c..127e594 100644 | |
--- a/app/models/game.rb | |
+++ b/app/models/game.rb | |
@@ -1,5 +1,5 @@ | |
class Game < ActiveRecord::Base | |
- has_many :results | |
+ has_many :results, :dependent => :destroy | |
has_many :teams, :through => :results | |
accepts_nested_attributes_for :results | |
@@ -13,6 +13,9 @@ class Game < ActiveRecord::Base | |
# validate :played?, :before => :update_attributes | |
+ scope :without_score, all(:joins => :results, | |
+ :conditions => "results.score IS NULL") | |
+ | |
paginates_per 5 | |
def played? | |
diff --git a/app/models/team.rb b/app/models/team.rb | |
index 3c1201b..d059e67 100644 | |
--- a/app/models/team.rb | |
+++ b/app/models/team.rb | |
@@ -4,6 +4,8 @@ class Team < ActiveRecord::Base | |
has_many :results | |
has_many :games, :through => :results | |
+ before_destroy :destroy_games_without_scores | |
+ | |
accepts_nested_attributes_for :memberships | |
attr_accessible :name, :memberships_attributes | |
@@ -51,4 +53,9 @@ class Team < ActiveRecord::Base | |
players.size > (players - t2.players).size | |
end | |
+ private | |
+ def destroy_games_without_scores | |
+ games.without_score.map(&:destroy) | |
+ end | |
+ | |
end | |
diff --git a/spec/requests/results_spec.rb b/spec/requests/results_spec.rb | |
index 061ce95..1023083 100644 | |
--- a/spec/requests/results_spec.rb | |
+++ b/spec/requests/results_spec.rb | |
@@ -7,6 +7,37 @@ describe "Results" do | |
end | |
it "should not allow Result without score and without Team" do | |
+ #Create new game | |
+ lambda do | |
+ visit new_game_path | |
+ select @team1.name, :from => "game_results_attributes_0_team_id" | |
+ select @team2.name, :from => "game_results_attributes_1_team_id" | |
+ click_button "Submit" | |
+ | |
+ page.should have_xpath("//div[@class='flash success']", | |
+ {:text => "Game successfully created!"}) | |
+ end.should change(Result, :count).by(2) | |
+ | |
+ game_id = Result.find(:all, :joins => "JOIN results AS r2 ON r2.game_id", | |
+ :conditions => ["results.team_id = ? AND r2.team_id = ?", | |
+ @team1.id, @team2.id]).first.game_id | |
+ | |
+ #Delete team without updating score and verify result count | |
+ #changes. | |
+ lambda do | |
+ click_link "Teams" | |
+ click_link @team1.name | |
+ click_link "Edit Team" | |
+ click_button "Delete" | |
+ page.should have_xpath("//div[@class='flash success']", | |
+ {:text => "Team deleted"}) | |
+ | |
+ | |
+ #Verify game was deleted. | |
+ assert Game.all(:conditions => "id = #{game_id}").empty?, | |
+ %{Team deletion should remove associated games that have | |
+ no scores} | |
+ end.should change(Result, :count).by(-2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment