Sha256: bc9957e3840df726df40a6c3bf83109f2894f22150dc1c10a9697ea5a65fe54a

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

module Logistics
  module Core
    class WeightRange < ApplicationRecord
      validates :weight_from, :weight_to, presence: true, allow_blank: false
      validates :weight_from, uniqueness: {scope: [:weight_from, :weight_to]}
      validate :weight_from_is_greater_than_weight_to

      def weight_range_name
        self.weight_from.to_s + ' - ' + self.weight_to.to_s
      end

      def self.fetch_all
        result = []
        WeightRange.all.each do |weight_range|
          result.push({id: weight_range.id,
                       weight_from: weight_range.weight_from,
                       weight_to:weight_range.weight_to,
                       weight_range_name: weight_range.weight_range_name})
        end
        return result
      end

      def self.check_range_overlap weight_range
        weight_ranges = WeightRange.all
        overlap = false
        weight_ranges.each do |we_range|
          if (weight_range.weight_from..weight_range.weight_to).overlaps?(we_range.weight_from..we_range.weight_to)
            overlap = true
          end
        end
        return overlap
      end

      def self.check_range_overlap_update weight_range, id
        weight_ranges = WeightRange.all
        result = { 'overlap' => false, 'id' => 0 }
        weight_ranges.each do |we_range|
          if (weight_range.weight_from..weight_range.weight_to).overlaps?(we_range.weight_from..we_range.weight_to)
            if we_range.id != id.to_i
              result['overlap'] = true
              result['id'] = we_range.id
            end
          end
        end
        return result
      end

      private

      def weight_from_is_greater_than_weight_to
        return if weight_from.blank? || weight_to.blank?

        if weight_to < weight_from
          errors.add(:weight_from, "cannot be greater than weight to")
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
logistics_core-21.11.2 app/models/logistics/core/weight_range.rb
logistics_core-21.11.1 app/models/logistics/core/weight_range.rb
logistics_core-21.08.1 app/models/logistics/core/weight_range.rb
logistics_core-21.03.1 app/models/logistics/core/weight_range.rb
logistics_core-20.10.3 app/models/logistics/core/weight_range.rb
logistics_core-20.10.2 app/models/logistics/core/weight_range.rb
logistics_core-20.10.1 app/models/logistics/core/weight_range.rb
logistics_core-20.9.1 app/models/logistics/core/weight_range.rb
logistics_core-20.8.1 app/models/logistics/core/weight_range.rb