Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Bug 1010818 - Make console cost aware
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Sep 23, 2013
1 parent a8a8ceb commit 387441d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 91 deletions.
2 changes: 1 addition & 1 deletion console/app/controllers/application_types_controller.rb
@@ -1,5 +1,5 @@
class ApplicationTypesController < ConsoleController
include Console::ModelHelper
include CostAware

def index
@capabilities = user_capabilities
Expand Down
54 changes: 54 additions & 0 deletions console/app/controllers/cost_aware.rb
@@ -0,0 +1,54 @@
# encoding: UTF-8
module CostAware
extend ActiveSupport::Concern

included do
include Console::CostHelper
helper_method :user_currency_symbol, :user_currency_cd, :number_to_user_currency
end

protected
def user_currency_cd
:usd
end

def user_currency_symbol
case user_currency_cd
when "eur"
"€"
when "cad"
"C$"
else
"$"
end
end

def number_to_user_currency(number)
return nil if number.nil?

case user_currency_cd
when 'eur'
unit = "€"
format = "%u %n"
when 'cad'
unit = "C$"
format = "%u%n"
else
unit = "$"
format = "%u%n"
end

options = {}
options[:unit] = unit
options[:format] = format
number_to_currency(number, options)
end

def gear_increase_cost(count, capabilities)
false
end

def gear_types_with_cost
[]
end
end
4 changes: 4 additions & 0 deletions console/app/helpers/console/console_helper.rb
Expand Up @@ -8,6 +8,10 @@ def logout_path
def outage_notification
end

def user_currency_symbol
"$"
end

def product_branding
content_tag(:span, "<strong>Open</strong>Shift Origin".html_safe, :class => 'brand-text headline')
end
Expand Down
78 changes: 78 additions & 0 deletions console/app/helpers/console/cost_helper.rb
@@ -0,0 +1,78 @@
module Console::CostHelper
def gear_increase_indicator(cartridges, scales, gear_type, existing, capabilities)
range = scales ? gear_estimate_for_scaled_app(cartridges) : (existing ? 0..0 : 1..1)
min = range.begin
max = range.end
increasing = (min > 0 || max > 0)

cost, title =
if gear_increase_cost(min, capabilities)
[true, "This will add #{pluralize(min, 'gear')} to your account and will result in additional charges."]
elsif gear_increase_cost(max, capabilities)
[true, "This will add at least #{pluralize(min, 'gear')} to your account and may result in additional charges."]
elsif !increasing
[false, "No gears will be added to your account."]
else
[false, "This will add #{pluralize(min, 'gear')} to your account."]
end
if cartridges_premium(cartridges)
cost = true
title = "#{title} Additional charges may be accrued for premium cartridges."
end
if increasing && gear_types_with_cost.include?(gear_type)
cost = true
title = "#{title} The selected gear type will have additional hourly charges."
end

content_tag(:span,
[
(if max == Float::INFINITY
"+#{min}-?"
elsif max != min
"+#{min}-#{max}"
else
"+#{min}"
end),
"<span data-icon=\"\ue014\" aria-hidden=\"true\"> </span>",
("<span class=\"label label-premium\">#{user_currency_symbol}</span>" if cost),
].compact.join(' ').html_safe,
:class => 'indicator-gear-increase',
:title => title,
)
end

def cartridges_premium(cartridges)
return cartridges.any?{ |(_, carts)| carts.any?{ |c| c.usage_rates.present? } } if cartridges.is_a? Hash
end
def gear_estimate_for_scaled_app(cartridges)
min = 0
max = 0
if cartridges.present?
cartridges.each_pair do |_, carts|
any = false
all = true
variable = false
carts.each do |cart|
if cart.service? || cart.web_framework?
any = true
elsif cart.custom?
variable = any = true
else
all = false
break if any
end
end
max += 1 if any
min += 1 if all && !variable
end
else
min = 1
max = Float::INFINITY
end
Range.new(min,max)
end

def usage_rate_indicator
content_tag :span, user_currency_symbol, :class => "label label-premium", :title => 'May include additional usage fees at certain levels, see plan for details.'
end
end
88 changes: 0 additions & 88 deletions console/app/helpers/console/model_helper.rb
@@ -1,5 +1,4 @@
module Console::ModelHelper

def cartridge_info(cartridge, application)
case
when cartridge.jenkins_client?
Expand Down Expand Up @@ -159,93 +158,6 @@ def warn_may_not_scale(type, capabilities)
end
end

def gear_increase_indicator(cartridges, scales, gear_type, existing, capabilities)
range = scales ? gear_estimate_for_scaled_app(cartridges) : (existing ? 0..0 : 1..1)
min = range.begin
max = range.end
increasing = (min > 0 || max > 0)

cost, title =
if gear_increase_cost(min, capabilities)
[true, "This will add #{pluralize(min, 'gear')} to your account and will result in additional charges."]
elsif gear_increase_cost(max, capabilities)
[true, "This will add at least #{pluralize(min, 'gear')} to your account and may result in additional charges."]
elsif !increasing
[false, "No gears will be added to your account."]
else
[false, "This will add #{pluralize(min, 'gear')} to your account."]
end
if cartridges_premium(cartridges)
cost = true
title = "#{title} Additional charges may be accrued for premium cartridges."
end
if increasing && gear_types_with_cost.include?(gear_type)
cost = true
title = "#{title} The selected gear type will have additional hourly charges."
end

content_tag(:span,
[
(if max == Float::INFINITY
"+#{min}-?"
elsif max != min
"+#{min}-#{max}"
else
"+#{min}"
end),
"<span data-icon=\"\ue014\" aria-hidden=\"true\"> </span>",
("<span class=\"label label-premium\">#{user_currency_symbol}</span>" if cost),
].compact.join(' ').html_safe,
:class => 'indicator-gear-increase',
:title => title,
)
end

def cartridges_premium(cartridges)
false
end
def gear_increase_cost(count, capabilities)
false
end
def gear_types_with_cost
[]
end
def gear_estimate_for_scaled_app(cartridges)
min = 0
max = 0
if cartridges.present?
cartridges.each_pair do |_, carts|
any = false
all = true
variable = false
carts.each do |cart|
if cart.service? || cart.web_framework?
any = true
elsif cart.custom?
variable = any = true
else
all = false
break if any
end
end
max += 1 if any
min += 1 if all && !variable
end
else
min = 1
max = Float::INFINITY
end
Range.new(min,max)
end

def user_currency_symbol
"$"
end

def usage_rate_indicator
content_tag :span, user_currency_symbol, :class => "label label-premium", :title => 'May include additional usage fees at certain levels, see plan for details.'
end

def in_groups_by_tag(ary, tags)
groups = {}
other = ary.reject do |t|
Expand Down
4 changes: 2 additions & 2 deletions console/app/views/application_types/show.html.haml
Expand Up @@ -224,7 +224,7 @@

= f.buttons do
= link_to "Back", application_types_path, :class => 'btn'
= f.commit_button :button_html => { :disabled => @disabled }
= f.commit_button :button_html => { :disabled => @disabled }
%span#gear_increase
= gear_increase_indicator(@cartridges, @application.scales?, @application.gear_profile, false, @capabilities)
= f.loading
Expand Down Expand Up @@ -278,7 +278,7 @@
var fields = $.grep(form.serializeArray(), function(field) {
return ['application[scale]', 'application[cartridges][]', 'application_type[cartridges][]', 'application[gear_profile]', 'unlock'].indexOf(field.name) != -1;
});
return "#{estimate_application_type_path(@application_type.id)}" + '?' + $.param(fields);
return "#{estimate_application_type_path(@application_type.id)}" + '?' + $.param(fields);
}
$('#application_scale, #application_gear_profile').change(function() {
$("#gear_increase").load(estimateUrl($(this).closest('form')));
Expand Down

0 comments on commit 387441d

Please sign in to comment.