Skip to content

Commit 5e80e11

Browse files
authored
Merge pull request #5099 from rubyforgood/packs-fixes
Custom Units / Packs fixes
2 parents ace0ee3 + 287bf0b commit 5e80e11

File tree

18 files changed

+70
-28
lines changed

18 files changed

+70
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ package-lock.json
8888
out/
8989

9090
.vscode/
91+
.aider*

app/controllers/partners/requests_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ def fetch_items
8181
@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: partner.id).call
8282
if Flipper.enabled?(:enable_packs)
8383
# hash of (item ID => hash of (request unit name => request unit plural name))
84-
@item_units = Item.where(id: @requestable_items.to_h.values).to_h do |i|
85-
[i.id, i.request_units.to_h { |u| [u.name, u.name.pluralize] }]
84+
item_ids = @requestable_items.to_h.values
85+
if item_ids.present?
86+
@item_units = Item.where(id: item_ids).to_h do |i|
87+
[i.id, i.request_units.to_h { |u| [u.name, u.name.pluralize] }]
88+
end
8689
end
8790
end
8891
end

app/javascript/controllers/item_units_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class extends Controller {
4343
this.requestSelectTarget.style.display = 'inline';
4444
this.clearOptions()
4545
this.addOption('-1', 'Please select a unit')
46-
this.addOption('', 'Units')
46+
this.addOption('', 'units')
4747
for (const [index, [name, displayName]] of Object.entries(Object.entries(units))) {
4848
this.addOption(name, displayName)
4949
}

app/javascript/controllers/select2_controller.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ export default class extends Controller {
2929
}
3030
}
3131
});
32+
33+
/**
34+
* This is a workaround to prevent select2 from filling in an existing
35+
* value even when you try to remove everything. This solution was found at
36+
* https://github.com/select2/select2/issues/3320#issuecomment-1440268574
37+
*/
38+
if ($(this.element).prop('multiple')) {
39+
select2.on("select2:unselecting", function (e) {
40+
$(this).on("select2:opening", function (ev) {
41+
ev.preventDefault();
42+
$(this).off("select2:opening");
43+
});
44+
});
45+
}
3246
}
3347
}

app/models/partners/item_request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def request_unit_is_supported
3838

3939
def name_with_unit(quantity_override = nil)
4040
if Flipper.enabled?(:enable_packs) && request_unit.present?
41-
"#{item.name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
41+
"#{item&.name || name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
4242
else
43-
item.name
43+
item&.name || name
4444
end
4545
end
4646
end

app/models/unit.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
#
1111
class Unit < ApplicationRecord
1212
belongs_to :organization
13-
validates :name, uniqueness: {scope: :organization}
13+
validates :name,
14+
uniqueness: {scope: :organization},
15+
format: {without: /\Aunits?\z/i, message: "'unit' is reserved."}
1416
end

app/pdfs/picklists_pdf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def data_with_units(line_items)
145145
data + line_items.map do |line_item|
146146
[line_item.name,
147147
line_item.quantity,
148-
line_item.request_unit&.capitalize&.pluralize(line_item.quantity),
148+
line_item.request_unit&.pluralize(line_item.quantity),
149149
"[ ]",
150150
""]
151151
end

app/services/organization_update_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ def update(organization, params)
1818
org_params["partner_form_fields"] = org_params["partner_form_fields"].compact_blank
1919
end
2020

21-
if Flipper.enabled?(:enable_packs) && org_params[:request_unit_names]
21+
if Flipper.enabled?(:enable_packs)
22+
request_unit_names = org_params[:request_unit_names] || []
2223
# Find or create units for the organization
23-
request_unit_ids = org_params[:request_unit_names].compact_blank.map do |request_unit_name|
24+
request_unit_ids = request_unit_names.compact_blank.map do |request_unit_name|
2425
Unit.find_or_create_by(organization: organization, name: request_unit_name).id
2526
end
2627
org_params.delete(:request_unit_names)

app/views/organizations/edit.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<%= f.input :distribute_monthly, label: 'Does your bank distribute monthly?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
110110

111111
<% if Flipper.enabled?(:enable_packs) %>
112-
<%= label_tag "organization[request_unit_names]", 'Custom request units used (please use singular form -- e.g. pack, not packs)' %>
112+
<%= label_tag "organization[request_unit_names]", 'Custom request units used. Please use singular form -- e.g. pack, not packs. There will be a default "unit" entry provided.' %>
113113
<%= select_tag(
114114
"organization[request_unit_names]",
115115
options_from_collection_for_select(

app/views/partners/requests/_item_request.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<%= field.number_field :quantity, label: false, step: 1, min: 1, class: 'form-control' %>
1313
</td>
1414

15-
<% if Flipper.enabled?(:enable_packs) && current_partner.organization.request_units.any? %>
15+
<% if Flipper.enabled?(:enable_packs) && (current_partner ? current_partner.organization.request_units.any? : current_organization.request_units.any?) %>
1616
<td>
1717
<%= field.label :request_unit, "Unit", {class: 'sr-only'} %>
18-
<%= field.select :request_unit, [], {include_blank: 'Units'},
18+
<%= field.select :request_unit, [], {include_blank: 'units'},
1919
{ :'data-item-units-target' => 'requestSelect', class: 'form-control'} %>
2020

2121
</td>

0 commit comments

Comments
 (0)