ProductCardComponent

InfoGenerateCreated ByPackages

This Ruby class defines a `ProductCardComponent` that generates an HTML structure for displaying a product card with provided details: product name, image URL, description, and product URL. It uses the `ApplicationComponent` superclass, and sets instance variables for the provided product attributes. The `template` method constructs the HTML card structure using a combination of `div`, `figure`, `img`, `h2`, `p`, and `a` tags, styled with specific CSS classes. The class appears to be built for a framework that allows components to be easily created and rendered, which enhances code organization and reuse.

# frozen_string_literal: true

class ProductCardComponent < ApplicationComponent
  def initialize(name:, image_url:, description:, product_url:)
    @name = name
    @image_url = image_url
    @description = description
    @product_url = product_url
  end

  def template
    div(class: "card card-compact w-96 bg-base-100 shadow-xl") do
      figure { img(src: @image_url, alt: @name) }
      div(class: "card-body") do
        h2(class: "card-title") { @name }
        p { @description }
        div(class: "card-actions justify-end") do
          a(href: @product_url, class: "btn btn-primary") { "View Product" }
        end
      end
    end
  end
end