Last active
November 26, 2015 13:22
-
-
Save Sephyros/9908fe1c77b6e36a90ee to your computer and use it in GitHub Desktop.
Moving next state with validation on STI
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 Order < ActiveRecord::Base | |
end | |
class WaitingOrder < Order | |
def cancel_order(params) | |
params = params.merge(type: CanceledOrder.to_s) | |
update_attributes(params) | |
end | |
end | |
class CanceledOrder < Order | |
validates :justification, presence: true | |
end | |
class WaitingOrderTest < ActiveSupport::TestCase | |
test "should cancel waiting order" do | |
p = { justification: nil } | |
f = WaitingOrder.new | |
assert_not f.cancel_order(p) | |
assert_equal 'WaitingOrder', f.type | |
assert f.errors[:justification].include?(I18n.t('activerecord.errors.messages.blank')) | |
p = { justification: 'Too late!' } | |
assert f.cancel_order(p) | |
assert_equal 'CanceledOrder', f.type | |
end | |
end |
Author
Sephyros
commented
Nov 26, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment