From e20954d30e3a320d92c1e086a141b9b27405cd35 Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Wed, 23 Sep 2020 10:53:17 -0400 Subject: [PATCH 01/15] Add initial models --- app/models/kit.rb | 20 +++++++++++++++++ app/models/kit_item.rb | 17 ++++++++++++++ db/migrate/20200923143835_create_kits.rb | 13 +++++++++++ db/migrate/20200923143843_create_kit_items.rb | 13 +++++++++++ db/schema.rb | 22 ++++++++++++++++++- spec/models/kit_item_spec.rb | 16 ++++++++++++++ spec/models/kit_spec.rb | 16 ++++++++++++++ 7 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 app/models/kit.rb create mode 100644 app/models/kit_item.rb create mode 100644 db/migrate/20200923143835_create_kits.rb create mode 100644 db/migrate/20200923143843_create_kit_items.rb create mode 100644 spec/models/kit_item_spec.rb create mode 100644 spec/models/kit_spec.rb diff --git a/app/models/kit.rb b/app/models/kit.rb new file mode 100644 index 0000000000..76ee3da0f0 --- /dev/null +++ b/app/models/kit.rb @@ -0,0 +1,20 @@ +# == Schema Information +# +# Table name: kits +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer +# storage_location_id :integer +# +class Kit < ApplicationRecord + has_many :kit_items + has_many :items, through: :kit_items + + belongs_to :storage_location + belongs_to :organization + + validates :storage_location, :organization, presence: true +end diff --git a/app/models/kit_item.rb b/app/models/kit_item.rb new file mode 100644 index 0000000000..94aade3025 --- /dev/null +++ b/app/models/kit_item.rb @@ -0,0 +1,17 @@ +# == Schema Information +# +# Table name: kit_items +# +# id :bigint not null, primary key +# quantity :integer +# created_at :datetime not null +# updated_at :datetime not null +# item_id :integer +# kit_id :integer +# +class KitItem < ApplicationRecord + belongs_to :kit + belongs_to :item + + validates :quantity, presence: true +end diff --git a/db/migrate/20200923143835_create_kits.rb b/db/migrate/20200923143835_create_kits.rb new file mode 100644 index 0000000000..a6ab37267b --- /dev/null +++ b/db/migrate/20200923143835_create_kits.rb @@ -0,0 +1,13 @@ +class CreateKits < ActiveRecord::Migration[6.0] + def change + create_table :kits do |t| + t.string :name + t.integer :storage_location_id + t.integer :organization_id + + t.timestamps + end + add_index :kits, :storage_location_id + add_index :kits, :organization_id + end +end diff --git a/db/migrate/20200923143843_create_kit_items.rb b/db/migrate/20200923143843_create_kit_items.rb new file mode 100644 index 0000000000..4becf331ad --- /dev/null +++ b/db/migrate/20200923143843_create_kit_items.rb @@ -0,0 +1,13 @@ +class CreateKitItems < ActiveRecord::Migration[6.0] + def change + create_table :kit_items do |t| + t.integer :item_id + t.integer :kit_id + t.integer :quantity + + t.timestamps + end + add_index :kit_items, :item_id + add_index :kit_items, :kit_id + end +end diff --git a/db/schema.rb b/db/schema.rb index dc330f554f..ec6a1fcb2a 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: 2020_09_22_144333) do +ActiveRecord::Schema.define(version: 2020_09_23_143843) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -233,6 +233,26 @@ t.index ["partner_key"], name: "index_items_on_partner_key" end + create_table "kit_items", force: :cascade do |t| + t.integer "item_id" + t.integer "kit_id" + t.integer "quantity" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["item_id"], name: "index_kit_items_on_item_id" + t.index ["kit_id"], name: "index_kit_items_on_kit_id" + end + + create_table "kits", force: :cascade do |t| + t.string "name" + t.integer "storage_location_id" + t.integer "organization_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["organization_id"], name: "index_kits_on_organization_id" + t.index ["storage_location_id"], name: "index_kits_on_storage_location_id" + end + create_table "line_items", id: :serial, force: :cascade do |t| t.integer "quantity" t.integer "item_id" diff --git a/spec/models/kit_item_spec.rb b/spec/models/kit_item_spec.rb new file mode 100644 index 0000000000..9951bfc451 --- /dev/null +++ b/spec/models/kit_item_spec.rb @@ -0,0 +1,16 @@ +# == Schema Information +# +# Table name: kit_items +# +# id :bigint not null, primary key +# quantity :integer +# created_at :datetime not null +# updated_at :datetime not null +# item_id :integer +# kit_id :integer +# +require 'rails_helper' + +RSpec.describe KitItem, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/kit_spec.rb b/spec/models/kit_spec.rb new file mode 100644 index 0000000000..cbfd27ddde --- /dev/null +++ b/spec/models/kit_spec.rb @@ -0,0 +1,16 @@ +# == Schema Information +# +# Table name: kits +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer +# storage_location_id :integer +# +require 'rails_helper' + +RSpec.describe Kit, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 7886e004fa39f3335824f0affde864a53a122a22 Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Wed, 23 Sep 2020 11:52:01 -0400 Subject: [PATCH 02/15] Use itemizable --- app/models/kit.rb | 2 ++ app/models/kit_item.rb | 17 ----------------- db/migrate/20200923143843_create_kit_items.rb | 13 ------------- db/schema.rb | 12 +----------- spec/models/kit_item_spec.rb | 16 ---------------- 5 files changed, 3 insertions(+), 57 deletions(-) delete mode 100644 app/models/kit_item.rb delete mode 100644 db/migrate/20200923143843_create_kit_items.rb delete mode 100644 spec/models/kit_item_spec.rb diff --git a/app/models/kit.rb b/app/models/kit.rb index 76ee3da0f0..8ddfddeb8a 100644 --- a/app/models/kit.rb +++ b/app/models/kit.rb @@ -10,6 +10,8 @@ # storage_location_id :integer # class Kit < ApplicationRecord + include Itemizable + has_many :kit_items has_many :items, through: :kit_items diff --git a/app/models/kit_item.rb b/app/models/kit_item.rb deleted file mode 100644 index 94aade3025..0000000000 --- a/app/models/kit_item.rb +++ /dev/null @@ -1,17 +0,0 @@ -# == Schema Information -# -# Table name: kit_items -# -# id :bigint not null, primary key -# quantity :integer -# created_at :datetime not null -# updated_at :datetime not null -# item_id :integer -# kit_id :integer -# -class KitItem < ApplicationRecord - belongs_to :kit - belongs_to :item - - validates :quantity, presence: true -end diff --git a/db/migrate/20200923143843_create_kit_items.rb b/db/migrate/20200923143843_create_kit_items.rb deleted file mode 100644 index 4becf331ad..0000000000 --- a/db/migrate/20200923143843_create_kit_items.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreateKitItems < ActiveRecord::Migration[6.0] - def change - create_table :kit_items do |t| - t.integer :item_id - t.integer :kit_id - t.integer :quantity - - t.timestamps - end - add_index :kit_items, :item_id - add_index :kit_items, :kit_id - end -end diff --git a/db/schema.rb b/db/schema.rb index ec6a1fcb2a..5beac777a7 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: 2020_09_23_143843) do +ActiveRecord::Schema.define(version: 2020_09_23_143835) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -233,16 +233,6 @@ t.index ["partner_key"], name: "index_items_on_partner_key" end - create_table "kit_items", force: :cascade do |t| - t.integer "item_id" - t.integer "kit_id" - t.integer "quantity" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["item_id"], name: "index_kit_items_on_item_id" - t.index ["kit_id"], name: "index_kit_items_on_kit_id" - end - create_table "kits", force: :cascade do |t| t.string "name" t.integer "storage_location_id" diff --git a/spec/models/kit_item_spec.rb b/spec/models/kit_item_spec.rb deleted file mode 100644 index 9951bfc451..0000000000 --- a/spec/models/kit_item_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -# == Schema Information -# -# Table name: kit_items -# -# id :bigint not null, primary key -# quantity :integer -# created_at :datetime not null -# updated_at :datetime not null -# item_id :integer -# kit_id :integer -# -require 'rails_helper' - -RSpec.describe KitItem, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end From 65a98faf14d910341921f993b3e1a93962d391cc Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Wed, 23 Sep 2020 16:55:20 -0400 Subject: [PATCH 03/15] Add ability to create a kit --- app/controllers/kits_controller.rb | 37 +++++++++++++ app/models/kit.rb | 15 ++++- app/models/organization.rb | 89 ++++++++++++++++-------------- app/views/items/index.html.erb | 3 +- app/views/kits/_form.html.erb | 46 +++++++++++++++ app/views/kits/new.html.erb | 23 ++++++++ config/routes.rb | 1 + 7 files changed, 168 insertions(+), 46 deletions(-) create mode 100644 app/controllers/kits_controller.rb create mode 100644 app/views/kits/_form.html.erb create mode 100644 app/views/kits/new.html.erb diff --git a/app/controllers/kits_controller.rb b/app/controllers/kits_controller.rb new file mode 100644 index 0000000000..64843bc0e9 --- /dev/null +++ b/app/controllers/kits_controller.rb @@ -0,0 +1,37 @@ +class KitsController < ApplicationController + def new + load_form_collections + @kit = current_organization.kits.new + @kit.line_items.build + end + + def edit + end + + def create + @kit = current_organization.kits.new(kit_params) + @kit.organization_id = current_organization.id + if @kit.save + flash[:notice] = "Kit created successfully" + redirect_to items_path + else + flash[:error] = @kit.errors.full_messages.to_sentence + load_form_collections + render :new + end + end + + def update + end + + private + + def load_form_collections + @items = current_organization.items.active.alphabetized + @storage_locations = current_organization.storage_locations + end + + def kit_params + params.require(:kit).permit(:name, :storage_location_id, line_items_attributes: [:item_id, :quantity, :_destroy]) + end +end diff --git a/app/models/kit.rb b/app/models/kit.rb index 8ddfddeb8a..0d46b81014 100644 --- a/app/models/kit.rb +++ b/app/models/kit.rb @@ -12,11 +12,20 @@ class Kit < ApplicationRecord include Itemizable - has_many :kit_items - has_many :items, through: :kit_items - belongs_to :storage_location belongs_to :organization validates :storage_location, :organization, presence: true + validate :can_build_kit? + + delegate :inventory_items, to: :storage_location + + def can_build_kit? + grouped_inventory_items = inventory_items.group_by(&:item_id) + line_items.each do |line_item| + inventory_item = grouped_inventory_items[line_item.item_id]&.first + next if inventory_item.quantity > line_item.quantity + self.errors.add(:base, "Not enough #{line_item.item.name} to build kit") + end + end end diff --git a/app/models/organization.rb b/app/models/organization.rb index a107b9e0d7..c2f2ee1040 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -36,53 +36,58 @@ class Organization < ApplicationRecord validates :reminder_day, numericality: { only_integer: true, less_than_or_equal_to: 14, greater_than_or_equal_to: 1, allow_nil: true } validate :deadline_after_reminder - has_many :adjustments, dependent: :destroy - has_many :barcode_items, dependent: :destroy do - def all - unscope(where: :organization_id).where("barcode_items.organization_id = ? OR barcode_items.barcodeable_type = ?", proxy_association.owner.id, "BaseItem") - end - end - has_many :distributions, dependent: :destroy do - def upcoming - this_week.scheduled.where('issued_at >= ?', Time.zone.today) - end - end - has_many :donations, dependent: :destroy - has_many :purchases, dependent: :destroy - has_many :donation_sites, dependent: :destroy - has_many :diaper_drives, dependent: :destroy - has_many :diaper_drive_participants, dependent: :destroy - has_many :manufacturers, dependent: :destroy - has_many :vendors, dependent: :destroy - has_many :storage_locations, dependent: :destroy - has_many :inventory_items, through: :storage_locations - has_many :items, dependent: :destroy do - def other - where(partner_key: "other") - end + with_options dependent: :destroy do + has_many :adjustments + has_many :audits + has_many :diaper_drive_participants + has_many :diaper_drives + has_many :donation_sites + has_many :donations + has_many :kits + has_many :manufacturers + has_many :partners + has_many :purchases + has_many :requests + has_many :storage_locations + has_many :inventory_items, through: :storage_locations + has_many :transfers + has_many :users + has_many :vendors + has_many :items do + def other + where(partner_key: "other") + end - def during(date_start, date_end = Time.zone.now.strftime("%Y-%m-%d")) - select("COUNT(line_items.id) as amount, name") - .joins(:line_items) - .where("line_items.created_at BETWEEN ? and ?", date_start, date_end) - .group(:name) - end + def during(date_start, date_end = Time.zone.now.strftime("%Y-%m-%d")) + select("COUNT(line_items.id) as amount, name") + .joins(:line_items) + .where("line_items.created_at BETWEEN ? and ?", date_start, date_end) + .group(:name) + end - def top(limit = 5) - order('count(line_items.id) DESC') - .limit(limit) - end + def top(limit = 5) + order('count(line_items.id) DESC') + .limit(limit) + end - def bottom(limit = 5) - order('count(line_items.id) ASC') - .limit(limit) + def bottom(limit = 5) + order('count(line_items.id) ASC') + .limit(limit) + end + end + has_many :barcode_items, dependent: :destroy do + def all + unscope(where: :organization_id).where("barcode_items.organization_id = ? OR barcode_items.barcodeable_type = ?", proxy_association.owner.id, "BaseItem") + end + end + has_many :distributions, dependent: :destroy do + def upcoming + this_week.scheduled.where('issued_at >= ?', Time.zone.today) + end end end - has_many :partners, dependent: :destroy - has_many :transfers, dependent: :destroy - has_many :users, dependent: :destroy - has_many :requests, dependent: :destroy - has_many :audits, dependent: :destroy + + before_update :update_partner_sections, if: :partner_form_fields_changed? ALL_PARTIALS = [ diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb index cab7ac8736..3cf633a8f4 100644 --- a/app/views/items/index.html.erb +++ b/app/views/items/index.html.erb @@ -55,6 +55,7 @@ <%= download_button_to(csv_path(format: :csv, type: "Item"), {text: "Export Items"}) if @items.any? %> <%= new_button_to new_item_path(organization_id: current_organization), {text: "New Item"} %> + <%= new_button_to new_kit_path(organization_id: current_organization), {text: "New Kit"} %> <% end # form %> @@ -96,4 +97,4 @@ - \ No newline at end of file + diff --git a/app/views/kits/_form.html.erb b/app/views/kits/_form.html.erb new file mode 100644 index 0000000000..fe7b686f4c --- /dev/null +++ b/app/views/kits/_form.html.erb @@ -0,0 +1,46 @@ +<%= simple_form_for @kit, remote: request.xhr?, html: { class: 'form-horizontal' } do |f| %> +
+
+
+
+
+
+ <%= f.input :name, label: "Name", wrapper: :input_group do %> + + <%= f.input_field :name, class: "form-control" %> + <% end %> + + <% if current_user.organization_admin? %> + <%= f.input :name, label: "On hand minimum quantity", wrapper: :input_group do %> + <%= f.input_field :on_hand_minimum_quantity, input_html: {value: 0}, class: "form-control" %> + <% end %> + <%= f.input :name, label: "On hand recommended quantity", wrapper: :input_group do %> + <%= f.input_field :on_hand_recommended_quantity, class: "form-control" %> + <% end %> + <% end %> + <%= render partial: "storage_locations/source", object: f %> + +
+ Items in this Kit + <%= f.simple_fields_for :line_items do |item| %> +
+ <%= render 'line_items/line_item_fields', f: item %> +
+ <% end %> + + +
+ +
+ +
+
+
+
+<% end %> diff --git a/app/views/kits/new.html.erb b/app/views/kits/new.html.erb new file mode 100644 index 0000000000..9821ce3543 --- /dev/null +++ b/app/views/kits/new.html.erb @@ -0,0 +1,23 @@ +
+
+
+
+ <% content_for :title, "New - Items - Inventory - #{current_organization.name}" %> +

New Kit for <%= current_organization.name %>

+
+
+ +
+
+
+
+ +<%= render 'form', object: @item %> diff --git a/config/routes.rb b/config/routes.rb index 0652f4e76e..6f4e15c59a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -117,6 +117,7 @@ def set_up_flipper post :import_csv end end + resources :kits resources :items do patch :restore, on: :member end From 2a715b1db78ba19c7c9f58ac378eb299a7638647 Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Thu, 24 Sep 2020 08:32:11 -0400 Subject: [PATCH 04/15] Have Kit be a subclass of Item --- app/models/kit.rb | 40 +++++++++++------------- db/migrate/20200923143835_create_kits.rb | 13 -------- db/schema.rb | 12 +------ spec/models/kit_spec.rb | 23 +++++++++----- 4 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 db/migrate/20200923143835_create_kits.rb diff --git a/app/models/kit.rb b/app/models/kit.rb index 0d46b81014..c164598aef 100644 --- a/app/models/kit.rb +++ b/app/models/kit.rb @@ -1,31 +1,27 @@ # == Schema Information # -# Table name: kits +# Table name: items # -# id :bigint not null, primary key -# name :string -# created_at :datetime not null -# updated_at :datetime not null -# organization_id :integer -# storage_location_id :integer +# id :integer not null, primary key +# active :boolean default(TRUE) +# barcode_count :integer +# category :string +# distribution_quantity :integer +# name :string +# on_hand_minimum_quantity :integer default(0), not null +# on_hand_recommended_quantity :integer +# package_size :integer +# partner_key :string +# value_in_cents :integer default(0) +# visible_to_partners :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer # -class Kit < ApplicationRecord +class Kit < Item include Itemizable - belongs_to :storage_location belongs_to :organization - validates :storage_location, :organization, presence: true - validate :can_build_kit? - - delegate :inventory_items, to: :storage_location - - def can_build_kit? - grouped_inventory_items = inventory_items.group_by(&:item_id) - line_items.each do |line_item| - inventory_item = grouped_inventory_items[line_item.item_id]&.first - next if inventory_item.quantity > line_item.quantity - self.errors.add(:base, "Not enough #{line_item.item.name} to build kit") - end - end + validates :organization, presence: true end diff --git a/db/migrate/20200923143835_create_kits.rb b/db/migrate/20200923143835_create_kits.rb deleted file mode 100644 index a6ab37267b..0000000000 --- a/db/migrate/20200923143835_create_kits.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreateKits < ActiveRecord::Migration[6.0] - def change - create_table :kits do |t| - t.string :name - t.integer :storage_location_id - t.integer :organization_id - - t.timestamps - end - add_index :kits, :storage_location_id - add_index :kits, :organization_id - end -end diff --git a/db/schema.rb b/db/schema.rb index 5beac777a7..109863331b 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: 2020_09_23_143835) do +ActiveRecord::Schema.define(version: 2020_09_21_182529) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -233,16 +233,6 @@ t.index ["partner_key"], name: "index_items_on_partner_key" end - create_table "kits", force: :cascade do |t| - t.string "name" - t.integer "storage_location_id" - t.integer "organization_id" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["organization_id"], name: "index_kits_on_organization_id" - t.index ["storage_location_id"], name: "index_kits_on_storage_location_id" - end - create_table "line_items", id: :serial, force: :cascade do |t| t.integer "quantity" t.integer "item_id" diff --git a/spec/models/kit_spec.rb b/spec/models/kit_spec.rb index cbfd27ddde..d2b2b62906 100644 --- a/spec/models/kit_spec.rb +++ b/spec/models/kit_spec.rb @@ -1,13 +1,22 @@ # == Schema Information # -# Table name: kits +# Table name: items # -# id :bigint not null, primary key -# name :string -# created_at :datetime not null -# updated_at :datetime not null -# organization_id :integer -# storage_location_id :integer +# id :integer not null, primary key +# active :boolean default(TRUE) +# barcode_count :integer +# category :string +# distribution_quantity :integer +# name :string +# on_hand_minimum_quantity :integer default(0), not null +# on_hand_recommended_quantity :integer +# package_size :integer +# partner_key :string +# value_in_cents :integer default(0) +# visible_to_partners :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer # require 'rails_helper' From 862f0e44e8a109b77be3c6c55044eedbe6a25daa Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Thu, 24 Sep 2020 11:41:36 -0400 Subject: [PATCH 05/15] Go back to separate kit model --- app/controllers/kits_controller.rb | 2 +- app/models/distribution.rb | 6 ++---- app/models/item.rb | 6 ++++-- app/models/kit.rb | 25 +++++++----------------- app/models/organization.rb | 2 +- db/migrate/20200924154023_create_kits.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- spec/models/kit_spec.rb | 22 ++++++--------------- 8 files changed, 41 insertions(+), 43 deletions(-) create mode 100644 db/migrate/20200924154023_create_kits.rb diff --git a/app/controllers/kits_controller.rb b/app/controllers/kits_controller.rb index 64843bc0e9..baddb5c62d 100644 --- a/app/controllers/kits_controller.rb +++ b/app/controllers/kits_controller.rb @@ -32,6 +32,6 @@ def load_form_collections end def kit_params - params.require(:kit).permit(:name, :storage_location_id, line_items_attributes: [:item_id, :quantity, :_destroy]) + params.require(:kit).permit(:name, line_items_attributes: [:item_id, :quantity, :_destroy]) end end diff --git a/app/models/distribution.rb b/app/models/distribution.rb index 6a0b175564..25d23dea39 100644 --- a/app/models/distribution.rb +++ b/app/models/distribution.rb @@ -29,6 +29,8 @@ class Distribution < ApplicationRecord # Distributions contain many different items include Itemizable include Exportable + include IssuedAt + include Filterable has_one :request, dependent: :nullify accepts_nested_attributes_for :request @@ -36,15 +38,11 @@ class Distribution < ApplicationRecord validates :storage_location, :partner, :organization, :delivery_method, presence: true validate :line_item_items_exist_in_inventory - include IssuedAt - before_save :combine_distribution enum state: { started: 0, scheduled: 5, complete: 10 } - enum delivery_method: { pick_up: 0, delivery: 1 } - include Filterable # add item_id scope to allow filtering distributions by item scope :by_item_id, ->(item_id) { joins(:items).where(items: { id: item_id }) } # partner scope to allow filtering by partner diff --git a/app/models/item.rb b/app/models/item.rb index 467b33aba3..78c6c4213c 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -20,8 +20,12 @@ # class Item < ApplicationRecord + include Filterable + include Exportable + belongs_to :organization # If these are universal this isn't necessary belongs_to :base_item, counter_cache: :item_count, primary_key: :partner_key, foreign_key: :partner_key, inverse_of: :items + validates :name, uniqueness: { scope: :organization } validates :name, presence: true validates :organization, presence: true @@ -34,8 +38,6 @@ class Item < ApplicationRecord has_many :donations, through: :line_items, source: :itemizable, source_type: Donation has_many :distributions, through: :line_items, source: :itemizable, source_type: Distribution - include Filterable - include Exportable scope :active, -> { where(active: true) } scope :visible, -> { where(visible_to_partners: true) } scope :alphabetized, -> { order(:name) } diff --git a/app/models/kit.rb b/app/models/kit.rb index c164598aef..51f0c41645 100644 --- a/app/models/kit.rb +++ b/app/models/kit.rb @@ -1,27 +1,16 @@ # == Schema Information # -# Table name: items +# Table name: kits # -# id :integer not null, primary key -# active :boolean default(TRUE) -# barcode_count :integer -# category :string -# distribution_quantity :integer -# name :string -# on_hand_minimum_quantity :integer default(0), not null -# on_hand_recommended_quantity :integer -# package_size :integer -# partner_key :string -# value_in_cents :integer default(0) -# visible_to_partners :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# organization_id :integer +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer # -class Kit < Item +class Kit < ApplicationRecord include Itemizable belongs_to :organization - validates :organization, presence: true end diff --git a/app/models/organization.rb b/app/models/organization.rb index c2f2ee1040..97f7ebcaf1 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -43,13 +43,13 @@ class Organization < ApplicationRecord has_many :diaper_drives has_many :donation_sites has_many :donations - has_many :kits has_many :manufacturers has_many :partners has_many :purchases has_many :requests has_many :storage_locations has_many :inventory_items, through: :storage_locations + has_many :kits has_many :transfers has_many :users has_many :vendors diff --git a/db/migrate/20200924154023_create_kits.rb b/db/migrate/20200924154023_create_kits.rb new file mode 100644 index 0000000000..55ae84b12f --- /dev/null +++ b/db/migrate/20200924154023_create_kits.rb @@ -0,0 +1,11 @@ +class CreateKits < ActiveRecord::Migration[6.0] + def change + create_table :kits do |t| + t.string :name + t.integer :organization_id + + t.timestamps + end + add_index :kits, :organization_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 109863331b..6351033f69 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: 2020_09_21_182529) do +ActiveRecord::Schema.define(version: 2020_09_24_154023) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -233,6 +233,14 @@ t.index ["partner_key"], name: "index_items_on_partner_key" end + create_table "kits", force: :cascade do |t| + t.string "name" + t.integer "organization_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["organization_id"], name: "index_kits_on_organization_id" + end + create_table "line_items", id: :serial, force: :cascade do |t| t.integer "quantity" t.integer "item_id" diff --git a/spec/models/kit_spec.rb b/spec/models/kit_spec.rb index d2b2b62906..237c8b16d7 100644 --- a/spec/models/kit_spec.rb +++ b/spec/models/kit_spec.rb @@ -1,22 +1,12 @@ # == Schema Information # -# Table name: items +# Table name: kits # -# id :integer not null, primary key -# active :boolean default(TRUE) -# barcode_count :integer -# category :string -# distribution_quantity :integer -# name :string -# on_hand_minimum_quantity :integer default(0), not null -# on_hand_recommended_quantity :integer -# package_size :integer -# partner_key :string -# value_in_cents :integer default(0) -# visible_to_partners :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# organization_id :integer +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# organization_id :integer # require 'rails_helper' From 2928802b7b844e95d0668f9ed497ef816b06210f Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Thu, 24 Sep 2020 12:26:06 -0400 Subject: [PATCH 06/15] Remove storage location from the Kit and add a Kits tab to the items views --- app/controllers/items_controller.rb | 1 + app/controllers/kits_controller.rb | 1 - app/views/items/_kits.html.erb | 24 ++++++++++++++++++++++++ app/views/items/index.html.erb | 6 ++++-- app/views/kits/_form.html.erb | 1 - 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 app/views/items/_kits.html.erb diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 3b1acc9865..cfff924b1a 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -3,6 +3,7 @@ class ItemsController < ApplicationController def index @items = current_organization.items.includes(:base_item).alphabetized.class_filter(filter_params) + @kits = current_organization.kits.includes(line_items: :item) @storages = current_organization.storage_locations.order(id: :asc) @include_inactive_items = params[:include_inactive_items] diff --git a/app/controllers/kits_controller.rb b/app/controllers/kits_controller.rb index baddb5c62d..9968f2aac9 100644 --- a/app/controllers/kits_controller.rb +++ b/app/controllers/kits_controller.rb @@ -28,7 +28,6 @@ def update def load_form_collections @items = current_organization.items.active.alphabetized - @storage_locations = current_organization.storage_locations end def kit_params diff --git a/app/views/items/_kits.html.erb b/app/views/items/_kits.html.erb new file mode 100644 index 0000000000..17e2c3c611 --- /dev/null +++ b/app/views/items/_kits.html.erb @@ -0,0 +1,24 @@ +
+ + + + + + + + + <% @kits.each do |kit| %> + + + + + <% end %> + +
NameItems
<%= kit.name %> +
    + <% kit.line_items.quantities_by_name.map do |id, item_hash| %> +
  • <%= "#{item_hash[:quantity]} #{item_hash[:name]}" %>
  • + <% end %> +
+
+
diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb index 3cf633a8f4..7841977f68 100644 --- a/app/views/items/index.html.erb +++ b/app/views/items/index.html.erb @@ -83,14 +83,16 @@ Items, Quantity, and Location +
- <%= render partial: 'item_list' %> - <%= render partial: 'items_quantity_and_location' %> + <%= render partial: 'kits' %>
diff --git a/app/views/kits/_form.html.erb b/app/views/kits/_form.html.erb index fe7b686f4c..158328a15e 100644 --- a/app/views/kits/_form.html.erb +++ b/app/views/kits/_form.html.erb @@ -18,7 +18,6 @@ <%= f.input_field :on_hand_recommended_quantity, class: "form-control" %> <% end %> <% end %> - <%= render partial: "storage_locations/source", object: f %>
Items in this Kit From 14da19de69383e302fd45908bd75ab2c6989b58a Mon Sep 17 00:00:00 2001 From: Matthew Dodds Date: Thu, 24 Sep 2020 14:02:55 -0400 Subject: [PATCH 07/15] Add new inventory subtab for kits --- app/controllers/kits_controller.rb | 4 ++ app/views/items/_kits.html.erb | 23 +------- app/views/kits/_form.html.erb | 1 - app/views/kits/_table.html.erb | 22 ++++++++ app/views/kits/index.html.erb | 71 +++++++++++++++++++++++++ app/views/layouts/_lte_sidebar.html.erb | 5 ++ 6 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 app/views/kits/_table.html.erb create mode 100644 app/views/kits/index.html.erb diff --git a/app/controllers/kits_controller.rb b/app/controllers/kits_controller.rb index 9968f2aac9..72fbd4d2d0 100644 --- a/app/controllers/kits_controller.rb +++ b/app/controllers/kits_controller.rb @@ -1,4 +1,8 @@ class KitsController < ApplicationController + def index + @kits = current_organization.kits + end + def new load_form_collections @kit = current_organization.kits.new diff --git a/app/views/items/_kits.html.erb b/app/views/items/_kits.html.erb index 17e2c3c611..4c1561e88e 100644 --- a/app/views/items/_kits.html.erb +++ b/app/views/items/_kits.html.erb @@ -1,24 +1,3 @@
- - - - - - - - - <% @kits.each do |kit| %> - - - - - <% end %> - -
NameItems
<%= kit.name %> -
    - <% kit.line_items.quantities_by_name.map do |id, item_hash| %> -
  • <%= "#{item_hash[:quantity]} #{item_hash[:name]}" %>
  • - <% end %> -
-
+ <%= render partial: 'kits/table' %>
diff --git a/app/views/kits/_form.html.erb b/app/views/kits/_form.html.erb index 158328a15e..1cb79cbe34 100644 --- a/app/views/kits/_form.html.erb +++ b/app/views/kits/_form.html.erb @@ -31,7 +31,6 @@ <%= add_line_item_button f, "#kit_line_items" %> -
diff --git a/app/views/kits/_table.html.erb b/app/views/kits/_table.html.erb new file mode 100644 index 0000000000..2621d4f24a --- /dev/null +++ b/app/views/kits/_table.html.erb @@ -0,0 +1,22 @@ + + + + + + + + + <% @kits.each do |kit| %> + + + + + <% end %> + +
NameItems
<%= kit.name %> +
    + <% kit.line_items.quantities_by_name.map do |id, item_hash| %> +
  • <%= "#{item_hash[:quantity]} #{item_hash[:name]}" %>
  • + <% end %> +
+
diff --git a/app/views/kits/index.html.erb b/app/views/kits/index.html.erb new file mode 100644 index 0000000000..d52a75715e --- /dev/null +++ b/app/views/kits/index.html.erb @@ -0,0 +1,71 @@ +
+
+
+
+ <% content_for :title, "Kits - #{current_organization.name}" %> +

+ Kits + for <%= current_organization.name %> +

+
+
+ +
+
+
+
+ +
+
+
+
+
+
+

Kit Filter

+
+
+ <%= form_tag(items_path, method: :get) do |f| %> +
+
+ <%= label_tag "Filter by Base Item" %> + <%= collection_select(:filters, :by_base_item, BaseItem.alphabetized, :partner_key, :name, {include_blank: true, selected: @selected_base_item}, class: "form-control") %> +
+
+
+
+ <%= check_box_tag("include_inactive_items", 1, @include_inactive_items) %> + <%= label_tag "Also include inactive items" %> +
+
+
+ + <% end # form %> +
+
+
+
+
+ +
+
+
+
+
+
+ <%= render partial: 'kits/table' %> +
+
+
+
+
+
diff --git a/app/views/layouts/_lte_sidebar.html.erb b/app/views/layouts/_lte_sidebar.html.erb index 675228ffc3..72c57455f3 100644 --- a/app/views/layouts/_lte_sidebar.html.erb +++ b/app/views/layouts/_lte_sidebar.html.erb @@ -101,6 +101,11 @@ Items & Inventory <% end %> + - + diff --git a/app/views/layouts/_lte_sidebar.html.erb b/app/views/layouts/_lte_sidebar.html.erb index 72c57455f3..f08ead96ce 100644 --- a/app/views/layouts/_lte_sidebar.html.erb +++ b/app/views/layouts/_lte_sidebar.html.erb @@ -89,8 +89,8 @@ <% end %> - - + <% if Flipper.enabled?(:kits) %> + + <% end %>
<%= render partial: 'item_list' %> <%= render partial: 'items_quantity_and_location' %> - <%= render partial: 'kits' %> + <% if Flipper.enabled?(:kits) %> + <%= render partial: 'kits' %> + <% end %>
diff --git a/app/views/layouts/_lte_sidebar.html.erb b/app/views/layouts/_lte_sidebar.html.erb index f08ead96ce..2e9d64718e 100644 --- a/app/views/layouts/_lte_sidebar.html.erb +++ b/app/views/layouts/_lte_sidebar.html.erb @@ -101,11 +101,13 @@ Items & Inventory <% end %> - + <% if Flipper.enabled?(:kits) %> + + <% end %>