Sha256: 754b82e391e060976c5b032387515c2c1b4b6c3d38f7e128785c8ff227c2f98e

Contents?: true

Size: 1.66 KB

Versions: 34

Compression:

Stored size: 1.66 KB

Contents

module Workarea
  module Search
    class OrderText
      def initialize(order)
        @order = order
      end

      def addresses_text
        addresses.map do |address|
          [
            address.first_name,
            address.last_name,
            address.company,
            address.street,
            address.street_2,
            address.city,
            address.region,
            address.region_name,
            address.postal_code,
            address.country.name,
            address.country.alpha2,
            address.phone_number
          ]
        end.flatten.join(' ')
      end

      def payment_text
        payment.try(:credit_card).try(:issuer)
      end

      def discount_text
        @order
          .price_adjustments
          .map { |pa| pa.data['discount_id'] }
          .compact
          .uniq
          .join(' ')
      end

      def shipping_text
        shippings
          .map(&:shipping_service)
          .map { |sm| sm.try(:name) }
          .reject(&:blank?)
      end

      def text
        [
          'order',
          @order.id,
          @order.email,
          @order.promo_codes,
          @order.token,
          @order.items.map { |i| "#{i.product_id} #{i.sku}" },
          addresses_text,
          payment_text,
          shipping_text,
          discount_text
        ].flatten.join(' ')
      end

      def payment
        @payment ||= Workarea::Payment.find_or_initialize_by(id: @order.id)
      end

      def shippings
        @shippings ||= Workarea::Shipping.where(order_id: @order.id)
      end

      def addresses
        (shippings.map(&:address) + [payment.try(:address)]).reject(&:blank?)
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/models/workarea/search/order_text.rb
workarea-core-3.4.44 app/models/workarea/search/order_text.rb
workarea-core-3.4.43 app/models/workarea/search/order_text.rb
workarea-core-3.4.42 app/models/workarea/search/order_text.rb
workarea-core-3.4.41 app/models/workarea/search/order_text.rb
workarea-core-3.4.40 app/models/workarea/search/order_text.rb
workarea-core-3.4.39 app/models/workarea/search/order_text.rb
workarea-core-3.4.38 app/models/workarea/search/order_text.rb
workarea-core-3.4.37 app/models/workarea/search/order_text.rb
workarea-core-3.4.36 app/models/workarea/search/order_text.rb
workarea-core-3.4.35 app/models/workarea/search/order_text.rb
workarea-core-3.4.34 app/models/workarea/search/order_text.rb
workarea-core-3.4.33 app/models/workarea/search/order_text.rb
workarea-core-3.4.32 app/models/workarea/search/order_text.rb
workarea-core-3.4.31 app/models/workarea/search/order_text.rb
workarea-core-3.4.30 app/models/workarea/search/order_text.rb
workarea-core-3.4.29 app/models/workarea/search/order_text.rb
workarea-core-3.4.28 app/models/workarea/search/order_text.rb
workarea-core-3.4.27 app/models/workarea/search/order_text.rb
workarea-core-3.4.26 app/models/workarea/search/order_text.rb