Last active
December 25, 2015 07:29
-
-
Save jvans1/6939689 to your computer and use it in GitHub Desktop.
This file contains 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
#The point of this method is to try executing a change on CustomField Kind, i.e. changing from string to text. | |
#since not all custom fields are interchangeable like this (boolean to integer for example) this validation | |
#executes he query that updates it, forces a rollback by raising an exception, do nothing if the exception was triggered | |
#by us(the query ran successfully) and then rescue every other exception and assume it's because of incompoatible types | |
def validate_value_kind_change | |
if should_migrate_values? | |
begin | |
begin | |
self.class.transaction do | |
migrate_values(custom_field_values.id_equals(custom_field_values.first.id)) | |
#doesn't trigger a rollback since we rescue this within the parent transaction, this isn't a "true" subtransaction | |
raise RollbackTransaction.new | |
end | |
rescue RollbackTransaction => e | |
end | |
rescue Exception => e | |
errors.add(:kind, "sorry you can't change from #{kind_was} to #{kind} because the 2 types are not related. Can you convert the data to the other?") | |
end | |
end | |
end | |
def validate_value_kind_change | |
if should_migrate_values? | |
begin | |
#mark this as a true sub transaction | |
self.class.transaction(:requires_new => true) do | |
migrate_values(custom_field_values.id_equals(custom_field_values.first.id)) | |
#this is a special active record exception that doesn't bubble up past the transaction so we dont' need to rescue it | |
raise ActiveRecord::Rollback.new | |
end | |
rescue Exception => e | |
errors.add(:kind, "sorry you can't change from #{kind_was} to #{kind} because the 2 types are not related. Can you convert the data to the other?") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment