Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4682 sort partners alphabetical #4963

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/controllers/admin/partners_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Admin::PartnersController < AdminController
def index
@partners = Partner.all.includes(:organization)
@partners = Partner.all.includes(:organization).order("LOWER(name)")
end

def show
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def create
@base_items = BaseItem.without_kit.alphabetized
# Define a @item to be used in the `new` action to be rendered with
# the provided parameters. This is required to render the page again
# with the error + the invalid parameters
# with the error + the invalid parameters.
@item_categories = current_organization.item_categories.order('name ASC') # Load categories here
@item = current_organization.items.new(item_params)
flash.now[:error] = result.error.record.errors.full_messages.to_sentence
render action: :new
Expand Down
48 changes: 48 additions & 0 deletions spec/controllers/admin/partners_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
RSpec.describe Admin::PartnersController, type: :controller do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use a request test instead of a controller test? We really should be moving over the existing ones.

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

# Use the super_admin instead of organization_admin
let(:super_admin) { create(:super_admin) }

# Ensure partners are created before the test
let!(:partner1) { create(:partner, name: "Bravo", organization: organization) }
let!(:partner2) { create(:partner, name: "alpha", organization: organization) }
let!(:partner3) { create(:partner, name: "Zeus", organization: organization) }

let(:default_params) do
{organization_id: organization.id}
end

context "When logged in as a super admin" do
before do
sign_in(super_admin) # Sign in as the super_admin
end

describe "GET #index" do
it "returns http success" do
get :index
expect(response).to be_successful
end

it "assigns partners ordered by name (case-insensitive)" do
get :index
expect(assigns(:partners)).to eq([partner2, partner1, partner3])
end
end
end

context "When logged in as a non-admin user" do
let(:user) { create(:user) }

before do
sign_in(user)
end

describe "GET #index" do
it "redirects to login or unauthorized page" do
get :index
expect(response).to be_redirect
end
end
end
end
37 changes: 37 additions & 0 deletions spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

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

describe "with an already existing item name" do
let(:item_params) do
Expand All @@ -134,6 +135,42 @@
end
end

describe "with invalid parameters" do
let(:invalid_item_params) do
{
item: {
name: "", # Invalid name
partner_key: create(:base_item).partner_key,
value_in_cents: -100, # Invalid value
package_size: nil,
distribution_quantity: nil
}
}
end

let!(:category1) { FactoryBot.create(:item_category, name: 'Bananas', organization: organization) }
let!(:category2) { FactoryBot.create(:item_category, name: 'Apples', organization: organization) }
let!(:category3) { FactoryBot.create(:item_category, name: 'Umbrella', organization: organization) }

let(:item_categories) { [category1, category2, category3] }

it "loads and displays the item categories when rendering new" do
# Attempt to create an item with invalid parameters
post items_path, params: invalid_item_params

# Expect to render the new template
expect(response).to render_template(:new)

# Ensure the item categories are assigned in the controller
expect(assigns(:item_categories)).to eq([category2, category1, category3])

# Verify the categories are included in the response body
item_categories.each do |category|
expect(response.body).to include("<option value=\"#{category.id}\">#{category.name}</option>")
end
end
end

describe 'GET #index' do
let(:storage_location) { create(:storage_location, organization: organization) }
let!(:item) { create(:item, organization: organization, name: "ACTIVEITEM") }
Expand Down