Class: Shoppe::ProductAttribute

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shoppe/product_attribute.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grouped_hashHash

Return the the available options as a hash

Returns:

  • (Hash)

23
24
25
26
27
28
# File 'app/models/shoppe/product_attribute.rb', line 23

def self.grouped_hash
  all.group_by(&:key).inject(Hash.new) do |h, (key, attributes)|
    h[key] = attributes.map(&:value).uniq
    h
  end
end

.publicObject


60
61
62
63
# File 'app/models/shoppe/product_attribute.rb', line 60

def self.public
  ActiveSupport::Deprecation.warn("The use of Shoppe::ProductAttribute.public is deprecated. use Shoppe::ProductAttribute.publicly_accessible.")
  self.publicly_accessible
end

.update_from_array(array) ⇒ Object

Create/update attributes for a product based on the provided hash of keys & values.

Parameters:

  • array (Array)

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/shoppe/product_attribute.rb', line 34

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

Instance Method Details

#productShoppe::Product

The associated product

Returns:


12
# File 'app/models/shoppe/product_attribute.rb', line 12

belongs_to :product, :class_name => 'Shoppe::Product'