Skip to content

Commit 7bf1805

Browse files
4682 sort partners alphabetical (#4963)
* sorting partners by name + category dropdown disappearing fix * Update ordering tests * Remove controller test file and add request test --------- Co-authored-by: Bella Deanhardt <[email protected]>
1 parent 5416b83 commit 7bf1805

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

app/controllers/admin/partners_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Admin::PartnersController < AdminController
22
def index
3-
@partners = Partner.all.includes(:organization)
3+
@partners = Partner.all.includes(:organization).order("LOWER(name)")
44
end
55

66
def show

app/controllers/items_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def create
4444
@base_items = BaseItem.without_kit.alphabetized
4545
# Define a @item to be used in the `new` action to be rendered with
4646
# the provided parameters. This is required to render the page again
47-
# with the error + the invalid parameters
47+
# with the error + the invalid parameters.
48+
@item_categories = current_organization.item_categories.order('name ASC') # Load categories here
4849
@item = current_organization.items.new(item_params)
4950
flash.now[:error] = result.error.record.errors.full_messages.to_sentence
5051
render action: :new

spec/requests/admin/partners_requests_spec.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,42 @@
88
sign_in(create(:super_admin, organization: nil))
99
end
1010

11-
let(:partner) { create(:partner) }
11+
let!(:partner1) { create(:partner, name: "Bravo", organization: organization) }
12+
let!(:partner2) { create(:partner, name: "alpha", organization: organization) }
13+
let!(:partner3) { create(:partner, name: "Zeus", organization: organization) }
1214

1315
describe "GET #index" do
1416
it "returns http success" do
1517
get admin_partners_path
1618
expect(response).to be_successful
1719
end
20+
21+
it "assigns partners ordered by name (case-insensitive)" do
22+
get admin_partners_path
23+
expect(assigns(:partners)).to eq([partner2, partner1, partner3])
24+
end
1825
end
1926

2027
describe "GET #show" do
2128
it "returns http success" do
22-
get admin_partner_path(id: partner.id)
29+
get admin_partner_path(id: partner1.id)
2330
expect(response).to be_successful
2431
end
2532
end
2633

2734
describe "GET #edit" do
2835
it "returns http success" do
29-
get edit_admin_partner_path(id: partner.id)
36+
get edit_admin_partner_path(id: partner1.id)
3037
expect(response).to be_successful
3138
end
3239
end
3340

3441
describe "PUT #update" do
3542
context "successful save" do
36-
subject { put admin_partner_path(id: partner.id, partner: { name: "Bar" }) }
43+
subject { put admin_partner_path(id: partner1.id, partner: { name: "Bar" }) }
3744

3845
it "updates partner" do
39-
expect { subject }.to change { partner.reload.name }.to "Bar"
46+
expect { subject }.to change { partner1.reload.name }.to "Bar"
4047
end
4148

4249
it "redirects" do
@@ -46,7 +53,7 @@
4653
end
4754

4855
context "unsuccessful save due to empty params" do
49-
subject { put admin_partner_path(id: partner.id, partner: { name: "" }) }
56+
subject { put admin_partner_path(id: partner1.id, partner: { name: "" }) }
5057

5158
it "renders #edit template with error message" do
5259
expect(subject).to render_template(:edit)

spec/requests/items_requests_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111

112112
describe "CREATE #create" do
113113
let!(:existing_item) { create(:item, organization: organization, name: "Really Good Item") }
114+
let!(:item_category) { create(:item_category, organization: organization, name: "Test Category") }
114115

115116
describe "with an already existing item name" do
116117
let(:item_params) do
@@ -134,6 +135,42 @@
134135
end
135136
end
136137

138+
describe "with invalid parameters" do
139+
let(:invalid_item_params) do
140+
{
141+
item: {
142+
name: "", # Invalid name
143+
partner_key: create(:base_item).partner_key,
144+
value_in_cents: -100, # Invalid value
145+
package_size: nil,
146+
distribution_quantity: nil
147+
}
148+
}
149+
end
150+
151+
let!(:category1) { FactoryBot.create(:item_category, name: 'Bananas', organization: organization) }
152+
let!(:category2) { FactoryBot.create(:item_category, name: 'Apples', organization: organization) }
153+
let!(:category3) { FactoryBot.create(:item_category, name: 'Umbrella', organization: organization) }
154+
155+
let(:item_categories) { [category1, category2, category3] }
156+
157+
it "loads and displays the item categories when rendering new" do
158+
# Attempt to create an item with invalid parameters
159+
post items_path, params: invalid_item_params
160+
161+
# Expect to render the new template
162+
expect(response).to render_template(:new)
163+
164+
# Ensure the item categories are assigned in the controller
165+
expect(assigns(:item_categories)).to eq([category2, category1, category3])
166+
167+
# Verify the categories are included in the response body
168+
item_categories.each do |category|
169+
expect(response.body).to include("<option value=\"#{category.id}\">#{category.name}</option>")
170+
end
171+
end
172+
end
173+
137174
describe 'GET #index' do
138175
let(:storage_location) { create(:storage_location, organization: organization) }
139176
let!(:item) { create(:item, organization: organization, name: "ACTIVEITEM") }

0 commit comments

Comments
 (0)