Skip to content

Commit

Permalink
Prevented deletion of clients/projects with associated data, and tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
boone committed Aug 4, 2011
1 parent 38fb3ab commit c5406d4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'kaminari'

group :test do
gem 'shoulda'
gem 'factory_girl_rails'
end

# Bundle gems for the local environment. Make sure to
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ GEM
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (2.0.2)
factory_girl_rails (1.1.0)
factory_girl (~> 2.0.0)
railties (>= 3.0.0)
i18n (0.5.0)
jquery-rails (1.0.12)
railties (~> 3.0)
Expand Down Expand Up @@ -78,6 +82,7 @@ PLATFORMS
ruby

DEPENDENCIES
factory_girl_rails
jquery-rails
kaminari
rails (= 3.0.9)
Expand Down
6 changes: 4 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
* don't allow deletion of clients/projects if event data exists
* limit users to one active event or allow parallel (make an option?)
* handling of unverified request?
* Github-style page sliding when changing pages?
* Monthly Report takes up too much width

* indexes on db columns
* testing/handling for:
* events that overlap midnight <- for now apply hours to start day
* events that overlap a DST-type change (handled automatically?)

* limit users to one active event or allow parallel (make an option?)
* what about personal projects or projects for small clients with no sub projects? how to make that clean and easy?
2 changes: 1 addition & 1 deletion app/models/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Client < ActiveRecord::Base
has_many :projects
has_many :projects, :dependent => :restrict
has_many :events, :through => :projects

default_scope order('LOWER(name) ASC')
Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Project < ActiveRecord::Base
belongs_to :client
has_many :events
has_many :events, :dependent => :restrict
default_scope order('LOWER(title) ASC')
validates_presence_of :title
end
17 changes: 17 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FactoryGirl.define do
factory :client do
name 'Foo Inc.'
end

factory :project do
client
title 'Hard Work'
end

factory :event do |f|
f.project
f.comment 'An event'
f.start { 1.hour.ago }
f.end { Time.now }
end
end
7 changes: 7 additions & 0 deletions test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
class ClientTest < ActiveSupport::TestCase
should have_many(:projects)
should validate_presence_of(:name)

should 'not be destroyable if there are projects' do
project = Factory.create(:project)
client = project.client
assert_raise(ActiveRecord::DeleteRestrictionError) { client.destroy }
assert !client.destroyed?, 'check for !client.destroyed?'
end
end
7 changes: 7 additions & 0 deletions test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ class ProjectTest < ActiveSupport::TestCase
should belong_to(:client)
should have_many(:events)
should validate_presence_of(:title)

should 'not be destroyable if there are events' do
event = Factory.create(:event)
project = event.project
assert_raise(ActiveRecord::DeleteRestrictionError) { project.destroy }
assert !project.destroyed?, 'check for !project.destroyed?'
end
end

0 comments on commit c5406d4

Please sign in to comment.