mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add basic form object for enterprise fee summary
This commit is contained in:
@@ -2681,6 +2681,11 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
signup:
|
||||
start_free_profile: "Start with a free profile, and expand when you're ready!"
|
||||
|
||||
order_management:
|
||||
reports:
|
||||
enterprise_fee_summary:
|
||||
date_end_before_start_error: "must be after start"
|
||||
|
||||
spree:
|
||||
# TODO: remove `email` key once we get to Spree 2.0
|
||||
email: Email
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
module OrderManagement
|
||||
module Reports
|
||||
module EnterpriseFeeSummary
|
||||
class Parameters
|
||||
@i18n_scope = "order_management.reports.enterprise_fee_summary"
|
||||
|
||||
DATE_END_BEFORE_START_ERROR = I18n.t("date_end_before_start_error", scope: @i18n_scope)
|
||||
|
||||
extend ActiveModel::Naming
|
||||
include ActiveModel::Validations
|
||||
|
||||
attr_accessor :start_at, :end_at, :distributor_ids, :producer_ids, :order_cycle_ids,
|
||||
:enterprise_fee_ids, :shipping_method_ids, :payment_method_ids
|
||||
|
||||
validates :start_at, :end_at, date_time_string: true
|
||||
validates :distributor_ids, :producer_ids, integer_array: true
|
||||
validates :order_cycle_ids, integer_array: true
|
||||
validates :enterprise_fee_ids, integer_array: true
|
||||
validates :shipping_method_ids, :payment_method_ids, integer_array: true
|
||||
|
||||
validate :require_valid_datetime_range
|
||||
|
||||
def initialize(attributes = {})
|
||||
self.distributor_ids = []
|
||||
self.producer_ids = []
|
||||
self.order_cycle_ids = []
|
||||
self.enterprise_fee_ids = []
|
||||
self.shipping_method_ids = []
|
||||
self.payment_method_ids = []
|
||||
|
||||
attributes.each do |key, value|
|
||||
public_send("#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def require_valid_datetime_range
|
||||
return if start_at.blank? || end_at.blank?
|
||||
|
||||
errors.add(:end_at, DATE_END_BEFORE_START_ERROR) unless start_at < end_at
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,44 @@
|
||||
require "spec_helper"
|
||||
|
||||
require "date_time_string_validator"
|
||||
require "order_management/reports/enterprise_fee_summary/parameters"
|
||||
|
||||
describe OrderManagement::Reports::EnterpriseFeeSummary::Parameters do
|
||||
describe "validation" do
|
||||
let(:parameters) { described_class.new }
|
||||
|
||||
it "allows all parameters to be blank" do
|
||||
expect(parameters).to be_valid
|
||||
end
|
||||
|
||||
context "for type of parameters" do
|
||||
it { expect(subject).to validate_date_time_format_of(:start_at) }
|
||||
it { expect(subject).to validate_date_time_format_of(:end_at) }
|
||||
it { expect(subject).to validate_integer_array(:distributor_ids) }
|
||||
it { expect(subject).to validate_integer_array(:producer_ids) }
|
||||
it { expect(subject).to validate_integer_array(:order_cycle_ids) }
|
||||
it { expect(subject).to validate_integer_array(:enterprise_fee_ids) }
|
||||
it { expect(subject).to validate_integer_array(:shipping_method_ids) }
|
||||
it { expect(subject).to validate_integer_array(:payment_method_ids) }
|
||||
|
||||
describe "requiring start_at to be before end_at" do
|
||||
let(:now) { Time.zone.now }
|
||||
|
||||
it "adds error when start_at is after end_at" do
|
||||
allow(subject).to receive(:start_at) { now.to_s }
|
||||
allow(subject).to receive(:end_at) { (now - 1.hour).to_s }
|
||||
|
||||
expect(subject).not_to be_valid
|
||||
expect(subject.errors[:end_at]).to eq([described_class::DATE_END_BEFORE_START_ERROR])
|
||||
end
|
||||
|
||||
it "does not add error when start_at is before end_at" do
|
||||
allow(subject).to receive(:start_at) { now.to_s }
|
||||
allow(subject).to receive(:end_at) { (now + 1.hour).to_s }
|
||||
|
||||
expect(subject).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user