Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/bundler/coverband-6.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
awwaiid authored Mar 1, 2024
2 parents a913704 + 37f4a62 commit cbcbdb7
Show file tree
Hide file tree
Showing 34 changed files with 479 additions and 199 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ group :development do
# Open sent e-mails in the browser instead of trying to send to a real mail server.
gem "letter_opener"
# Used as a dependency for Guard.
gem "listen", "~> 3.8.0"
gem "listen", "~> 3.9.0"
# Generate a diagram based on Rails models.
gem "rails-erd"
# Allows to create a console in the browser.
Expand All @@ -198,7 +198,7 @@ group :test do
# More concise test ("should") matchers
gem 'shoulda-matchers', '~> 6.1'
# Mock HTTP requests and ensure they are not called during tests.
gem "webmock", "~> 3.21"
gem "webmock", "~> 3.23"
# Interface capybara to chrome headless
gem "cuprite"
end
Expand Down
22 changes: 11 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ GEM
discard (1.3.0)
activerecord (>= 4.2, < 8)
docile (1.4.0)
dotenv (3.0.2)
dotenv-rails (3.0.2)
dotenv (= 3.0.2)
dotenv (3.1.0)
dotenv-rails (3.1.0)
dotenv (= 3.1.0)
railties (>= 6.1)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -239,7 +239,7 @@ GEM
concurrent-ruby (~> 1.1)
webrick (~> 1.7)
websocket-driver (>= 0.6, < 0.8)
ffi (1.15.5)
ffi (1.16.3)
filterrific (5.2.5)
flipper (1.2.2)
concurrent-ruby (< 2)
Expand Down Expand Up @@ -309,7 +309,7 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
knapsack_pro (6.0.4)
knapsack_pro (7.0.0)
rake
language_server-protocol (3.17.0.3)
launchy (2.5.2)
Expand All @@ -320,7 +320,7 @@ GEM
libv8-node (18.16.0.0-x86_64-darwin)
libv8-node (18.16.0.0-x86_64-linux)
lint_roller (1.1.0)
listen (3.8.0)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lograge (0.14.0)
Expand Down Expand Up @@ -451,7 +451,7 @@ GEM
puma (6.4.2)
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack (2.2.8.1)
rack-protection (3.1.0)
rack (~> 2.2, >= 2.2.4)
rack-test (2.1.0)
Expand Down Expand Up @@ -620,7 +620,7 @@ GEM
activerecord (>= 5.2)
terser (1.2.0)
execjs (>= 0.3.0, < 3)
thor (1.3.0)
thor (1.3.1)
tilt (2.2.0)
timeout (0.4.0)
ttfunk (1.7.0)
Expand All @@ -640,7 +640,7 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webmock (3.21.2)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
Expand Down Expand Up @@ -707,7 +707,7 @@ DEPENDENCIES
kaminari
knapsack_pro
letter_opener
listen (~> 3.8.0)
listen (~> 3.9.0)
lograge
magic_test
matrix
Expand Down Expand Up @@ -748,7 +748,7 @@ DEPENDENCIES
terser
turbo-rails
web-console
webmock (~> 3.21)
webmock (~> 3.23)

BUNDLED WITH
2.5.4
10 changes: 2 additions & 8 deletions app/controllers/partners/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def new
family = current_partner.families.find_by!(id: params[:family_id])
@child = family.children.new

requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
@formatted_requestable_items = requestable_items.map do |rt|
[rt.name, rt.id]
end
@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
end

def active
Expand All @@ -50,10 +47,7 @@ def active

def edit
@child = current_partner.children.find_by(id: params[:id])
requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
@formatted_requestable_items = requestable_items.map do |rt|
[rt.name, rt.id]
end
@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
end

def create
Expand Down
11 changes: 3 additions & 8 deletions app/controllers/partners/individuals_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ module Partners
class IndividualsRequestsController < BaseController
def new
@request = FamilyRequest.new({}, initial_items: 1)

requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
@formatted_requestable_items = requestable_items.map do |rt|
[rt.name, rt.id]
end.sort
@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
end

def create
Expand All @@ -24,9 +20,8 @@ def create
else
@request = FamilyRequest.new({}, initial_items: 1)
@errors = create_service.errors
@requestable_items = Organization.find(current_partner.organization_id).valid_items.map do |item|
[item[:name], item[:id]]
end.sort

@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call

Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_user.id} reason=#{@errors.full_messages}")

Expand Down
10 changes: 3 additions & 7 deletions app/controllers/partners/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ def new
@partner_request = ::Request.new
@partner_request.item_requests.build

requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
@formatted_requestable_items = requestable_items.map do |rt|
[rt.name, rt.id]
end.sort
@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
end

def show
Expand All @@ -36,9 +33,8 @@ def create

@partner_request = create_service.partner_request
@errors = create_service.errors
@formatted_requestable_items = Organization.find(current_partner.organization_id).valid_items.map do |item|
[item[:name], item[:id]]
end.sort

@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call

Rails.logger.info("[Request Creation Failure] partner_user_id=#{current_user.id} reason=#{@errors.full_messages}")

Expand Down
6 changes: 5 additions & 1 deletion app/controllers/product_drives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ def index
setup_date_range_picker
@product_drives = current_organization
.product_drives
.includes(donations: {line_items: :item})
.class_filter(filter_params)
.within_date_range(@selected_date_range)
.order(start_date: :desc)
@item_categories = current_organization.item_categories
@selected_name_filter = filter_params[:by_name]
@selected_item_category = filter_params[:by_item_category_id]

respond_to do |format|
format.html
Expand Down Expand Up @@ -55,6 +58,7 @@ def edit

def show
@selected_name_filter = filter_params[:by_name]
@selected_item_category = filter_params[:by_item_category_id]
@product_drive = current_organization.product_drives.includes(:donations).find(params[:id])
end

Expand Down Expand Up @@ -99,6 +103,6 @@ def date_range_filter
def filter_params
return {} unless params.key?(:filters)

params.require(:filters).permit(:by_name)
params.require(:filters).permit(:by_name, :by_item_category_id)
end
end
1 change: 0 additions & 1 deletion app/javascript/controllers/request_item_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default class extends Controller {
}

removeItem(event) {
console.log(event.target);
const wrapper = event.target.closest("tr");
const removeSoft = event.target.dataset.removeSoft === "false";

Expand Down
10 changes: 4 additions & 6 deletions app/javascript/controllers/turbo_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {

connect() {

/**
* Scrolls to the top after turbo:submit-end event if the
* Scrolls to the top after turbo:submit-end event if the
* request was unsuccessful with status code of 4xx or 5xx.
*/
document.addEventListener("turbo:submit-end", (event) => {
let status = event.detail.fetchResponse.response.status

if (status >= 400) {
scrollTo(0, 0)
if (!event.detail.success) {
scrollTo(0, 0);
}
})
}
Expand Down
13 changes: 13 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ def self.daily_quantities_by_source(start, stop)
.sum("line_items.quantity")
end

def details
case source
when SOURCES[:product_drive]
product_drive.name
when SOURCES[:manufacturer]
manufacturer.name
when SOURCES[:donation_site]
donation_site.name
when SOURCES[:misc]
comment&.truncate(25, separator: /\s/)
end
end

def remove(item)
# doing this will handle either an id or an object
item_id = item.to_i
Expand Down
28 changes: 20 additions & 8 deletions app/models/product_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ProductDrive < ApplicationRecord
include Filterable

scope :by_name, ->(name_filter) { where(name: name_filter) }
scope :by_item_category_id, ->(item_category_id) {
joins(donations: {line_items: :item})
.where(item: { item_category_id: item_category_id })
}

scope :within_date_range, ->(search_range) {
search_dates = search_date_range(search_range)
Expand Down Expand Up @@ -76,15 +80,23 @@ def item_quantities_by_name_and_date(date_range)
end
end

def donation_quantity_by_date(date_range)
donations.joins(:line_items)
.during(date_range)
.sum('line_items.quantity')
def donation_quantity_by_date(date_range, item_category_id = nil)
query = donations.during(date_range)
query = if item_category_id.present?
query.joins(line_items: :item).where(item: {item_category_id: item_category_id})
else
query.joins(:line_items)
end
query.sum('line_items.quantity')
end

def distinct_items_count_by_date(date_range)
donations.joins(:line_items)
.during(date_range)
.select('line_items.item_id').distinct.count
def distinct_items_count_by_date(date_range, item_category_id = nil)
query = donations.during(date_range)
query = if item_category_id.present?
query.joins(line_items: :item).where(item: {item_category_id: item_category_id})
else
query.joins(:line_items)
end
query.select('line_items.item_id').distinct.count
end
end
6 changes: 3 additions & 3 deletions app/services/exports/export_donations_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def headers_with_indexes
def base_table
{
"Source" => ->(donation) {
donation.source_view
donation.source
},
"Date" => ->(donation) {
donation.issued_at.strftime("%F")
},
"Donation Site" => ->(donation) {
donation.donation_site.try(:name)
"Details" => ->(donation) {
donation.details
},
"Storage Location" => ->(donation) {
donation.storage_view
Expand Down
41 changes: 17 additions & 24 deletions app/services/historical_trend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@ def initialize(organization_id, type)
end

def series
items = []

@organization.items.active.sort.each do |item|
next if item.line_items.where(itemizable_type: @type, item: item).blank?

month_offset = [*1..12].rotate(Time.zone.today.month)

dates = (1..12).index_with { |i| 0 }

total_items(item.line_items, @type).each do |line_item|
month = line_item.dig(0).to_date.month
dates[(month_offset.index(month) + 1)] = line_item.dig(1)
# Preload line_items with a single query to avoid N+1 queries.
items_with_line_items = @organization.items.active
.includes(:line_items)
.where(line_items: {itemizable_type: @type, created_at: 1.year.ago.beginning_of_month..Time.current})
.order(:name)

month_offset = [*1..12].rotate(Time.zone.today.month)
default_dates = (1..12).index_with { |i| 0 }

items_with_line_items.each_with_object([]) do |item, array_of_items|
dates = default_dates.deep_dup

item.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
end

items << {name: item.name, data: dates.values, visible: false}
array_of_items << {name: item.name, data: dates.values, visible: false} unless dates.values.sum.zero?
end

items.sort_by { |hsh| hsh[:name] }
end

private

def total_items(line_items, type)
line_items.where(created_at: 1.year.ago.beginning_of_month..Time.current)
.where(itemizable_type: type)
.group_by_month(:created_at)
.sum(:quantity)
end
end
8 changes: 6 additions & 2 deletions app/services/partner_fetch_requestable_items_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ def initialize(partner_id:)
end

def call
return organization.items.active.visible.sort if partner.partner_group.blank?
requestable_items = if partner.partner_group.blank?
organization.items.active.visible
else
partner.requestable_items.active.visible
end

partner.requestable_items.active.visible
requestable_items.map { |item| [item.name, item.id] }.sort
end

private
Expand Down
Loading

0 comments on commit cbcbdb7

Please sign in to comment.