Extract permitted attributes to separate service

This commit is contained in:
Luis Ramos
2020-03-21 19:05:01 +00:00
parent 58c83d056d
commit 79b0867507
2 changed files with 38 additions and 12 deletions

View File

@@ -157,18 +157,7 @@ module Admin
end
def subscription_params
return params[:subscription] if params[:subscription].empty?
params.require(:subscription).permit(
:id, :shop_id, :schedule_id, :customer_id,
:payment_method_id, :shipping_method_id,
:begins_at, :ends_at,
:canceled_at, :paused_at,
:shipping_fee_estimate, :payment_fee_estimate,
:subscription_line_items_attributes => [:id, :quantity, :variant_id, :price_estimate, :_destroy],
:bill_address_attributes => permitted_address_attributes,
:ship_address_attributes => permitted_address_attributes
)
PermittedAttributes::Subscription.new(params).call
end
end
end

View File

@@ -0,0 +1,37 @@
# frozen_string_literal: true
module PermittedAttributes
class Subscription
def initialize(params)
@params = params
end
def call
return @params[:subscription] if @params[:subscription].empty?
@params.require(:subscription).permit(basic_permitted_attributes + other_permitted_attributes)
end
private
def basic_permitted_attributes
[
:id, :shop_id, :schedule_id, :customer_id,
:payment_method_id, :shipping_method_id,
:begins_at, :ends_at,
:canceled_at, :paused_at,
:shipping_fee_estimate, :payment_fee_estimate,
]
end
def other_permitted_attributes
[
subscription_line_items_attributes: [
:id, :quantity, :variant_id, :price_estimate, :_destroy
],
bill_address_attributes: PermittedAttributes::Address.attributes,
ship_address_attributes: PermittedAttributes::Address.attributes
]
end
end
end