Skip to content

Commit d73c67a

Browse files
authored
Merge pull request #5032 from coalest/refactor-methods-to-storage-location-model
Refactor Audit/Adjustment class methods to StorageLocation scopes
2 parents 62c4009 + 227a626 commit d73c67a

8 files changed

Lines changed: 38 additions & 32 deletions

File tree

app/controllers/adjustments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def index
1414
.during(helpers.selected_range)
1515
@paginated_adjustments = @adjustments.page(params[:page])
1616

17-
@storage_locations = Adjustment.storage_locations_adjusted_for(current_organization).uniq
17+
@storage_locations = StorageLocation.with_adjustments_for(current_organization).select(:id, :name)
1818
@users = current_organization.users
1919

2020
respond_to do |format|

app/controllers/audits_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AuditsController < ApplicationController
66
def index
77
@selected_location = filter_params[:at_location]
88
@audits = current_organization.audits.class_filter(filter_params)
9-
@storage_locations = Audit.storage_locations_audited_for(current_organization).uniq
9+
@storage_locations = StorageLocation.with_audits_for(current_organization).select(:id, :name)
1010
end
1111

1212
def show

app/models/adjustment.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class Adjustment < ApplicationRecord
2626

2727
validate :storage_locations_belong_to_organization
2828

29-
def self.storage_locations_adjusted_for(organization)
30-
includes(:storage_location).joins(:storage_location).where(organization_id: organization.id, storage_location: {discarded_at: nil}).collect(&:storage_location).sort
31-
end
32-
3329
def split_difference
3430
pre_adjustment = line_items.partition { |line_item| line_item.quantity.positive? }
3531
increasing_adjustment, decreasing_adjustment = pre_adjustment.map { |adjustment| Adjustment.new(line_items: adjustment) }

app/models/audit.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class Audit < ApplicationRecord
3131
validate :line_items_unique_by_item_id
3232
validate :user_is_organization_admin_of_the_organization
3333

34-
def self.storage_locations_audited_for(organization)
35-
includes(:storage_location).joins(:storage_location).where(organization_id: organization.id, storage_location: {discarded_at: nil}).collect(&:storage_location).sort
36-
end
37-
3834
def self.finalized_since?(itemizable, *location_ids)
3935
item_ids = itemizable.line_items.pluck(:item_id)
4036
where(status: "finalized")

app/models/storage_location.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class StorageLocation < ApplicationRecord
2727
].freeze
2828

2929
belongs_to :organization
30+
has_many :adjustments, dependent: :destroy
31+
has_many :audits, dependent: :destroy
3032
has_many :inventory_items, -> { includes(:item).order("items.name") },
3133
inverse_of: :storage_location,
3234
dependent: :destroy
@@ -54,6 +56,12 @@ class StorageLocation < ApplicationRecord
5456

5557
scope :alphabetized, -> { order(:name) }
5658
scope :active, -> { where(discarded_at: nil) }
59+
scope :with_adjustments_for, ->(organization) {
60+
joins(:adjustments).where(organization_id: organization.id).distinct.active.alphabetized
61+
}
62+
scope :with_audits_for, ->(organization) {
63+
joins(:audits).where(organization_id: organization.id).distinct.active.alphabetized
64+
}
5765
scope :with_transfers_to, ->(organization) {
5866
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
5967
}

spec/models/adjustment_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@
5757
end
5858

5959
context "Methods >" do
60-
it "`self.storage_locations_adjusted_for` returns only storage_locations that are used in adjustments for one org" do
61-
storage_location1 = create(:storage_location, organization: organization)
62-
storage_location2 = create(:storage_location, organization: organization)
63-
create(:storage_location, organization: organization)
64-
storage_location4 = create(:storage_location, organization: create(:organization))
65-
create(:adjustment, storage_location: storage_location1, organization: organization)
66-
create(:adjustment, storage_location: storage_location2, organization: organization)
67-
create(:adjustment, storage_location: storage_location4, organization: storage_location4.organization)
68-
expect(Adjustment.storage_locations_adjusted_for(organization).to_a).to match_array([storage_location1, storage_location2])
69-
end
70-
7160
describe "split_difference" do
7261
it "returns two adjustment objects" do
7362
item = create(:item)

spec/models/audit_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,6 @@
103103
end
104104

105105
context "Methods >" do
106-
it "`self.storage_locations_audited_for` returns only storage_locations that are used for one org" do
107-
storage_location1 = create(:storage_location, organization: organization)
108-
storage_location2 = create(:storage_location, organization: organization)
109-
create(:storage_location, organization: organization)
110-
storage_location4 = create(:storage_location, organization: create(:organization))
111-
create(:audit, storage_location: storage_location1, organization: organization)
112-
create(:audit, storage_location: storage_location2, organization: organization)
113-
create(:audit, storage_location: storage_location4, organization: storage_location4.organization)
114-
expect(Audit.storage_locations_audited_for(organization).to_a).to match_array([storage_location1, storage_location2])
115-
end
116-
117106
it "`self.finalized_since?` returns true iff some finalized audit occurred after itemizable created_at that shares item for location(s)" do
118107
storage_location1 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)
119108
storage_location2 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)

spec/models/storage_location_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@
7979
end
8080
end
8181

82+
context "Scopes >" do
83+
describe "with_audits_for" do
84+
it "returns only storage locations that are used for one org" do
85+
storage_location1 = create(:storage_location, organization: organization)
86+
storage_location2 = create(:storage_location, organization: organization)
87+
create(:storage_location, organization: organization)
88+
storage_location4 = create(:storage_location, organization: create(:organization))
89+
create(:audit, storage_location: storage_location1, organization: organization)
90+
create(:audit, storage_location: storage_location2, organization: organization)
91+
create(:audit, storage_location: storage_location4, organization: storage_location4.organization)
92+
expect(StorageLocation.with_audits_for(organization).to_a).to match_array([storage_location1, storage_location2])
93+
end
94+
end
95+
96+
describe "with_adjustments_for" do
97+
it "returns only storage locations that are used in adjustments for one org" do
98+
storage_location1 = create(:storage_location, organization: organization)
99+
storage_location2 = create(:storage_location, organization: organization)
100+
create(:storage_location, organization: organization)
101+
storage_location4 = create(:storage_location, organization: create(:organization))
102+
create(:adjustment, storage_location: storage_location1, organization: organization)
103+
create(:adjustment, storage_location: storage_location2, organization: organization)
104+
create(:adjustment, storage_location: storage_location4, organization: storage_location4.organization)
105+
expect(StorageLocation.with_adjustments_for(organization).to_a).to match_array([storage_location1, storage_location2])
106+
end
107+
end
108+
end
109+
82110
context "Methods >" do
83111
let(:item) { create(:item) }
84112
subject { create(:storage_location, :with_items, item_quantity: 10, item: item, organization: organization) }

0 commit comments

Comments
 (0)