Module: Foodsoft::ExpansionVariables

Defined in:
app/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

Returns Number of ordergroups that have been active in the past 3 months.

Returns:

  • (Number)

    Number of ordergroups that have been active in the past 3 months



63
64
65
66
67
# File 'app/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

Returns Number of suppliers that has been ordered from in the past 3 months.

Returns:

  • (Number)

    Number of suppliers that has been ordered from in the past 3 months



70
71
72
73
74
# File 'app/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

Returns Comma-separated list of suppliers that has been ordered from in the past 3 months.

Returns:

  • (String)

    Comma-separated list of suppliers that has been ordered from in the past 3 months



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

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

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

Expand variables in a string

Parameters:

  • str (String)

    String to expand variables in

  • options (Hash<String, String>) (defaults to: {})

    Extra variables to expand

Returns:

  • (String)

    Expanded string



56
57
58
59
60
# File 'app/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

Returns:

  • (String)

    Expanded variable



47
48
49
50
# File 'app/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

Returns Variables and their values.

Returns:

  • (Hash)

    Variables and their values



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

cattr_accessor :variables