Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add validation for image urls

  • Loading branch information...
commit beda27a2d5da750423508378d460f63a7c5c7be7 1 parent 84ad2e4
@FuriKuri authored
View
5 app/models/book.rb
@@ -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
View
12 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
View
9 test/unit/book_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.