diff options
author | Alessandro Desantis <desa.alessandro@gmail.com> | 2020-09-25 13:08:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-25 13:08:45 +0200 |
commit | b401b1656d9a9582485f0eca6df1975922466173 (patch) | |
tree | 0cf98ab8758e835665008329134f7cb4ee97b54d /app | |
parent | a04f1e41a68a24fa0fd8107ba6c276f590480ebd (diff) | |
parent | 847802b38c31b41d06d6fb20ad255ee5387c7016 (diff) |
Merge pull request #148 from solidusio-contrib/aldesantis/update-initializer
Installation and configuration updates
Diffstat (limited to 'app')
42 files changed, 232 insertions, 155 deletions
diff --git a/app/assets/javascripts/spree/frontend/solidus_subscriptions.js b/app/assets/javascripts/spree/frontend/solidus_subscriptions.js deleted file mode 100644 index a79f2e9..0000000 --- a/app/assets/javascripts/spree/frontend/solidus_subscriptions.js +++ /dev/null @@ -1,2 +0,0 @@ -// Placeholder manifest file. -// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'
\ No newline at end of file diff --git a/app/assets/stylesheets/spree/backend/solidus_subscriptions.css b/app/assets/stylesheets/spree/backend/solidus_subscriptions.css deleted file mode 100644 index e3c2366..0000000 --- a/app/assets/stylesheets/spree/backend/solidus_subscriptions.css +++ /dev/null @@ -1,4 +0,0 @@ -/* -Placeholder manifest file. -the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css' -*/ diff --git a/app/assets/stylesheets/spree/frontend/solidus_subscriptions.css b/app/assets/stylesheets/spree/frontend/solidus_subscriptions.css deleted file mode 100644 index da23623..0000000 --- a/app/assets/stylesheets/spree/frontend/solidus_subscriptions.css +++ /dev/null @@ -1,4 +0,0 @@ -/* -Placeholder manifest file. -the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/frontend/all.css' -*/ diff --git a/app/controllers/solidus_subscriptions/api/v1/line_items_controller.rb b/app/controllers/solidus_subscriptions/api/v1/line_items_controller.rb index 3ef1ef0..1d6817a 100644 --- a/app/controllers/solidus_subscriptions/api/v1/line_items_controller.rb +++ b/app/controllers/solidus_subscriptions/api/v1/line_items_controller.rb @@ -1,35 +1,43 @@ -class SolidusSubscriptions::Api::V1::LineItemsController < Spree::Api::BaseController - before_action :load_line_item, only: [:update, :destroy] - wrap_parameters :subscription_line_item - - def update - authorize! :crud, @line_item, @order - if @line_item.update(line_item_params) - render json: @line_item.to_json - else - render json: @line_item.errors.to_json, status: 422 - end - end +# frozen_string_literal: true - def destroy - authorize! :crud, @line_item, @order - return render json: {}, status: 400 if @line_item.order.complete? +module SolidusSubscriptions + module Api + module V1 + class LineItemsController < ::Spree::Api::BaseController + before_action :load_line_item, only: [:update, :destroy] + wrap_parameters :subscription_line_item - @line_item.destroy! - @line_item.order.recalculate + def update + authorize! :update, @line_item, @order + if @line_item.update(line_item_params) + render json: @line_item.to_json + else + render json: @line_item.errors.to_json, status: :unprocessable_entity + end + end - render json: @line_item.to_json - end + def destroy + authorize! :destroy, @line_item, @order + return render json: {}, status: :bad_request if @line_item.order.complete? - private + @line_item.destroy! + @line_item.order.recalculate - def line_item_params - params.require(:subscription_line_item).permit( - SolidusSubscriptions::PermittedAttributes.subscription_line_item_attributes - [:subscribable_id] - ) - end + render json: @line_item.to_json + end - def load_line_item - @line_item = SolidusSubscriptions::LineItem.find(params[:id]) + private + + def line_item_params + params.require(:subscription_line_item).permit( + SolidusSubscriptions::PermittedAttributes.subscription_line_item_attributes - [:subscribable_id] + ) + end + + def load_line_item + @line_item = SolidusSubscriptions::LineItem.find(params[:id]) + end + end + end end end diff --git a/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb b/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb index 8db616d..3f0c384 100644 --- a/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb +++ b/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb @@ -1,50 +1,53 @@ -class SolidusSubscriptions::Api::V1::SubscriptionsController < Spree::Api::BaseController - before_action :load_subscription, only: [:cancel, :update, :skip] - - protect_from_forgery unless: -> { request.format.json? } - - def update - if @subscription.update(subscription_params) - render json: @subscription.to_json(include: [:line_items, :shipping_address, :billing_address]) - else - render json: @subscription.errors.to_json, status: 422 - end - end - - def skip - if @subscription.skip - render json: @subscription.to_json - else - render json: @subscription.errors.to_json, status: 422 - end - end - - def cancel - if @subscription.cancel - render json: @subscription.to_json - else - render json: @subscription.errors.to_json, status: 422 +# frozen_string_literal: true + +module SolidusSubscriptions + module Api + module V1 + class SubscriptionsController < ::Spree::Api::BaseController + before_action :load_subscription, only: [:cancel, :update, :skip] + + protect_from_forgery unless: -> { request.format.json? } + + def update + if @subscription.update(subscription_params) + render json: @subscription.to_json(include: [:line_items, :shipping_address, :billing_address]) + else + render json: @subscription.errors.to_json, status: :unprocessable_entity + end + end + + def skip + if @subscription.skip + render json: @subscription.to_json + else + render json: @subscription.errors.to_json, status: :unprocessable_entity + end + end + + def cancel + if @subscription.cancel + render json: @subscription.to_json + else + render json: @subscription.errors.to_json, status: :unprocessable_entity + end + end + + private + + def load_subscription + @subscription = current_api_user.subscriptions.find(params[:id]) + end + + def subscription_params + params.require(:subscription).permit(SolidusSubscriptions.configuration.subscription_attributes | [ + line_items_attributes: line_item_attributes, + ]) + end + + def line_item_attributes + SolidusSubscriptions.configuration.subscription_line_item_attributes - [:subscribable_id] + [:id] + end + end end end - - private - - def load_subscription - @subscription = current_api_user.subscriptions.find(params[:id]) - end - - def subscription_params - params.require(:subscription).permit( - :interval_units, - :interval_length, - :end_date, - line_items_attributes: line_item_attributes, - shipping_address_attributes: Spree::PermittedAttributes.address_attributes, - billing_address_attributes: Spree::PermittedAttributes.address_attributes, - ) - end - - def line_item_attributes - SolidusSubscriptions::Config.subscription_line_item_attributes - [:subscribable_id] + [:id] - end end diff --git a/app/controllers/spree/admin/installments_controller.rb b/app/controllers/spree/admin/installments_controller.rb index e687746..97e6f4a 100644 --- a/app/controllers/spree/admin/installments_controller.rb +++ b/app/controllers/spree/admin/installments_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree module Admin class InstallmentsController < ResourceController @@ -9,8 +11,8 @@ module Spree @search = collection.ransack((params[:q] || {}).reverse_merge(s: 'created_at desc')) @installments = @search.result(distinct: true). - page(params[:page]). - per(params[:per_page] || Spree::Config[:orders_per_page]) + page(params[:page]). + per(params[:per_page] || Spree::Config[:orders_per_page]) end private diff --git a/app/controllers/spree/admin/subscription_events_controller.rb b/app/controllers/spree/admin/subscription_events_controller.rb index ad86887..71fa6f0 100644 --- a/app/controllers/spree/admin/subscription_events_controller.rb +++ b/app/controllers/spree/admin/subscription_events_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree module Admin class SubscriptionEventsController < ResourceController @@ -9,8 +11,8 @@ module Spree @search = collection.ransack((params[:q] || {}).reverse_merge(s: 'created_at desc')) @subscription_events = @search.result(distinct: true). - page(params[:page]). - per(params[:per_page] || 20) + page(params[:page]). + per(params[:per_page] || 20) end private diff --git a/app/controllers/spree/admin/subscriptions_controller.rb b/app/controllers/spree/admin/subscriptions_controller.rb index 596516d..cde63a0 100644 --- a/app/controllers/spree/admin/subscriptions_controller.rb +++ b/app/controllers/spree/admin/subscriptions_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree module Admin class SubscriptionsController < ResourceController @@ -5,12 +7,12 @@ module Spree def index @search = SolidusSubscriptions::Subscription. - accessible_by(current_ability, :index).ransack(params[:q]) + accessible_by(current_ability, :index).ransack(params[:q]) @subscriptions = @search.result(distinct: true). - includes(:line_items, :user). - page(params[:page]). - per(params[:per_page] || Spree::Config[:orders_per_page]) + includes(:line_items, :user). + page(params[:page]). + per(params[:per_page] || Spree::Config[:orders_per_page]) end def new @@ -29,9 +31,9 @@ module Spree if @subscription.payment_method&.source_required? @subscription.payment_source = @subscription - .payment_method - .payment_source_class - .find_by(id: params[:subscription][:payment_source_id]) + .payment_method + .payment_source_class + .find_by(id: params[:subscription][:payment_source_id]) else @subscription.payment_source = nil end @@ -45,11 +47,11 @@ module Spree @subscription.cancel end - if @subscription.errors.none? - notice = I18n.t('spree.admin.subscriptions.successfully_canceled') - else - notice = @subscription.errors.full_messages.to_sentence - end + notice = if @subscription.errors.none? + I18n.t('spree.admin.subscriptions.successfully_canceled') + else + @subscription.errors.full_messages.to_sentence + end redirect_back(fallback_location: spree.admin_subscriptions_path, notice: notice) end @@ -57,11 +59,11 @@ module Spree def activate @subscription.activate - if @subscription.errors.none? - notice = I18n.t('spree.admin.subscriptions.successfully_activated') - else - notice = @subscription.errors.full_messages.to_sentence - end + notice = if @subscription.errors.none? + I18n.t('spree.admin.subscriptions.successfully_activated') + else + @subscription.errors.full_messages.to_sentence + end redirect_back(fallback_location: spree.admin_subscriptions_path, notice: notice) end diff --git a/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb b/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb index d24de49..7872a2a 100644 --- a/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb +++ b/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Create new subscription line items associated to the current order, when # a line item is added to the cart which includes subscription_line_item # params. diff --git a/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb b/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb index 5925089..6b39b4c 100644 --- a/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb +++ b/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Create new subscription line items associated to the current order, when # a line item is added to the cart which includes subscription_line_item # params. diff --git a/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb b/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb index 2faead3..ccb483f 100644 --- a/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb +++ b/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Each Spree::LineItem can have multiple subscription_line_items. This # allows a cart to represent multiple subscriptions to the same item in # the same order. diff --git a/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb b/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb index 234929a..c67cb96 100644 --- a/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb +++ b/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions module Spree module Order diff --git a/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb b/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb index 6117c35..568e5a8 100644 --- a/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb +++ b/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Once an order is finalized its subscriptions line items should be converted # into active subscriptions. This hooks into Spree::Order#finalize! and # passes all subscription_line_items present on the order to the Subscription diff --git a/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb b/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb index 1d6fde4..3b48225 100644 --- a/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb +++ b/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Spree::Orders may contain many subscription_line_items. When the order is # finalized these subscription_line_items are converted into subscritpions. # The order needs to be able to get a list of associated subscription_line_items diff --git a/app/decorators/models/solidus_subscriptions/spree/product/delegate_subscribable.rb b/app/decorators/models/solidus_subscriptions/spree/product/delegate_subscribable.rb index cdabb7f..740b9d8 100644 --- a/app/decorators/models/solidus_subscriptions/spree/product/delegate_subscribable.rb +++ b/app/decorators/models/solidus_subscriptions/spree/product/delegate_subscribable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions module Spree module Product diff --git a/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb b/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb index 1d5524f..561e5a1 100644 --- a/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb +++ b/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Spree::Users maintain a list of the subscriptions associated with them module SolidusSubscriptions module Spree diff --git a/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb b/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb index 9b2a5f8..671dcbe 100644 --- a/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb +++ b/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions module Spree module Variant diff --git a/app/jobs/solidus_subscriptions/process_installments_job.rb b/app/jobs/solidus_subscriptions/process_installments_job.rb index ba54fa8..e7e1f63 100644 --- a/app/jobs/solidus_subscriptions/process_installments_job.rb +++ b/app/jobs/solidus_subscriptions/process_installments_job.rb @@ -1,22 +1,24 @@ +# frozen_string_literal: true + # This job is responsible for creating a consolidated installment from a # list of installments and processing it. module SolidusSubscriptions - class ProcessInstallmentsJob < ActiveJob::Base - queue_as Config.processing_queue + class ProcessInstallmentsJob < ApplicationJob + queue_as SolidusSubscriptions.configuration.processing_queue - # Process a collection of installments - # - # @param installment_ids [Array<Integer>] The ids of the - # installments to be processed together and fulfilled by the same order - # - # @return [Spree::Order] The order which fulfills the list of installments - def perform(installment_ids) - return if installment_ids.empty? + # Process a collection of installments + # + # @param installment_ids [Array<Integer>] The ids of the + # installments to be processed together and fulfilled by the same order + # + # @return [Spree::Order] The order which fulfills the list of installments + def perform(installment_ids) + return if installment_ids.empty? - installments = SolidusSubscriptions::Installment.where(id: installment_ids). - includes(subscription: [:line_items, :user]) - Checkout.new(installments).process - end + installments = SolidusSubscriptions::Installment.where(id: installment_ids). + includes(subscription: [:line_items, :user]) + Checkout.new(installments).process + end end end diff --git a/app/models/solidus_subscriptions/installment.rb b/app/models/solidus_subscriptions/installment.rb index 1cad63e..33146e6 100644 --- a/app/models/solidus_subscriptions/installment.rb +++ b/app/models/solidus_subscriptions/installment.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + # This class represents a single iteration of a subscription. It is fulfilled # by a completed order and maintains an association which tracks all attempts # successful or otherwise at fulfilling this installment module SolidusSubscriptions - class Installment < ActiveRecord::Base + class Installment < ApplicationRecord has_many :details, class_name: 'SolidusSubscriptions::InstallmentDetail' belongs_to( :subscription, @@ -30,9 +32,7 @@ module SolidusSubscriptions # object # # @return [SolidusSubscriptions::LineItemBuilder] - def line_item_builder - subscription.line_item_builder - end + delegate :line_item_builder, to: :subscription # Mark this installment as out of stock. # @@ -92,7 +92,7 @@ module SolidusSubscriptions # # @return [Boolean] def fulfilled? - details.where(success: true).exists? + details.exists?(success: true) end # Returns the state of this fulfillment @@ -126,8 +126,9 @@ module SolidusSubscriptions end def next_actionable_date - return if Config.reprocessing_interval.nil? - (DateTime.current + Config.reprocessing_interval).beginning_of_minute + return if SolidusSubscriptions.configuration.reprocessing_interval.nil? + + (DateTime.current + SolidusSubscriptions.configuration.reprocessing_interval).beginning_of_minute end end end diff --git a/app/models/solidus_subscriptions/installment_detail.rb b/app/models/solidus_subscriptions/installment_detail.rb index cfd4e70..cf5df73 100644 --- a/app/models/solidus_subscriptions/installment_detail.rb +++ b/app/models/solidus_subscriptions/installment_detail.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + # This class represents a single attempt to fulfill an installment. It will # indicate the result of that attempt. module SolidusSubscriptions - class InstallmentDetail < ActiveRecord::Base + class InstallmentDetail < ApplicationRecord belongs_to( :installment, class_name: 'SolidusSubscriptions::Installment', diff --git a/app/models/solidus_subscriptions/interval.rb b/app/models/solidus_subscriptions/interval.rb index 6c747cf..909ee0a 100644 --- a/app/models/solidus_subscriptions/interval.rb +++ b/app/models/solidus_subscriptions/interval.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This module is intended to be included into any active record # model which needs to be aware of how intervals are stored and # calculated in the db. diff --git a/app/models/solidus_subscriptions/line_item.rb b/app/models/solidus_subscriptions/line_item.rb index bd424fd..46b0a0e 100644 --- a/app/models/solidus_subscriptions/line_item.rb +++ b/app/models/solidus_subscriptions/line_item.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # The LineItem class is responsible for associating Line items to subscriptions. # It tracks the following values: # # [Spree::LineItem] :spree_line_item The spree object which created this instance @@ -15,7 +17,7 @@ # # [Integer] :installments How many subscription orders should be placed module SolidusSubscriptions - class LineItem < ActiveRecord::Base + class LineItem < ApplicationRecord include Interval belongs_to( @@ -32,7 +34,7 @@ module SolidusSubscriptions optional: true ) - validates :subscribable_id, presence: :true + validates :subscribable_id, presence: true validates :quantity, numericality: { greater_than: 0 } validates :interval_length, numericality: { greater_than: 0 }, unless: -> { subscription } diff --git a/app/models/solidus_subscriptions/subscription.rb b/app/models/solidus_subscriptions/subscription.rb index 2f23a6e..295d19b 100644 --- a/app/models/solidus_subscriptions/subscription.rb +++ b/app/models/solidus_subscriptions/subscription.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + # The subscription class is responsible for grouping together the # information required for the system to place a subscriptions order on # behalf of a specific user. module SolidusSubscriptions - class Subscription < ActiveRecord::Base + class Subscription < ApplicationRecord include Interval - PROCESSING_STATES = [:pending, :failed, :success] + PROCESSING_STATES = [:pending, :failed, :success].freeze belongs_to :user, class_name: "::#{::Spree.user_class}" has_many :line_items, class_name: 'SolidusSubscriptions::LineItem', inverse_of: :subscription @@ -18,7 +20,7 @@ module SolidusSubscriptions belongs_to :payment_method, class_name: '::Spree::PaymentMethod', optional: true belongs_to :payment_source, polymorphic: true, optional: true - validates :user, presence: :true + validates :user, presence: true validates :skip_count, :successive_skip_count, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :interval_length, numericality: { greater_than: 0 } validates :payment_method, presence: true, if: -> { payment_source } @@ -29,8 +31,8 @@ module SolidusSubscriptions accepts_nested_attributes_for :line_items, allow_destroy: true, reject_if: ->(p) { p[:quantity].blank? } before_validation :set_payment_method - before_update :update_actionable_date_if_interval_changed after_create :track_creation_event + before_update :update_actionable_date_if_interval_changed # Find all subscriptions that are "actionable"; that is, ones that have an # actionable_date in the past and are not invalid or canceled. @@ -57,7 +59,7 @@ module SolidusSubscriptions when :pending includes(:installments).where(solidus_subscriptions_installments: { id: nil }) else - raise ArgumentError.new("state must be one of: :success, :failed, :pending") + raise ArgumentError, "state must be one of: :success, :failed, :pending" end end) @@ -94,7 +96,7 @@ module SolidusSubscriptions state_machine :state, initial: :active do event :cancel do transition [:active, :pending_cancellation] => :canceled, - if: ->(subscription) { subscription.can_be_canceled? } + if: ->(subscription) { subscription.can_be_canceled? } transition active: :pending_cancellation end @@ -103,7 +105,7 @@ module SolidusSubscriptions event :deactivate do transition active: :inactive, - if: ->(subscription) { subscription.can_be_deactivated? } + if: ->(subscription) { subscription.can_be_deactivated? } end event :activate do @@ -134,7 +136,8 @@ module SolidusSubscriptions # pending cancellation will still be processed. def can_be_canceled? return true if actionable_date.nil? - (actionable_date - Config.minimum_cancellation_notice).future? + + (actionable_date - SolidusSubscriptions.configuration.minimum_cancellation_notice).future? end def skip(check_skip_limits: true) @@ -169,6 +172,7 @@ module SolidusSubscriptions # eligible to be processed. def next_actionable_date return nil unless active? + new_date = (actionable_date || Time.zone.now) (new_date + interval).beginning_of_minute end @@ -201,6 +205,7 @@ module SolidusSubscriptions # if the last installment was fulfilled. def processing_state return 'pending' if installments.empty? + installments.last.fulfilled? ? 'success' : 'failed' end @@ -227,17 +232,17 @@ module SolidusSubscriptions private def check_successive_skips_exceeded - return unless Config.maximum_successive_skips + return unless SolidusSubscriptions.configuration.maximum_successive_skips - if successive_skip_count >= Config.maximum_successive_skips + if successive_skip_count >= SolidusSubscriptions.configuration.maximum_successive_skips errors.add(:successive_skip_count, :exceeded) end end def check_total_skips_exceeded - return unless Config.maximum_total_skips + return unless SolidusSubscriptions.configuration.maximum_total_skips - if skip_count >= Config.maximum_total_skips + if skip_count >= SolidusSubscriptions.configuration.maximum_total_skips errors.add(:skip_count, :exceeded) end end @@ -245,10 +250,10 @@ module SolidusSubscriptions def update_actionable_date_if_interval_changed if persisted? && (interval_length_previously_changed? || interval_units_previously_changed?) base_date = if installments.any? - installments.last.created_at - else - created_at - end + installments.last.created_at + else + created_at + end new_date = interval.since(base_date) diff --git a/app/models/solidus_subscriptions/subscription_event.rb b/app/models/solidus_subscriptions/subscription_event.rb index 93552fd..8476470 100644 --- a/app/models/solidus_subscriptions/subscription_event.rb +++ b/app/models/solidus_subscriptions/subscription_event.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions class SubscriptionEvent < ApplicationRecord belongs_to :subscription, class_name: 'SolidusSubscriptions::Subscription', inverse_of: :events diff --git a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb index b78b96a..17d280e 100644 --- a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb +++ b/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions class SubscriptionOrderPromotionRule < ::Spree::PromotionRule # Promotion can be applied to an entire order. Will only be true diff --git a/app/models/solidus_subscriptions/subscription_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_promotion_rule.rb index 9a76136..fd99979 100644 --- a/app/models/solidus_subscriptions/subscription_promotion_rule.rb +++ b/app/models/solidus_subscriptions/subscription_promotion_rule.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions class SubscriptionPromotionRule < ::Spree::PromotionRule # Promotion can be applied to an entire order. Will only be true diff --git a/app/overrides/views/admin_subscribable_product_checkbox.rb b/app/overrides/views/admin_subscribable_product_checkbox.rb index 221ed1d..aafa2ef 100644 --- a/app/overrides/views/admin_subscribable_product_checkbox.rb +++ b/app/overrides/views/admin_subscribable_product_checkbox.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Deface::Override.new( virtual_path: "spree/admin/products/_form", name: "solidus_subscriptions_product_subscribable_checkbox", diff --git a/app/overrides/views/admin_subscribable_variant_checkbox.rb b/app/overrides/views/admin_subscribable_variant_checkbox.rb index afd44b7..744fee5 100644 --- a/app/overrides/views/admin_subscribable_variant_checkbox.rb +++ b/app/overrides/views/admin_subscribable_variant_checkbox.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Deface::Override.new( virtual_path: "spree/admin/variants/_form", name: "solidus_subscriptions_variant_subscribable_checkbox", diff --git a/app/overrides/views/admin_subscriptions_menu_link.rb b/app/overrides/views/admin_subscriptions_menu_link.rb index d709590..aaffe33 100644 --- a/app/overrides/views/admin_subscriptions_menu_link.rb +++ b/app/overrides/views/admin_subscriptions_menu_link.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if !Spree::Backend::Config.respond_to?(:menu_items) Deface::Override.new( virtual_path: 'spree/admin/shared/_menu', diff --git a/app/overrides/views/subscription_line_item_fields.rb b/app/overrides/views/subscription_line_item_fields.rb index 5870c0c..a076026 100644 --- a/app/overrides/views/subscription_line_item_fields.rb +++ b/app/overrides/views/subscription_line_item_fields.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Deface::Override.new( virtual_path: "spree/products/_cart_form", name: "subscription_line_item_fields", diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb index dbc3174..c40849a 100644 --- a/app/services/solidus_subscriptions/checkout.rb +++ b/app/services/solidus_subscriptions/checkout.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This class takes a collection of installments and populates a new spree # order with the correct contents based on the subscriptions associated to the # intallments. This is to group together subscriptions being @@ -31,13 +33,13 @@ module SolidusSubscriptions return if installments.empty? if checkout - Config.success_dispatcher_class.new(installments, order).dispatch + SolidusSubscriptions.configuration.success_dispatcher_class.new(installments, order).dispatch return order end # A new order will only have 1 payment that we created if order.payments.any?(&:failed?) - Config.payment_failed_dispatcher_class.new(installments, order).dispatch + SolidusSubscriptions.configuration.payment_failed_dispatcher_class.new(installments, order).dispatch installments.clear nil end @@ -45,7 +47,7 @@ module SolidusSubscriptions # Any installments that failed to be processed will be reprocessed unfulfilled_installments = installments.select(&:unfulfilled?) if unfulfilled_installments.any? - Config.failure_dispatcher_class. + SolidusSubscriptions.configuration.failure_dispatcher_class. new(unfulfilled_installments, order).dispatch end end @@ -101,10 +103,11 @@ module SolidusSubscriptions # They will be reprocessed later @installments -= unfulfilled_installments if unfulfilled_installments.any? - Config.out_of_stock_dispatcher_class.new(unfulfilled_installments).dispatch + SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new(unfulfilled_installments).dispatch end return if installments.empty? + order_builder.add_line_items(order_line_items) end diff --git a/app/services/solidus_subscriptions/dispatcher.rb b/app/services/solidus_subscriptions/dispatcher.rb index f52745d..b2162c0 100644 --- a/app/services/solidus_subscriptions/dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions class Dispatcher attr_reader :installments, :order diff --git a/app/services/solidus_subscriptions/failure_dispatcher.rb b/app/services/solidus_subscriptions/failure_dispatcher.rb index 13e15b6..4fdb828 100644 --- a/app/services/solidus_subscriptions/failure_dispatcher.rb +++ b/app/services/solidus_subscriptions/failure_dispatcher.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + # A handler for behaviour that should happen after installments are marked as # failures module SolidusSubscriptions class FailureDispatcher < Dispatcher def dispatch order.touch :completed_at - order.cancel! + order.cancel installments.each { |i| i.failed!(order) } super end diff --git a/app/services/solidus_subscriptions/line_item_builder.rb b/app/services/solidus_subscriptions/line_item_builder.rb index b288629..1d3ea9b 100644 --- a/app/services/solidus_subscriptions/line_item_builder.rb +++ b/app/services/solidus_subscriptions/line_item_builder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This class is responsible for taking SubscriptionLineItems and building # them into Spree::LineItems which can be added to an order module SolidusSubscriptions @@ -22,7 +24,7 @@ module SolidusSubscriptions line_items = subscription_line_items.map do |subscription_line_item| variant = subscribables.fetch(subscription_line_item.subscribable_id) - raise UnsubscribableError.new(variant) unless variant.subscribable? + raise UnsubscribableError, variant unless variant.subscribable? next unless variant.can_supply?(subscription_line_item.quantity) ::Spree::LineItem.new(variant: variant, quantity: subscription_line_item.quantity) diff --git a/app/services/solidus_subscriptions/order_builder.rb b/app/services/solidus_subscriptions/order_builder.rb index 30cf8a2..a577e98 100644 --- a/app/services/solidus_subscriptions/order_builder.rb +++ b/app/services/solidus_subscriptions/order_builder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This class is responsible for adding line items to order without going # through order contents. module SolidusSubscriptions diff --git a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb b/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb index 811e8eb..b9c181e 100644 --- a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb +++ b/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This service class is intended to provide callback behaviour to handle # the case where an installment cannot be processed due to lack of stock. module SolidusSubscriptions diff --git a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/payment_failed_dispatcher.rb index 9fb45fb..d404db0 100644 --- a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb +++ b/app/services/solidus_subscriptions/payment_failed_dispatcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This service class is intended to provide callback behaviour to handle # the case where a subscription order cannot be processed because a payment # failed @@ -5,8 +7,7 @@ module SolidusSubscriptions class PaymentFailedDispatcher < Dispatcher def dispatch order.touch :completed_at - order.cancel! - + order.cancel installments.each { |i| i.payment_failed!(order) } super end diff --git a/app/services/solidus_subscriptions/subscription_generator.rb b/app/services/solidus_subscriptions/subscription_generator.rb index cf3743f..8153912 100644 --- a/app/services/solidus_subscriptions/subscription_generator.rb +++ b/app/services/solidus_subscriptions/subscription_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This module is responsible for taking SolidusSubscriptions::LineItem # objects and creating SolidusSubscriptions::Subscription Objects module SolidusSubscriptions @@ -47,7 +49,7 @@ module SolidusSubscriptions subscription_line_items.group_by do |li| subscription_configuration(li) end. - values + values end private diff --git a/app/services/solidus_subscriptions/subscription_line_item_builder.rb b/app/services/solidus_subscriptions/subscription_line_item_builder.rb index 9dc0960..7354102 100644 --- a/app/services/solidus_subscriptions/subscription_line_item_builder.rb +++ b/app/services/solidus_subscriptions/subscription_line_item_builder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions module SubscriptionLineItemBuilder private @@ -14,7 +16,7 @@ module SolidusSubscriptions def subscription_params params.require(:subscription_line_item).permit( - SolidusSubscriptions::Config.subscription_line_item_attributes + SolidusSubscriptions.configuration.subscription_line_item_attributes ) end end diff --git a/app/services/solidus_subscriptions/success_dispatcher.rb b/app/services/solidus_subscriptions/success_dispatcher.rb index 3335222..ea4a8d3 100644 --- a/app/services/solidus_subscriptions/success_dispatcher.rb +++ b/app/services/solidus_subscriptions/success_dispatcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This service class is intended to provide callback behaviour to handle # an installment successfully being processed module SolidusSubscriptions diff --git a/app/services/solidus_subscriptions/unsubscribable_error.rb b/app/services/solidus_subscriptions/unsubscribable_error.rb index 3785983..b7e431f 100644 --- a/app/services/solidus_subscriptions/unsubscribable_error.rb +++ b/app/services/solidus_subscriptions/unsubscribable_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This error should be raised if a user attempts to subscribe to a item which # is not subscribable module SolidusSubscriptions diff --git a/app/services/solidus_subscriptions/user_mismatch_error.rb b/app/services/solidus_subscriptions/user_mismatch_error.rb index c3bf513..1d227ca 100644 --- a/app/services/solidus_subscriptions/user_mismatch_error.rb +++ b/app/services/solidus_subscriptions/user_mismatch_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusSubscriptions class UserMismatchError < StandardError def initialize(installments) |