From e7793f08846b2e35fbb0c58b64ceab65714ea830 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sat, 14 Mar 2015 06:33:49 +0100 Subject: [PATCH] added suppliers, weight to item --- .../app/controllers/suppliers_controller.rb | 4 + back-office/app/helpers/suppliers_helper.rb | 2 + back-office/app/models/cart.rb | 4 + back-office/app/models/item.rb | 5 +- back-office/app/models/item_in_cart.rb | 4 + back-office/app/models/supplier.rb | 6 + back-office/app/models/user.rb | 10 ++ back-office/config/routes.rb | 1 + .../20150314033113_create_suppliers.rb | 16 ++ .../20150314033704_add_supplier_to_item.rb | 5 + back-office/db/schema.rb | 149 ++++++++++++++++++ .../controllers/suppliers_controller_test.rb | 49 ++++++ back-office/test/fixtures/suppliers.yml | 21 +++ back-office/test/models/supplier_test.rb | 7 + .../20150314033743_add_weigth_to_item.rb | 5 + front-api/db/schema.rb | 17 +- front-api/models/user.rb | 1 + 17 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 back-office/app/controllers/suppliers_controller.rb create mode 100644 back-office/app/helpers/suppliers_helper.rb create mode 100644 back-office/app/models/supplier.rb create mode 100644 back-office/app/models/user.rb create mode 100644 back-office/db/migrate/20150314033113_create_suppliers.rb create mode 100644 back-office/db/migrate/20150314033704_add_supplier_to_item.rb create mode 100644 back-office/db/schema.rb create mode 100644 back-office/test/controllers/suppliers_controller_test.rb create mode 100644 back-office/test/fixtures/suppliers.yml create mode 100644 back-office/test/models/supplier_test.rb create mode 100644 front-api/db/migrate/20150314033743_add_weigth_to_item.rb diff --git a/back-office/app/controllers/suppliers_controller.rb b/back-office/app/controllers/suppliers_controller.rb new file mode 100644 index 0000000..0ad3371 --- /dev/null +++ b/back-office/app/controllers/suppliers_controller.rb @@ -0,0 +1,4 @@ +class SuppliersController < ApplicationController + active_scaffold :"supplier" do |conf| + end +end diff --git a/back-office/app/helpers/suppliers_helper.rb b/back-office/app/helpers/suppliers_helper.rb new file mode 100644 index 0000000..0120e8c --- /dev/null +++ b/back-office/app/helpers/suppliers_helper.rb @@ -0,0 +1,2 @@ +module SuppliersHelper +end \ No newline at end of file diff --git a/back-office/app/models/cart.rb b/back-office/app/models/cart.rb index 3f5c372..a78e067 100644 --- a/back-office/app/models/cart.rb +++ b/back-office/app/models/cart.rb @@ -1,2 +1,6 @@ class Cart < ActiveRecord::Base + + has_many :item_in_carts + belongs_to :user + end diff --git a/back-office/app/models/item.rb b/back-office/app/models/item.rb index ac61ae3..28a4463 100644 --- a/back-office/app/models/item.rb +++ b/back-office/app/models/item.rb @@ -2,6 +2,9 @@ class Item < ActiveRecord::Base belongs_to :unit has_many :multi_media_descriptions belongs_to :sub_category + belongs_to :supplier + + validates_presence_of :name, :description, :list_price, :current_input_price, :tags, :unit_id, :code, :sub_category_id, :weight, :supplier_id + - validates_presence_of :name, :description, :list_price, :current_input_price, :tags, :unit_id, :code, :sub_category_id end diff --git a/back-office/app/models/item_in_cart.rb b/back-office/app/models/item_in_cart.rb index faa4611..c18c2ae 100644 --- a/back-office/app/models/item_in_cart.rb +++ b/back-office/app/models/item_in_cart.rb @@ -5,4 +5,8 @@ class ItemInCart < ActiveRecord::Base validates_uniqueness_of :item_id, scope: :cart_id validates :item_id, presence: true validates :cart_id, presence: true + + def to_s + item.name + " " + count.to_s + " " + price.to_s + end end diff --git a/back-office/app/models/supplier.rb b/back-office/app/models/supplier.rb new file mode 100644 index 0000000..707c177 --- /dev/null +++ b/back-office/app/models/supplier.rb @@ -0,0 +1,6 @@ +class Supplier < ActiveRecord::Base + has_many :items + + validates_uniqueness_of :name + validates_presence_of :name +end diff --git a/back-office/app/models/user.rb b/back-office/app/models/user.rb new file mode 100644 index 0000000..92779e5 --- /dev/null +++ b/back-office/app/models/user.rb @@ -0,0 +1,10 @@ +class User < ActiveRecord::Base + has_many :carts + + validates_presence_of :first_name, :last_name, :password, :email, :password_confirmation + + validates :email, :uniqueness => {:case_sensitive => false, :message => "Email already exists!"}, + format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: "invalid email" } + + validates :password, confirmation: true, length: { in:6..20, too_short: 'password needs to be at least 6 characters long' } +end diff --git a/back-office/config/routes.rb b/back-office/config/routes.rb index 84a1a15..3b7fe11 100644 --- a/back-office/config/routes.rb +++ b/back-office/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do + resources :suppliers do as_routes end resources :places do as_routes end resources :delivery_destinations do as_routes end mount RailsAdmin::Engine => '/admin', as: 'rails_admin' diff --git a/back-office/db/migrate/20150314033113_create_suppliers.rb b/back-office/db/migrate/20150314033113_create_suppliers.rb new file mode 100644 index 0000000..9fe9aee --- /dev/null +++ b/back-office/db/migrate/20150314033113_create_suppliers.rb @@ -0,0 +1,16 @@ +class CreateSuppliers < ActiveRecord::Migration + def change + create_table :suppliers do |t| + t.string :name + t.string :address + t.string :postal_code + t.string :town + t.string :phone + t.string :contact_person + t.string :email + t.text :note + + t.timestamps null: false + end + end +end diff --git a/back-office/db/migrate/20150314033704_add_supplier_to_item.rb b/back-office/db/migrate/20150314033704_add_supplier_to_item.rb new file mode 100644 index 0000000..5de8b07 --- /dev/null +++ b/back-office/db/migrate/20150314033704_add_supplier_to_item.rb @@ -0,0 +1,5 @@ +class AddSupplierToItem < ActiveRecord::Migration + def change + add_column :items, :supplier_id, :integer + end +end diff --git a/back-office/db/schema.rb b/back-office/db/schema.rb new file mode 100644 index 0000000..e29cedb --- /dev/null +++ b/back-office/db/schema.rb @@ -0,0 +1,149 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20150314033704) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "carts", force: :cascade do |t| + t.integer "user_id" + t.boolean "ordered", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "anonymous_id_string" + t.integer "delivery_destination_id" + end + + create_table "categories", force: :cascade do |t| + t.string "name" + t.integer "section_id" + t.string "image_url" + end + + create_table "delivery_destinations", force: :cascade do |t| + t.string "name" + t.string "address" + t.string "place" + t.string "postal_code" + t.string "phone" + t.string "email" + t.text "note" + t.boolean "email_verified" + t.boolean "phone_verified" + t.string "phone_verification_code" + t.string "email_verification_code" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "user_id" + t.string "anonymous_id_string" + end + + create_table "filter_criteria", force: :cascade do |t| + t.string "title" + t.string "field_name" + t.integer "type" + t.string "owner_type" + t.integer "owner_id" + end + + create_table "filter_criteria_values", force: :cascade do |t| + t.string "filter_text" + t.string "filter_value" + t.integer "filter_criteria_id" + end + + create_table "item_in_carts", force: :cascade do |t| + t.integer "cart_id" + t.integer "item_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "count" + t.decimal "price" + end + + create_table "items", force: :cascade do |t| + t.string "name" + t.string "code", limit: 10 + t.decimal "current_input_price", precision: 5, scale: 2 + t.decimal "list_price", precision: 5, scale: 2 + t.integer "unit_id" + t.decimal "units_in_pack", precision: 5, scale: 3 + t.text "description" + t.integer "sub_category_id" + t.integer "stock" + t.boolean "on_display" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "tags" + t.json "traits" + t.integer "supplier_id" + end + + create_table "media_types", force: :cascade do |t| + t.string "name" + end + + create_table "multi_media_descriptions", force: :cascade do |t| + t.string "url" + t.integer "item_id" + t.integer "media_type_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "places", force: :cascade do |t| + t.string "postal_code" + t.decimal "delivery_price" + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "sections", force: :cascade do |t| + t.string "name" + end + + create_table "sub_categories", force: :cascade do |t| + t.string "name" + t.integer "category_id" + end + + create_table "suppliers", force: :cascade do |t| + t.string "name" + t.string "address" + t.string "postal_code" + t.string "town" + t.string "phone" + t.string "contact_person" + t.string "email" + t.text "note" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "units", force: :cascade do |t| + t.string "name" + t.string "short_name", limit: 4 + t.string "description_suffix" + t.string "number_of_pieces_suffix" + end + + create_table "users", force: :cascade do |t| + t.string "first_name" + t.string "last_name" + t.string "email" + t.string "password_digest" + end + +end diff --git a/back-office/test/controllers/suppliers_controller_test.rb b/back-office/test/controllers/suppliers_controller_test.rb new file mode 100644 index 0000000..66a6c52 --- /dev/null +++ b/back-office/test/controllers/suppliers_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class SuppliersControllerTest < ActionController::TestCase + setup do + @supplier = suppliers(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:suppliers) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create supplier" do + assert_difference('Supplier.count') do + post :create, supplier: { address: @supplier.address, contact_person: @supplier.contact_person, email: @supplier.email, name: @supplier.name, note: @supplier.note, phone: @supplier.phone, postal_code: @supplier.postal_code, town: @supplier.town } + end + + assert_redirected_to supplier_path(assigns(:supplier)) + end + + test "should show supplier" do + get :show, id: @supplier + assert_response :success + end + + test "should get edit" do + get :edit, id: @supplier + assert_response :success + end + + test "should update supplier" do + patch :update, id: @supplier, supplier: { address: @supplier.address, contact_person: @supplier.contact_person, email: @supplier.email, name: @supplier.name, note: @supplier.note, phone: @supplier.phone, postal_code: @supplier.postal_code, town: @supplier.town } + assert_redirected_to supplier_path(assigns(:supplier)) + end + + test "should destroy supplier" do + assert_difference('Supplier.count', -1) do + delete :destroy, id: @supplier + end + + assert_redirected_to suppliers_path + end +end diff --git a/back-office/test/fixtures/suppliers.yml b/back-office/test/fixtures/suppliers.yml new file mode 100644 index 0000000..59c8a01 --- /dev/null +++ b/back-office/test/fixtures/suppliers.yml @@ -0,0 +1,21 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + address: MyString + postal_code: MyString + town: MyString + phone: MyString + contact_person: MyString + email: MyString + note: MyText + +two: + name: MyString + address: MyString + postal_code: MyString + town: MyString + phone: MyString + contact_person: MyString + email: MyString + note: MyText diff --git a/back-office/test/models/supplier_test.rb b/back-office/test/models/supplier_test.rb new file mode 100644 index 0000000..5ec9e21 --- /dev/null +++ b/back-office/test/models/supplier_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SupplierTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/front-api/db/migrate/20150314033743_add_weigth_to_item.rb b/front-api/db/migrate/20150314033743_add_weigth_to_item.rb new file mode 100644 index 0000000..e26e1bf --- /dev/null +++ b/front-api/db/migrate/20150314033743_add_weigth_to_item.rb @@ -0,0 +1,5 @@ +class AddWeigthToItem < ActiveRecord::Migration + def change + add_column :items, :weight, :decimal, precision: 5, scale: 3 + end +end diff --git a/front-api/db/schema.rb b/front-api/db/schema.rb index 58d5973..66c0255 100644 --- a/front-api/db/schema.rb +++ b/front-api/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150313065130) do +ActiveRecord::Schema.define(version: 20150314033743) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -87,6 +87,8 @@ ActiveRecord::Schema.define(version: 20150313065130) do t.datetime "updated_at", null: false t.string "tags" t.json "traits" + t.integer "supplier_id" + t.decimal "weight", precision: 5, scale: 3 end create_table "media_types", force: :cascade do |t| @@ -118,6 +120,19 @@ ActiveRecord::Schema.define(version: 20150313065130) do t.integer "category_id" end + create_table "suppliers", force: :cascade do |t| + t.string "name" + t.string "address" + t.string "postal_code" + t.string "town" + t.string "phone" + t.string "contact_person" + t.string "email" + t.text "note" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "units", force: :cascade do |t| t.string "name" t.string "short_name", limit: 4 diff --git a/front-api/models/user.rb b/front-api/models/user.rb index b49bc16..41ba46d 100644 --- a/front-api/models/user.rb +++ b/front-api/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + has_many :carts has_secure_password validates_presence_of :first_name, :last_name, :password, :email, :password_confirmation