Skip to content

Instantly share code, notes, and snippets.

@jvans1
Last active December 25, 2015 07:29
Show Gist options
  • Save jvans1/6939689 to your computer and use it in GitHub Desktop.
Save jvans1/6939689 to your computer and use it in GitHub Desktop.
#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