diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 4ab62d3ac4..4c9f2c09c8 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -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 diff --git a/app/models/organization.rb b/app/models/organization.rb index cd03a56f2a..d8f46221a6 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -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 diff --git a/app/models/unit.rb b/app/models/unit.rb index 53527345ec..979f03b7be 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -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 diff --git a/app/services/organization_update_service.rb b/app/services/organization_update_service.rb index 0b5b3c72c6..46287344ea 100644 --- a/app/services/organization_update_service.rb +++ b/app/services/organization_update_service.rb @@ -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 diff --git a/app/views/organizations/_details.html.erb b/app/views/organizations/_details.html.erb index d92788f59f..90dc479f6f 100644 --- a/app/views/organizations/_details.html.erb +++ b/app/views/organizations/_details.html.erb @@ -125,6 +125,24 @@ <%= humanize_boolean(@organization.distribute_monthly) %>

+ <% if Flipper.enabled?(:enable_packs) %> +
+

Custom Request Units

+

+ <% if @organization.request_units.length > 0 %> + <% @organization.request_units.map do |unit| %> + <%= fa_icon "angle-right" %> + + <%= unit.name.titlecase %> +
+ <% end %> + <% else %> + <%= fa_icon "angle-right" %> + None + <% end %> +

+
+ <% end %>

Child Based Requests?

diff --git a/app/views/organizations/edit.html.erb b/app/views/organizations/edit.html.erb index 4c7cc0928d..a2696c05ce 100644 --- a/app/views/organizations/edit.html.erb +++ b/app/views/organizations/edit.html.erb @@ -40,7 +40,9 @@

-<%= 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| %>
@@ -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 %> diff --git a/app/views/units/_request_units.html.erb b/app/views/units/_request_units.html.erb new file mode 100644 index 0000000000..0e4b670fd0 --- /dev/null +++ b/app/views/units/_request_units.html.erb @@ -0,0 +1,15 @@ + + + + + + + + <%= render 'units/unit_line_item' , f: f, object: current_organization.request_units %> + +
Custom request units used (please use singular form -- e.g. pack, not packs)
+
+ <%= 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 %> +
diff --git a/app/views/units/_unit_line_item.html.erb b/app/views/units/_unit_line_item.html.erb new file mode 100644 index 0000000000..71027616c6 --- /dev/null +++ b/app/views/units/_unit_line_item.html.erb @@ -0,0 +1,11 @@ +<%= f.fields_for :request_units, defined?(object) ? object : nil, include_id: false do |field| %> + + + <%= field.input_field :name, class: "form-control" %> + <%= field.hidden_field(:id) %> + + + <%= remove_element_button "Remove" , container_selector: 'whatecwer' %> + + +<% end %> diff --git a/spec/models/item_unit_spec.rb b/spec/models/item_unit_spec.rb index 70ebd9cc06..be63d21e09 100644 --- a/spec/models/item_unit_spec.rb +++ b/spec/models/item_unit_spec.rb @@ -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 diff --git a/spec/models/unit_spec.rb b/spec/models/unit_spec.rb new file mode 100644 index 0000000000..9d3d731652 --- /dev/null +++ b/spec/models/unit_spec.rb @@ -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 diff --git a/spec/requests/organization_requests_spec.rb b/spec/requests/organization_requests_spec.rb index 60df080987..86ce6869f3 100644 --- a/spec/requests/organization_requests_spec.rb +++ b/spec/requests/organization_requests_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/services/organization_update_service_spec.rb b/spec/services/organization_update_service_spec.rb index 3ed51f340c..fe4b2a6f00 100644 --- a/spec/services/organization_update_service_spec.rb +++ b/spec/services/organization_update_service_spec.rb @@ -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",