mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
There were a few changes needed: * Plugins are now specified through `plugin:` config keyword. * All plugin gems need to be specified explicitly in Gemfile since they are no longer dependencies of plugins already specified explicitly. * All plugin gems need to be updated in other to use the new APIs. * One cop was renamed. * New offenses safe to correct were corrected directly with `bundle exec rubocop -a`. * New offenses unsafe to correct were added to the TODO configuration with `bundle exec rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 1400 --no-auto-gen-timestamp`.
57 lines
1.4 KiB
Ruby
57 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Spree
|
|
class Calculator < ApplicationRecord
|
|
self.belongs_to_required_by_default = false
|
|
|
|
belongs_to :calculable, polymorphic: true
|
|
|
|
# This method must be overriden in concrete calculator.
|
|
#
|
|
# It should return amount computed based on #calculable and/or optional parameter
|
|
def compute(_something = nil)
|
|
raise NotImplementedError, 'please use concrete calculator'
|
|
end
|
|
|
|
# overwrite to provide description for your calculators
|
|
def self.description
|
|
'Base Calculator'
|
|
end
|
|
|
|
###################################################################
|
|
|
|
def self.register(*klasses); end
|
|
|
|
# Returns all calculators applicable for kind of work
|
|
def self.calculators
|
|
Rails.application.config.spree.calculators
|
|
end
|
|
|
|
def to_s
|
|
self.class.name.titleize.gsub("Calculator/", "")
|
|
end
|
|
|
|
delegate :description, to: :class
|
|
|
|
def available?(_object)
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
# Given an object which might be an Order or a LineItem (amongst
|
|
# others), return a collection of line items.
|
|
def line_items_for(object)
|
|
return [object] if object.is_a?(Spree::LineItem)
|
|
|
|
if object.respond_to? :line_items
|
|
object.line_items
|
|
elsif object.respond_to?(:order) && object.order.present?
|
|
object.order.line_items
|
|
else
|
|
[object]
|
|
end
|
|
end
|
|
end
|
|
end
|