Created
December 12, 2019 12:40
-
-
Save apneadiving/77b8e654b2932df169a3e3bd3c8685d4 to your computer and use it in GitHub Desktop.
refactoring post
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
class ApplyProductStateOrder | |
def initialize(status_change_order) | |
@status_change_order = status_change_order | |
end | |
def call | |
with_valid_state_change do | |
ActiveRecord::Base.transaction do | |
product.send(transition) | |
product.save! | |
update_order(transition) | |
end | |
end | |
end | |
private | |
attr_reader :status_change_order | |
def with_valid_state_change | |
if status_change_order.missed? | |
Rails.logger.warn do | |
"Not applying product state order #{status_change_order.id} because it has already ended." | |
end | |
update_order(:none) and return | |
end | |
unless can_transition? | |
Rails.logger.warn do | |
"Cancelling product_state_order #{status_change_order.id}->#{status_change_order.order_type}. Current state is #{product.state}" | |
end | |
update_order(:none) and return | |
end | |
yield | |
end | |
def update_order(state_change) | |
status_change_order.update_timestamps(state_change) | |
status_change_order.save! | |
end | |
def transition | |
@transition ||= status_change_order.product_transition | |
end | |
def can_transition? | |
product.aasm.may_fire_event?(transition) | |
end | |
def product | |
status_change_order.product | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment