rails g model Policy start_date:date end_date:date contractable:references\{polymorphic\}
has_many :policies, as: :contractable
rails g model Claim occurrence_date:date type:string
class Smartphone::DamageClaim < Claim
end
class Auto::DamageClaim < Claim
end
To be refactored:
class Policy < ApplicationRecord
RATE_IOF = 1.0738
RATE_BROKERAGE = 0.0001
validates :premium_value_cents, numericality: {greater_than: 0}
has_many :premium_movements, dependent: :destroy
has_many :events, as: :publisher
aasm do
before_all_events :memoize_current_attributes
after_all_events :publish_transition_events
end
def publish_created_event
publish_event("created", data: attributes)
end
# ...
end
class Claim < ApplicationRecord
has_many :claim_movements, dependent: :destroy
validates :user_id, presence: true
has_many :events, as: :publisher
aasm do
before_all_events :memoize_current_attributes
after_all_events :publish_transition_events
end
def publish_created_event
publish_event("created", data: attributes)
end
# ...
end
New Concern
module EventPublishable
extend ActiveSupport::Concern
included do
has_many :events, as: :publisher
aasm do
before_all_events :memoize_current_attributes
after_all_events :publish_transition_event
end
end
def publish_created_event
publish_event("created", data: attributes)
end
# ...
end