fix a bug when pulling the localized select options of an content type
did
committed Jan 17, 2016
commit 3a0f1c2549fb50646fd8cabaf08841d6ec89397f
Showing 2
changed files with
50 additions
and 3 deletions
locomotive/wagon/commands/pull_sub_commands/pull_content_types_command.rb b/lib/locomotive/wagon/commands/pull_sub_commands/pull_content_types_command.rb
+9
-3
| @@ | @@ -37,12 +37,18 @@ module Locomotive::Wagon |
| end | |
| def select_options_yaml(options) | |
| + | return if options.blank? |
| + | |
| + | ordered_options = options.sort { |option| option['position'] } |
| + | |
| if locales.size > 1 | |
| - | locales.inject do |_options, locale| |
| - | _options[locale] = attributes.map { |option| options['name'][locale.to_s] } |
| + | {}.tap do |_options| |
| + | ordered_options.each do |option| |
| + | locales.each { |locale| (_options[locale] ||= []) << option['name'][locale.to_s] } |
| + | end |
| end | |
| else | |
| - | options.map { |option| option['name'][default_locale] } |
| + | ordered_options.map { |option| option['name'][default_locale] } |
| end | |
| end | |
spec/unit/commands/pull_sub_commands/pull_content_types_command_spec.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | # encoding: utf-8 |
| + | |
| + | require 'spec_helper' |
| + | |
| + | require 'active_support' |
| + | require 'active_support/core_ext' |
| + | require 'locomotive/wagon/commands/pull_sub_commands/pull_base_command' |
| + | require 'locomotive/wagon/commands/pull_sub_commands/pull_content_types_command' |
| + | |
| + | describe Locomotive::Wagon::PullContentTypesCommand do |
| + | |
| + | let(:locales) { ['en'] } |
| + | let(:site) { instance_double('Site', locales: locales) } |
| + | let(:command) { described_class.new(nil, site, nil) } |
| + | |
| + | describe '#select_options_yaml' do |
| + | |
| + | subject { command.send(:select_options_yaml, options) } |
| + | |
| + | context 'the site is localized' do |
| + | |
| + | let(:locales) { ['en', 'fr'] } |
| + | let(:options) { [{ 'id' => '1', 'name' => { 'en' => 'team', 'fr' => 'équipe' }, 'position' => 2 }, { 'id' => '2', 'name' => { 'en' => 'accounting', 'fr' => 'compta' }, 'position' => 1 }] } |
| + | |
| + | it { expect(subject['en']).to eq(['accounting', 'team']) } |
| + | it { expect(subject['fr']).to eq(['compta', 'équipe']) } |
| + | |
| + | end |
| + | |
| + | context 'the site is not localized' do |
| + | |
| + | let(:locales) { ['fr'] } |
| + | let(:options) { [{ 'id' => '1', 'name' => { 'fr' => 'équipe' }, 'position' => 2 }, { 'id' => '2', 'name' => {'fr' => 'compta' }, 'position' => 1 }] } |
| + | |
| + | it { is_expected.to eq(['compta', 'équipe']) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |