Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve promotion rules names #215

Merged
merged 5 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

40 changes: 0 additions & 40 deletions app/models/solidus_subscriptions/subscription_promotion_rule.rb

This file was deleted.

4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/solidus_subscriptions/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Engine < Rails::Engine
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down