Last active
December 11, 2015 17:48
-
-
Save DanielWright/4637075 to your computer and use it in GitHub Desktop.
Localized buy-online link localization
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
require 'dynamo/country' | |
require 'aldo/online_store_locale' | |
module VariantsHelper | |
def where_to_buy(variant) | |
content_tag(:ul) do | |
list_items = [] | |
if tag = buy_online_tag(variant) | |
list_items << content_tag(:li, tag, :class => t(:'.buy_online').parameterize) | |
list_items << content_tag(:li) { link_to t(:'.find_a_store'), locations_path } | |
list_items << content_tag(:li, t(:'.availability_notice'), :class => :'availability-notice') if outside_availability_zones? | |
else | |
list_items << content_tag(:li, t(:'.only_available_in', :countries => variant_countries(variant).to_sentence)) | |
end | |
list_items.join.html_safe | |
end | |
end | |
def buy_online_tag(variant) | |
url = location_aware_store_url(variant) | |
link_to(t(:'.buy_online'), url, :target => :blank) if url | |
end | |
def location_aware_store_url(variant) | |
locale = outside_availability_zones? ? :US : look_up_store_locale | |
variant.link_for(locale.downcase) | |
end | |
private | |
def variant_countries(variant) | |
variant.find_available_countries.collect { |c| t(:"countries.#{c}") } | |
end | |
def look_up_country_code | |
session[:country_code] ||= Dynamo::Country.find(request.ip).code | |
end | |
def outside_availability_zones? | |
look_up_country_code == Dynamo::Country.default.code | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment