Module: Spree::ProductDecorator

Defined in:
app/decorators/models/spree/product_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
# File 'app/decorators/models/spree/product_decorator.rb', line 5

def self.prepended(base)
  base.class_eval do
    include SolidusSeo::Model
  end
end

Instance Method Details

#in_stock?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/decorators/models/spree/product_decorator.rb', line 43

def in_stock?
  available? && variants_including_master.suppliable.any?
end

#jsonld_dataObject



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
# File 'app/decorators/models/spree/product_decorator.rb', line 74

def jsonld_data
  {
    "@context": "http://schema.org/",
    "@type": "Product",
    "name": name,
    "url": seo_url,
    "image": seo_images,
    "description": seo_name,
    "sku": sku,
    "brand": seo_brand,
    # TODO: ratings/reviews
    # "aggregateRating": {
    #   "@type": "AggregateRating",
    #   "ratingValue": "4.4",
    #   "reviewCount": "89"
    # },
    "offers": {
      "@type": "Offer",
      "priceCurrency": seo_currency,
      "price": seo_price,
      "itemCondition": "http://schema.org/NewCondition",
      "availability": "http://schema.org/#{ in_stock? ? 'InStock' : 'OutOfStock'}",
    }
  }
end

#seo_brandObject



31
32
33
# File 'app/decorators/models/spree/product_decorator.rb', line 31

def seo_brand
  @brand ||= taxons.detect { |it| it.root.name =~ /^brands?$/i }.try(:name)
end

#seo_currencyObject



35
36
37
# File 'app/decorators/models/spree/product_decorator.rb', line 35

def seo_currency
  master.default_price.currency
end

#seo_dataObject



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
# File 'app/decorators/models/spree/product_decorator.rb', line 47

def seo_data
  {
    title: seo_name,
    description: seo_description,
    name: seo_name,
    image_src: seo_images.first,
    og: {
      type: 'product',
      url: seo_url,
      brand: seo_brand,
      image: {
        _: :image_src,
        alt: seo_name,
      }
    },
    product: {
      price: {
        amount: seo_price,
        currency: seo_currency,
      }
    },
    twitter: {
      card: 'summary_large_image',
    }
  }
end

#seo_descriptionObject



27
28
29
# File 'app/decorators/models/spree/product_decorator.rb', line 27

def seo_description
  plain_text(try(:meta_description).presence || try(:description))
end

#seo_imagesObject



19
20
21
22
23
24
25
# File 'app/decorators/models/spree/product_decorator.rb', line 19

def seo_images
  return [] unless gallery.images.any? && gallery.images.first.attachment.file?

  [
    url_helper.image_url(gallery.images.first.attachment.url(:large), host: store_host),
  ].compact
end

#seo_nameObject



11
12
13
# File 'app/decorators/models/spree/product_decorator.rb', line 11

def seo_name
  plain_text(try(:meta_title).presence) || name
end

#seo_priceObject



39
40
41
# File 'app/decorators/models/spree/product_decorator.rb', line 39

def seo_price
  master.default_price.amount
end

#seo_urlObject



15
16
17
# File 'app/decorators/models/spree/product_decorator.rb', line 15

def seo_url
  spree_route_helper.product_url(self, host: store_host)
end