Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created February 24, 2010 22:50
Show Gist options
  • Select an option

  • Save lukeredpath/313983 to your computer and use it in GitHub Desktop.

Select an option

Save lukeredpath/313983 to your computer and use it in GitHub Desktop.
class Asset < ActiveRecord::Base
belongs_to :task
validate :task_belongs_to_creator
attr_accessor :creator
private
def task_belongs_to_creator
if creator && task.user != creator
errors.add(:task, "should belong to it's creator")
end
end
end
class AssetTest < Test::Unit::TestCase
context "An asset with a creator" do
setup do
@user = User.create!
@asset = Asset.new(:creator => @user)
end
should "not be allowed to associate with tasks of another user" do
another_user = User.create!
@asset.task = another_user.tasks.create!
@asset.valid?
assert_match /should belong to it's creator/, @asset.errors[:task]
end
should "be allowed to associate with tasks of its creator" do
@asset.task = @user.tasks.create!
@asset.valid?
assert_nil @asset.errors[:task]
end
end
end
class AssetsController
def create
@asset = Asset.new(params[:asset].merge(:creator => current_user))
@asset.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment