Module: Foodsoft::ExpansionVariables

Defined in:
lib/foodsoft/expansion_variables.rb

Overview

Variable expansions for user-specified texts.

This is used in wiki-pages and the footer, for example, to allow foodcoops to show dynamic information in the text.

Plugins can modify the variables by means of the ‘#variables` accessor. Please be thoughful when choosing names as to avoid collisions. Do not put non-public info in variables.

Constant Summary collapse

ACTIVE_MONTHS =
3

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_ordergroup_countNumber



63
64
65
66
67
# File 'lib/foodsoft/expansion_variables.rb', line 63

def self.active_ordergroup_count
  GroupOrder
    .where('updated_on > ?', ACTIVE_MONTHS.months.ago)
    .select(:ordergroup_id).distinct.count
end

.active_supplier_countNumber



70
71
72
73
74
# File 'lib/foodsoft/expansion_variables.rb', line 70

def self.active_supplier_count
  Order
    .where('starts > ?', ACTIVE_MONTHS.months.ago)
    .select(:supplier_id).distinct.count
end

.active_suppliersString



77
78
79
80
81
82
# File 'lib/foodsoft/expansion_variables.rb', line 77

def self.active_suppliers
  Supplier.joins(:orders)
          .where('orders.starts > ?', ACTIVE_MONTHS.months.ago)
          .order(:name).distinct
          .pluck(:name).join(', ')
end

.expand(str, options = {}) ⇒ String

Expand variables in a string



56
57
58
59
60
# File 'lib/foodsoft/expansion_variables.rb', line 56

def self.expand(str, options = {})
  str.gsub(/{{([._a-zA-Z0-9]+)}}/) do
    options[::Regexp.last_match(1)] || get(::Regexp.last_match(1))
  end
end

.get(var) ⇒ String

Return expanded variable



47
48
49
50
# File 'lib/foodsoft/expansion_variables.rb', line 47

def self.get(var)
  s = @variables[var.to_s]
  s.respond_to?(:call) ? s.call : s.to_s
end

Instance Method Details

#variablesHash



14
# File 'lib/foodsoft/expansion_variables.rb', line 14

cattr_accessor :variables