From 632143b09dc0594a0bc5ef83040eb004c4a24155 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 12 Feb 2018 11:49:11 -0500 Subject: [PATCH 01/12] Change type category on models --- ...0180209171717_change_type_category_asset_description.rb | 7 +++++++ ...171835_change_type_category_organization_description.rb | 7 +++++++ ...20180209172057_change_type_category_supplier_address.rb | 7 +++++++ ...180209172202_change_type_category_supply_description.rb | 7 +++++++ .../20180209172241_change_type_category_supply_note.rb | 7 +++++++ .../20180209172347_change_type_category_user_address.rb | 7 +++++++ 6 files changed, 42 insertions(+) create mode 100644 db/migrate/20180209171717_change_type_category_asset_description.rb create mode 100644 db/migrate/20180209171835_change_type_category_organization_description.rb create mode 100644 db/migrate/20180209172057_change_type_category_supplier_address.rb create mode 100644 db/migrate/20180209172202_change_type_category_supply_description.rb create mode 100644 db/migrate/20180209172241_change_type_category_supply_note.rb create mode 100644 db/migrate/20180209172347_change_type_category_user_address.rb diff --git a/db/migrate/20180209171717_change_type_category_asset_description.rb b/db/migrate/20180209171717_change_type_category_asset_description.rb new file mode 100644 index 0000000..a7882fd --- /dev/null +++ b/db/migrate/20180209171717_change_type_category_asset_description.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategoryAssetDescription < ActiveRecord::Migration[5.1] + def up + change_table :assets do |t| + t.change :description, :text + end + end +end diff --git a/db/migrate/20180209171835_change_type_category_organization_description.rb b/db/migrate/20180209171835_change_type_category_organization_description.rb new file mode 100644 index 0000000..189b8f3 --- /dev/null +++ b/db/migrate/20180209171835_change_type_category_organization_description.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategoryOrganizationDescription < ActiveRecord::Migration[5.1] + def up + change_table :organizations do |t| + t.change :description, :text + end + end +end diff --git a/db/migrate/20180209172057_change_type_category_supplier_address.rb b/db/migrate/20180209172057_change_type_category_supplier_address.rb new file mode 100644 index 0000000..66ceb2e --- /dev/null +++ b/db/migrate/20180209172057_change_type_category_supplier_address.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategorySupplierAddress < ActiveRecord::Migration[5.1] + def up + change_table :suppliers do |t| + t.change :address, :text + end + end +end diff --git a/db/migrate/20180209172202_change_type_category_supply_description.rb b/db/migrate/20180209172202_change_type_category_supply_description.rb new file mode 100644 index 0000000..4ff62cd --- /dev/null +++ b/db/migrate/20180209172202_change_type_category_supply_description.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategorySupplyDescription < ActiveRecord::Migration[5.1] + def up + change_table :supplies do |t| + t.change :description, :text + end + end +end diff --git a/db/migrate/20180209172241_change_type_category_supply_note.rb b/db/migrate/20180209172241_change_type_category_supply_note.rb new file mode 100644 index 0000000..8c74532 --- /dev/null +++ b/db/migrate/20180209172241_change_type_category_supply_note.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategorySupplyNote < ActiveRecord::Migration[5.1] + def up + change_table :supplies do |t| + t.change :note, :text + end + end +end diff --git a/db/migrate/20180209172347_change_type_category_user_address.rb b/db/migrate/20180209172347_change_type_category_user_address.rb new file mode 100644 index 0000000..39b0363 --- /dev/null +++ b/db/migrate/20180209172347_change_type_category_user_address.rb @@ -0,0 +1,7 @@ +class ChangeTypeCategoryUserAddress < ActiveRecord::Migration[5.1] + def up + change_table :users do |t| + t.change :address, :text + end + end +end From 04d5117204f49e0bd168c15851a7da91967f117a Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 12 Feb 2018 11:51:29 -0500 Subject: [PATCH 02/12] Add column in space model --- db/migrate/20180209174923_add_full_name_to_space.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20180209174923_add_full_name_to_space.rb diff --git a/db/migrate/20180209174923_add_full_name_to_space.rb b/db/migrate/20180209174923_add_full_name_to_space.rb new file mode 100644 index 0000000..74f58eb --- /dev/null +++ b/db/migrate/20180209174923_add_full_name_to_space.rb @@ -0,0 +1,5 @@ +class AddFullNameToSpace < ActiveRecord::Migration[5.1] + def change + add_column :spaces, :full_name, :string + end +end From 6a24cebd39738fbb347ce84fabfe1e9f48fd9dd1 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 12 Feb 2018 11:55:19 -0500 Subject: [PATCH 03/12] Create ContractSpace model --- app/models/contract.rb | 6 ++++- app/models/contract_space.rb | 5 ++++ app/models/space.rb | 23 ++++++++++++++++++- ...212043454_remove_references_on_contract.rb | 5 ++++ .../20180212160438_create_contract_spaces.rb | 10 ++++++++ spec/factories/contract_spaces.rb | 4 ++++ spec/models/contract_space_spec.rb | 10 ++++++++ spec/models/contract_spec.rb | 3 +-- 8 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 app/models/contract_space.rb create mode 100644 db/migrate/20180212043454_remove_references_on_contract.rb create mode 100644 db/migrate/20180212160438_create_contract_spaces.rb create mode 100644 spec/factories/contract_spaces.rb create mode 100644 spec/models/contract_space_spec.rb diff --git a/app/models/contract.rb b/app/models/contract.rb index e8eca78..7aeaf6d 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -2,6 +2,10 @@ class Contract < ApplicationRecord validates :start_date, presence: true validates :finish_date, presence: true - belongs_to :space + has_many :contract_spaces + has_many :payments belongs_to :user + def name + "#{start_date} | #{finish_date}" + end end diff --git a/app/models/contract_space.rb b/app/models/contract_space.rb new file mode 100644 index 0000000..4020d89 --- /dev/null +++ b/app/models/contract_space.rb @@ -0,0 +1,5 @@ +# ContractSpace +class ContractSpace < ApplicationRecord + belongs_to :contract + belongs_to :space +end diff --git a/app/models/space.rb b/app/models/space.rb index f10b9c9..822f84c 100644 --- a/app/models/space.rb +++ b/app/models/space.rb @@ -4,7 +4,28 @@ class Space < ApplicationRecord validates :name, presence: true belongs_to :parent, inverse_of: :children, foreign_key: :space_id, class_name: 'Space', optional: true has_many :children, inverse_of: :parent, foreign_key: :space_id, class_name: 'Space' - has_many :contracts has_many :assets + has_many :contract_spaces has_one :warehouse + + before_save :generate_full_name + + protected + + def generate_full_name + new_name = name + next_parent = parent + while next_parent + new_name = "#{parent.name} | #{new_name}" + next_parent = next_parent.parent + end + self.full_name = parent ? new_name : name + end end + +# def generate_full_name +# return self.full_name = name unless parent +# while next_parent = parent +# full_name = "#{next_parent.name} | #{full_name}" +# end +# end diff --git a/db/migrate/20180212043454_remove_references_on_contract.rb b/db/migrate/20180212043454_remove_references_on_contract.rb new file mode 100644 index 0000000..f8304aa --- /dev/null +++ b/db/migrate/20180212043454_remove_references_on_contract.rb @@ -0,0 +1,5 @@ +class RemoveReferencesOnContract < ActiveRecord::Migration[5.1] + def change + remove_reference(:contracts, :space, index: true) + end +end diff --git a/db/migrate/20180212160438_create_contract_spaces.rb b/db/migrate/20180212160438_create_contract_spaces.rb new file mode 100644 index 0000000..7d8e858 --- /dev/null +++ b/db/migrate/20180212160438_create_contract_spaces.rb @@ -0,0 +1,10 @@ +class CreateContractSpaces < ActiveRecord::Migration[5.1] + def change + create_table :contract_spaces do |t| + t.references :contract, foreign_key: true + t.references :space, foreign_key: true + + t.timestamps + end + end +end diff --git a/spec/factories/contract_spaces.rb b/spec/factories/contract_spaces.rb new file mode 100644 index 0000000..03e14b3 --- /dev/null +++ b/spec/factories/contract_spaces.rb @@ -0,0 +1,4 @@ +FactoryBot.define do + factory :contract_space do + end +end diff --git a/spec/models/contract_space_spec.rb b/spec/models/contract_space_spec.rb new file mode 100644 index 0000000..47a6977 --- /dev/null +++ b/spec/models/contract_space_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe ContractSpace, type: :model do + it 'is valid ' do + contract = build(:contract) + space = build(:space) + contract_space = build(:contract_space, contract: contract, space: space) + expect(contract_space).to be_valid + end +end diff --git a/spec/models/contract_spec.rb b/spec/models/contract_spec.rb index 80146f7..4f42f3c 100644 --- a/spec/models/contract_spec.rb +++ b/spec/models/contract_spec.rb @@ -3,8 +3,7 @@ RSpec.describe Contract, type: :model do it 'is valid with a start date and finish date' do user = build(:user) - space = build(:space) - contract = build(:contract, user: user, space: space) + contract = build(:contract, user: user) expect(contract).to be_valid end From d01a5c4e22221492bf3a3a962769b8d06c0c70c9 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 12 Feb 2018 11:57:04 -0500 Subject: [PATCH 04/12] Create Payment model --- app/models/payment.rb | 8 ++++ db/migrate/20180212161659_create_payments.rb | 15 +++++++ db/schema.rb | 43 +++++++++++++++----- spec/factories/payments.rb | 10 +++++ spec/models/payment_spec.rb | 14 +++++++ 5 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 app/models/payment.rb create mode 100644 db/migrate/20180212161659_create_payments.rb create mode 100644 spec/factories/payments.rb create mode 100644 spec/models/payment_spec.rb diff --git a/app/models/payment.rb b/app/models/payment.rb new file mode 100644 index 0000000..607953d --- /dev/null +++ b/app/models/payment.rb @@ -0,0 +1,8 @@ +# Payment +class Payment < ApplicationRecord + validates :payment, presence: true + validates :payment_date, presence: true + validates :quantity, presence: true + validates :status, presence: true + belongs_to :contract +end diff --git a/db/migrate/20180212161659_create_payments.rb b/db/migrate/20180212161659_create_payments.rb new file mode 100644 index 0000000..92e84da --- /dev/null +++ b/db/migrate/20180212161659_create_payments.rb @@ -0,0 +1,15 @@ +class CreatePayments < ActiveRecord::Migration[5.1] + def change + create_table :payments do |t| + t.integer :payment + t.date :payment_date + t.date :warning_date + t.integer :quantity + t.string :status + t.text :note + t.references :contract, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ccd36aa..5ece05c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180208203810) do +ActiveRecord::Schema.define(version: 20180212161659) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -32,7 +32,7 @@ t.string "category" t.string "sub_category" t.integer "status" - t.string "description" + t.text "description" t.integer "interval" t.bigint "space_id" t.datetime "created_at", null: false @@ -40,14 +40,21 @@ t.index ["space_id"], name: "index_assets_on_space_id" end + create_table "contract_spaces", force: :cascade do |t| + t.bigint "contract_id" + t.bigint "space_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["contract_id"], name: "index_contract_spaces_on_contract_id" + t.index ["space_id"], name: "index_contract_spaces_on_space_id" + end + create_table "contracts", force: :cascade do |t| t.date "start_date" t.date "finish_date" - t.bigint "space_id" t.bigint "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["space_id"], name: "index_contracts_on_space_id" t.index ["user_id"], name: "index_contracts_on_user_id" end @@ -64,13 +71,26 @@ create_table "organizations", force: :cascade do |t| t.string "name" - t.string "description" + t.text "description" t.string "organization_identifier" t.string "website" t.datetime "created_at", null: false t.datetime "updated_at", null: false end + create_table "payments", force: :cascade do |t| + t.integer "payment" + t.date "payment_date" + t.date "warning_date" + t.integer "quantity" + t.string "status" + t.text "note" + t.bigint "contract_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["contract_id"], name: "index_payments_on_contract_id" + end + create_table "spaces", force: :cascade do |t| t.string "space_identifier" t.string "name" @@ -80,6 +100,7 @@ t.bigint "space_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "full_name" t.index ["space_id"], name: "index_spaces_on_space_id" end @@ -90,7 +111,7 @@ t.integer "ranking" t.string "services" t.string "category" - t.string "address" + t.text "address" t.string "bank" t.string "current_account" t.string "email" @@ -102,7 +123,7 @@ create_table "supplies", force: :cascade do |t| t.string "name" - t.string "description" + t.text "description" t.string "brand" t.string "color" t.string "presentation" @@ -112,7 +133,7 @@ t.integer "stock" t.integer "stock_minimun" t.integer "stock_reposition" - t.string "note" + t.text "note" t.datetime "created_at", null: false t.datetime "updated_at", null: false end @@ -127,7 +148,7 @@ t.date "birthday" t.string "role" t.integer "user_identifier" - t.string "address" + t.text "address" t.text "bio" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -147,10 +168,12 @@ add_foreign_key "asset_suppliers", "assets" add_foreign_key "asset_suppliers", "suppliers" add_foreign_key "assets", "spaces" - add_foreign_key "contracts", "spaces" + add_foreign_key "contract_spaces", "contracts" + add_foreign_key "contract_spaces", "spaces" add_foreign_key "contracts", "users" add_foreign_key "maintenances", "assets" add_foreign_key "maintenances", "suppliers" + add_foreign_key "payments", "contracts" add_foreign_key "spaces", "spaces" add_foreign_key "users", "organizations" add_foreign_key "warehouses", "spaces" diff --git a/spec/factories/payments.rb b/spec/factories/payments.rb new file mode 100644 index 0000000..e5356a6 --- /dev/null +++ b/spec/factories/payments.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :payment do + payment FFaker::PhoneNumberAU.home_work_phone_prefix + payment_date FFaker::IdentificationESCO.expedition_date + warning_date FFaker::IdentificationESCO.expedition_date + quantity FFaker::PhoneNumberAU.home_work_phone_prefix + status 'Completo' + note FFaker::Book.description + end +end diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb new file mode 100644 index 0000000..bec837c --- /dev/null +++ b/spec/models/payment_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe Payment, type: :model do + it 'is valid with a payment, payment date, quantity and status' do + contract = build(:contract) + payment = build(:payment, contract: contract) + expect(payment).to be_valid + end + + it { should validate_presence_of(:payment) } + it { should validate_presence_of(:payment_date) } + it { should validate_presence_of(:quantity) } + it { should validate_presence_of(:status) } +end From a86985fde36e506a44f5a80cc50f86ee64246089 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 12 Feb 2018 11:59:01 -0500 Subject: [PATCH 05/12] Remove id column from rails admin --- config/initializers/rails_admin.rb | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 570a244..245fc1a 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -38,4 +38,88 @@ # history_index # history_show end + config.model 'User' do + list do + configure :id do + hide + end + end + end + config.model 'ContractSpace' do + list do + configure :id do + hide + end + end + end + config.model 'Payment' do + list do + configure :id do + hide + end + end + end + config.model 'Organization' do + list do + configure :id do + hide + end + end + end + config.model 'Space' do + list do + configure :id do + hide + end + end + end + config.model 'Contract' do + list do + configure :id do + hide + end + end + end + config.model 'Asset' do + list do + configure :id do + hide + end + end + end + config.model 'Supplier' do + list do + configure :id do + hide + end + end + end + config.model 'Maintenance' do + list do + configure :id do + hide + end + end + end + config.model 'AssetSupplier' do + list do + configure :id do + hide + end + end + end + config.model 'Supply' do + list do + configure :id do + hide + end + end + end + config.model 'Warehouse' do + list do + configure :id do + hide + end + end + end end From 51e0802881e62e15f7648a1a751df61ddbcde63c Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Tue, 13 Feb 2018 16:39:33 -0500 Subject: [PATCH 06/12] Hide id column of the models --- config/initializers/rails_admin.rb | 109 +++++++---------------------- 1 file changed, 25 insertions(+), 84 deletions(-) diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 245fc1a..9fc40b3 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -38,88 +38,29 @@ # history_index # history_show end - config.model 'User' do - list do - configure :id do - hide - end - end - end - config.model 'ContractSpace' do - list do - configure :id do - hide - end - end - end - config.model 'Payment' do - list do - configure :id do - hide - end - end - end - config.model 'Organization' do - list do - configure :id do - hide - end - end - end - config.model 'Space' do - list do - configure :id do - hide - end - end - end - config.model 'Contract' do - list do - configure :id do - hide - end - end - end - config.model 'Asset' do - list do - configure :id do - hide - end - end - end - config.model 'Supplier' do - list do - configure :id do - hide - end - end - end - config.model 'Maintenance' do - list do - configure :id do - hide - end - end - end - config.model 'AssetSupplier' do - list do - configure :id do - hide - end - end - end - config.model 'Supply' do - list do - configure :id do - hide - end - end - end - config.model 'Warehouse' do - list do - configure :id do - hide - end - end - end + + config.model (User) { list { configure :id { hide } } } + + config.model (ContractSpace) { list { configure :id { hide } } } + + config.model (Payment) { list { configure :id { hide } } } + + config.model (Organization) { list { configure :id { hide } } } + + config.model (Space) { list { configure :id { hide } } } + + config.model (Contract) { list { configure :id { hide } } } + + config.model (Asset) { list { configure :id { hide } } } + + config.model (Supplier) { list { configure :id { hide } } } + + config.model (Maintenance) { list { configure :id { hide } } } + + config.model (AssetSupplier) { list { configure :id { hide } } } + + config.model (Supply) { list { configure :id { hide } } } + + config.model (Warehouse) { list { configure :id { hide } } } + end From 5afe481ac781e4418c638a020bbfe11753221ec8 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Tue, 13 Feb 2018 16:40:56 -0500 Subject: [PATCH 07/12] Refactor on spec --- spec/models/contract_spec.rb | 1 + spec/models/space_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/models/contract_spec.rb b/spec/models/contract_spec.rb index 4f42f3c..d888a2a 100644 --- a/spec/models/contract_spec.rb +++ b/spec/models/contract_spec.rb @@ -5,6 +5,7 @@ user = build(:user) contract = build(:contract, user: user) expect(contract).to be_valid + expect(contract.name).to eq("#{contract.start_date} | #{contract.finish_date}") end it { should validate_presence_of(:start_date) } diff --git a/spec/models/space_spec.rb b/spec/models/space_spec.rb index 2bab79b..b91bbf7 100644 --- a/spec/models/space_spec.rb +++ b/spec/models/space_spec.rb @@ -1,10 +1,10 @@ require 'rails_helper' RSpec.describe Space, type: :model do - it 'is valid with a code, name and capacity' do + it { space = build(:space) expect(space).to be_valid - end + } it { should validate_presence_of(:space_identifier) } it { should validate_presence_of(:name) } From a90d219eb87228579ab1902138a44b1cbe1965b7 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Wed, 14 Feb 2018 11:23:01 -0500 Subject: [PATCH 08/12] wip --- config/initializers/rails_admin.rb | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 9fc40b3..570a244 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -38,29 +38,4 @@ # history_index # history_show end - - config.model (User) { list { configure :id { hide } } } - - config.model (ContractSpace) { list { configure :id { hide } } } - - config.model (Payment) { list { configure :id { hide } } } - - config.model (Organization) { list { configure :id { hide } } } - - config.model (Space) { list { configure :id { hide } } } - - config.model (Contract) { list { configure :id { hide } } } - - config.model (Asset) { list { configure :id { hide } } } - - config.model (Supplier) { list { configure :id { hide } } } - - config.model (Maintenance) { list { configure :id { hide } } } - - config.model (AssetSupplier) { list { configure :id { hide } } } - - config.model (Supply) { list { configure :id { hide } } } - - config.model (Warehouse) { list { configure :id { hide } } } - end From 7061d14ea27e84424f74fa797e38c442d6afd725 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 19 Feb 2018 18:29:04 -0500 Subject: [PATCH 09/12] Update payment model --- app/models/payment.rb | 4 ++-- db/migrate/20180214175011_changes_on_payment.rb | 8 ++++++++ db/schema.rb | 11 +++-------- spec/factories/payments.rb | 5 ++--- spec/models/payment_spec.rb | 11 +++-------- 5 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20180214175011_changes_on_payment.rb diff --git a/app/models/payment.rb b/app/models/payment.rb index 607953d..ce2509b 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -1,8 +1,8 @@ # Payment class Payment < ApplicationRecord - validates :payment, presence: true + validates :due_date, presence: true validates :payment_date, presence: true - validates :quantity, presence: true + validates :amount, presence: true validates :status, presence: true belongs_to :contract end diff --git a/db/migrate/20180214175011_changes_on_payment.rb b/db/migrate/20180214175011_changes_on_payment.rb new file mode 100644 index 0000000..b64ee0e --- /dev/null +++ b/db/migrate/20180214175011_changes_on_payment.rb @@ -0,0 +1,8 @@ +class ChangesOnPayment < ActiveRecord::Migration[5.1] + def change + rename_column :payments, :warning_date, :due_date + rename_column :payments, :quantity, :amount + remove_column :payments, :payment, :integer + change_column :payments, :amount, :float + end +end diff --git a/db/schema.rb b/db/schema.rb index 5ece05c..64a74d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180212161659) do +ActiveRecord::Schema.define(version: 20180215181142) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -79,10 +79,9 @@ end create_table "payments", force: :cascade do |t| - t.integer "payment" t.date "payment_date" - t.date "warning_date" - t.integer "quantity" + t.date "due_date" + t.float "amount" t.string "status" t.text "note" t.bigint "contract_id" @@ -165,11 +164,7 @@ t.index ["supply_id"], name: "index_warehouses_on_supply_id" end - add_foreign_key "asset_suppliers", "assets" - add_foreign_key "asset_suppliers", "suppliers" add_foreign_key "assets", "spaces" - add_foreign_key "contract_spaces", "contracts" - add_foreign_key "contract_spaces", "spaces" add_foreign_key "contracts", "users" add_foreign_key "maintenances", "assets" add_foreign_key "maintenances", "suppliers" diff --git a/spec/factories/payments.rb b/spec/factories/payments.rb index e5356a6..2955370 100644 --- a/spec/factories/payments.rb +++ b/spec/factories/payments.rb @@ -1,9 +1,8 @@ FactoryBot.define do factory :payment do - payment FFaker::PhoneNumberAU.home_work_phone_prefix payment_date FFaker::IdentificationESCO.expedition_date - warning_date FFaker::IdentificationESCO.expedition_date - quantity FFaker::PhoneNumberAU.home_work_phone_prefix + due_date FFaker::IdentificationESCO.expedition_date + amount FFaker::PhoneNumberAU.home_work_phone_prefix status 'Completo' note FFaker::Book.description end diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index bec837c..7d3268b 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -1,14 +1,9 @@ require 'rails_helper' RSpec.describe Payment, type: :model do - it 'is valid with a payment, payment date, quantity and status' do - contract = build(:contract) - payment = build(:payment, contract: contract) - expect(payment).to be_valid - end - - it { should validate_presence_of(:payment) } + it { expect(build(:payment, contract: build(:contract))).to be_valid } + it { should validate_presence_of(:due_date) } it { should validate_presence_of(:payment_date) } - it { should validate_presence_of(:quantity) } + it { should validate_presence_of(:amount) } it { should validate_presence_of(:status) } end From 846db7a4dcc851d1471eae9aa01d28fdeefa67f1 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 19 Feb 2018 18:51:46 -0500 Subject: [PATCH 10/12] Create relation contract and spaces model --- app/models/contract.rb | 3 ++- app/models/space.rb | 24 +++++++------------ ...212043454_remove_references_on_contract.rb | 2 +- ...33_remove_references_on_contract_spaces.rb | 6 +++++ .../20180214194838_drop_contract_space.rb | 5 ++++ db/migrate/20180214201411_contracts_spaces.rb | 6 +++++ ...5171145_remove_relation_contracts_space.rb | 5 ++++ ... 20180215173742_create_contract_spaces.rb} | 7 +++--- ...5181142_remove_column_on_contract_space.rb | 5 ++++ spec/models/contract_space_spec.rb | 7 +----- spec/models/contract_spec.rb | 7 +++--- spec/models/space_spec.rb | 6 +---- 12 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 db/migrate/20180214194333_remove_references_on_contract_spaces.rb create mode 100644 db/migrate/20180214194838_drop_contract_space.rb create mode 100644 db/migrate/20180214201411_contracts_spaces.rb create mode 100644 db/migrate/20180215171145_remove_relation_contracts_space.rb rename db/migrate/{20180212160438_create_contract_spaces.rb => 20180215173742_create_contract_spaces.rb} (55%) create mode 100644 db/migrate/20180215181142_remove_column_on_contract_space.rb diff --git a/app/models/contract.rb b/app/models/contract.rb index 7aeaf6d..55930ad 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -2,8 +2,9 @@ class Contract < ApplicationRecord validates :start_date, presence: true validates :finish_date, presence: true - has_many :contract_spaces has_many :payments + has_many :contract_spaces + has_many :spaces, through: :contract_spaces belongs_to :user def name "#{start_date} | #{finish_date}" diff --git a/app/models/space.rb b/app/models/space.rb index 822f84c..a0c30c1 100644 --- a/app/models/space.rb +++ b/app/models/space.rb @@ -5,27 +5,21 @@ class Space < ApplicationRecord belongs_to :parent, inverse_of: :children, foreign_key: :space_id, class_name: 'Space', optional: true has_many :children, inverse_of: :parent, foreign_key: :space_id, class_name: 'Space' has_many :assets - has_many :contract_spaces has_one :warehouse + has_many :contract_spaces + has_many :contracts, through: :contract_spaces before_save :generate_full_name protected def generate_full_name - new_name = name - next_parent = parent - while next_parent - new_name = "#{parent.name} | #{new_name}" - next_parent = next_parent.parent - end - self.full_name = parent ? new_name : name + self.full_name = array_parents.reverse.map(&:name).join(' | ') end -end -# def generate_full_name -# return self.full_name = name unless parent -# while next_parent = parent -# full_name = "#{next_parent.name} | #{full_name}" -# end -# end + def array_parents + objects = [self] + objects.push(objects.last.parent) while objects.last.parent + objects + end +end diff --git a/db/migrate/20180212043454_remove_references_on_contract.rb b/db/migrate/20180212043454_remove_references_on_contract.rb index f8304aa..6a32f68 100644 --- a/db/migrate/20180212043454_remove_references_on_contract.rb +++ b/db/migrate/20180212043454_remove_references_on_contract.rb @@ -1,5 +1,5 @@ class RemoveReferencesOnContract < ActiveRecord::Migration[5.1] def change - remove_reference(:contracts, :space, index: true) + remove_reference(:contracts, :space, index: true) end end diff --git a/db/migrate/20180214194333_remove_references_on_contract_spaces.rb b/db/migrate/20180214194333_remove_references_on_contract_spaces.rb new file mode 100644 index 0000000..f72b666 --- /dev/null +++ b/db/migrate/20180214194333_remove_references_on_contract_spaces.rb @@ -0,0 +1,6 @@ +class RemoveReferencesOnContractSpaces < ActiveRecord::Migration[5.1] + def change + remove_reference(:contract_spaces, :space, index: true) + remove_reference(:contract_spaces, :contract, index: true) + end +end diff --git a/db/migrate/20180214194838_drop_contract_space.rb b/db/migrate/20180214194838_drop_contract_space.rb new file mode 100644 index 0000000..762cfd2 --- /dev/null +++ b/db/migrate/20180214194838_drop_contract_space.rb @@ -0,0 +1,5 @@ +class DropContractSpace < ActiveRecord::Migration[5.1] + def change + drop_table :contract_spaces + end +end diff --git a/db/migrate/20180214201411_contracts_spaces.rb b/db/migrate/20180214201411_contracts_spaces.rb new file mode 100644 index 0000000..07b3927 --- /dev/null +++ b/db/migrate/20180214201411_contracts_spaces.rb @@ -0,0 +1,6 @@ +class ContractsSpaces < ActiveRecord::Migration[5.1] + create_table :contracts_spaces, id: false do |t| + t.belongs_to :contract, index: true + t.belongs_to :space, index: true + end +end diff --git a/db/migrate/20180215171145_remove_relation_contracts_space.rb b/db/migrate/20180215171145_remove_relation_contracts_space.rb new file mode 100644 index 0000000..707856c --- /dev/null +++ b/db/migrate/20180215171145_remove_relation_contracts_space.rb @@ -0,0 +1,5 @@ +class RemoveRelationContractsSpace < ActiveRecord::Migration[5.1] + def change + drop_table :contracts_spaces + end +end diff --git a/db/migrate/20180212160438_create_contract_spaces.rb b/db/migrate/20180215173742_create_contract_spaces.rb similarity index 55% rename from db/migrate/20180212160438_create_contract_spaces.rb rename to db/migrate/20180215173742_create_contract_spaces.rb index 7d8e858..50abc57 100644 --- a/db/migrate/20180212160438_create_contract_spaces.rb +++ b/db/migrate/20180215173742_create_contract_spaces.rb @@ -1,9 +1,10 @@ class CreateContractSpaces < ActiveRecord::Migration[5.1] def change create_table :contract_spaces do |t| - t.references :contract, foreign_key: true - t.references :space, foreign_key: true - + t.belongs_to :contract, index: true + t.belongs_to :space, index: true + t.datetime :appointment_date + t.timestamps end end diff --git a/db/migrate/20180215181142_remove_column_on_contract_space.rb b/db/migrate/20180215181142_remove_column_on_contract_space.rb new file mode 100644 index 0000000..4b84040 --- /dev/null +++ b/db/migrate/20180215181142_remove_column_on_contract_space.rb @@ -0,0 +1,5 @@ +class RemoveColumnOnContractSpace < ActiveRecord::Migration[5.1] + def change + remove_column :contract_spaces, :appointment_date, :datetime + end +end diff --git a/spec/models/contract_space_spec.rb b/spec/models/contract_space_spec.rb index 47a6977..774cf50 100644 --- a/spec/models/contract_space_spec.rb +++ b/spec/models/contract_space_spec.rb @@ -1,10 +1,5 @@ require 'rails_helper' RSpec.describe ContractSpace, type: :model do - it 'is valid ' do - contract = build(:contract) - space = build(:space) - contract_space = build(:contract_space, contract: contract, space: space) - expect(contract_space).to be_valid - end + it { expect(build(:contract_space, contract: build(:contract), space: build(:space))).to be_valid } end diff --git a/spec/models/contract_spec.rb b/spec/models/contract_spec.rb index d888a2a..c9282c5 100644 --- a/spec/models/contract_spec.rb +++ b/spec/models/contract_spec.rb @@ -1,12 +1,11 @@ require 'rails_helper' RSpec.describe Contract, type: :model do - it 'is valid with a start date and finish date' do - user = build(:user) - contract = build(:contract, user: user) + it { + contract = build(:contract, user: build(:user)) expect(contract).to be_valid expect(contract.name).to eq("#{contract.start_date} | #{contract.finish_date}") - end + } it { should validate_presence_of(:start_date) } it { should validate_presence_of(:finish_date) } diff --git a/spec/models/space_spec.rb b/spec/models/space_spec.rb index b91bbf7..c8a9f9e 100644 --- a/spec/models/space_spec.rb +++ b/spec/models/space_spec.rb @@ -1,11 +1,7 @@ require 'rails_helper' RSpec.describe Space, type: :model do - it { - space = build(:space) - expect(space).to be_valid - } - + it { expect(build(:space)).to be_valid } it { should validate_presence_of(:space_identifier) } it { should validate_presence_of(:name) } it { should validate_uniqueness_of(:space_identifier) } From a6b5d723df75b20805f7ef8f0ee21f7af3d5fd66 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 19 Feb 2018 18:59:28 -0500 Subject: [PATCH 11/12] Create relation asset and supplier model --- app/models/asset.rb | 1 + app/models/supplier.rb | 3 ++- .../20180214211752_remove_references_on_asset_supplier.rb | 6 ++++++ db/migrate/20180214212102_drop_asset_supplier.rb | 5 +++++ db/migrate/20180214212252_assets_suppliers.rb | 6 ++++++ .../20180215171311_remove_relation_assets_suppliers.rb | 5 +++++ ...uppliers.rb => 20180215175137_create_asset_suppliers.rb} | 6 +++--- 7 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20180214211752_remove_references_on_asset_supplier.rb create mode 100644 db/migrate/20180214212102_drop_asset_supplier.rb create mode 100644 db/migrate/20180214212252_assets_suppliers.rb create mode 100644 db/migrate/20180215171311_remove_relation_assets_suppliers.rb rename db/migrate/{20180204230315_create_asset_suppliers.rb => 20180215175137_create_asset_suppliers.rb} (61%) diff --git a/app/models/asset.rb b/app/models/asset.rb index 136f4fe..8dede4a 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -6,5 +6,6 @@ class Asset < ApplicationRecord validates :status, presence: true belongs_to :space has_many :asset_suppliers + has_many :suppliers, through: :asset_suppliers has_many :maintenances end diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 110330c..3f9f510 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -4,6 +4,7 @@ class Supplier < ApplicationRecord validates :supplier_identity, presence: true, uniqueness: true validates :services, presence: true validates :category, presence: true - has_many :asset_suppliers has_many :maintenances + has_many :asset_suppliers + has_many :assets, through: :asset_suppliers end diff --git a/db/migrate/20180214211752_remove_references_on_asset_supplier.rb b/db/migrate/20180214211752_remove_references_on_asset_supplier.rb new file mode 100644 index 0000000..3302837 --- /dev/null +++ b/db/migrate/20180214211752_remove_references_on_asset_supplier.rb @@ -0,0 +1,6 @@ +class RemoveReferencesOnAssetSupplier < ActiveRecord::Migration[5.1] + def change + remove_reference(:asset_suppliers, :asset, index: true) + remove_reference(:asset_suppliers, :supplier, index: true) + end +end diff --git a/db/migrate/20180214212102_drop_asset_supplier.rb b/db/migrate/20180214212102_drop_asset_supplier.rb new file mode 100644 index 0000000..2651317 --- /dev/null +++ b/db/migrate/20180214212102_drop_asset_supplier.rb @@ -0,0 +1,5 @@ +class DropAssetSupplier < ActiveRecord::Migration[5.1] + def change + drop_table :asset_suppliers + end +end diff --git a/db/migrate/20180214212252_assets_suppliers.rb b/db/migrate/20180214212252_assets_suppliers.rb new file mode 100644 index 0000000..fb128eb --- /dev/null +++ b/db/migrate/20180214212252_assets_suppliers.rb @@ -0,0 +1,6 @@ +class AssetsSuppliers < ActiveRecord::Migration[5.1] + create_table :assets_suppliers, id: false do |t| + t.belongs_to :asset, index: true + t.belongs_to :supplier, index: true + end +end diff --git a/db/migrate/20180215171311_remove_relation_assets_suppliers.rb b/db/migrate/20180215171311_remove_relation_assets_suppliers.rb new file mode 100644 index 0000000..fdb0aae --- /dev/null +++ b/db/migrate/20180215171311_remove_relation_assets_suppliers.rb @@ -0,0 +1,5 @@ +class RemoveRelationAssetsSuppliers < ActiveRecord::Migration[5.1] + def change + drop_table :assets_suppliers + end +end diff --git a/db/migrate/20180204230315_create_asset_suppliers.rb b/db/migrate/20180215175137_create_asset_suppliers.rb similarity index 61% rename from db/migrate/20180204230315_create_asset_suppliers.rb rename to db/migrate/20180215175137_create_asset_suppliers.rb index b58209a..4f4bd00 100644 --- a/db/migrate/20180204230315_create_asset_suppliers.rb +++ b/db/migrate/20180215175137_create_asset_suppliers.rb @@ -1,9 +1,9 @@ class CreateAssetSuppliers < ActiveRecord::Migration[5.1] def change create_table :asset_suppliers do |t| - t.references :asset, foreign_key: true - t.references :supplier, foreign_key: true - + t.belongs_to :asset, index: true + t.belongs_to :supplier, index: true + t.timestamps end end From e52e4aef695c529951569d2e52d5ea33e996e7d1 Mon Sep 17 00:00:00 2001 From: Andersson Velasquez Date: Mon, 19 Feb 2018 19:00:09 -0500 Subject: [PATCH 12/12] Refactor spec and rails admin --- config/initializers/rails_admin.rb | 4 +++- spec/models/asset_spec.rb | 7 +------ spec/models/asset_supplier_spec.rb | 7 +------ spec/models/maintenance_spec.rb | 8 +------- spec/models/organization_spec.rb | 6 +----- spec/models/supplier_spec.rb | 6 +----- spec/models/supply_spec.rb | 6 +----- spec/models/user_spec.rb | 7 +++---- spec/models/warehouse_spec.rb | 7 +------ 9 files changed, 13 insertions(+), 45 deletions(-) diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 8ea9584..4838237 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -42,11 +42,13 @@ config.model (User) { list { configure :id { hide } } } config.model (Organization) { list { configure :id { hide } } } config.model (Asset) { list { configure :id { hide } } } - config.model (AssetSupplier) { list { configure :id { hide } } } config.model (Contract) { list { configure :id { hide } } } config.model (Maintenance) { list { configure :id { hide } } } config.model (Space) { list { configure :id { hide } } } config.model (Supplier) { list { configure :id { hide } } } config.model (Supply) { list { configure :id { hide } } } config.model (Warehouse) { list { configure :id { hide } } } + config.model (Payment) { list { configure :id { hide } } } + config.model (ContractSpace) { list { configure :id { hide } } } + config.model (AssetSupplier) { list { configure :id { hide } } } end diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb index d8e5d3f..5d29b2d 100644 --- a/spec/models/asset_spec.rb +++ b/spec/models/asset_spec.rb @@ -1,12 +1,7 @@ require 'rails_helper' RSpec.describe Asset, type: :model do - it 'is valid with a name ,category , sub_category and status' do - space = build(:space) - asset = build(:asset, space: space) - expect(asset).to be_valid - end - + it { expect(build(:asset, space: build(:space))).to be_valid } it { should validate_presence_of(:name) } it { should validate_presence_of(:category) } it { should validate_presence_of(:sub_category) } diff --git a/spec/models/asset_supplier_spec.rb b/spec/models/asset_supplier_spec.rb index f6516ac..af46e13 100644 --- a/spec/models/asset_supplier_spec.rb +++ b/spec/models/asset_supplier_spec.rb @@ -1,10 +1,5 @@ require 'rails_helper' RSpec.describe AssetSupplier, type: :model do - it 'is valid' do - supplier = build(:supplier) - asset = build(:asset) - asset_supplier = build(:asset_supplier, supplier: supplier, asset: asset) - expect(asset_supplier).to be_valid - end + it { expect(build(:asset_supplier, asset: build(:asset), supplier: build(:supplier))).to be_valid } end diff --git a/spec/models/maintenance_spec.rb b/spec/models/maintenance_spec.rb index 4e34651..ecfddeb 100644 --- a/spec/models/maintenance_spec.rb +++ b/spec/models/maintenance_spec.rb @@ -1,13 +1,7 @@ require 'rails_helper' RSpec.describe Maintenance, type: :model do - it 'is valid with a registration date and status' do - supplier = build(:supplier) - asset = build(:asset) - maintenance = build(:maintenance, supplier: supplier, asset: asset) - expect(maintenance).to be_valid - end - + it { expect(build(:maintenance, supplier: build(:supplier), asset: build(:asset))).to be_valid } it { should validate_presence_of(:registration_date) } it { should validate_presence_of(:status) } end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 01a45b4..a1ca9a7 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -1,11 +1,7 @@ require 'rails_helper' RSpec.describe Organization, type: :model do - it 'is valid with a name and organization_identifier' do - organization = build(:organization) - expect(organization).to be_valid - end - + it { expect(build(:organization)).to be_valid } it { should validate_presence_of(:name) } it { should validate_presence_of(:organization_identifier) } it { should validate_uniqueness_of(:organization_identifier) } diff --git a/spec/models/supplier_spec.rb b/spec/models/supplier_spec.rb index 5306589..c36cecf 100644 --- a/spec/models/supplier_spec.rb +++ b/spec/models/supplier_spec.rb @@ -1,11 +1,7 @@ require 'rails_helper' RSpec.describe Supplier, type: :model do - it 'is valid with a name, supplier_identity, services, category' do - supplier = build(:supplier) - expect(supplier).to be_valid - end - + it { expect(build(:supplier)).to be_valid } it { should validate_presence_of(:name) } it { should validate_presence_of(:supplier_identity) } it { should validate_uniqueness_of(:supplier_identity) } diff --git a/spec/models/supply_spec.rb b/spec/models/supply_spec.rb index d73ff90..4775ae5 100644 --- a/spec/models/supply_spec.rb +++ b/spec/models/supply_spec.rb @@ -1,11 +1,7 @@ require 'rails_helper' RSpec.describe Supply, type: :model do - it 'is valid with a name ,presentation, stock, stock minumin and stock reposition' do - supply = build(:supply) - expect(supply).to be_valid - end - + it { expect(build(:supply)).to be_valid } it { should validate_presence_of(:name) } it { should validate_presence_of(:stock) } it { should validate_presence_of(:stock_minimun) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0105198..22c8cc4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,12 +1,11 @@ require 'rails_helper' RSpec.describe User, type: :model do - it 'is valid with a first name, last name, email, role and user_identifier' do - organization = build(:organization) - user = build(:user, organization: organization) + it { + user = build(:user, organization: build(:organization)) expect(user).to be_valid expect(user.name).to eq("#{user.first_name} #{user.last_name}") - end + } it { should validate_presence_of(:first_name) } it { should validate_presence_of(:last_name) } diff --git a/spec/models/warehouse_spec.rb b/spec/models/warehouse_spec.rb index 56f8165..f868383 100644 --- a/spec/models/warehouse_spec.rb +++ b/spec/models/warehouse_spec.rb @@ -1,10 +1,5 @@ require 'rails_helper' RSpec.describe Warehouse, type: :model do - it 'is valid ' do - supply = build(:supply) - space = build(:space) - warehouse = build(:warehouse, space: space, supply: supply) - expect(warehouse).to be_valid - end + it { expect(build(:warehouse, space: build(:space), supply: build(:supply))).to be_valid } end