Skip to content

Commit

Permalink
On foodcoops#764: Switched to enum for config options
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Oct 17, 2020
1 parent 8381a11 commit 1dddd96
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/models/group_order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,18 @@ def calculate_result(total = nil)
order_quantities = GroupOrderArticleQuantity.where(group_order_article_id: order_article.group_order_article_ids).order('created_on')
logger.debug "GroupOrderArticleQuantity records found: #{order_quantities.size}"

first_order_first_serve = (FoodsoftConfig[:distribution_strategy] == FoodsoftConfig::DistributionStrategy::FIRST_ORDER_FIRST_SERVE)

# Determine quantities to be ordered...
order_quantities.each do |goaq|
q = goaq.quantity
q = [q, total - total_quantity].min if FoodsoftConfig[:distribution_strategy] == :first_order_first_serve
q = [q, total - total_quantity].min if first_order_first_serve
total_quantity += q
if goaq.group_order_article_id == self.id
logger.debug "increasing quantity by #{q}"
quantity += q
end
break if total_quantity >= total && FoodsoftConfig[:distribution_strategy] == :first_order_first_serve
break if total_quantity >= total && first_order_first_serve
end

# Determine tolerance to be ordered...
Expand Down
1 change: 0 additions & 1 deletion app/serializers/config_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class ConfigSerializer < ActiveModel::Serializer

# details
attributes :name, :homepage, :contact

Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/configs/_tab_others.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= config_input form, :use_nick, as: :boolean
= config_input form, :tolerance_is_costly, as: :boolean
- distribution_options = [[t('config.keys.distribution_strategy_options.first_order_first_serve'), :first_order_first_serve],
[t('config.keys.distribution_strategy_options.no_automatic_distribution'), :no_automatic_distribution]]
- distribution_options = [[t('config.keys.distribution_strategy_options.first_order_first_serve'), FoodsoftConfig::DistributionStrategy::FIRST_ORDER_FIRST_SERVE],
[t('config.keys.distribution_strategy_options.no_automatic_distribution'), FoodsoftConfig::DistributionStrategy::NO_AUTOMATIC_DISTRIBUTION]]
= config_input form, :distribution_strategy, as: :select, collection: distribution_options,
include_blank: false, input_html: {class: 'input-xxlarge'}
= config_input form, :disable_invite, as: :boolean
Expand Down
8 changes: 7 additions & 1 deletion lib/foodsoft_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class FoodsoftConfig
# Loaded configuration
APP_CONFIG = ActiveSupport::HashWithIndifferentAccess.new

# distribution strategy config values enum
module DistributionStrategy
FIRST_ORDER_FIRST_SERVE = 'first_order_first_serve'
NO_AUTOMATIC_DISTRIBUTION = 'no_automatic_distribution'
end

class << self

# Load and initialize foodcoop configuration file.
Expand Down Expand Up @@ -260,7 +266,7 @@ def get_default_config
tasks_period_days: 7,
tasks_upfront_days: 49,
shared_supplier_article_sync_limit: 200,
distribution_strategy: :first_order_first_serve,
distribution_strategy: FoodsoftConfig::DistributionStrategy::FIRST_ORDER_FIRST_SERVE,
# The following keys cannot, by default, be set by foodcoops themselves.
protected: {
multi_coop_install: true,
Expand Down

0 comments on commit 1dddd96

Please sign in to comment.