Sha256: ac7e10d5c664c57f7842ab3ebe17b7ec99c482a7cdcb53002abd83817e2e918b

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Address::Component < SolidusAdmin::BaseComponent
  include SolidusAdmin::Layout::PageHelpers

  VALID_TYPES = ['ship', 'bill'].freeze

  def initialize(order:, address:, user: nil, type: 'ship')
    @order = order
    @user = user
    @address = address
    @addresses = user&.addresses.to_a.reject(&:new_record?)
    @type = validate_address_type(type)
  end

  def form_id
    @form_id ||= "#{stimulus_id}--form-#{@type}-#{@order.id}"
  end

  def address_frame_id
    @table_frame_id ||= "#{stimulus_id}--#{@type}-address-frame-#{@order.id}"
  end

  def use_attribute
    case @type
    when 'ship'
      'use_shipping'
    when 'bill'
      'use_billing'
    end
  end

  def format_address(address)
    safe_join([
      address.name,
      tag.br,
      address.address1,
      tag.br,
      address.address2,
      address.city,
      address.zipcode,
      address.state&.name,
      tag.br,
      address.country.name,
      tag.br,
      address.phone,
    ], " ")
  end

  def validate_address_type(type)
    VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}")
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_admin-0.3.2 app/components/solidus_admin/orders/show/address/component.rb
solidus_admin-0.3.1 app/components/solidus_admin/orders/show/address/component.rb
solidus_admin-0.3.0 app/components/solidus_admin/orders/show/address/component.rb
solidus_admin-0.2.0 app/components/solidus_admin/orders/show/address/component.rb
solidus_admin-0.1.0 app/components/solidus_admin/orders/show/address/component.rb