Last active
December 12, 2019 12:55
-
-
Save apneadiving/c453eb75adf4d34f5878d5b8cb70010e 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 | |
ActiveRecord::Base.transaction do | |
state_change = update_product | |
status_change_order.update_timestamps(state_change) | |
status_change_order.save! | |
end | |
end | |
private | |
attr_reader :status_change_order | |
def update_product | |
product = status_change_order.product | |
state_change = product_state_change | |
unless state_change == :none | |
product.send(state_change) | |
product.save! | |
end | |
state_change | |
end | |
def product_state_change | |
product = status_change_order.product | |
if status_change_order.missed? | |
Rails.logger.warn do | |
"Not applying product state order #{status_change_order.id} because it has already ended." | |
end | |
return :none | |
end | |
transition = status_change_order.product_transition | |
unless can_transition?(status_change_order.product, transition) | |
Rails.logger.warn do | |
"Cancelling product_state_order #{status_change_order.id}->#{status_change_order.order_type}. Current state is #{product.state}" | |
end | |
return :none | |
end | |
transition | |
end | |
def can_transition?(product, transition) | |
product.aasm.may_fire_event?(transition) | |
end | |
end |
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