From 9df483cf4e25bb5d2e52af6439b31d548fcb294a Mon Sep 17 00:00:00 2001 From: wvengen Date: Mon, 19 Jan 2015 01:48:42 +0100 Subject: [PATCH] Add foodsoft expansion variables for wiki --- lib/foodsoft/expansion_variables.rb | 87 +++++++++++++++++++ plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb | 4 + 2 files changed, 91 insertions(+) create mode 100644 lib/foodsoft/expansion_variables.rb diff --git a/lib/foodsoft/expansion_variables.rb b/lib/foodsoft/expansion_variables.rb new file mode 100644 index 00000000..ad00c1b7 --- /dev/null +++ b/lib/foodsoft/expansion_variables.rb @@ -0,0 +1,87 @@ +module Foodsoft + # 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. + module ExpansionVariables + + ACTIVE_MONTHS = 3 + + # @return [Hash] Variables and their values + cattr_accessor :variables + + # Hash of variables. Note that keys are Strings. + @@variables = { + '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.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.created_at.to_date } + } + + # Return expanded variable + # @return [String] Expanded variable + def self.get(var) + s = @@variables[var.to_s] + s.respond_to?(:call) ? s.call : s.to_s + end + + # Expand variables in a string + # @param str [String] String to expand variables in + # @param options [Hash] Extra variables to expand + # @return [String] Expanded string + def self.expand(str, options = {}) + str.gsub /{{([._a-zA-Z0-9]+)}}/ do + options[$1] || self.get($1) + end + end + + + # @return [Number] Number of ordergroups that have been active in the past 3 months + def self.active_ordergroup_count + GroupOrder + .where('updated_on > ?', ACTIVE_MONTHS.months.ago) + .select(:ordergroup_id).distinct.count + end + + # @return [Number] Number of suppliers that has been ordered from in the past 3 months + def self.active_supplier_count + Order + .where('starts > ?', ACTIVE_MONTHS.months.ago) + .select(:supplier_id).distinct.count + end + + # @return [String] Comma-separated list of suppliers that has been ordered from in the past 3 months + def self.active_suppliers + Supplier.joins(:orders) + .where('orders.starts > ?', ACTIVE_MONTHS.months.ago) + .order(:name).select(:name).distinct + .map(&:name).join(', ') + end + + end +end diff --git a/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb b/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb index 2f425dde..95e471e2 100644 --- a/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb +++ b/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb @@ -1,6 +1,10 @@ module FoodsoftWiki class WikiParser < WikiCloth::Parser + template do |template| + Foodsoft::ExpansionVariables.get(template) + end + url_for do |page| url_for page end