Skip to content

Commit

Permalink
Add a picture validation for duplicate filenames. Ensure the picture …
Browse files Browse the repository at this point in the history
…is valid before redirecting or responding in the picture controller.
  • Loading branch information
quattro004 committed Jul 7, 2013
1 parent dc03172 commit d311e75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/controllers/pictures_controller.rb
Expand Up @@ -55,12 +55,14 @@ def update

# Since both albums and recipes contain pictures we need some special redirect/response logic.
def respond_or_redirect
if (@picture.imageable_type == 'Album')
redirect_to(album_path(@picture.imageable_id))
return
elsif (@picture.imageable_type == 'Recipe')
redirect_to(recipe_path(@picture.imageable_id))
return
if(@picture.valid?)
if (@picture.imageable_type == 'Album')
redirect_to(album_path(@picture.imageable_id))
return
elsif (@picture.imageable_type == 'Recipe')
redirect_to(recipe_path(@picture.imageable_id))
return
end
end
respond_with(@picture)
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/picture.rb
Expand Up @@ -10,6 +10,8 @@ class Picture < ActiveRecord::Base

validates_size_of :data, :maximum => 3.megabyte, :message => 'cannot be greater than 3mb'

validate :picture_doesnt_exist

def uploaded_picture=(picture_field)
self.name = base_part_of(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
Expand All @@ -26,4 +28,10 @@ def base_part_of(file_name)
def picture_contains_data
errors.add('Picture data', 'must not be blank') unless !self.data.blank?
end

def picture_doesnt_exist
if (Picture.find_by_name(self.name))
errors.add('A picture with this filename', 'already exists')
end
end
end

0 comments on commit d311e75

Please sign in to comment.