Class: KktShoppe::ProductAttribute
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- KktShoppe::ProductAttribute
- Defined in:
- app/models/kkt_shoppe/product_attribute.rb
Class Method Summary collapse
-
.grouped_hash ⇒ Hash
Return the the available options as a hash.
- .public ⇒ Object
-
.update_from_array(array) ⇒ Object
Create/update attributes for a product based on the provided hash of keys & values.
Instance Method Summary collapse
-
#product ⇒ KktShoppe::Product
The associated product.
Class Method Details
.grouped_hash ⇒ Hash
Return the the available options as a hash
23 24 25 26 27 28 |
# File 'app/models/kkt_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 |
.public ⇒ Object
60 61 62 63 |
# File 'app/models/kkt_shoppe/product_attribute.rb', line 60 def self.public ActiveSupport::Deprecation.warn("The use of KktShoppe::ProductAttribute.public is deprecated. use KktShoppe::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.
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/kkt_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
#product ⇒ KktShoppe::Product
The associated product
12 |
# File 'app/models/kkt_shoppe/product_attribute.rb', line 12 belongs_to :product, :class_name => 'KktShoppe::Product' |