This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1010818 - Make console cost aware
- Loading branch information
1 parent
a8a8ceb
commit 387441d
Showing
6 changed files
with
139 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters