Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 0. "trust me it works"
def cancelled_at=(time)
super.tap do
if cancelled_at_change && ((!cancelled_at_change.first) ^ (!cancelled_at_change.last))
time ? self.status_event = "cancel" : self.status_event = "reserve"
end
end
end
# 1. remove tap
def cancelled_at=(time)
result = super
if cancelled_at_change && ((!cancelled_at_change.first) ^ (!cancelled_at_change.last))
time ? self.status_event = "cancel" : self.status_event = "reserve"
end
result
end
# 2. capture changing attribute without relying on ActiveModel::Dirty tracking
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
if (before_change != after_change) && ((!before_change) ^ (!after_change))
time ? self.status_event = "cancel" : self.status_event = "reserve"
end
result
end
# 3. expand ternary to if/else, show checking for nil?
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
if (before_change != after_change) && ((!before_change) ^ (!after_change))
if time.nil?
self.status_event = "cancel"
else
self.status_event = "reserve"
end
end
result
end
# 4. ! means nil?
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
if (before_change != after_change) && (before_change.nil? ^ after_change.nil?)
if time.nil?
self.status_event = "cancel"
else
self.status_event = "reserve"
end
end
result
end
# 5. name the comparisons for before/after
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
value_changed = (before_change != after_change)
only_one_is_nil = (before_change.nil? ^ after_change.nil?)
changing_to_nil = time.nil?
if value_changed && only_one_is_nil
if changing_to_nil
self.status_event = "cancel"
else
self.status_event = "reserve"
end
end
result
end
# 6. expand the XOR comparison to && || comparison
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
value_changed = before_change != after_change
only_one_is_nil = (before_change.nil? && after_change.present?) || (before_change.present? && after_change.nil?)
changing_to_nil = time.nil?
if value_changed && only_one_is_nil
if changing_to_nil
self.status_event = "cancel"
else
self.status_event = "reserve"
end
end
result
end
# 7. name the parts of the && || comparison
def cancelled_at=(time)
before_change = cancelled_at
result = super
after_change = cancelled_at
value_changed = before_change != after_change
changing_nil_to_time = before_change.nil? && after_change.present?
changing_time_to_nil = before_change.present? && after_change.nil?
only_one_is_nil = changing_nil_to_time || changing_time_to_nil
changing_to_nil = time.nil?
if value_changed && only_one_is_nil
if changing_to_nil
self.status_event = "cancel"
else
self.status_event = "reserve"
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment