From a7a953dadb6d83ed2e52135a73d761b618b3b9b2 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 8 Apr 2015 09:37:39 +0200 Subject: [PATCH] Setup of product variants --- .gitignore | 2 + .../app/components/menu-product.js.coffee | 7 ++- .../supplier/app/models/product.js.coffee | 7 +++ .../app/models/product_variant.js.coffee | 8 +++ .../templates/components/menu-product.emblem | 16 +++-- .../suppliers/product_variants_controller.rb | 62 +++++++++++++++++++ .../suppliers/suppliers_controller.rb | 2 +- app/models/product.rb | 1 + app/models/product_variant.rb | 7 +++ app/serializers/product_serializer.rb | 3 + app/serializers/product_variant_serializer.rb | 4 ++ config/routes.rb | 1 + fig.yml | 3 +- 13 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/supplier/app/models/product_variant.js.coffee create mode 100644 app/controllers/suppliers/product_variants_controller.rb create mode 100644 app/models/product_variant.rb create mode 100644 app/serializers/product_variant_serializer.rb diff --git a/.gitignore b/.gitignore index 2f615afe..28b4d021 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ erl_crash.dump /.project # dia /*.autosave +/db/data +/db/config diff --git a/app/assets/javascripts/supplier/app/components/menu-product.js.coffee b/app/assets/javascripts/supplier/app/components/menu-product.js.coffee index 7506ef19..ea1c5a2a 100644 --- a/app/assets/javascripts/supplier/app/components/menu-product.js.coffee +++ b/app/assets/javascripts/supplier/app/components/menu-product.js.coffee @@ -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: -> diff --git a/app/assets/javascripts/supplier/app/models/product.js.coffee b/app/assets/javascripts/supplier/app/models/product.js.coffee index 68714560..5b6f3f32 100644 --- a/app/assets/javascripts/supplier/app/models/product.js.coffee +++ b/app/assets/javascripts/supplier/app/models/product.js.coffee @@ -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)?$/) diff --git a/app/assets/javascripts/supplier/app/models/product_variant.js.coffee b/app/assets/javascripts/supplier/app/models/product_variant.js.coffee new file mode 100644 index 00000000..c3fbcc2e --- /dev/null +++ b/app/assets/javascripts/supplier/app/models/product_variant.js.coffee @@ -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 diff --git a/app/assets/javascripts/supplier/app/templates/components/menu-product.emblem b/app/assets/javascripts/supplier/app/templates/components/menu-product.emblem index 72a034bf..38386557 100644 --- a/app/assets/javascripts/supplier/app/templates/components/menu-product.emblem +++ b/app/assets/javascripts/supplier/app/templates/components/menu-product.emblem @@ -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 diff --git a/app/controllers/suppliers/product_variants_controller.rb b/app/controllers/suppliers/product_variants_controller.rb new file mode 100644 index 00000000..625e8607 --- /dev/null +++ b/app/controllers/suppliers/product_variants_controller.rb @@ -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 diff --git a/app/controllers/suppliers/suppliers_controller.rb b/app/controllers/suppliers/suppliers_controller.rb index a2d9221a..797e969a 100644 --- a/app/controllers/suppliers/suppliers_controller.rb +++ b/app/controllers/suppliers/suppliers_controller.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb index 94485a08..3a051a00 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/app/models/product_variant.rb b/app/models/product_variant.rb new file mode 100644 index 00000000..b76a1a29 --- /dev/null +++ b/app/models/product_variant.rb @@ -0,0 +1,7 @@ +class ProductVariant + include SimplyStored::Couch + include ActiveModel::SerializerSupport + property :name + property :position, type: Fixnum, default: 0 + belongs_to :product +end diff --git a/app/serializers/product_serializer.rb b/app/serializers/product_serializer.rb index c294f6a5..fae7b8d8 100644 --- a/app/serializers/product_serializer.rb +++ b/app/serializers/product_serializer.rb @@ -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)} diff --git a/app/serializers/product_variant_serializer.rb b/app/serializers/product_variant_serializer.rb new file mode 100644 index 00000000..a6c72831 --- /dev/null +++ b/app/serializers/product_variant_serializer.rb @@ -0,0 +1,4 @@ +class ProductVariantSerializer < Qwaiter::Serializer + root 'product_variant' + attributes :name +end diff --git a/config/routes.rb b/config/routes.rb index 045001df..4824d9ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -139,6 +139,7 @@ Qwaiter::Application.routes.draw do get :preview_products end end + resources :product_variants resources :lists do collection do get :active diff --git a/fig.yml b/fig.yml index cfaa69df..4ce9b969 100644 --- a/fig.yml +++ b/fig.yml @@ -1,7 +1,8 @@ db: image: bterkuile/couchdb volumes: - - db:/usr/local/var/lib/couchdb + - db/data:/usr/local/var/lib/couchdb + - db/config:/usr/local/etc/couchdb expose: - 5984 net: host