Created
February 24, 2010 22:50
-
-
Save lukeredpath/313983 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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