From a0976404644b240350cdfaae2d69493025787a4b Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Fri, 21 Sep 2018 17:52:09 +0800 Subject: [PATCH] Add basic form object for enterprise fee summary --- config/locales/en.yml | 5 ++ .../enterprise_fee_summary/parameters.rb | 46 +++++++++++++++++++ .../enterprise_fee_summary/parameters_spec.rb | 44 ++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 lib/order_management/reports/enterprise_fee_summary/parameters.rb create mode 100644 spec/lib/order_management/reports/enterprise_fee_summary/parameters_spec.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index f59b6d4bba..01fd3aaec4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/lib/order_management/reports/enterprise_fee_summary/parameters.rb b/lib/order_management/reports/enterprise_fee_summary/parameters.rb new file mode 100644 index 0000000000..62b10f308e --- /dev/null +++ b/lib/order_management/reports/enterprise_fee_summary/parameters.rb @@ -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 diff --git a/spec/lib/order_management/reports/enterprise_fee_summary/parameters_spec.rb b/spec/lib/order_management/reports/enterprise_fee_summary/parameters_spec.rb new file mode 100644 index 0000000000..94aac5e323 --- /dev/null +++ b/spec/lib/order_management/reports/enterprise_fee_summary/parameters_spec.rb @@ -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