Commit acba8492289dedb770c5ca26cbdfb548f91f3d37
1 parent
937d1828
Exists in
colab
and in
4 other branches
Project#destroy overrided so it destroys the attributes
Signed off by: Heitor Reis <marcheing@gmail.com>
Showing
2 changed files
with
33 additions
and
0 deletions
Show diff stats
app/models/project.rb
| @@ -11,4 +11,10 @@ class Project < KalibroClient::Entities::Processor::Project | @@ -11,4 +11,10 @@ class Project < KalibroClient::Entities::Processor::Project | ||
| 11 | @project_attributes ||= ProjectAttributes.find_by_project_id(self.id) | 11 | @project_attributes ||= ProjectAttributes.find_by_project_id(self.id) |
| 12 | @project_attributes.nil? ? ProjectAttributes.new : @project_attributes | 12 | @project_attributes.nil? ? ProjectAttributes.new : @project_attributes |
| 13 | end | 13 | end |
| 14 | + | ||
| 15 | + def destroy | ||
| 16 | + self.attributes.destroy if self.attributes | ||
| 17 | + @project_attributes = nil | ||
| 18 | + super | ||
| 19 | + end | ||
| 14 | end | 20 | end |
spec/models/project_spec.rb
| @@ -38,5 +38,32 @@ describe Project, :type => :model do | @@ -38,5 +38,32 @@ describe Project, :type => :model do | ||
| 38 | expect(subject.attributes).to eq(project_attributes) | 38 | expect(subject.attributes).to eq(project_attributes) |
| 39 | end | 39 | end |
| 40 | end | 40 | end |
| 41 | + | ||
| 42 | + describe 'destroy' do | ||
| 43 | + context 'when attributes exist' do | ||
| 44 | + let!(:project) { FactoryGirl.build(:project) } | ||
| 45 | + let!(:project_attributes) { FactoryGirl.build(:project_attributes, project_id: project.id) } | ||
| 46 | + | ||
| 47 | + it 'should be destroyed' do | ||
| 48 | + project.expects(:attributes).twice.returns(project_attributes) | ||
| 49 | + project_attributes.expects(:destroy) | ||
| 50 | + KalibroClient::Entities::Processor::Project.any_instance.expects(:destroy).returns(project) | ||
| 51 | + project.destroy | ||
| 52 | + end | ||
| 53 | + | ||
| 54 | + it 'is expected to clean the attributes memoization' do | ||
| 55 | + # Call attributes once so it memoizes | ||
| 56 | + ProjectAttributes.expects(:find_by).with(project_id: project.id).returns(project_attributes) | ||
| 57 | + expect(project.attributes).to eq(project_attributes) | ||
| 58 | + | ||
| 59 | + # Destroying | ||
| 60 | + project.destroy | ||
| 61 | + | ||
| 62 | + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed | ||
| 63 | + ProjectAttributes.expects(:find_by).with(project_id: project.id).returns(nil) | ||
| 64 | + expect(project.attributes).to_not eq(project_attributes) | ||
| 65 | + end | ||
| 66 | + end | ||
| 67 | + end | ||
| 41 | end | 68 | end |
| 42 | end | 69 | end |