Sha256: c7b6d62c9abc008a25eaaab2ae6caf051185a519bd02a60e87a0ddaff162d4fe

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module CompareProductsHelper
  # Return the transposed matrix of the comparison table.
  # 
  # Example:
  #   [
  #     ["Product", "product1 image", "product2 image"],
  #     ["Name",    "product1 name",  "product2 name"],
  #     ...
  #     ["Price",   "product1 price", "product2 price"]
  #   ]
  def comparison_rows_for(products, properties)
    fields = [comparison_fields_for(products, properties)]
    products.each do |product|
      fields << (product_fields_for(product, properties))
    end
    fields.transpose
  end
  
  # Return an array with the values of the fields to be compared for
  # the specified product.
  # 
  # Example:
  #   ["product1 image", "product1 name", ..., "product1 price"]
  
  def product_fields_for(product, properties)
    [ link_to(small_image(product), product), link_to(product.name, product)].tap { |fields|
      properties.each do |property|
        fields << product.product_properties.find_by_property_id(property.id).try(:value)
      end
    }.tap { |fields| fields << number_to_currency(product.price) }
  end
  
  # Returns an array with the translated names of the fields to be
  # compared.
  # 
  # Example:
  #   ["Product", "Name", ..., "Price"]
  def comparison_fields_for(products, properties)
    [t('product'), t('name')].tap { |fields|
      properties.each { |property| fields << property.presentation }
    }.tap { |fields|
      fields << t('price')
    }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_compare_products-0.50.0 app/helpers/compare_products_helper.rb