Class: Shoppe::ProductAttribute
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Shoppe::ProductAttribute
- Defined in:
- app/models/shoppe/product_attribute.rb
Class Method Summary collapse
-
.grouped_hash ⇒ Object
Return the the available options as a hash.
-
.update_from_array(array) ⇒ Object
Create/update attributes for a product based on the provided hash of keys & values.
Class Method Details
.grouped_hash ⇒ Object
Return the the available options as a hash
17 18 19 20 21 22 |
# File 'app/models/shoppe/product_attribute.rb', line 17 def self.grouped_hash all.group_by(&:key).inject(Hash.new) do |h, (key, attributes)| h[key] = attributes.map(&:value).uniq h end end |
.update_from_array(array) ⇒ Object
Create/update attributes for a product based on the provided hash of keys & values
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/shoppe/product_attribute.rb', line 26 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 end |