Skip to content

Commit

Permalink
Merge pull request #1859 from elzj/faq_urls
Browse files Browse the repository at this point in the history
Fix for leaky locales and FAQ anchor editing
  • Loading branch information
elzj committed Sep 29, 2014
2 parents 77b6a02 + 063e9b9 commit 1bb905b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/controllers/archive_faqs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ArchiveFaqsController < ApplicationController
before_filter :admin_only, :except => [:index, :show]
before_filter :set_locale
before_filter :require_language_id
around_filter :with_locale

# GET /archive_faqs
def index
Expand Down Expand Up @@ -113,19 +114,25 @@ def default_url_options
{ language_id: set_locale.to_s }
end

# Set the locale as an instance variable first
def set_locale
if params[:language_id] && session[:language_id] != params[:language_id]
session[:language_id] = params[:language_id]
end
I18n.locale = session[:language_id] || I18n.default_locale
@i18n_locale = session[:language_id] || I18n.default_locale
end

def require_language_id
if params[:language_id].blank?
redirect_to url_for(request.query_parameters.merge(language_id: I18n.locale.to_s))
redirect_to url_for(request.query_parameters.merge(language_id: @i18n_locale.to_s))
end
end

# Setting I18n.locale directly is not thread safe
def with_locale
I18n.with_locale(@i18n_locale) { yield }
end

# GET /archive_faqs/1/confirm_delete
def confirm_delete
@archive_faq = ArchiveFaq.find_by_slug(params[:id])
Expand Down
2 changes: 1 addition & 1 deletion app/views/archive_faqs/_question_answer_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<%= form.label :anchor, ts("Anchor name") + "*" %>
</dt>
<dd class="required">
<%= form.text_field :anchor %>
<%= form.text_field :anchor, disabled: (I18n.locale != I18n.default_locale) %>
<%= live_validation_for_field(field_id(form, "anchor").to_sym, :failureMessage => ts("Please enter an anchor name.")) %>
</dd>

Expand Down

0 comments on commit 1bb905b

Please sign in to comment.