Skip to content

Commit

Permalink
Add record.spotify_uri format validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mllocs committed Dec 9, 2012
1 parent 0cdb3eb commit 5e4c29c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/assets/templates/records/record.jst.ejs
Expand Up @@ -2,10 +2,10 @@
<a href="#" id="edit_record_<%= record.get('id') %>" class="edit">Edit</a> ·
<a href="#" id="delete_record_<%= record.get('id') %>" class="delete">Delete</a>
</div>
<a class="cover" href="<%= record.get('spotify_uri') %>">
<a class="cover" href="<%= record.get('spotify_uri') || "#" %>">
<img alt="" width="200px" height="200px" title="<%= record.get('title') %>" src="<%= record.get('cover_url') %>"></img>
</a>
<a class="title" href="<%= record.get('spotify_uri') %>" title="<%= record.get('title') %>">
<a class="title" href="<%= record.get('spotify_uri') || "#" %>" title="<%= record.get('title') %>">
<%= record.get('title').truncate(26) %>
</a>
<span class='artist' title="<%= record.get('artist') %>">
Expand Down
6 changes: 6 additions & 0 deletions app/models/record.rb
@@ -1,8 +1,14 @@
class Record < ActiveRecord::Base
attr_accessible :artist, :title, :cover_url, :spotify_uri

validates :artist, presence: true
validates :title, presence: true

validates_format_of :cover_url, with: %r{\.(gif|jpe?g|png)$}i,
message: "should be an image URL (jpg, gif or png)",
allow_blank: true

validates_format_of :spotify_uri, with: %r{spotify:(album|track):[a-zA-Z0-9]+},

This comment has been minimized.

Copy link
@micho

micho Dec 10, 2012

%r{} 👍

message: "should be a correct album Spotify URI",
allow_blank: true
end
2 changes: 1 addition & 1 deletion db/seeds.rb
Expand Up @@ -17,7 +17,7 @@
title: "The Heart Of Sunday Night",
artist: "Tom Waits",
cover_url: "http://ijustreadaboutthat.files.wordpress.com/2011/09/75112.jpg",
spotify_uri: "spotify:track:23X3UiWmkM09Ug2heM6z38"
spotify_uri: "spotify:album:7MLzdbFViVmpQdnNmxQXoE"
)

Record.create!(
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/records.yml
Expand Up @@ -12,11 +12,11 @@ two:
title: The Heart Of Sunday Night
artist: Tom Waits
cover_url: http://ijustreadaboutthat.files.wordpress.com/2011/09/75112.jpg
spotify_uri: spotify:track:23X3UiWmkM09Ug2heM6z38
spotify_uri: spotify:album:7MLzdbFViVmpQdnNmxQXoE

three:
id: 3
title: "OH (Ohio)"
artist: "Lambchop"
cover_url: "http://expressmilwaukee.com/imgs/hed/art5489widea.jpg"
spotify_uri: "spotify:album:4y9ebrmAx1TMyUlyCVJJL6"
title: OH (Ohio)
artist: Lambchop
cover_url: http://expressmilwaukee.com/imgs/hed/art5489widea.jpg
spotify_uri: spotify:album:4y9ebrmAx1TMyUlyCVJJL6
5 changes: 3 additions & 2 deletions test/integration/records_pages_test.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
require 'test_helper'

class RecordsPagesTest < ActionDispatch::IntegrationTest
Expand All @@ -9,7 +10,7 @@ class RecordsPagesTest < ActionDispatch::IntegrationTest
test "basic record elements exist" do
visit "/"
within("h1") do
assert page.has_content?("Zoh My Records")
assert page.has_content?("ZOH·MY·RECORDS")
end
within("#container") do
assert page.has_selector?("ul#records")
Expand All @@ -32,7 +33,7 @@ class RecordsPagesTest < ActionDispatch::IntegrationTest
fill_in "title", :with => "The Harrow And The Harvest"
fill_in "artist", :with => "Gillian Welch"
fill_in "cover_url", :with => "http://upload.wikimedia.org/wikipedia/en/thumb/1/18/Theharrowandtheharvest.jpg"
fill_in "spotify_uri", :with => "spotify:user:maxim.colls:playlist:0gxuTvV1XhCrnePamtPRrO"
fill_in "spotify_uri", :with => "spotify:album:7MLzdbFViVmpQdnNmxQXoE"
click_button "Add Record"

within("ul#records") do
Expand Down
5 changes: 5 additions & 0 deletions test/unit/record_test.rb
Expand Up @@ -17,4 +17,9 @@ class RecordTest < ActiveSupport::TestCase
assert !record.save
end

test "should not save record with an incorrect spotify_url" do
record = Record.new(title: "Take Five", artist: "Dave Brubeck", spotify_uri: "Not a valid spotify URI")
assert !record.save
end

end

0 comments on commit 5e4c29c

Please sign in to comment.