Skip to content

Instantly share code, notes, and snippets.

@pgr0ss
Created October 3, 2013 04:50
Show Gist options
  • Save pgr0ss/99ffd38dad5fe106f3fc to your computer and use it in GitHub Desktop.
Save pgr0ss/99ffd38dad5fe106f3fc to your computer and use it in GitHub Desktop.
class Subscription < ActiveRecord::Base
before_save :_check_internal_consistency
def _check_internal_consistency
if billing_period_start_date && billing_period_start_date > billing_period_end_date
raise "Subscription internal state is inconsistent -- billing_period_start_date vs billing_period_end_date: #{inspect}"
end
case status
when Status::Active
if billing_period_start_date && _correct_next_billing_date != next_billing_date
raise "Subscription internal state is inconsistent -- next_billing_date incorrect: #{inspect}"
end
when Status::PastDue
if failure_count > MAX_RETRY_COUNT && _correct_next_billing_date != next_billing_date
raise "Subscription internal state is inconsistent -- next_billing_date for past due subscription over max retry count: #{inspect}"
end
end
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment