Class: ExportedCatalogItemPacketAttribute

Inherits:
ApplicationRecord show all
Defined in:
app/models/exported_catalog_item_packet_attribute.rb

Overview

== Schema Information

Table name: exported_catalog_item_packet_attributes
Database name: primary

id :integer not null, primary key
attr_connected_to :string
attr_name :string
attr_position :integer
attr_rename :string
attr_type :string
attr_value :string
raw :boolean default(FALSE)
selected :boolean default(FALSE)
exported_catalog_item_packet_id :integer

Indexes

ecip_id_attr_name (exported_catalog_item_packet_id,attr_name)
idx_attr_ecip_id_selected (exported_catalog_item_packet_id,selected)
idx_ecip_id_attr_position (exported_catalog_item_packet_id,attr_position)

Belongs to collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Method Details

#effective_nameObject



29
30
31
# File 'app/models/exported_catalog_item_packet_attribute.rb', line 29

def effective_name
  attr_rename.presence || attr_name.presence
end

#exported_catalog_item_packetExportedCatalogItemPacket



24
# File 'app/models/exported_catalog_item_packet_attribute.rb', line 24

belongs_to :exported_catalog_item_packet, optional: true

#render_value(ec_item) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/exported_catalog_item_packet_attribute.rb', line 33

def render_value(ec_item)
  case attr_type
  when 'static'
    if attr_connected_to.nil?
      attr_value
    else
      'Not Found'
    end
  when 'catalog'
    cip = ec_item.catalog_item
    case attr_name
    when 'name'
      cip.item.name.to_s.squish
    when 'description'
      cip.item.detailed_description.to_s.squish
    when 'catalog_price'
      if raw
        cip.amount
      else
        ActionController::Base.helpers.number_to_currency(cip.amount, unit: cip.catalog.currency_symbol).to_s
      end
    when 'parent_catalog_price'
      if raw
        cip.parent_catalog_item_amount
      else
        ActionController::Base.helpers.number_to_currency(cip.parent_catalog_item_amount, unit: (cip.parent_catalog_item.amount_currency_symbol rescue "$"))
      end
    when 'discount'
      if cip.parent_catalog_item
        parent_amount = cip.parent_catalog_item.amount
        if parent_amount == cip.amount
          disc = 0
        elsif parent_amount > 0
          disc = ((1 - cip.amount / parent_amount) * 100).round(2)
        else
          disc = 100
        end
        disc
      else
        'N/A'
      end
    end
  when 'image'
    ec_images = ec_item.exported_catalog_item_images.where(excluded: false).to_a
    if (idx = attr_name[/image_url_(\d+)/, 1])
      ec_images[idx.to_i - 1]&.image_url
    elsif (idx = attr_name[/image_name_(\d+)/, 1])
      ec_images[idx.to_i - 1]&.file_name
    end
  else
    i = ec_item.catalog_item.item
    if raw
      i.rendered_product_specifications.dig(attr_name.to_sym,:raw) || (i.send(attr_name) rescue nil)
    else
      i.rendered_product_specifications.dig(attr_name.to_sym,:output) || (i.send(attr_name) rescue nil)
    end
  end
end