select fields were not playing well with localization (fix issue locomotivecms/engine#1151)

did committed May 26, 2016
commit af1f37b9126ce1bee2eb64a4dc03b0262ab3393b
Showing 6 changed files with 39 additions and 9 deletions
locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +9 -1
@@ @@ -31,7 +31,15 @@ module Locomotive
def modify_for_selects(attributes)
content_type.select_fields.each do |field|
- attributes[:"#{field.name}_id"] = attributes.delete(field.name.to_sym)
+ if (option = attributes.delete(field.name.to_sym)).is_a?(Hash)
+ attributes[:"#{field.name}_id"] = option.inject({}) do |memo, (locale, name)|
+ field.select_options.scope.locale = locale
+ memo[locale] = field.select_options.by_name(name).try(:_id)
+ memo
+ end
+ else
+ attributes[:"#{field.name}_id"] = option
+ end
end
end
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +5 -4
@@ @@ -164,16 +164,17 @@ module Locomotive::Steam
end
def _cast_select(field)
- _cast_convertor(:"#{field.name}_id") do |value|
- field.select_options.find(value).try(:name)
+ _cast_convertor(:"#{field.name}_id", true) do |value, locale|
+ name = field.select_options.find(value).try(:name)
+ locale.nil? ? name : name.try(:[], locale)
end
end
- def _cast_convertor(name, &block)
+ def _cast_convertor(name, nil_locale = false, &block)
if (value = attributes[name]).respond_to?(:translations)
value.apply(&block)
else
- yield(value)
+ nil_locale ? yield(value, nil) : yield(value)
end
end
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb +1 -1
@@ @@ -30,7 +30,7 @@ module Locomotive::Steam
end
def localized_names
- fields.localized_names + select_fields.map(&:name)
+ fields.localized_names
end
def persisted_field_names
locomotive/steam/repositories/content_type_field_repository.rb b/lib/locomotive/steam/repositories/content_type_field_repository.rb +3 -1
@@ @@ -58,7 +58,9 @@ module Locomotive
end
def localized_names
- query { where(localized: true) }.all.map(&:name)
+ query { where(localized: true) }.all.map do |field|
+ field.type == :select ? [field.name, "#{field.name}_id"] : field.name
+ end.flatten
end
def default
spec/fixtures/default/data/updates.yml +4 -2
@@ @@ -5,7 +5,9 @@
en: john posted a new post
fr: phrase FR
date: 2009/11/16
- category: General
+ category:
+ en: General
+ fr: Général
- "Update #5":
title:
fr: "Mise a jour #5"
@@ @@ -45,4 +47,4 @@
en: added some free stuff
fr: phrase FR
date: 2009/05/12
- category: General
\ No newline at end of file
+ category: General
spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +17 -0
@@ @@ -45,6 +45,23 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do
end
+ context 'a content type with a localized field' do
+
+ let(:options_scope) { instance_double('Scope', :locale= => true) }
+ let(:options) { instance_double('SelectOptionsRepository', scope: options_scope) }
+ let(:field) { instance_double('Field', name: 'category', type: :select, localized: true, select_options: options) }
+ let(:content_type) { instance_double('Updates', slug: 'updates', select_fields: [field], association_fields: [], file_fields: []) }
+
+ it 'adds a new localized attribute for the foreign key' do
+ option = instance_double('Option', _id: 'General')
+ allow(options).to receive(:by_name).with('General').and_return(option)
+ allow(options).to receive(:by_name).with('Général').and_return(option)
+ expect(subject.last[:category_id]).to eq({ en: 'General', fr: 'General' })
+ expect(subject.last[:category]).to eq nil
+ end
+
+ end
+
context 'a content type with a file field' do
let(:field) { instance_double('Field', name: 'cover', type: :file) }