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

4396 packs 1 add custom request units at organizational level #4415

3 changes: 2 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def organization_params
:ytd_on_distribution_printout, :one_step_partner_invite,
:hide_value_columns_on_receipt, :hide_package_column_on_receipt,
:signature_for_distribution_pdf,
partner_form_fields: []
partner_form_fields: [],
request_units_attributes: [:id, :organization_id, :name]
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def flipper_id

has_one_attached :logo

accepts_nested_attributes_for :users, :account_request
accepts_nested_attributes_for :users, :account_request, :request_units

include Geocodable

Expand Down
2 changes: 2 additions & 0 deletions app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
#
class Unit < ApplicationRecord
belongs_to :organization
# This validation prevent duplicates except when creating two units of the same name at the same time (on the organization update page)
validates :name, uniqueness: {scope: :organization}
end
12 changes: 11 additions & 1 deletion app/services/organization_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ def update(organization, params)
if params.has_key?("partner_form_fields")
params["partner_form_fields"].delete_if { |field| field == "" }
end
result = organization.update(params)

if Flipper.enabled?(:enable_packs)
# Needs logic to keep units that are in use
units = params[:request_units_attributes].to_h.values
new_units = units.map { |unit| Unit.find_or_initialize_by(unit) }
result = organization.update(params.except(:request_units_attributes))
organization.request_units = new_units
else
result = organization.update(params)
end

return false unless result
update_partner_flags(organization)
true
Expand Down
18 changes: 18 additions & 0 deletions app/views/organizations/_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@
<%= humanize_boolean(@organization.distribute_monthly) %>
</p>
</div>
<% if Flipper.enabled?(:enable_packs) %>
<div class="mb-4">
<h3 class='font-bold'>Custom Request Units</h3>
<p>
<% if @organization.request_units.length > 0 %>
<% @organization.request_units.map do |unit| %>
<%= fa_icon "angle-right" %>
<span>
<%= unit.name.titlecase %>
</span> <br>
<% end %>
<% else %>
<%= fa_icon "angle-right" %>
<span> None </span>
<% end %>
</p>
</div>
<% end %>
<div class="mb-4">
<h3 class='font-bold'>Child Based Requests?</h3>
<p>
Expand Down
9 changes: 8 additions & 1 deletion app/views/organizations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
</div><!-- /.container-fluid -->
</section>

<%= simple_form_for current_organization, url: {controller: "organizations", action: "update"} do |f| %>
<%= simple_form_for current_organization,
data: { controller: "form-input" },
url: {controller: "organizations", action: "update"} do |f| %>
<section class="content">
<div class="container-fluid">
<div class="row">
Expand Down Expand Up @@ -100,6 +102,11 @@

<%= f.input :repackage_essentials, label: 'Does your bank repackage essentials?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :distribute_monthly, label: 'Does your bank distribute monthly?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>

<% if Flipper.enabled?(:enable_packs) %>
<%= render 'units/request_units' , f: f %>
<% end %>

<%= f.input :enable_child_based_requests, label: 'Enable partners to make child-based requests?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :enable_individual_requests, label: 'Enable partners to make requests for individuals?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :enable_quantity_based_requests, label: 'Enable partners to make quantity-based requests?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
Expand Down
15 changes: 15 additions & 0 deletions app/views/units/_request_units.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<table class='table'>
<thead>
<tr>
<th>Custom request units used (please use singular form -- e.g. pack, not packs)</th>
</tr>
</thead>
<tbody id='request-fields'>
<%= render 'units/unit_line_item' , f: f, object: current_organization.request_units %>
</tbody>
</table>
<div>
<%= add_element_button('Add Another Unit', container_selector: '#request-fields' ) do %>
<%= render 'units/unit_line_item' , f: f, object: current_organization.request_units.new %>
<% end %>
</div>
11 changes: 11 additions & 0 deletions app/views/units/_unit_line_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= f.fields_for :request_units, defined?(object) ? object : nil, include_id: false do |field| %>
<tr data-form-input-target="removeContainer" data-controller="form-input" id="td-remove">
<td>
<%= field.input_field :name, class: "form-control" %>
<%= field.hidden_field(:id) %>
</td>
<td>
<%= remove_element_button "Remove" , container_selector: 'whatecwer' %>
</td>
</tr>
<% end %>
2 changes: 1 addition & 1 deletion spec/models/item_unit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == Schema Information
#
# Table name: item_request_units
# Table name: item_units
#
# id :bigint not null, primary key
# name :string not null
Expand Down
26 changes: 26 additions & 0 deletions spec/models/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# == Schema Information
#
# Table name: units
#
# id :bigint not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint
#
require "rails_helper"

RSpec.describe Unit, type: :model do
let!(:organization) { create(:organization) }
let!(:unit_1) { create(:unit, name: "WolfPack", organization: organization) }

describe "Validations" do
it "validates uniqueness of name in context of organization" do
expect { described_class.create!(name: "WolfPack", organization: organization) }.to raise_exception(ActiveRecord::RecordInvalid).with_message("Validation failed: Name has already been taken")
end
end

describe "Associations" do
it { should belong_to(:organization) }
end
end
18 changes: 18 additions & 0 deletions spec/requests/organization_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:organization) { create(:organization) }
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
let!(:unit) { create(:unit, name: "WolfPack", organization: organization) }

context "While signed in as a normal user" do
before do
Expand All @@ -22,6 +23,14 @@
url: organization.url
)
end

context "when enable_packs flipper is on" do
it "displays organization's custom units" do
Flipper.enable(:enable_packs)
get organization_path
expect(response.body).to include unit.name.titlecase
end
end
end

describe "GET #edit" do
Expand Down Expand Up @@ -58,6 +67,15 @@
url: organization.url
)
end

context "when enable_packs flipper is on" do
it "should display custom units and units form" do
Flipper.enable(:enable_packs)
get edit_organization_path
expect(response.body).to include("Custom request units used (please use singular form -- e.g. pack, not packs)")
expect(response.body).to include unit.name
end
end
end

describe "PATCH #update" do
Expand Down
13 changes: 13 additions & 0 deletions spec/services/organization_update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
expect(organization.errors.none?).to eq(true)
expect(organization.reload.name).to eq("A brand NEW NEW name")
end

it "Should add request_units to the organization if flipper is on" do
Flipper.enable(:enable_packs)
params = {request_units_attributes: {"1" => {"name" => "newpack"}}}
described_class.update(organization, params)
expect(organization.errors.none?).to eq(true)
expect(organization.reload.request_units.pluck(:name)).to match_array(["newpack"])
end
end

# organization with request_units box, flat
# on update, if params dont include new unit pack, organization.testunits also include
# if params dont include any units, org doesnt include any units
# if flipper is off, units dont change

context "when object is invalid" do
it "should not update and return false" do
params = {name: "A brand NEW NEW name",
Expand Down