From 5b01d2a962f7bda1a33c3d88c2db36af6d1de072 Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Mon, 27 Jul 2020 14:37:32 +0200 Subject: Update default permitted attributes The billing address as well as the interval and end date can now be set directly on the subscription, so we need to permit those attributes by default. --- lib/solidus_subscriptions/config.rb | 14 ++++++++------ lib/solidus_subscriptions/engine.rb | 17 ++++++++++------- lib/solidus_subscriptions/permitted_attributes.rb | 20 +------------------- 3 files changed, 19 insertions(+), 32 deletions(-) (limited to 'lib') diff --git a/lib/solidus_subscriptions/config.rb b/lib/solidus_subscriptions/config.rb index d6dbbf6..549c1ce 100644 --- a/lib/solidus_subscriptions/config.rb +++ b/lib/solidus_subscriptions/config.rb @@ -56,27 +56,25 @@ module SolidusSubscriptions # This is useful in the case where certain fields should not be allowed to # be modified by the user. This locks these attributes from being passed # in to the orders controller (or the api controller). - # Ie. if a store does not want to allow users to configure the end date of # a subscription. Add this to an initializer: - # ``` # SolidusSubscriptions::Config.subscription_line_item_attributes = [ # :quantity, + # :subscribable_id, # :interval_length, # :interval_units, - # :subscribable_id # ] # ``` # This configuration also easily allows the gem to be customized to track - # more information on the subcriptions line items. + # more information on the subscriptions line items. mattr_accessor(:subscription_line_item_attributes) do [ :quantity, :subscribable_id, :interval_length, :interval_units, - :end_date + :end_date, ] end @@ -84,8 +82,12 @@ module SolidusSubscriptions # be updated from user data mattr_accessor(:subscription_attributes) do [ + :interval_length, + :interval_units, + :end_date, :actionable_date, - shipping_address_attributes: Spree::PermittedAttributes.address_attributes + shipping_address_attributes: Spree::PermittedAttributes.address_attributes, + billing_address_attributes: Spree::PermittedAttributes.address_attributes, ] end end diff --git a/lib/solidus_subscriptions/engine.rb b/lib/solidus_subscriptions/engine.rb index 39cb754..8b5b083 100644 --- a/lib/solidus_subscriptions/engine.rb +++ b/lib/solidus_subscriptions/engine.rb @@ -20,19 +20,22 @@ module SolidusSubscriptions g.test_framework :rspec end - initializer 'configure spree subcription permitted attributes', after: 'require subscription lib helpers' do - PermittedAttributes.update_spree_permiteed_attributes + initializer 'solidus_subscriptions.update_permitted_attributes' do + ::Spree::PermittedAttributes.line_item_attributes << { + subscription_line_items_attributes: PermittedAttributes.subscription_line_item_attributes | [:id], + } + + ::Spree::PermittedAttributes.user_attributes << { + subscriptions_attributes: PermittedAttributes.subscription_attributes | [:id], + } end - initializer 'solidus_subscriptions.configs', before: "require subscription lib helpers" do - end - - initializer 'register_subscription_promotion_rule', after: 'spree.promo.register.promotion.rules' do |app| + initializer 'solidus_subscriptions.register_promotion_rules', after: 'spree.promo.register.promotion.rules' do |app| app.config.spree.promotions.rules << 'SolidusSubscriptions::SubscriptionPromotionRule' app.config.spree.promotions.rules << 'SolidusSubscriptions::SubscriptionOrderPromotionRule' end - initializer 'subscriptions_backend' do + initializer 'solidus_subscriptions.configure_backend' do next unless ::Spree::Backend::Config.respond_to?(:menu_items) ::Spree::Backend::Config.configure do |config| config.menu_items << config.class::MenuItem.new( diff --git a/lib/solidus_subscriptions/permitted_attributes.rb b/lib/solidus_subscriptions/permitted_attributes.rb index c93b10b..d1ae5a1 100644 --- a/lib/solidus_subscriptions/permitted_attributes.rb +++ b/lib/solidus_subscriptions/permitted_attributes.rb @@ -4,33 +4,15 @@ module SolidusSubscriptions module PermittedAttributes class << self - def update_spree_permiteed_attributes - ::Spree::PermittedAttributes.line_item_attributes << { - subscription_line_items_attributes: nested( - subscription_line_item_attributes - ) - } - - ::Spree::PermittedAttributes.user_attributes << { - subscriptions_attributes: nested(subscription_attributes) - } - end - def subscription_line_item_attributes [Config.subscription_line_item_attributes] end def subscription_attributes Config.subscription_attributes | [ - { line_items_attributes: nested(subscription_line_item_attributes) - [:subscribable_id] } + line_items_attributes: (subscription_line_item_attributes | [:id] - [:subscribable_id]), ] end - - private - - def nested(attributes) - attributes | [:id] - end end end end -- cgit v1.2.3