Class: Spree::CSV::ProductVariantPresenter
- Inherits:
-
Object
- Object
- Spree::CSV::ProductVariantPresenter
- Includes:
- ImagesHelper
- Defined in:
- app/presenters/spree/csv/product_variant_presenter.rb
Constant Summary collapse
- CSV_HEADERS =
[ 'product_id', 'sku', 'name', 'slug', 'status', 'vendor_name', 'brand_name', 'description', 'meta_title', 'meta_description', 'meta_keywords', 'tags', 'labels', 'price', 'compare_at_price', 'currency', 'width', 'height', 'depth', 'dimensions_unit', 'weight', 'weight_unit', 'available_on', 'discontinue_on', 'track_inventory', 'inventory_count', 'inventory_backorderable', 'tax_category', 'shipping_category', 'image1_src', 'image2_src', 'image3_src', 'option1_name', 'option1_value', 'option2_name', 'option2_value', 'option3_name', 'option3_value', 'category1', 'category2', 'category3', ].freeze
Instance Attribute Summary collapse
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#index ⇒ Object
Returns the value of attribute index.
-
#metafields ⇒ Object
Returns the value of attribute metafields.
-
#product ⇒ Object
Returns the value of attribute product.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#store ⇒ Object
Returns the value of attribute store.
-
#taxons ⇒ Object
Returns the value of attribute taxons.
-
#variant ⇒ Object
Returns the value of attribute variant.
Instance Method Summary collapse
-
#call ⇒ Array
Generates an array representing a CSV row of product variant data.
-
#initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, metafields = []) ⇒ ProductVariantPresenter
constructor
A new instance of ProductVariantPresenter.
- #option_type(index) ⇒ Object
- #option_value(option_type) ⇒ Object
Methods included from ImagesHelper
#spree_asset_aspect_ratio, #spree_image_tag, #spree_image_url, #spree_image_variant_options
Constructor Details
#initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, metafields = []) ⇒ ProductVariantPresenter
Returns a new instance of ProductVariantPresenter.
50 51 52 53 54 55 56 57 58 59 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 50 def initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, = []) @product = product @variant = variant @index = index @properties = properties @taxons = taxons @store = store || product.stores.first @currency = @store.default_currency = end |
Instance Attribute Details
#currency ⇒ Object
Returns the value of attribute currency.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def currency @currency end |
#index ⇒ Object
Returns the value of attribute index.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def index @index end |
#metafields ⇒ Object
Returns the value of attribute metafields.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def end |
#product ⇒ Object
Returns the value of attribute product.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def product @product end |
#properties ⇒ Object
Returns the value of attribute properties.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def properties @properties end |
#store ⇒ Object
Returns the value of attribute store.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def store @store end |
#taxons ⇒ Object
Returns the value of attribute taxons.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def taxons @taxons end |
#variant ⇒ Object
Returns the value of attribute variant.
61 62 63 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 61 def variant @variant end |
Instance Method Details
#call ⇒ Array
Generates an array representing a CSV row of product variant data.
For the primary variant row (when the index is zero), product-level details such as name, slug, status, vendor and brand names, description, meta tags, and tag/label lists are included. In all cases, variant-specific attributes (e.g., id, SKU, pricing, dimensions, weight, availability dates, inventory count, shipping category, tax category, image URLs via original_url, and the first three option types and corresponding option values) are appended. Additionally, when the index is zero, associated taxons and properties are added.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 74 def call total_on_hand = variant.total_on_hand csv = [ product.id, variant.sku, index.zero? ? product.name : nil, product.slug, index.zero? ? product.status : nil, index.zero? ? product.try(:vendor_name) : nil, index.zero? ? product.try(:brand_name) : nil, index.zero? ? product.description&.html_safe : nil, index.zero? ? product. : nil, index.zero? ? product. : nil, index.zero? ? product. : nil, index.zero? ? product.tag_list.to_s : nil, index.zero? ? product.label_list.to_s : nil, variant.amount_in(currency).to_f, variant.compare_at_amount_in(currency).to_f, currency, variant.width, variant.height, variant.depth, variant.dimensions_unit, variant.weight, variant.weight_unit, variant.available_on&.strftime('%Y-%m-%d %H:%M:%S'), (variant.discontinue_on || product.discontinue_on)&.strftime('%Y-%m-%d %H:%M:%S'), variant.track_inventory?, total_on_hand == BigDecimal::INFINITY ? '∞' : total_on_hand, variant.backorderable?, variant.tax_category&.name, product.shipping_category&.name, spree_image_url(variant.images[0], ), spree_image_url(variant.images[1], ), spree_image_url(variant.images[2], ), index.positive? ? option_type(0)&.presentation : nil, index.positive? ? option_value(option_type(0)) : nil, index.positive? ? option_type(1)&.presentation : nil, index.positive? ? option_value(option_type(1)) : nil, index.positive? ? option_type(2)&.presentation : nil, index.positive? ? option_value(option_type(2)) : nil ] if index.zero? csv += taxons csv += properties csv += end csv end |
#option_type(index) ⇒ Object
127 128 129 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 127 def option_type(index) product.option_types[index] end |
#option_value(option_type) ⇒ Object
131 132 133 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 131 def option_value(option_type) variant.option_values.find { |ov| ov.option_type == option_type }&.presentation end |