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

Fix bullet warnings in dashboard page #2413

Merged
Merged
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
12 changes: 6 additions & 6 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class DashboardController < ApplicationController
def index
setup_date_range_picker

@donations = current_organization.donations.includes(:diaper_drive, line_items: [:item]).during(helpers.selected_range)
@donations = current_organization.donations.during(helpers.selected_range)
@recent_donations = @donations.recent
@purchases = current_organization.purchases.includes(:vendor, line_items: [:item]).during(helpers.selected_range)
@recent_purchases = @purchases.recent
@recent_distributions = current_organization.distributions.includes(:partner, line_items: [:item]).during(helpers.selected_range).recent
@purchases = current_organization.purchases.during(helpers.selected_range)
@recent_purchases = @purchases.recent.includes(:vendor)
@recent_distributions = current_organization.distributions.includes(:partner).during(helpers.selected_range).recent

if Flipper.enabled?(:itemized_distributions, current_user)
@itemized_distributions = current_organization.distributions.includes(line_items: [:item]).during(helpers.selected_range)
@itemized_distributions = current_organization.distributions.includes(:line_items).during(helpers.selected_range)
@onhand_quantities = current_organization.inventory_items.group("items.name").sum(:quantity)
@onhand_minimums = current_organization.inventory_items
.group("items.name")
Expand All @@ -24,7 +24,7 @@ def index

# calling .recent on recent donations by manufacturers will only count the last 3 donations
# which may not make sense when calculating total count using a date range
@recent_donations_from_manufacturers = current_organization.donations.includes(:manufacturer, line_items: [:item]).during(helpers.selected_range).by_source(:manufacturer)
@recent_donations_from_manufacturers = current_organization.donations.during(helpers.selected_range).by_source(:manufacturer)
@top_manufacturers = current_organization.manufacturers.by_donation_count
end
end
5 changes: 3 additions & 2 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ class Donation < ApplicationRecord
# TODO: move this to Organization.donations as an extension
scope :during, ->(range) { where(donations: { issued_at: range }) }
scope :by_source, ->(source) {
source = SOURCES[source] if source.is_a?(Symbol)
where(source: source)
return where(source: source) unless source.is_a?(Symbol)

includes(source).where(source: SOURCES[source])
}
scope :recent, ->(count = 3) { order(issued_at: :desc).limit(count) }

Expand Down
38 changes: 34 additions & 4 deletions spec/models/donation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,47 @@
end

describe "by_source >" do
subject(:by_source) { Donation.by_source(source).count }

before(:each) do
create(:donation, source: Donation::SOURCES[:misc])
create(:diaper_drive_donation)
end

it "returns all donations with the provided source" do
expect(Donation.by_source(Donation::SOURCES[:diaper_drive]).count).to eq(1)
context "when source is not a symbol" do
context "when source comes from the SOURCES hash" do
let(:source) { Donation::SOURCES[:diaper_drive] }

it "returns all donations with the provided source" do
is_expected.to eq(1)
end
end

context "when source is invalid" do
let(:source) { "Invalid String" }

it "does not throw errors, returns no results" do
is_expected.to be_zero
end
end
end

it "allows a symbol as an argument, referencing the SOURCES hash" do
expect(Donation.by_source(:diaper_drive).count).to eq(1)
context "when source is a symbol" do
context "when source is valid" do
let(:source) { :diaper_drive }

it "allows a symbol as an argument, referencing the SOURCES hash" do
is_expected.to eq(1)
end
end

context "when source is invalid" do
let(:source) { :invalid }

it "does not throw errors, returns no results" do
is_expected.to be_zero
end
end
end
end
end
Expand Down