<commit_before>module Spree
  class Promotion::Actions::CreateAdjustment < PromotionAction
    calculated_adjustments

    delegate :eligible?, :to => :promotion

    before_validation :ensure_action_has_calculator

    def perform(options = {})
      return unless order = options[:order]
      # Nothing to do if the promotion is already associated with the order
      return if order.promotion_credit_exists?(promotion)

      order.adjustments.promotion.reload.clear
      order.update!
      create_adjustment("#{I18n.t(:promotion)} (#{promotion.name})", order, order)
    end

    # override of CalculatedAdjustments#create_adjustment so promotional
    # adjustments are added all the time. They will get their eligability
    # set to false if the amount is 0
    def create_adjustment(label, target, calculable, mandatory=false)
      amount = compute_amount(calculable)
      target.adjustments.create(:amount => amount,
                                :source => calculable,
                                :originator => self,
                                :label => label,
                                :mandatory => mandatory)
    end

    # Ensure a negative amount which does not exceed the sum of the order's item_total and ship_total
    def compute_amount(calculable)
      [(calculable.item_total + calculable.ship_total), super.to_f.abs].min * -1
    end

    private
    def ensure_action_has_calculator
      return if self.calculator
      self.calculator = Calculator::FlatPercentItemTotal.new
    end
  end
end<commit_msg>Create an adjustment without protection inside Promotion::CreateAdjustment#create_adjustment<commit_after>module Spree
  class Promotion::Actions::CreateAdjustment < PromotionAction
    calculated_adjustments

    delegate :eligible?, :to => :promotion

    before_validation :ensure_action_has_calculator

    def perform(options = {})
      return unless order = options[:order]
      # Nothing to do if the promotion is already associated with the order
      return if order.promotion_credit_exists?(promotion)

      order.adjustments.promotion.reload.clear
      order.update!
      create_adjustment("#{I18n.t(:promotion)} (#{promotion.name})", order, order)
    end

    # override of CalculatedAdjustments#create_adjustment so promotional
    # adjustments are added all the time. They will get their eligability
    # set to false if the amount is 0
    def create_adjustment(label, target, calculable, mandatory=false)
      amount = compute_amount(calculable)
      params = { :amount => amount,
                :source => calculable,
                :originator => self,
                :label => label,
                :mandatory => mandatory }
      target.adjustments.create(params, :without_protection => true)
    end

    # Ensure a negative amount which does not exceed the sum of the order's item_total and ship_total
    def compute_amount(calculable)
      [(calculable.item_total + calculable.ship_total), super.to_f.abs].min * -1
    end

    private
    def ensure_action_has_calculator
      return if self.calculator
      self.calculator = Calculator::FlatPercentItemTotal.new
    end
  end
end
{'fraction_non_alphanumeric': 0.07556675062972293, 'fraction_numerical': 0.0025188916876574307, 'mean_word_length': 2.9564134495641343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870674', 'n_tokens_mistral': 878, 'n_tokens_neox': 831, 'n_words': 241}