Skip to content

Commit

Permalink
Fix various rspecs
Browse files Browse the repository at this point in the history
* KitCreateService shouldn't try to find organization until calling valid?
* rename Item.line_items association to Item.contained_in_line_items to avoid name conflict, add rspec
  • Loading branch information
jimmyli97 committed Aug 16, 2024
1 parent f0e3fa3 commit 811db7a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Item < ApplicationRecord

validate :has_no_line_items, if: -> { kit.blank? }

has_many :line_items, dependent: :destroy
has_many :contained_in_line_items, class_name: "LineItem", dependent: :destroy
has_many :inventory_items, dependent: :destroy
has_many :barcode_items, as: :barcodeable, dependent: :destroy
has_many :storage_locations, through: :inventory_items
Expand Down
8 changes: 5 additions & 3 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ def other

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)
.joins(:contained_in_line_items)
.merge(LineItem.where(created_at: date_start..date_end))
.group(:name)
end

def top(limit = 5)
order('count(line_items.id) DESC')
.joins(:contained_in_line_items)
.limit(limit)
end

def bottom(limit = 5)
order('count(line_items.id) ASC')
.joins(:contained_in_line_items)
.limit(limit)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/historical_trend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(organization_id, type)
def series
# Preload line_items with a single query to avoid N+1 queries.
items_with_line_items = @organization.items.active
.includes(:line_items)
.includes(:contained_in_line_items)
.where(line_items: {itemizable_type: @type, created_at: 1.year.ago.beginning_of_month..Time.current})
.order(:name)

Expand All @@ -17,7 +17,7 @@ def series
items_with_line_items.each_with_object([]) do |item, array_of_items|
dates = default_dates.deep_dup

item.line_items.each do |line_item|
item.contained_in_line_items.each do |line_item|
month = line_item.created_at.month
index = month_offset.index(month) + 1
dates[index] = dates[index] + line_item.quantity
Expand Down
11 changes: 9 additions & 2 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@

let(:organization) { create(:organization) }

describe 'Assocations >' do
describe 'Associations >' do
it { should belong_to(:item_category).optional }
it "should return line items containing the item" do
item = create(:item)
create(:line_item, item: item)
expect(item.contained_in_line_items.count).to eq(1)
end
end
context "Validations >" do
it "requires a unique name" do
Expand All @@ -44,8 +49,10 @@
it { should validate_numericality_of(:on_hand_recommended_quantity).is_greater_than_or_equal_to(0) }

context "Doesn't house a kit >" do
it "ensures associated line_items are empty" do
it "ensures associated itemizable line_items don't exist without invalidating associated line_items" do
item = create(:item)
create(:line_item, item: item)
expect(item).to be_valid
item.line_items << build(:line_item, quantity: 1)
expect(item).not_to be_valid
end
Expand Down

0 comments on commit 811db7a

Please sign in to comment.