Skip to content

Instantly share code, notes, and snippets.

@DanielWright
Last active December 11, 2015 17:48
Show Gist options
  • Save DanielWright/4637075 to your computer and use it in GitHub Desktop.
Save DanielWright/4637075 to your computer and use it in GitHub Desktop.
Localized buy-online link localization
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