Updated

app/models/deal_product / create_or_update.rb

A
41 lines of codes
6 methods
4.3 complexity/method
2 churn
25.64 complexity
0 duplications
class DealProduct::CreateOrUpdate
  1. DealProduct::CreateOrUpdate has no descriptive comment
def initialize(deal_product, params) @deal_product = deal_product @params = params end def call
  1. DealProduct::CreateOrUpdate#call has approx 8 statements
@deal_product.assign_attributes(@params) return false if @deal_product.invalid? if needs_recalculation? ActiveRecord::Base.transaction do update_deal_product @deal_product.save!
  1. DealProduct::CreateOrUpdate#call calls '@deal_product.save!' 2 times Locations: 0 1
Deal::RecalculateAndSaveAllMonetaryValues.new(@deal_product.deal).call end else @deal_product.save!
  1. DealProduct::CreateOrUpdate#call calls '@deal_product.save!' 2 times Locations: 0 1
end @deal_product end private def needs_recalculation? should_recalculate_base_values? end def update_deal_product recalculate_from_base_values if should_recalculate_base_values? end def recalculate_from_base_values @deal_product.total_amount_in_cents = @deal_product.quantity * @deal_product.unit_amount_in_cents end def should_recalculate_base_values? @deal_product.quantity_changed? || @deal_product.unit_amount_in_cents_changed? || @deal_product.new_record? end end