From ff4ab44bcc776bae706c650220c6326e60223a54 Mon Sep 17 00:00:00 2001 From: benni Date: Sat, 29 Sep 2012 17:52:25 +0200 Subject: [PATCH] Add option to restrict ordering when not enough apples. --- app/controllers/group_orders_controller.rb | 9 ++++ app/models/ordergroup.rb | 9 ++++ app/views/home/_apple_bar.html.haml | 14 +++++++ app/views/home/_stats.erb | 27 ------------ app/views/home/index.html.haml | 2 +- app/views/shared/_open_orders.html.haml | 3 ++ config/app_config.yml.SAMPLE | 4 ++ config/locales/de.yml | 6 +++ lib/apple_bar.rb | 48 ++++++++++++++++++++++ public/stylesheets/main.css | 5 +++ public/stylesheets/sass/main.sass | 5 +++ 11 files changed, 104 insertions(+), 28 deletions(-) create mode 100644 app/views/home/_apple_bar.html.haml delete mode 100644 app/views/home/_stats.erb create mode 100644 lib/apple_bar.rb diff --git a/app/controllers/group_orders_controller.rb b/app/controllers/group_orders_controller.rb index 4b8811f4..beaa2b6c 100644 --- a/app/controllers/group_orders_controller.rb +++ b/app/controllers/group_orders_controller.rb @@ -5,6 +5,7 @@ class GroupOrdersController < ApplicationController before_filter :ensure_ordergroup_member before_filter :ensure_open_order, :only => [:new, :create, :edit, :update, :order, :stock_order, :saveOrder] before_filter :ensure_my_group_order, only: [:show, :edit, :update] + before_filter :enough_apples?, only: [:new, :create] # Index page. def index @@ -88,4 +89,12 @@ class GroupOrdersController < ApplicationController redirect_to group_orders_url, alert: 'Fehlerhafte URL, das ist nicht Deine Bestellung.' end + def enough_apples? + if @ordergroup.not_enough_apples? + redirect_to group_orders_url, + alert: t('not_enough_apples', scope: 'group_orders.messages', apples: @ordergroup.apples, + stop_ordering_under: FoodsoftConfig[:stop_ordering_under]) + end + end + end diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index c1a7d4f2..7be7f2f4 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -72,6 +72,15 @@ class Ordergroup < Group ((avg_jobs_per_euro / Ordergroup.avg_jobs_per_euro) * 100).to_i rescue 0 end + # If the the option stop_ordering_under is set, the ordergroup is only allowed to participate in an order, + # when the apples value is above the configured amount. + # Only ordergroups, which have participated in more than 5 order are affected + def not_enough_apples? + FoodsoftConfig[:stop_ordering_under].present? and + apples < FoodsoftConfig[:stop_ordering_under] and + group_orders.count > 5 + end + # Global average def self.avg_jobs_per_euro stats = Ordergroup.all.collect(&:stats) diff --git a/app/views/home/_apple_bar.html.haml b/app/views/home/_apple_bar.html.haml new file mode 100644 index 00000000..13171110 --- /dev/null +++ b/app/views/home/_apple_bar.html.haml @@ -0,0 +1,14 @@ +Engagement Deiner Bestellgruppe +.stats-bar{style: "width:#{apple_bar.length_of_group_bar}px; background-color:#{apple_bar.group_bar_color}"} + = apple_bar.apples + = " Äpfel" if apple_bar.length_of_group_bar > 50 + +Durchschnittsengagement +.stats-bar{style: "width:#{apple_bar.length_of_global_bar}px"} + 100 Birnen + +%span.description + Abgebildet ist das Verhältnis von erledigten Aufgaben zu dem Bestellvolumen Deiner Bestellgruppe im Vergleich zum Durchschnitt in der Foodcoop. + Konkret: Pro #{number_to_currency(apple_bar.mean_order_amount_per_job, :precision => 0 )} Bestellsumme solltest Du eine Aufgabe machen! + - if FoodsoftConfig[:stop_ordering_under].present? + Achtung, hast Du weniger als #{FoodsoftConfig[:stop_ordering_under]} Äpfel, darfst Du nicht mehr bestellen! \ No newline at end of file diff --git a/app/views/home/_stats.erb b/app/views/home/_stats.erb deleted file mode 100644 index 9f06fa0a..00000000 --- a/app/views/home/_stats.erb +++ /dev/null @@ -1,27 +0,0 @@ -<% -max_width = 600 - -global_avg = Ordergroup.avg_jobs_per_euro -group_avg = ordergroup.avg_jobs_per_euro.to_f - -unless global_avg == 0 or global_avg.nan? - length_of_global_bar = max_width / 2.0 - length_of_group_bar = (group_avg / global_avg) * length_of_global_bar - - length_of_group_bar = max_width if length_of_group_bar > max_width - - color = group_avg >= global_avg ? "#78b74e" : "red" -%> - Engagement Deiner Bestellgruppe -
- <%= ordergroup.apples -%><%= " Äpfel" if length_of_group_bar > 50 -%> -
- Durchschnittsengagement -
- 100 Birnen -
- - Abgebildet ist das Verhältnis von erledigten Aufgaben zu dem Bestellvolumen Deiner Bestellgruppe im Vergleich zum Durchschnitt in der Foodcoop. - Konkret: Pro <%= number_to_currency( (1/global_avg).round, :precision => 0 ) %> Bestellsumme solltest Du eine Aufgabe machen! - -<%- end -%> diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 58fce56c..d39cb9f8 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -36,7 +36,7 @@ .box_title %h2 Engagement Deiner Bestellgruppe .column_content - = render :partial => "stats", :locals => {:ordergroup => current_user.ordergroup} + = render :partial => "apple_bar", :locals => {:apple_bar => AppleBar.new(current_user.ordergroup)} - unless Message.public.empty? .box_title diff --git a/app/views/shared/_open_orders.html.haml b/app/views/shared/_open_orders.html.haml index b301ec06..9a8b68f6 100644 --- a/app/views/shared/_open_orders.html.haml +++ b/app/views/shared/_open_orders.html.haml @@ -1,6 +1,9 @@ .box_title %h2 Laufende Bestellungen .column_content + - if ordergroup.not_enough_apples? + .warning + %p Achtung, Deine Bestellgruppe hat zu wenig Äpfel um Bestellen zu können! - unless Order.open.empty? %table.list %thead diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index 218d40b7..01b191e5 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -38,6 +38,10 @@ default: &defaults # for total article price as long as the order is not finished. tolerance_is_costly: false + # Ordergroups, which have less than 75 apples should not be allowed to make new orders + # Comment out this option to activate this restriction + # stop_ordering_under: 75 + # email address to be used as sender email_sender: foodsoft@myfoodcoop.org diff --git a/config/locales/de.yml b/config/locales/de.yml index be0f7d52..50d57eb4 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -4,6 +4,12 @@ de: home: index: title: Startseite + + group_orders: + messages: + not_enough_apples: + Um zu Bestellen brauchst Du mindestends %{stop_ordering_under} Äpfel. + Momentan hat Deine Bestellgruppe aber nur %{apples} Äpfel. date: abbr_day_names: - So diff --git a/lib/apple_bar.rb b/lib/apple_bar.rb new file mode 100644 index 00000000..057212a1 --- /dev/null +++ b/lib/apple_bar.rb @@ -0,0 +1,48 @@ +class AppleBar + + BAR_MAX_WITH = 600 + + def initialize(ordergroup) + @ordergroup = ordergroup + @group_avg = ordergroup.avg_jobs_per_euro.to_f + @global_avg = Ordergroup.avg_jobs_per_euro + end + + def length_of_global_bar + BAR_MAX_WITH / 2.0 + end + + def length_of_group_bar + length = (@group_avg / @global_avg) * length_of_global_bar + length > BAR_MAX_WITH ? BAR_MAX_WITH : length + end + + # Show group bar in following colors: + # Green if higher than 100 + # Yellow if lower than 100 an higher than stop_ordering_under option value + # Red if below stop_ordering_under, the ordergroup isn't allowed to participate in an order anymore + def group_bar_color + if apples >= 100 + "#78b74e" + else + if FoodsoftConfig[:stop_ordering_under].present? and + apples >= FoodsoftConfig[:stop_ordering_under] + 'yellow' + else + 'red' + end + end + end + + def mean_order_amount_per_job + (1/@global_avg).round + end + + def apples + @apples ||= @ordergroup.apples + end + + def with_restriction? + FoodsoftConfig[:stop_ordering_under].present? + end +end \ No newline at end of file diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index e7a0b118..fd169ef2 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -96,6 +96,11 @@ option { .hidden { display: none; } +.warning { + background: yellow; + font-weight: bold; + padding: 1px 10px; } + #login { margin: auto; width: 35em; diff --git a/public/stylesheets/sass/main.sass b/public/stylesheets/sass/main.sass index 0e2fadc5..5faa9d1d 100644 --- a/public/stylesheets/sass/main.sass +++ b/public/stylesheets/sass/main.sass @@ -104,6 +104,11 @@ option .hidden display: none +.warning + background: yellow + font-weight: bold + padding: 1px 10px + // ********************************* loginpage #login :margin auto