Sha256: e58808a0cf7b30d473a40adfb12563ea4b76b39bcd9b3d0ff3ad1c63e24c1224

Contents?: true

Size: 1.05 KB

Versions: 127

Compression:

Stored size: 1.05 KB

Contents

module Comee
  module Core
    class OrderSource < ApplicationRecord
      has_many :field_mappings
      validates :base_url, :name, presence: true
      validate :validate_field_mapping

      def validate_field_mapping
        return unless field_mapping

        mappings = field_mapping.map(&:with_indifferent_access)
        order_fields = %w[order_number order_date delivery_address invoice_address]
        item_fields = %w[customer_item_no customer_item_description unit_code quantity price delivery_date]
        order_dest = mappings.select { |mapping| mapping[:level] == "order" }.map { |m| m[:destination] }
        item_dest = mappings.select { |mapping| mapping[:level] == "item" }.map { |m| m[:destination] }
        order_diff = order_fields - order_dest
        item_diff = item_fields - item_dest
        errors.add(:field_mapping, "has fields #{order_diff} missing at order level.") if order_diff.count.positive?

        errors.add(:field_mapping, "has fields #{item_diff} missing at item level.") if item_diff.count.positive?
      end
    end
  end
end

Version data entries

127 entries across 127 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/models/comee/core/order_source.rb
comee_core-0.3.61 app/models/comee/core/order_source.rb
comee_core-0.3.60 app/models/comee/core/order_source.rb
comee_core-0.3.59 app/models/comee/core/order_source.rb
comee_core-0.3.58 app/models/comee/core/order_source.rb
comee_core-0.3.57 app/models/comee/core/order_source.rb
comee_core-0.3.56 app/models/comee/core/order_source.rb
comee_core-0.3.55 app/models/comee/core/order_source.rb
comee_core-0.3.54 app/models/comee/core/order_source.rb
comee_core-0.3.53 app/models/comee/core/order_source.rb
comee_core-0.3.51 app/models/comee/core/order_source.rb
comee_core-0.3.50 app/models/comee/core/order_source.rb
comee_core-0.3.49 app/models/comee/core/order_source.rb
comee_core-0.3.48 app/models/comee/core/order_source.rb
comee_core-0.3.47 app/models/comee/core/order_source.rb
comee_core-0.3.46 app/models/comee/core/order_source.rb
comee_core-0.3.45 app/models/comee/core/order_source.rb
comee_core-0.3.44 app/models/comee/core/order_source.rb
comee_core-0.3.43 app/models/comee/core/order_source.rb
comee_core-0.3.42 app/models/comee/core/order_source.rb