story.rb
Code:
class Story < ActiveRecord::Base
validates_presence_of :name, :link
has_many :votes do
def latest
find :all, :order => 'id DESC', :limit => 3
end
end
def to_param
"#{id}-#{name.gsub(/\W/, '-').downcase}"
end
end
vote.rb
Code:
class Vote < ActiveRecord::Base
belongs_to :story
end
story_test.rb
Code:
require 'test_helper'
class StoryTest < ActiveSupport::TestCase
def test_should_not_be_valid_without_name
s = Story.create(:name => nil, :link => 'http://www.testsubmission.com/')
assert s.errors.on(:name)
end
def test_should_be_valid_without_link
s = Story.create(:name => 'My test submission', :link => nil)
assert s.errors.on(:link)
end
def test_should_create_story
s = Story.create(
:name => 'My test submission',
:link => 'http://www.testsubmission.com/')
assert s.valid?
end
def test_should_have_a_votes_association
assert_equal [ votes(:one), votes(:two) ], stories(:one).votes
end
def test_should_return_highest_vote_id_first
assert_equal votes(:two), stories(:one).votes.latest.first
end
def test_should_return_3_latest_votes
10.times { stories(:one).votes.create }
assert_equal 3, stories(:one).votes.latest.size
end
end
vote_test.rb
Code:
require 'test_helper'
class VoteTest < ActiveSupport::TestCase
def test_story_association
assert_equal stories(:one), votes(:one).story
end
end
thanks!
Bookmarks