Skip to content

Instantly share code, notes, and snippets.

@maier-stefan
Last active August 29, 2015 14:05
Show Gist options
  • Save maier-stefan/b7eb526532c51eef6249 to your computer and use it in GitHub Desktop.
Save maier-stefan/b7eb526532c51eef6249 to your computer and use it in GitHub Desktop.
Scope Language and Shipping Country
Routes:
scope ":country", country: /#{Country.pluck(:code2).join("|")}/ do
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root :to => "pages#home"
end
end
#Fallback
get '*path', to: redirect("/de/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/de/#{I18n.default_locale}/" }
get '', to: redirect("/de/#{I18n.default_locale}")
Application Controller:
include CurrentCart
protect_from_forgery with: :exception
before_filter :set_locale, :set_shipping_to
before_filter :configure_permitted_parameters, if: :devise_controller?
before_action :set_cart
def change_shipping_country
if params[:germany_button]
params[:code2] = "de"
params[:country] = "de"
end
if params[:austria_button]
params[:code2] = "at"
params[:country] = "at"
end
if params[:switzerland_button]
params[:code2] = "ch"
params[:country] = "ch"
end
cookies[:shipping_country] = params[:code2]
if has_cart?
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
end
#@cart.freeze if has_cart?
redirect_to :back
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :company, :phone, :address_line_1, :address_line_2, :address_line_3, :address_line_4, :country_id, :email, :password, :password_confirmation, :current_password) }
end
# which country is chosen
def set_shipping_to
code2 = cookies[:shipping_country] || "de"
begin
@shipping_country = Country.find_by_code2(code2.downcase)
rescue Exception
@shipping_country = Country.find_by_code2("de")
end
end
# Has an active cart?
def has_cart?
@cart
end
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
# current_user.locale
# request.subdomain
# request.env["HTTP_ACCEPT_LANGUAGE"]
# request.remote_ip
end
def default_url_options(options = {})
{locale: I18n.locale}
end
helper_method :has_cart?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment