diff options
author | Alessandro Desantis <desa.alessandro@gmail.com> | 2021-03-22 15:30:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-22 15:30:53 +0100 |
commit | af29cabe832c06adef993057c03451a3859dcf84 (patch) | |
tree | 223ff15ac4db381d4aea92645d7b2537a44522fa | |
parent | 32d8e59f56a9ed76daebcd7d58ec913da0b35b07 (diff) | |
parent | 4c2fecd938c826e5119d50aef5eb8702fadb1161 (diff) |
Merge pull request #215 from solidusio-contrib/luca-landa/improve-promotion-rules-names
Improve promotion rules names
11 files changed, 88 insertions, 71 deletions
@@ -90,6 +90,11 @@ The task creates ActiveJob jobs which can be fulfilled by your queue library of We suggest using the [Whenever](https://github.com/javan/whenever) gem to schedule the task. +### Promotion rules +This extensions adds the following [Promotion rules](https://guides.solidus.io/developers/promotions/promotion-rules.html): +* `SolidusSubscriptions::Promotion::Rules::SubscriptionCreationOrder` which applies if the order is creating a subscription; +* `SolidusSubscriptions::Promotion::Rules::SubscriptionInstallmentOrder` which applies if the order is an installment of a subscription. + ### API documentation You can find the API documentation [here](https://stoplight.io/p/docs/gh/solidusio-contrib/solidus_subscriptions?group=master). diff --git a/app/models/solidus_subscriptions/promotion/rules/subscription_creation_order.rb b/app/models/solidus_subscriptions/promotion/rules/subscription_creation_order.rb new file mode 100644 index 0000000..1b44125 --- /dev/null +++ b/app/models/solidus_subscriptions/promotion/rules/subscription_creation_order.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module SolidusSubscriptions + module Promotion + module Rules + class SubscriptionCreationOrder < ::Spree::PromotionRule + # Promotion can be applied to an entire order. Will only be true + # for Spree::Order + # + # @param promotable [Object] Any object which could have this + # promotion rule applied to it. + # + # @return [Boolean] + def applicable?(promotable) + promotable.is_a? ::Spree::Order + end + + # An order is eligible if it contains a line item with an associates + # subscription_line_item. This rule applies to order and so its eligibility + # will always be considered against an order. Will only return true for + # orders containing Spree::Line item with associated subscription_line_items + # + # @param order [Spree::Order] The order which could have this rule applied + # to it. + # + # @return [Boolean] + def eligible?(order, **_options) + order.subscription_line_items.any? + end + + # Certain actions create adjustments on line items. In this case, only + # line items with associated subscription_line_items are eligible to be + # adjusted. Will only return true # if :line_item has an associated + # subscription. + # + # @param line_item [Spree::LineItem] The line item which could be adjusted + # by the promotion. + def actionable?(line_item) + line_item.subscription_line_items.present? + end + end + end + end +end diff --git a/app/models/solidus_subscriptions/promotion/rules/subscription_installment_order.rb b/app/models/solidus_subscriptions/promotion/rules/subscription_installment_order.rb new file mode 100644 index 0000000..455825d --- /dev/null +++ b/app/models/solidus_subscriptions/promotion/rules/subscription_installment_order.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module SolidusSubscriptions + module Promotion + module Rules + class SubscriptionInstallmentOrder < ::Spree::PromotionRule + # Promotion can be applied to an entire order. Will only be true + # for Spree::Order + # + # @param promotable [Object] Any object which could have this + # promotion rule applied to it. + # + # @return [Boolean] + def applicable?(promotable) + promotable.is_a? ::Spree::Order + end + + # An order is eligible if it fulfills a subscription Installment. Will only + # return true if the order fulfills one or more Installments + # + # @param order [Spree::Order] The order which could have this rule applied + # to it. + # + # @return [Boolean] + def eligible?(order, **_options) + order.subscription_order? + end + end + end + end +end diff --git a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb deleted file mode 100644 index 17d280e..0000000 --- a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - class SubscriptionOrderPromotionRule < ::Spree::PromotionRule - # Promotion can be applied to an entire order. Will only be true - # for Spree::Order - # - # @param promotable [Object] Any object which could have this - # promotion rule applied to it. - # - # @return [Boolean] - def applicable?(promotable) - promotable.is_a? ::Spree::Order - end - - # An order is eligible if it fulfills a subscription Installment. Will only - # return true if the order fulfills one or more Installments - # - # @param order [Spree::Order] The order which could have this rule applied - # to it. - # - # @return [Boolean] - def eligible?(order, **_options) - order.subscription_order? - end - end -end diff --git a/app/models/solidus_subscriptions/subscription_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_promotion_rule.rb deleted file mode 100644 index fd99979..0000000 --- a/app/models/solidus_subscriptions/subscription_promotion_rule.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - class SubscriptionPromotionRule < ::Spree::PromotionRule - # Promotion can be applied to an entire order. Will only be true - # for Spree::Order - # - # @param promotable [Object] Any object which could have this - # promotion rule applied to it. - # - # @return [Boolean] - def applicable?(promotable) - promotable.is_a? ::Spree::Order - end - - # An order is eligible if it contains a line item with an associates - # subscription_line_item. This rule applies to order and so its eligibility - # will always be considered against an order. Will only return true for - # orders containing Spree::Line item with associated subscription_line_items - # - # @param order [Spree::Order] The order which could have this rule applied - # to it. - # - # @return [Boolean] - def eligible?(order, **_options) - order.subscription_line_items.any? - end - - # Certain actions create adjustments on line items. In this case, only - # line items with associated subscription_line_items are eligible to be - # adjusted. Will only return true # if :line_item has an associated - # subscription. - # - # @param line_item [Spree::LineItem] The line item which could be adjusted - # by the promotion. - def actionable?(line_item) - line_item.subscription_line_items.present? - end - end -end diff --git a/app/views/spree/admin/promotions/rules/_subscription_order_promotion_rule.html.erb b/app/views/spree/admin/promotions/rules/_subscription_creation_order.html.erb index e69de29..e69de29 100644 --- a/app/views/spree/admin/promotions/rules/_subscription_order_promotion_rule.html.erb +++ b/app/views/spree/admin/promotions/rules/_subscription_creation_order.html.erb diff --git a/app/views/spree/admin/promotions/rules/_subscription_promotion_rule.html.erb b/app/views/spree/admin/promotions/rules/_subscription_installment_order.html.erb index e69de29..e69de29 100644 --- a/app/views/spree/admin/promotions/rules/_subscription_promotion_rule.html.erb +++ b/app/views/spree/admin/promotions/rules/_subscription_installment_order.html.erb diff --git a/config/locales/en.yml b/config/locales/en.yml index b921a2c..7055aad 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -97,6 +97,10 @@ en: solidus_subscriptions/installment/state: fulfilled: Fulfilled unfulfilled: Unfulfilled + solidus_subscriptions/promotion/rules/subscription_creation_order: + description: Creates a subscription + solidus_subscriptions/promotion/rules/subscription_installment_order: + description: Is a subscription installment models: solidus_subscriptions/subscription: one: Subscription diff --git a/lib/solidus_subscriptions/engine.rb b/lib/solidus_subscriptions/engine.rb index 2f3067e..0f274d8 100644 --- a/lib/solidus_subscriptions/engine.rb +++ b/lib/solidus_subscriptions/engine.rb @@ -31,8 +31,8 @@ module SolidusSubscriptions end 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' + app.config.spree.promotions.rules << 'SolidusSubscriptions::Promotion::Rules::SubscriptionCreationOrder' + app.config.spree.promotions.rules << 'SolidusSubscriptions::Promotion::Rules::SubscriptionInstallmentOrder' end initializer 'solidus_subscriptions.configure_backend' do diff --git a/spec/lib/solidus_subscriptions/subscription_promotion_rule_spec.rb b/spec/lib/solidus_subscriptions/promotion/rules/subscription_creation_order_spec.rb index f4c65cc..f7975f1 100644 --- a/spec/lib/solidus_subscriptions/subscription_promotion_rule_spec.rb +++ b/spec/lib/solidus_subscriptions/promotion/rules/subscription_creation_order_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -RSpec.describe SolidusSubscriptions::SubscriptionPromotionRule do +RSpec.describe SolidusSubscriptions::Promotion::Rules::SubscriptionCreationOrder do let(:rule) { described_class.new } describe '#applicable' do diff --git a/spec/lib/solidus_subscriptions/subscription_order_promotion_rule_spec.rb b/spec/lib/solidus_subscriptions/promotion/rules/subscription_installment_order_spec.rb index bacbef2..d17fa61 100644 --- a/spec/lib/solidus_subscriptions/subscription_order_promotion_rule_spec.rb +++ b/spec/lib/solidus_subscriptions/promotion/rules/subscription_installment_order_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -RSpec.describe SolidusSubscriptions::SubscriptionOrderPromotionRule do +RSpec.describe SolidusSubscriptions::Promotion::Rules::SubscriptionInstallmentOrder do let(:rule) { described_class.new } describe '#applicable' do |