Commit acba8492289dedb770c5ca26cbdfb548f91f3d37

Authored by Rafael Manzo
1 parent 937d1828

Project#destroy overrided so it destroys the attributes

Signed off by: Heitor Reis <marcheing@gmail.com>
app/models/project.rb
... ... @@ -11,4 +11,10 @@ class Project &lt; KalibroClient::Entities::Processor::Project
11 11 @project_attributes ||= ProjectAttributes.find_by_project_id(self.id)
12 12 @project_attributes.nil? ? ProjectAttributes.new : @project_attributes
13 13 end
  14 +
  15 + def destroy
  16 + self.attributes.destroy if self.attributes
  17 + @project_attributes = nil
  18 + super
  19 + end
14 20 end
... ...
spec/models/project_spec.rb
... ... @@ -38,5 +38,32 @@ describe Project, :type =&gt; :model do
38 38 expect(subject.attributes).to eq(project_attributes)
39 39 end
40 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 68 end
42 69 end
... ...