Fix Metrics/ClassLength

This commit is contained in:
Neal Chambers
2023-07-08 21:58:26 +09:00
parent bfb2fc528e
commit fb1ae855c2
6 changed files with 171 additions and 36 deletions

View File

@@ -522,7 +522,6 @@ Metrics/BlockNesting:
Metrics/ClassLength:
Exclude:
- 'app/components/products_table_component.rb'
- 'app/controllers/admin/enterprise_fees_controller.rb'
- 'app/controllers/admin/enterprises_controller.rb'
- 'app/controllers/admin/order_cycles_controller.rb'
- 'app/controllers/admin/product_import_controller.rb'

View File

@@ -6,8 +6,6 @@ module Admin
class EnterpriseFeesController < Admin::ResourceController
before_action :load_enterprise_fee_set, only: :index
before_action :load_data
before_action :check_enterprise_fee_input, only: [:bulk_update]
before_action :check_calculators_compatibility_with_taxes, only: [:bulk_update]
def index
@include_calculators = params[:include_calculators].present?
@@ -36,7 +34,7 @@ module Admin
end
def bulk_update
@enterprise_fee_set = Sets::EnterpriseFeeSet.new(enterprise_fee_bulk_params)
@enterprise_fee_set = Forms::EnterpriseFeesBulkUpdate.new(enterprise_fee_bulk_params)
if @enterprise_fee_set.save
redirect_to redirect_path, notice: I18n.t(:enterprise_fees_update_notice)
@@ -99,37 +97,5 @@ module Admin
]
)
end
def check_enterprise_fee_input
enterprise_fee_bulk_params['collection_attributes'].each do |_, fee_row|
enterprise_fees = fee_row['calculator_attributes']&.slice(
:preferred_flat_percent, :preferred_amount,
:preferred_first_item, :preferred_additional_item,
:preferred_minimal_amount, :preferred_normal_amount,
:preferred_discount_amount, :preferred_per_unit
)
next unless enterprise_fees
enterprise_fees.each do |_, enterprise_amount|
unless enterprise_amount.nil? || Float(enterprise_amount, exception: false)
flash[:error] = I18n.t(:calculator_preferred_value_error)
return redirect_to redirect_path
end
end
end
end
def check_calculators_compatibility_with_taxes
enterprise_fee_bulk_params['collection_attributes'].each do |_, enterprise_fee|
next unless enterprise_fee['inherits_tax_category'] == "true"
next unless EnterpriseFee::PER_ORDER_CALCULATORS.include?(enterprise_fee['calculator_type'])
flash[:error] = I18n.t(
'activerecord.errors.models.enterprise_fee.inherit_tax_requires_per_item_calculator'
)
return redirect_to redirect_path
end
end
end
end

View File

@@ -0,0 +1,75 @@
# frozen_string_literal: true
module Forms
class EnterpriseFeesBulkUpdate
include ActiveModel::Model
validate :check_enterprise_fee_input
validate :check_calculators_compatibility_with_taxes
def initialize(params)
@errors = ActiveModel::Errors.new self
@params = params
end
def save
return false unless valid?
@enterprise_fee_set = Sets::EnterpriseFeeSet.new(enterprise_fee_bulk_params)
unless @enterprise_fee_set.save
@enterprise_fee_set.errors.each do |attribute, message|
@errors.add(attribute, message)
end
return false
end
true
end
private
def check_enterprise_fee_input
enterprise_fee_bulk_params['collection_attributes'].each do |_, fee_row|
enterprise_fees = fee_row['calculator_attributes']&.slice(
:preferred_flat_percent, :preferred_amount,
:preferred_first_item, :preferred_additional_item,
:preferred_minimal_amount, :preferred_normal_amount,
:preferred_discount_amount, :preferred_per_unit
)
next unless enterprise_fees
enterprise_fees.each do |_, enterprise_amount|
unless enterprise_amount.nil? || Float(enterprise_amount, exception: false)
@errors.add(:base, I18n.t(:calculator_preferred_value_error))
end
end
end
end
def check_calculators_compatibility_with_taxes
enterprise_fee_bulk_params['collection_attributes'].each do |_, enterprise_fee|
next unless enterprise_fee['inherits_tax_category'] == "true"
next unless EnterpriseFee::PER_ORDER_CALCULATORS.include?(enterprise_fee['calculator_type'])
@errors.add(
:base,
I18n.t(
'activerecord.errors.models.enterprise_fee.inherit_tax_requires_per_item_calculator'
)
)
end
end
def enterprise_fee_bulk_params
@params.require(:sets_enterprise_fee_set).permit(
collection_attributes: [
:id, :enterprise_id, :fee_type, :name, :tax_category_id,
:inherits_tax_category, :calculator_type,
{ calculator_attributes: PermittedAttributes::Calculator.attributes }
]
)
end
end
end

View File

@@ -0,0 +1,60 @@
# frozen_string_literal: true
module Forms
class EnterpriseFeesBulkUpdate
include ActiveModel::Model
validate :check_enterprise_fee_input
validate :check_calculators_compatibility_with_taxes
def initialize(params)
@errors = ActiveModel::Errors.new self
@params = params
end
def save
return false unless valid?
@enterprise_fee_set = Sets::EnterpriseFeeSet.new(@params)
@enterprise_fee_set.save
true
end
def check_enterprise_fee_input
@params['collection_attributes'].each do |_, fee_row|
enterprise_fees = fee_row['calculator_attributes']&.slice(
:preferred_flat_percent, :preferred_amount,
:preferred_first_item, :preferred_additional_item,
:preferred_minimal_amount, :preferred_normal_amount,
:preferred_discount_amount, :preferred_per_unit
)
next unless enterprise_fees
enterprise_fees.each do |_, enterprise_amount|
unless enterprise_amount.nil? || Float(enterprise_amount, exception: false)
@errors.add(:base, I18n.t(:calculator_preferred_value_error))
return false
end
end
return true
end
end
def check_calculators_compatibility_with_taxes
@params['collection_attributes'].each do |_, enterprise_fee|
next unless enterprise_fee['inherits_tax_category'] == "true"
next unless EnterpriseFee::PER_ORDER_CALCULATORS.include?(enterprise_fee['calculator_type'])
@errors.add(
:base,
I18n.t(
'activerecord.errors.models.enterprise_fee.inherit_tax_requires_per_item_calculator'
)
)
return false
end
true
end
end
end

View File

@@ -99,6 +99,24 @@ FactoryBot.define do
end
end
factory :enterprise_fee_bulk_update, class: Forms::EnterpriseFeesBulkUpdate do
transient { enterprise_fee { build(:enterprise_fee)}}
collection_attributes do
{
[
id: enterprise_fee.id,
enterprise_id: enterprise_fee.enterprise_id,
fee_type: enterprise_fee.fee_type,
name: enterprise_fee.name,
tax_category_id: enterprise_fee.tax_category_id,
inherits_tax_category: enterprise_fee.inherits_tax_category,
calculator_type: enterprise_fee.calculator_type,
{ calculator_attributes: enterprise_fee.calculator.attributes }
]
}
end
factory :adjustment_metadata, class: AdjustmentMetadata do
adjustment { FactoryBot.create(:adjustment) }
enterprise { FactoryBot.create(:distributor_enterprise) }

View File

@@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'spec_helper'
describe Forms::EnterpriseFeesBulkUpdate do
describe "error reporting" do
it "adds errors from set creation" do
subject = build(:enterprise_fee_bulk_update)
end
end
end