Sha256: 5f9e2fb0de08c0fd786d9bdc9a9dfe4445be3c684750fc05b6e08211afe05f33

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

module KktShoppe
  class ProductAttribute < ActiveRecord::Base
  
    self.table_name = 'kkt_shoppe_product_attributes'  
    
    # Validations
    validates :key, :presence => true
  
    # The associated product
    #
    # @return [KktShoppe::Product]
    belongs_to :product, :class_name => 'KktShoppe::Product'
  
    # All attributes which are searchable
    scope :searchable, -> { where(:searchable => true) }
    
    # All attributes which are public
    scope :publicly_accessible, -> { where(:public => true) }
  
    # Return the the available options as a hash
    #
    # @return [Hash]
    def self.grouped_hash
      all.group_by(&:key).inject(Hash.new) do |h, (key, attributes)|
        h[key] = attributes.map(&:value).uniq
        h
      end
    end
  
    # Create/update attributes for a product based on the provided hash of
    # keys & values.
    #
    # @param array [Array]
    def self.update_from_array(array)
      existing_keys = self.pluck(:key)
      index = 0
      array.each do |hash|
        next if hash['key'].blank?
        index += 1
        params = hash.merge({
          :searchable => hash['searchable'].to_s == '1',
          :public => hash['public'].to_s == '1',
          :position => index
        })
        if existing_attr = self.where(:key => hash['key']).first
          if hash['value'].blank?
            existing_attr.destroy
            index -= 1
          else
            existing_attr.update_attributes(params)
          end
        else
          attribute = self.create(params)
        end
      end
      self.where(:key => existing_keys - array.map { |h| h['key']}).delete_all
      true
    end
    
    def self.public
      ActiveSupport::Deprecation.warn("The use of KktShoppe::ProductAttribute.public is deprecated. use KktShoppe::ProductAttribute.publicly_accessible.")
      self.publicly_accessible
    end
  
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kkt_shoppe-2.0.2 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-2.0.1 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-2.0.0 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.3.0 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.2.1 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.2.0 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.1.2 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.1.1 app/models/kkt_shoppe/product_attribute.rb
kkt_shoppe-1.1.0 app/models/kkt_shoppe/product_attribute.rb