Skip to content

Commit

Permalink
Restore locale_columns sorting of default locale first
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Aug 13, 2014
1 parent be386c0 commit b62baf3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
12 changes: 6 additions & 6 deletions lib/traco/class_methods.rb
Expand Up @@ -4,17 +4,17 @@ def locales_for_attribute(attribute)
traco_cache(attribute, LocaleFallbacks::ANY_FALLBACK).keys
end

# Consider this method internal.
def _locale_columns_for_attribute(attribute, fallback)
traco_cache(attribute, fallback).values
end

def locale_columns(*attributes)
attributes.each_with_object([]) do |attribute, columns|
columns.concat(_locale_columns_for_attribute(attribute, LocaleFallbacks::ANY_FALLBACK))
columns.concat(_locale_columns_for_attribute(attribute, LocaleFallbacks::DEFAULT_FIRST_FALLBACK))
end
end

# Consider this method internal.
def _locale_columns_for_attribute(attribute, fallback)
traco_cache(attribute, fallback).values
end

def current_locale_column(attribute)
Traco.column(attribute, I18n.locale)
end
Expand Down
10 changes: 6 additions & 4 deletions lib/traco/locale_fallbacks.rb
Expand Up @@ -6,6 +6,7 @@ class LocaleFallbacks
DEFAULT_FALLBACK = :default,
ANY_FALLBACK = :any,
NO_FALLBACK = false,
DEFAULT_FIRST_FALLBACK = :default_first,
]

attr_reader :fallback_option
Expand All @@ -19,10 +20,11 @@ def initialize(fallback_option)

def [](current_locale)
case fallback_option
when DEFAULT_FALLBACK then [ current_locale, @default_locale ]
when ANY_FALLBACK then [ current_locale, @default_locale, *@available_locales ].uniq
when NO_FALLBACK then [ current_locale ]
else raise "Unknown fallback." # Should never get here.
when DEFAULT_FALLBACK then [ current_locale, @default_locale ]
when ANY_FALLBACK then [ current_locale, @default_locale, *@available_locales ].uniq
when NO_FALLBACK then [ current_locale ]
when DEFAULT_FIRST_FALLBACK then [ @default_locale, *@available_locales ].uniq
else raise "Unknown fallback." # Should never get here.
end
end

Expand Down
17 changes: 14 additions & 3 deletions spec/locale_fallbacks_spec.rb
Expand Up @@ -10,28 +10,39 @@
end

describe "#[]" do
context "with the :default option" do
context "with the ':default' option" do
it "returns given locale, then default locale" do
I18n.default_locale = :en
subject = Traco::LocaleFallbacks.new(:default)
expect(subject[:sv]).to eq [ :sv, :en ]
end
end

context "with the :any option" do
context "with the ':any' option" do
it "returns given locale, then default locale, then remaining available locales alphabetically" do
I18n.default_locale = :en
I18n.available_locales = [ :en, :sv, :uk, :de ]

subject = Traco::LocaleFallbacks.new(:any)
expect(subject[:sv]).to eq [ :sv, :en, :de, :uk ]
end
end

context "with the false option" do
context "with the 'false' option" do
it "returns only given locale" do
subject = Traco::LocaleFallbacks.new(false)
expect(subject[:sv]).to eq [ :sv ]
end
end

context "with the ':default_first' option" do
it "returns default locale, then remaining available locales alphabetically" do
I18n.default_locale = :uk
I18n.available_locales = [ :en, :sv, :uk, :de ]

subject = Traco::LocaleFallbacks.new(:default_first)
expect(subject[:sv]).to eq [ :uk, :de, :en, :sv ]
end
end
end
end
5 changes: 4 additions & 1 deletion spec/spec_helper_models.rb
@@ -1,9 +1,12 @@
RSpec.configure do |config|
# Clear class state before each spec.
config.before(:each) do
# Clear class state before each spec.
Object.send(:remove_const, 'Post')
Object.send(:remove_const, 'SubPost')
load "app/post.rb"

# Known state.
I18n.default_locale = :sv
end
end

Expand Down
15 changes: 8 additions & 7 deletions spec/traco_spec.rb
Expand Up @@ -97,27 +97,28 @@
describe Post, ".locale_columns" do
before do
Post.translates :title
I18n.locale = :"pt-BR"
I18n.default_locale = :"pt-BR"
I18n.locale = :en
end

it "lists the columns-with-locale for current locale with :any locale fallback" do
expect(Traco).to receive(:locale_with_fallbacks).with(:"pt-BR", :any).and_return([:"pt-BR", :en, :sv])
it "lists the columns-with-locale for that attribute, default locale first and then alphabetically" do
expect(Post.locale_columns(:title)).to eq [
:title_pt_br, :title_en, :title_sv
]
end

it "doesn't include a column-with-locale if it doesn't exist" do
expect(Traco).to receive(:locale_with_fallbacks).with(:"pt-BR", :any).and_return([:"pt-BR", :ru])
expect(Post.locale_columns(:title)).to eq [ :title_pt_br ]
I18n.available_locales += [ :ru ]

expect(Post.locale_columns(:title)).not_to include(:title_ru)
end

it "supports multiple attributes" do
Post.translates :body
expect(Traco).to receive(:locale_with_fallbacks).with(:"pt-BR", :any).twice.and_return([:"pt-BR", :en, :sv])

expect(Post.locale_columns(:body, :title)).to eq [
:body_pt_br, :body_en, :body_sv,
:title_pt_br, :title_en, :title_sv
:title_pt_br, :title_en, :title_sv,
]
end
end
Expand Down

0 comments on commit b62baf3

Please sign in to comment.