Files
openfoodnetwork/app/models/calculator/flexi_rate.rb
Neal Chambers e9f448fad9 Safely autocorrect Lint/AmbiguousOperatorPrecedence
Inspecting 1480 files
...................................................................................................................................................................................................................................W..........................W........................................................W................W..............W................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Offenses:

app/models/calculator/flexi_rate.rb:38:7: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
      count * preferred_additional_item.to_f + preferred_first_item.to_f
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:362:12: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
    cat << "sells_" + sells
           ^^^^^^^^^^^^^^^^
app/models/enterprise.rb:496:21: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
    phone_number && "https://wa.me/" + phone_number.tr('+ ', '')
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/ability.rb:27:33: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          order.user == user || order.token && token == order.token
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/ability.rb:30:33: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          order.user == user || order.token && token == order.token
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/line_item.rb:205:16: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
      (price + fees / quantity).round(2)
               ^^^^^^^^^^^^^^^
app/models/spree/preferences/store.rb:28:11: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          should_persist? && Spree::Preference.where(key: key).exists?
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1480 files inspected, 7 offenses detected, 7 offenses corrected
2023-07-30 21:29:19 +09:00

42 lines
966 B
Ruby

# frozen_string_literal: false
module Calculator
class FlexiRate < Spree::Calculator
preference :first_item, :decimal, default: 0.0
preference :additional_item, :decimal, default: 0.0
preference :max_items, :integer, default: 0
validates :preferred_first_item,
:preferred_additional_item,
numericality: true
def self.description
I18n.t(:flexible_rate)
end
def self.available?(_object)
true
end
def compute(object)
max = preferred_max_items.to_i
items_count = line_items_for(object).map(&:quantity).sum
# check max value to avoid divide by 0 errors
return 0 if max.zero?
if items_count > max
compute_for(max - 1)
elsif items_count <= max
compute_for(items_count - 1)
end
end
private
def compute_for(count)
(count * preferred_additional_item.to_f) + preferred_first_item.to_f
end
end
end