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.1 app/models/comee/core/order_source.rb
comee_core-0.3.0 app/models/comee/core/order_source.rb
comee_core-0.2.99 app/models/comee/core/order_source.rb
comee_core-0.2.98 app/models/comee/core/order_source.rb
comee_core-0.2.97 app/models/comee/core/order_source.rb
comee_core-0.2.96 app/models/comee/core/order_source.rb
comee_core-0.2.95 app/models/comee/core/order_source.rb
comee_core-0.2.94 app/models/comee/core/order_source.rb
comee_core-0.2.93 app/models/comee/core/order_source.rb
comee_core-0.2.92 app/models/comee/core/order_source.rb
comee_core-0.2.91 app/models/comee/core/order_source.rb
comee_core-0.2.90 app/models/comee/core/order_source.rb
comee_core-0.2.89 app/models/comee/core/order_source.rb
comee_core-0.2.88 app/models/comee/core/order_source.rb
comee_core-0.2.87 app/models/comee/core/order_source.rb
comee_core-0.2.86 app/models/comee/core/order_source.rb
comee_core-0.2.85 app/models/comee/core/order_source.rb
comee_core-0.2.84 app/models/comee/core/order_source.rb
comee_core-0.2.83 app/models/comee/core/order_source.rb
comee_core-0.2.82 app/models/comee/core/order_source.rb