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

5 changes: 3 additions & 2 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def edit
def update
@organization = current_organization

if OrganizationUpdateService.update(@organization, organization_params)
@units = Unit.where(params.dig(:request_units_attributes)&.dig(:name))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO deleting all units - needs fix

if OrganizationUpdateService.update(@organization, @units, organization_params)
redirect_to organization_path, notice: "Updated your organization!"
else
flash[:error] = @organization.errors.full_messages.join("\n")
Expand Down Expand Up @@ -99,7 +100,7 @@ def organization_params
:enable_individual_requests, :enable_quantity_based_requests,
:ytd_on_distribution_printout, :one_step_partner_invite,
:hide_value_columns_on_receipt, :hide_package_column_on_receipt,
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 @@ -134,7 +134,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_uniqueness_of :name, scope: :organization
end
16 changes: 14 additions & 2 deletions app/services/organization_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@ class << self
# @param organization [Organization]
# @param params [ActionDispatch::Http::Parameters]
# @return [Boolean]
def update(organization, params)
def update(organization, units, params)
return false unless valid?(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)
# Should be updated to not allow deletion of units in use
organization_units = organization.request_units.pluck(:name)
new_units = units.pluck(:name)
units_to_delete = organization_units - new_units
#organization.request_units.destroy_all
result = organization.update(params)
organization.request_units.where(name: units_to_delete).destroy_all
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: :request_units %>
</tbody>
</table>
<div>
<%= add_element_button('Add Another Unit', container_selector: '#request-fields' ) do %>
<%= render 'units/unit_line_item' , f: f, object: Unit.new %>
<% end %>
</div>
10 changes: 10 additions & 0 deletions app/views/units/_unit_line_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= f.fields_for :request_units, defined?(object) ? object : nil do |field| %>
<tr data-form-input-target="removeContainer" data-controller="form-input" id = "td-remove" >
<td>
<%= field.input_field :name, class: "form-control" %>
</td>
<td>
<%= remove_element_button "Remove" , container_selector: '#td-remove'%>
</td>
</tr>
<% end %>
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
Loading