attr = DS.attr
App.Product = DS.Model.extend Ember.Validations.Mixin,
  name: attr 'string'
  price: attr 'number', defaultValue: 0
  code: attr 'string'
  description: attr 'string'
  visible: attr('boolean', defaultValue: true)
  active: attr('boolean', defaultValue: true)
  position: attr('number', defaultValue: 0)
  image: attr()
  product_category: DS.belongsTo('product_category', async: false)
  product_orders: DS.hasMany('product_order', async: false)
  product_variants: DS.hasMany('product_variant', async: false)

  image_src: (->
    image = @get('image')
    return "" unless image
    return image.small if image.small and typeof(image.small) is "string"
    image
  ).property('image')

  sorted_product_variants: Ember.computed 'product_variants.@each.name', 'product_variants.@each.position', ->
    @get('product_variants').sortBy('position')

  variantsDisplay: Ember.computed 'sorted_product_variants', ->
    @get('sorted_product_variants').mapBy('name').join(', ')

  #isValid: (->
    #return false unless price = @get('price')
    #return false unless "#{price}".match(/^[+-]?\d+(\.?\d?\d)?$/)
    #true
  #).property('price')
  validations:
    name:
      presence: true
    price:
      format:
        width: /^[+-]?\d*(\.?\d?\d)?$/
