Setup of product variants

This commit is contained in:
2015-04-08 09:37:39 +02:00
parent 2503e09f65
commit a7a953dadb
13 changed files with 116 additions and 7 deletions
@@ -24,8 +24,11 @@ App.MenuProductComponent = Ember.Component.extend
makeEditable: -> @set('editMode', true)
save: ->
return unless @get('product.isValid')
@get('product.product_variants').forEach (product_variant)->
product_variant.save() if product_variant.get('isDirty')
if @get('product.isDirty')
@get('product').save().then((=> @set 'editMode', false), (-> true))
@get('product').save()
@set 'editMode', false
destroyProduct: (product)->
if product.get('isNew')
product.deleteRecord()
@@ -38,6 +41,8 @@ App.MenuProductComponent = Ember.Component.extend
if @get('product.isNew')
@get('product').deleteRecord()
else
@get('product.product_variants').forEach (product_variant)->
product_variant.rollback()
@get('product').rollback()
@set 'editMode', false
didInsertElement: ->
@@ -10,6 +10,7 @@ App.Product = DS.Model.extend Ember.Validations.Mixin,
image: attr()
product_category: DS.belongsTo('product_category')
product_orders: DS.hasMany('product_order')
product_variants: DS.hasMany('product_variant')
image_src: (->
image = @get('image')
@@ -18,6 +19,12 @@ App.Product = DS.Model.extend Ember.Validations.Mixin,
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)?$/)
@@ -0,0 +1,8 @@
attr = DS.attr
App.ProductVariant = DS.Model.extend Ember.Validations.Mixin,
name: attr 'string'
product: DS.belongsTo 'product'
position: attr 'number', defaultValue: 0
validations:
name:
presence: true
@@ -3,14 +3,14 @@ if editMode
.small-6.medium-3.columns.name
= input value=product.name placeholder=namePlaceholder action="save"
= errors product.errors.name
.small-6.medium-3.columns.actions
a.rollback-product-action{action "rollbackProduct"}: span
a.destroy-product-action{action "destroyProduct" product}: span
a.save-product-action{action "save"}: span
.small-6.medium-3.columns.price
= edit-currency value=product.price action="save"
= errors product.errors.price
.small-6.medium-3.columns.code= input value=product.code placeholder=codePlaceholder
.small-6.medium-3.columns.actions
a.rollback-product-action{action "rollbackProduct"}: span
a.destroy-product-action{action "destroyProduct" product}: span
a.save-product-action{action "save"}: span
.row
.small-3.columns= t 'attributes.product.active'
.small-9.columns: view boolean-switch value=product.active
@@ -21,6 +21,11 @@ if editMode
.small-12.medium-6.columns
= view "upload-file" name="image" accept="image/*" file=product.image
img src=product.image_src
each product_variant in product.product_variants
.row
.small-3.medium-2.large-1.columns plus
.small-9.medium-5.large-4.columns.end
= input value=product_variant.name
else
if showProduct
.row
@@ -34,3 +39,6 @@ else
.small-3.columns
a.edit-product-action{action "makeEditable"}: span
/img src=product.image_src
if product.product_variants
.row
.small-12.columns= product.variantsDisplay
@@ -0,0 +1,62 @@
module Suppliers
class ProductVariantsController < Suppliers::ApplicationController
layout 'tablet'
# GET /product_variants
# GET /product_variants.json
def index
end
# GET /product_variants/1
# GET /product_variants/1.json
def show
end
# GET /product_variants/new
# GET /product_variants/new.json
def new
end
# GET /product_variants/1/edit
def edit
end
# POST /product_variants
# POST /product_variants.json
def create
end
# PUT /product_variants/1
# PUT /product_variants/1.json
def update
@product_variant = ProductVariant.find(params[:id])
head :unprocessable_entity and return unless product_id = product_variant_params[:product_id]
product = Product.find(product_id)
head :forbidden and return unless product.supplier_id == current_supplier.id
if @product_variant.update_attributes(product_variant_params)
render json: @product_variant
else
render json: {errors: @product_variant.errors}, status: :unprocessable_entity
end
end
# DELETE /product_variants/1
# DELETE /product_variants/1.json
def destroy
@product_variant = ProductVariant.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
@product_variant.destroy
respond_to do |format|
format.html { redirect_to suppliers_product_variants_url, notice: t('action.destroy.successfull', model: ProductVariant.model_name.human) }
format.json { head :no_content }
end
end
private
def product_variant_params
params.require(:product_variant).permit(:name, :product_id)
end
end
end
@@ -5,7 +5,7 @@ module Suppliers
end
def show
[current_supplier].include_relations(sections: :tables, product_categories: :products)
[current_supplier].include_relations(sections: :tables, product_categories: {products: :product_variants})
render json: current_supplier, serializer: Suppliers::SupplierSerializer #.new(current_supplier).as_json
end
+1
View File
@@ -15,6 +15,7 @@ class Product
#has_and_belongs_to_many :product_categories, storing_keys: false
belongs_to :supplier # direct! category is an aid
has_many :product_orders
has_many :product_variants
attr_protected :supplier_id
+7
View File
@@ -0,0 +1,7 @@
class ProductVariant
include SimplyStored::Couch
include ActiveModel::SerializerSupport
property :name
property :position, type: Fixnum, default: 0
belongs_to :product
end
+3
View File
@@ -1,6 +1,9 @@
class ProductSerializer < Qwaiter::Serializer
root 'product'
attributes :name, :price, :description, :image, :code, :position, :visible, :active, :product_category_id
has_many :product_variants
def image
if object.image.present?
{small: object.image.url(:small)}
@@ -0,0 +1,4 @@
class ProductVariantSerializer < Qwaiter::Serializer
root 'product_variant'
attributes :name
end