Sha256: e570bc0f720ecce9dfcd88f3665fd967ccaf6d69ab5b51335c5d871d07fd2e1f

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

module Shoppe
  class ProductAttribute < ActiveRecord::Base
  
    self.table_name = 'shoppe_product_attributes'  
    
    # Validations
    validates :key, :presence => true
  
    # The associated product
    #
    # @return [Shoppe::Product]
    belongs_to :product, :class_name => 'Shoppe::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 Shoppe::ProductAttribute.public is deprecated. use Shoppe::ProductAttribute.publicly_accessible.")
      self.publicly_accessible
    end
  
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
shoppe-1.1.1 app/models/shoppe/product_attribute.rb
shoppe-1.1.0 app/models/shoppe/product_attribute.rb
shoppe-1.0.9 app/models/shoppe/product_attribute.rb
shoppe-1.0.8 app/models/shoppe/product_attribute.rb
kylekthompson-shoppe-1.0.7 app/models/shoppe/product_attribute.rb
shoppe-1.0.7 app/models/shoppe/product_attribute.rb
shoppe-1.0.6 app/models/shoppe/product_attribute.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/shoppe-1.0.5/app/models/shoppe/product_attribute.rb
shoppe-1.0.5 app/models/shoppe/product_attribute.rb
shoppe-1.0.3 app/models/shoppe/product_attribute.rb
shoppe-1.0.2 app/models/shoppe/product_attribute.rb
shoppe-1.0.1 app/models/shoppe/product_attribute.rb
shoppe-1.0.0 app/models/shoppe/product_attribute.rb