Skip to content

Commit

Permalink
add validation for image urls
Browse files Browse the repository at this point in the history
  • Loading branch information
FuriKuri committed Nov 4, 2012
1 parent 84ad2e4 commit beda27a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/models/book.rb
Expand Up @@ -2,4 +2,9 @@ class Book < ActiveRecord::Base
attr_accessible :description, :image_url, :lent_to_user_id, :owner_id, :title

validates_presence_of :description, :title

validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
12 changes: 6 additions & 6 deletions test/fixtures/books.yml
@@ -1,15 +1,15 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
title: MyString
description: MyText
image_url: MyString
title: Clean Code
description: A book about clean code.
image_url: image.png
owner_id: 1
lent_to_user_id: 1

two:
title: MyString
description: MyText
image_url: MyString
title: Java for Beginners
description: Java book.
image_url:
owner_id: 1
lent_to_user_id: 1
9 changes: 9 additions & 0 deletions test/unit/book_test.rb
Expand Up @@ -4,6 +4,7 @@ class BookTest < ActiveSupport::TestCase
test "should save book" do
book = Book.new
book.title = 'Clean Code'
book.image_url = 'cleancode.jpg'
book.description = 'Clean Code book form Robert C. Martin'
assert book.valid?
end
Expand All @@ -19,4 +20,12 @@ class BookTest < ActiveSupport::TestCase
book.title = 'Clean Code'
assert book.invalid?
end

test "should not save book with invalid image url" do
book = Book.new
book.title = 'Clean Code'
book.image_url = 'wrong_image'
book.description = 'Clean Code book form Robert C. Martin'
assert book.invalid?
end
end

0 comments on commit beda27a

Please sign in to comment.