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.
-
#price_only ⇒ Object
Returns the value of attribute price_only.
-
#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 = [], currency = nil) ⇒ 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 = [], currency = nil) ⇒ ProductVariantPresenter
Returns a new instance of ProductVariantPresenter.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 50 def initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, = [], currency = nil) @product = product @variant = variant @index = index @properties = properties @taxons = taxons @store = store || product.stores.first @currency = currency || @store.default_currency @price_only = @currency != @store.default_currency @metafields = end |
Instance Attribute Details
#currency ⇒ Object
Returns the value of attribute currency.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def currency @currency end |
#index ⇒ Object
Returns the value of attribute index.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def index @index end |
#metafields ⇒ Object
Returns the value of attribute metafields.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def @metafields end |
#price_only ⇒ Object
Returns the value of attribute price_only.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def price_only @price_only end |
#product ⇒ Object
Returns the value of attribute product.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def product @product end |
#properties ⇒ Object
Returns the value of attribute properties.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def properties @properties end |
#store ⇒ Object
Returns the value of attribute store.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def store @store end |
#taxons ⇒ Object
Returns the value of attribute taxons.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 def taxons @taxons end |
#variant ⇒ Object
Returns the value of attribute variant.
62 63 64 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62 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.
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 126 127 128 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 75 def call return price_only_row if price_only 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
130 131 132 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 130 def option_type(index) product.option_types[index] end |
#option_value(option_type) ⇒ Object
134 135 136 |
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 134 def option_value(option_type) variant.option_values.find { |ov| ov.option_type == option_type }&.presentation end |