Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/adjustments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def new
@adjustment = current_organization.adjustments.new
@adjustment.line_items.build
@storage_locations = current_organization.storage_locations.active
@items = current_organization.items.loose.active.alphabetized
@items = ConcreteItem.where(organization: current_organization).active.alphabetized
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add current_organization.concrete_items


# POST /adjustments
Expand All @@ -55,7 +55,7 @@ def create

def load_form_collections
@storage_locations = current_organization.storage_locations.active
@items = current_organization.items.loose.alphabetized
@items = ConcreteItem.where(organization: current_organization).alphabetized
end

def adjustment_params
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class ItemsController < ApplicationController
def index
@items = current_organization
.items
.includes(:kit, :line_items, :request_units, :item_category)
.includes(:line_items, :request_units, :item_category)
.alphabetized
.class_filter(filter_params)
.group('items.id')
@items = @items.active unless params[:include_inactive_items]

@item_categories = current_organization.item_categories.includes(:items).order('name ASC')
@kits = current_organization.kits.includes(kit_item: {line_items: :item})
@kits = current_organization.kit_items.includes(line_items: :item)
@storages = current_organization.storage_locations.active.order(id: :asc)

@include_inactive_items = params[:include_inactive_items]
Expand Down Expand Up @@ -139,7 +139,7 @@ def remove_category

def reporting_category_hint
item = current_organization.items.find(params[:id])
if item.kit_id
if item.is_a?(KitItem)
"Kits are reported based on their contents."
end
end
Expand Down
35 changes: 14 additions & 21 deletions app/controllers/kits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def show
end

def index
@kits = current_organization.kits.includes(kit_item: {line_items: :item}).class_filter(filter_params)
@kits = current_organization.kit_items.includes(line_items: :item).class_filter(filter_params)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: consider renaming kit_items to kits, and therefore these diffs will look different and the semantics are lifted to the rename

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main problem is having to rewrite the type for every entry in the items table that right now has KitItem. It doesn't save anything on the events migration (since we have to change the IDs from the kits table to the items table). I'm not too fussed about that, because it's easy to back out / fix if needed.

Claude estimates we'd save about 30 lines of churn by doing it all at once. I'm not really sure which way I'm leaning right now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awwaiid I put in the commit to rename. IMO things look much smoother now.

@inventory = View::Inventory.new(current_organization.id)
unless params[:include_inactive_items]
@kits = @kits.active
Expand All @@ -15,9 +15,8 @@ def index
def new
load_form_collections

@kit = current_organization.kits.new
@kit.kit_item = KitItem.new(organization: current_organization)
@kit.kit_item.line_items.build
@kit = current_organization.kit_items.new
@kit.line_items.build
end

def create
Expand All @@ -32,26 +31,22 @@ def create
.map { |error| formatted_error_message(error) }
.join(", ")

# Extract kit and item params separately since line_items belong to Item, not Kit
kit_only_params = kit_params.except(:line_items_attributes)
@kit = Kit.new(kit_only_params)
load_form_collections
@kit.kit_item ||= KitItem.new(organization: current_organization,
**kit_params.slice(:line_items_attributes))
@kit.kit_item.line_items.build if @kit.kit_item.line_items.empty?
@kit = current_organization.kit_items.new(kit_params)
@kit.line_items.build if @kit.line_items.empty?

render :new
end
end

def deactivate
@kit = current_organization.kits.find(params[:id])
@kit.deactivate
@kit = current_organization.kit_items.find(params[:id])
@kit.deactivate!
redirect_back_or_to(dashboard_path, notice: "Kit has been deactivated!")
end

def reactivate
@kit = current_organization.kits.find(params[:id])
@kit = current_organization.kit_items.find(params[:id])
if @kit.can_reactivate?
@kit.reactivate
redirect_back_or_to(dashboard_path, notice: "Kit has been reactivated!")
Expand All @@ -61,15 +56,15 @@ def reactivate
end

def allocations
@kit = current_organization.kits.find(params[:id])
@kit = current_organization.kit_items.find(params[:id])
@storage_locations = current_organization.storage_locations.active
@inventory = View::Inventory.new(current_organization.id)

load_form_collections
end

def allocate
@kit = current_organization.kits.find(params[:id])
@kit = current_organization.kit_items.find(params[:id])
@storage_location = current_organization.storage_locations.active.find(kit_adjustment_params[:storage_location_id])
@change_by = kit_adjustment_params[:change_by].to_i
begin
Expand All @@ -92,14 +87,12 @@ def load_form_collections
end

def kit_params
kit_params = params.require(:kit).permit(
params.require(:kit_item).permit(
:name,
:visible_to_partners,
:value_in_dollars
)
item_params = params.require(:kit_item)
.permit(line_items_attributes: [:item_id, :quantity, :_destroy])
kit_params.to_h.merge(item_params.to_h)
:value_in_dollars,
line_items_attributes: [:item_id, :quantity, :_destroy]
).to_h
end

def kit_adjustment_params
Expand Down
18 changes: 9 additions & 9 deletions app/events/kit_allocate_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class KitAllocateEvent < Event
def self.event_line_items(kit, storage_location, quantity)
items = kit.kit_item.line_items.map do |item|
def self.event_line_items(kit_item, storage_location, quantity)
items = kit_item.line_items.map do |item|
EventTypes::EventLineItem.new(
quantity: item.quantity * quantity,
item_id: item.item_id,
Expand All @@ -11,22 +11,22 @@ def self.event_line_items(kit, storage_location, quantity)
end
items.push(EventTypes::EventLineItem.new(
quantity: quantity,
item_id: kit.kit_item.id,
item_value_in_cents: kit.kit_item.value_in_cents,
item_id: kit_item.id,
item_value_in_cents: kit_item.value_in_cents,
to_storage_location: storage_location,
from_storage_location: nil
))
items
end

def self.publish(kit, storage_location, quantity)
def self.publish(kit_item, storage_location, quantity)
create!(
eventable: kit,
group_id: "kit-allocate-#{kit.id}-#{SecureRandom.hex}",
organization_id: kit.organization_id,
eventable: kit_item,
group_id: "kit-allocate-#{kit_item.id}-#{SecureRandom.hex}",
organization_id: kit_item.organization_id,
event_time: Time.zone.now,
data: EventTypes::InventoryPayload.new(
items: event_line_items(kit, storage_location, quantity)
items: event_line_items(kit_item, storage_location, quantity)
)
)
end
Expand Down
18 changes: 9 additions & 9 deletions app/events/kit_deallocate_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class KitDeallocateEvent < Event
def self.event_line_items(kit, storage_location, quantity)
items = kit.kit_item.line_items.map do |item|
def self.event_line_items(kit_item, storage_location, quantity)
items = kit_item.line_items.map do |item|
EventTypes::EventLineItem.new(
quantity: item.quantity * quantity,
item_id: item.item_id,
Expand All @@ -11,22 +11,22 @@ def self.event_line_items(kit, storage_location, quantity)
end
items.push(EventTypes::EventLineItem.new(
quantity: quantity,
item_id: kit.kit_item.id,
item_value_in_cents: kit.kit_item.value_in_cents,
item_id: kit_item.id,
item_value_in_cents: kit_item.value_in_cents,
from_storage_location: storage_location,
to_storage_location: nil
))
items
end

def self.publish(kit, storage_location, quantity)
def self.publish(kit_item, storage_location, quantity)
create(
eventable: kit,
group_id: "kit-deallocate-#{kit.id}-#{SecureRandom.hex}",
organization_id: kit.organization_id,
eventable: kit_item,
group_id: "kit-deallocate-#{kit_item.id}-#{SecureRandom.hex}",
organization_id: kit_item.organization_id,
event_time: Time.zone.now,
data: EventTypes::InventoryPayload.new(
items: event_line_items(kit, storage_location, quantity)
items: event_line_items(kit_item, storage_location, quantity)
)
)
end
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module EventsHelper
# KitItems (kit allocate/deallocate events) don't have their own route, so polymorphic
# routing would look for a non-existent kit_item_path. Link them to the kits page instead.
def eventable_path(eventable)
eventable.is_a?(KitItem) ? kit_path(eventable) : eventable
end
end
2 changes: 1 addition & 1 deletion app/helpers/kits_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module KitsHelper
def deactivate_kit_button(kit, inventory)
options = {class: "deactivate-kit-button"}
span_options = {}
unless kit.can_deactivate?(inventory)
unless kit.can_deactivate_or_delete?(inventory)
msg = "Can't deactivate while a storage location still has kits."
options[:enabled] = false
span_options = {title: msg, class: "tooltip-target"}
Expand Down
1 change: 1 addition & 0 deletions app/models/concrete_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
# organization_id :integer
#
class ConcreteItem < Item
validates :reporting_category, presence: true
end
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Event < ApplicationRecord
def no_intervening_snapshot
return if is_a?(SnapshotEvent)
return unless eventable.respond_to?(:organization)
return if eventable.is_a?(Kit)
return if eventable.is_a?(KitItem)

intervening = SnapshotEvent.intervening(eventable)
if intervening.present?
Expand Down
28 changes: 9 additions & 19 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class Item < ApplicationRecord
include Itemizable

after_initialize :set_default_distribution_quantity, if: :new_record?
after_update :update_associated_kit_name, if: -> { kit.present? }
before_destroy :validate_destroy, prepend: true

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, optional: true
belongs_to :kit, optional: true
belongs_to :item_category, optional: true

validates :additional_info, length: { maximum: 500 }
Expand All @@ -46,7 +44,6 @@ class Item < ApplicationRecord
validates :on_hand_recommended_quantity, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true
validates :on_hand_minimum_quantity, numericality: { greater_than_or_equal_to: 0 }
validates :package_size, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true
validates :reporting_category, presence: true, unless: proc { |i| i.kit }
validate -> { line_items_quantity_is_at_least(1) }

has_many :used_line_items, dependent: :destroy, class_name: "LineItem"
Expand All @@ -57,10 +54,6 @@ class Item < ApplicationRecord
has_many :request_units, class_name: "ItemUnit", dependent: :destroy

scope :active, -> { where(active: true) }

# :housing_a_kit are items which house a kit, NOT items is_in_kit
scope :housing_a_kit, -> { where.not(kit_id: nil) }
scope :loose, -> { where(kit_id: nil) }
scope :inactive, -> { where.not(active: true) }

scope :visible, -> { where(visible_to_partners: true) }
Expand Down Expand Up @@ -106,17 +99,17 @@ def in_request?

def is_in_kit?(kits = nil)
if kits
kits.any? { |k| k.kit_item.line_items.map(&:item_id).include?(id) }
kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
else
organization.kits
organization.kit_items
.active
.joins(kit_item: :line_items)
.joins(:line_items)
.where(line_items: { item_id: id}).any?
end
end

def can_delete?(inventory = nil, kits = nil)
can_deactivate_or_delete?(inventory, kits) && used_line_items.none? && !barcode_count&.positive? && !in_request? && kit.blank?
can_deactivate_or_delete?(inventory, kits) && used_line_items.none? && !barcode_count&.positive? && !in_request?
end

# @return [Boolean]
Expand All @@ -141,11 +134,7 @@ def deactivate!
unless can_deactivate_or_delete?
raise "Cannot deactivate item - it is in a storage location or kit!"
end
if kit
kit.deactivate
else
update!(active: false)
end
update!(active: false)
end

# @return [String]
Expand Down Expand Up @@ -199,10 +188,11 @@ def sync_request_units!(unit_ids)
private

def set_default_distribution_quantity
self.distribution_quantity ||= kit_id.present? ? 1 : 50
self.distribution_quantity ||= default_distribution_quantity
end

def update_associated_kit_name
kit.update(name: name)
# Overridden in KitItem (kits default to 1).
def default_distribution_quantity
50
end
end
52 changes: 0 additions & 52 deletions app/models/kit.rb

This file was deleted.

Loading
Loading