Sha256: 9f4faa744b695e87610c4cf2fe374a28d230b1a40d6835cca86c8bbbbe983153

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Gemgento

  # @author Gemgento LLC
  class ProductAttributeOption < ActiveRecord::Base
    belongs_to :product_attribute
    belongs_to :store

    has_many :product_attribute_values,
              Proc.new { |join_or_model|
               if join_or_model.is_a? ProductAttributeOption
                 where(product_attribute_id: join_or_model.product_attribute_id)
               else
                 where('gemgento_product_attribute_values.product_attribute_id = gemgento_product_attribute_options.product_attribute_id')
               end
             },
             foreign_key: 'value',
             primary_key: 'value'
    has_many :products, through: :product_attribute_values

    before_save :sync_local_to_magento, if: -> { sync_needed? }

    default_scope -> { order(:order) }

    # Push local attribute option changes to magento.
    #
    # @return [Void]
    def sync_local_to_magento
      response = API::SOAP::Catalog::ProductAttribute.add_option(self, self.product_attribute)

      if response.success?
        self.sync_needed = false
        return true
      else
        errors.add(:base, response.body[:faultstring])
       return false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 app/models/gemgento/product_attribute_option.rb