Module: Spree::SpreeChannable::ProductDecorator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/product_decorator.rb', line 6

def self.prepended(base)
  class << base

    def to_channable_variant_xml(products)
      builder = Nokogiri::XML::Builder.new do |xml|
        xml.feed(xmlns: 'http://www.w3.org/2005/Atom') do
          xml.title 'Channable variant feed'
          xml.link(rel: 'self', href: ::SpreeChannable.configuration.host)
          xml.updated DateTime.now.strftime('%Y-%m-%dT%H:%M:%S%z')

          xml.variants do
            products.map {|product| product.to_channable_variant_xml}.each do |variants_xml|
              variants_xml.each do |variant_xml|
                xml.parent << Nokogiri::XML(variant_xml).at('variant')
              end
            end
          end


        end
      end

      builder.to_xml
    end

    def to_channable_product_xml(products)
      builder = Nokogiri::XML::Builder.new do |xml|
        xml.feed(xmlns: 'http://www.w3.org/2005/Atom') do
          xml.title 'Channable product feed'
          xml.link(rel: 'self', href: ::SpreeChannable.configuration.host)
          xml.updated DateTime.now.strftime('%Y-%m-%dT%H:%M:%S%z')

          xml.products do
            products.map {|product| product.to_channable_product_xml}.each do |product_xml|
              xml.parent << Nokogiri::XML(product_xml).at('product')
            end
          end

        end
      end
      builder.to_xml
    end
  end
end

Instance Method Details

#normalized_descriptionObject



127
128
129
130
131
132
133
# File 'app/models/spree/product_decorator.rb', line 127

def normalized_description
  if description.blank? || description.length < 3
    name
  else
    description
  end
end

#to_channable_product_xmlObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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/models/spree/product_decorator.rb', line 58

def to_channable_product_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.product do
      xml.id id
      xml.title "#{name}"
      xml.description ActionController::Base.helpers.strip_tags(normalized_description)
      xml.link URI.join(::SpreeChannable.configuration.host, "/#{::SpreeChannable.configuration.url_prefix}/" + "#{slug}").to_s
      (xml.image_link URI.join(::SpreeChannable.configuration.image_host, images.first.attachment.url(:large)).to_s) if images.any?
      xml.condition property('product_condition') || ::SpreeChannable.configuration.product_condition

      xml.images do
        images.each do |image|
          xml.image URI.join(::SpreeChannable.configuration.image_host, image.attachment.url(:large)).to_s
        end
      end

      xml.price price

      xml.brand property('brand') || ::SpreeChannable.configuration.brand

      xml.categories do
        taxons.each do |taxon|
          xml.category taxon.self_and_ancestors.collect(&:name).join('|')
        end
      end

      xml.currency Spree::Config.currency
      xml.locale I18n.default_locale

      # Property fields

      xml.gender property('gender') || 'Not set'
      xml.delivery_period property('delivery_period') || ::SpreeChannable.configuration.delivery_period
      xml.material property('material') || 'Not set'

      xml.variants do
        (variants.any? ? variants : variants_including_master).each do |variant|
          xml.variant do
            xml.id variant.id
            xml.product_id id
            xml.options_text variant.options_text
            (xml.image_link URI.join(::SpreeChannable.configuration.image_host, variant.get_images.first.attachment.url(:large)).to_s) unless variant.get_images.empty?
            xml.images do
              variant.get_images.each do |image|
                xml.image URI.join(::SpreeChannable.configuration.image_host, image.attachment.url(:large)).to_s
              end
            end

            xml.availability variant.can_supply?
            xml.stock variant.total_on_hand
            xml.price variant.price
            xml.sale_price variant.respond_to?(:sale_price) ? (variant.sale_price || variant.price) : variant.price

            xml.sku variant.sku

            xml.currency Spree::Config.currency
            xml.locale I18n.default_locale

            variant.option_values.each do |option_value|
              xml.send(option_value.option_type.name, option_value.presentation)
            end
          end
        end
      end

    end
  end.to_xml
end

#to_channable_variant_xmlObject



52
53
54
55
56
# File 'app/models/spree/product_decorator.rb', line 52

def to_channable_variant_xml
  (variants.any? ? variants : variants_including_master).active.uniq.map do |variant|
    variant.to_channable_feed_entry
  end
end