From 79b08675070ce8693050f5abe61545f3a0c57c1e Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 21 Mar 2020 19:05:01 +0000 Subject: [PATCH] Extract permitted attributes to separate service --- .../admin/subscriptions_controller.rb | 13 +------ .../permitted_attributes/subscription.rb | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 app/services/permitted_attributes/subscription.rb diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/admin/subscriptions_controller.rb index 6b65ddda86..4561ef3a79 100644 --- a/app/controllers/admin/subscriptions_controller.rb +++ b/app/controllers/admin/subscriptions_controller.rb @@ -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 diff --git a/app/services/permitted_attributes/subscription.rb b/app/services/permitted_attributes/subscription.rb new file mode 100644 index 0000000000..2ab3956fca --- /dev/null +++ b/app/services/permitted_attributes/subscription.rb @@ -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