select_options of a content type field were not liquified
did
committed Mar 17, 2016
commit c44f003e117a04424f18b1763486aee4d44dcce0
Showing 2
changed files with
23 additions
and 3 deletions
locomotive/steam/liquid/drops/content_entry_collection.rb b/lib/locomotive/steam/liquid/drops/content_entry_collection.rb
+11
-1
| @@ | @@ -42,7 +42,7 @@ module Locomotive |
| if (meth.to_s =~ /^group_by_(.+)$/) == 0 | |
| repository.group_by_select_option(@content_type, $1) | |
| elsif (meth.to_s =~ /^(.+)_options$/) == 0 | |
| - | content_type_repository.select_options(@content_type, $1) |
| + | select_options($1) |
| else | |
| Locomotive::Common::Logger.warn "[Liquid template] trying to call #{meth} on a content_type object" | |
| nil | |
| @@ | @@ -76,6 +76,16 @@ module Locomotive |
| @repository || services.repositories.content_entry.with(@content_type) | |
| end | |
| + | def select_options(name) |
| + | locale = @context.registers[:locale] |
| + | default_locale = @context.registers[:site].try(:default_locale) |
| + | |
| + | (content_type_repository.select_options(@content_type, name) || []).map do |option| |
| + | _option = Locomotive::Steam::Decorators::I18nDecorator.new(option, locale, default_locale) |
| + | _option.name |
| + | end |
| + | end |
| + | |
| end | |
| end | |
spec/unit/liquid/drops/content_entry_collection_spec.rb
+12
-2
| @@ | @@ -5,7 +5,7 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do |
| let(:assigns) { {} } | |
| let(:content_type) { instance_double('ContentType', slug: 'articles') } | |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| - | let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) } |
| + | let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, locale: :en }) } |
| let(:drop) { described_class.new(content_type).tap { |d| d.context = context } } | |
| before { allow(services).to receive(:current_site).and_return(nil) } | |
| @@ | @@ -74,8 +74,11 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do |
| describe 'get options of a select field' do | |
| + | let(:option_a) { build_select_option(en: 'a') } |
| + | let(:option_b) { build_select_option('b') } |
| + | |
| before do | |
| - | expect(services.repositories.content_type).to receive(:select_options).with(content_type, 'category').and_return(['a', 'b']) |
| + | expect(services.repositories.content_type).to receive(:select_options).with(content_type, 'category').and_return([option_a, option_b]) |
| end | |
| it { expect(drop.before_method(:category_options)).to eq ['a', 'b'] } | |
| @@ | @@ -98,4 +101,11 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do |
| end | |
| + | def build_select_option(name) |
| + | _name = Locomotive::Steam::Models::I18nField.new('name', name) |
| + | Locomotive::Steam::ContentTypeField::SelectOption.new(name: _name).tap do |option| |
| + | option.localized_attributes = [:name] |
| + | end |
| + | end |
| + | |
| end | |