Commit 8d612e1b3036394b8553a0a41e205eec32e4a931
Committed by
Eduardo Passos
1 parent
c5b5a296
Exists in
staging
and in
26 other branches
Add method to destroy images from communities when a profile is removed
few image_destroy adjustments
Showing
2 changed files
with
12 additions
and
2 deletions
Show diff stats
lib/acts_as_having_image.rb
| @@ -2,7 +2,7 @@ module ActsAsHavingImage | @@ -2,7 +2,7 @@ module ActsAsHavingImage | ||
| 2 | 2 | ||
| 3 | module ClassMethods | 3 | module ClassMethods |
| 4 | def acts_as_having_image | 4 | def acts_as_having_image |
| 5 | - belongs_to :image | 5 | + belongs_to :image, dependent: :destroy |
| 6 | scope :with_image, :conditions => [ "#{table_name}.image_id IS NOT NULL" ] | 6 | scope :with_image, :conditions => [ "#{table_name}.image_id IS NOT NULL" ] |
| 7 | scope :without_image, :conditions => [ "#{table_name}.image_id IS NULL" ] | 7 | scope :without_image, :conditions => [ "#{table_name}.image_id IS NULL" ] |
| 8 | self.send(:include, ActsAsHavingImage) | 8 | self.send(:include, ActsAsHavingImage) |
| @@ -19,4 +19,4 @@ module ActsAsHavingImage | @@ -19,4 +19,4 @@ module ActsAsHavingImage | ||
| 19 | 19 | ||
| 20 | end | 20 | end |
| 21 | 21 | ||
| 22 | -ActiveRecord::Base.extend(ActsAsHavingImage::ClassMethods) | 22 | -ActiveRecord::Base.extend(ActsAsHavingImage::ClassMethods) |
| 23 | +ActiveRecord::Base.extend(ActsAsHavingImage::ClassMethods) | ||
| 23 | \ No newline at end of file | 24 | \ No newline at end of file |
test/unit/profile_test.rb
| @@ -110,6 +110,16 @@ class ProfileTest < ActiveSupport::TestCase | @@ -110,6 +110,16 @@ class ProfileTest < ActiveSupport::TestCase | ||
| 110 | assert_equal total - mine, Article.count | 110 | assert_equal total - mine, Article.count |
| 111 | end | 111 | end |
| 112 | 112 | ||
| 113 | + should 'remove images when removing profile' do | ||
| 114 | + profile = build(Profile, :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) | ||
| 115 | + image = profile.image | ||
| 116 | + image.save! | ||
| 117 | + profile.destroy | ||
| 118 | + assert_raise ActiveRecord::RecordNotFound do | ||
| 119 | + image.reload | ||
| 120 | + end | ||
| 121 | + end | ||
| 122 | + | ||
| 113 | def test_should_avoid_reserved_identifiers | 123 | def test_should_avoid_reserved_identifiers |
| 114 | Profile::RESERVED_IDENTIFIERS.each do |identifier| | 124 | Profile::RESERVED_IDENTIFIERS.each do |identifier| |
| 115 | assert_invalid_identifier identifier | 125 | assert_invalid_identifier identifier |