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
@@variables =

Hash of variables. Note that keys are Strings.

{
  'scope'                   => ->{ FoodsoftConfig.scope },
  'name'                    => ->{ FoodsoftConfig[:name] },
  'contact.street'          => ->{ FoodsoftConfig[:contact][:street] },
  'contact.zip_code'        => ->{ FoodsoftConfig[:contact][:zip_code] },
  'contact.city'            => ->{ FoodsoftConfig[:contact][:city] },
  'contact.country'         => ->{ FoodsoftConfig[:contact][:country] },
  'contact.email'           => ->{ FoodsoftConfig[:contact][:email] },
  'contact.phone'           => ->{ FoodsoftConfig[:contact][:phone] },
  'price_markup'            => ->{ FoodsoftConfig[:price_markup] },
  'homepage'                => ->{ FoodsoftConfig[:homepage] },
   'help_url'                => ->{ FoodsoftConfig[:help_url] },
  'applepear_url'           => ->{ FoodsoftConfig[:applepear_url] },
   'foodsoft.url'            => ->{ FoodsoftConfig[:foodsoft_url] },
  'foodsoft.version'        => Foodsoft::VERSION,
  'foodsoft.revision'       => Foodsoft::REVISION,
   'user_count'              => ->{ User.undeleted.count },
  'ordergroup_count'        => ->{ Ordergroup.undeleted.count },
  'active_ordergroup_count' => ->{ active_ordergroup_count },
  'supplier_count'          => ->{ Supplier.undeleted.count },
  'active_supplier_count'   => ->{ active_supplier_count },
  'active_suppliers'        => ->{ active_suppliers },
  'first_order_date'        => ->{ I18n.l Order.first.try{|o| o.starts.to_date} }
}

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


65
66
67
68
69
# File 'lib/foodsoft/expansion_variables.rb', line 65

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


72
73
74
75
76
# File 'lib/foodsoft/expansion_variables.rb', line 72

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


79
80
81
82
83
84
# File 'lib/foodsoft/expansion_variables.rb', line 79

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


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

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

.get(var) ⇒ String

Return expanded variable

Returns:

  • (String)

    Expanded variable


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

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


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

cattr_accessor :variables