Commit 92682a8821ca37466d309f35d3f9f96885331432

Authored by Rodrigo Souto
Committed by Daniela Feitosa
1 parent 081ad754

Validates existence of user on ActionTracker::Record

(ActionItem1874)
vendor/plugins/action_tracker/lib/action_tracker_model.rb
@@ -12,6 +12,11 @@ module ActionTracker @@ -12,6 +12,11 @@ module ActionTracker
12 12
13 validates_presence_of :verb 13 validates_presence_of :verb
14 validates_presence_of :user 14 validates_presence_of :user
  15 + validate :user_existence
  16 +
  17 + def user_existence
  18 + errors.add(:user, "user doesn't exists") if user && !user.class.exists?(user)
  19 + end
15 20
16 alias_method :subject, :user 21 alias_method :subject, :user
17 22
vendor/plugins/action_tracker/test/action_tracker_model_test.rb
@@ -67,9 +67,32 @@ class ActionTrackerModelTest < ActiveSupport::TestCase @@ -67,9 +67,32 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
67 end 67 end
68 end 68 end
69 69
  70 + def test_user_is_mandatory
  71 + ta = ActionTracker::Record.new :user_type => 'SomeModel', :verb => :some_verb
  72 + ta.valid?
  73 + assert ta.errors.on(:user)
  74 + assert_raise ActiveRecord::RecordInvalid do
  75 + ta.save!
  76 + end
  77 +
  78 + ta = ActionTracker::Record.new :user_id => 2, :verb => :some_verb
  79 + ta.valid?
  80 + assert ta.errors.on(:user)
  81 + assert_raise ActiveRecord::RecordInvalid do
  82 + ta.save!
  83 + end
  84 + end
  85 +
70 def test_user_exists_indeed 86 def test_user_exists_indeed
71 - ta = ActionTracker::Record.new(:user_id => -1, :user_type => "SomeModel", :verb => :some_verb)  
72 - assert !ta.valid? 87 + ta = ActionTracker::Record.new(:verb => :some_verb)
  88 + ta.valid?
  89 + assert ta.errors.on(:user)
  90 + user = SomeModel.create!
  91 + ta.user = user
  92 + assert ta.valid?
  93 + user.destroy
  94 + ta.valid?
  95 + assert ta.errors.on(:user)
73 end 96 end
74 97
75 def test_verb_must_be_declared_previously 98 def test_verb_must_be_declared_previously
@@ -333,24 +356,4 @@ class ActionTrackerModelTest < ActiveSupport::TestCase @@ -333,24 +356,4 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
333 assert_equal(["foo 1", "bar 2"], t.collect_group_with_index(:test){|x, i| "#{x} #{i+1}" }) 356 assert_equal(["foo 1", "bar 2"], t.collect_group_with_index(:test){|x, i| "#{x} #{i+1}" })
334 end 357 end
335 358
336 - def test_user_id_is_mandatory  
337 - ActionTrackerConfig.verbs = { :some => { :description => "Some" } }  
338 - ta = ActionTracker::Record.new :user_type => 'SomeModel', :verb => :some  
339 - ta.valid?  
340 - assert ta.errors.on(:user_id)  
341 - assert_raise ActiveRecord::RecordInvalid do  
342 - ta.save!  
343 - end  
344 - end  
345 -  
346 - def test_user_type_is_mandatory  
347 - ActionTrackerConfig.verbs = { :some => { :description => "Some" } }  
348 - ta = ActionTracker::Record.new :user_id => 2, :verb => :some  
349 - ta.valid?  
350 - assert ta.errors.on(:user_type)  
351 - assert_raise ActiveRecord::RecordInvalid do  
352 - ta.save!  
353 - end  
354 - end  
355 -  
356 end 359 end