Class: AdditionalCalculatorRate

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/additional_calculator_rate.rb

Constant Summary collapse

WEIGHT =

Types (0 is the default value for the column, therefore I’m not using it)

1
QNTY =

Total item weight

2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_typesObject

All complex calculator rate types



25
26
27
# File 'app/models/additional_calculator_rate.rb', line 25

def self.all_types
  [WEIGHT, QNTY]
end

.find_previous_rate(calculator_id, rate_type, value) ⇒ Object

Find the previous rate for the specified value



37
38
39
40
# File 'app/models/additional_calculator_rate.rb', line 37

def self.find_previous_rate(calculator_id, rate_type, value)
  rate = for_calculator(calculator_id).for_type(rate_type).where("to_value < ?", value).order("rate DESC").first()
  rate.nil? ? nil : rate.rate
end

.find_rate(calculator_id, rate_type, value) ⇒ Object

Find the rate for the specified value



30
31
32
33
34
# File 'app/models/additional_calculator_rate.rb', line 30

def self.find_rate(calculator_id, rate_type, value)
  # get the lowes rate if multiple rates are defined (overlap)
  rate = for_calculator(calculator_id).for_type(rate_type).for_value(value).order("rate").first()
  rate.nil? ? nil : rate.rate
end

Instance Method Details

#validate_from_value_smaller_than_to_valueObject



17
18
19
20
21
22
# File 'app/models/additional_calculator_rate.rb', line 17

def validate_from_value_smaller_than_to_value
  # ignore following cases
  return if from_value.nil? || to_value.nil?

  errors.add(:base, I18n.t('errors.from_value_greater_than_to_value')) if from_value > to_value
end