the with_scope tag accepts now belongs_to field without the _id suffix
did
committed Nov 10, 2015
commit c9cb7f67a6e38e5ac08068ed66e754a332e60c39
Showing 4
changed files with
64 additions
and 16 deletions
locomotive/steam/liquid/tags/with_scope.rb b/lib/locomotive/steam/liquid/tags/with_scope.rb
+4
-1
| @@ | @@ -46,8 +46,11 @@ module Locomotive |
| _key = key.to_s == '_permalink' ? '_slug' : key.to_s | |
| hash[_key] = (case value | |
| - | # regexp inside a string |
| + | # regexp inside a string |
| when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2]) | |
| + | # content entry drop? Use the source (entity) instead |
| + | when Locomotive::Steam::Liquid::Drops::ContentEntry |
| + | value.send(:_source) |
| else | |
| value | |
| end) | |
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb
+52
-11
| @@ | @@ -130,22 +130,32 @@ module Locomotive |
| end | |
| def prepare_conditions(*conditions) | |
| - | _conditions = conditions.first.try(:with_indifferent_access) |
| + | # _conditions = conditions.first.try(:with_indifferent_access) |
| - | prepare_conditions_for_select_fields(_conditions) if _conditions |
| + | _conditions = Conditions.new(conditions.first, self.content_type.fields).prepare |
| super({ _visible: true }, _conditions) | |
| end | |
| - | # select fields? if so, use the _id of the option instead of the option name |
| - | def prepare_conditions_for_select_fields(conditions) |
| - | self.content_type.fields.selects.each do |field| |
| - | if value = conditions[name = field.name.to_s] |
| - | conditions.delete(name) |
| - | conditions[name + '_id'] = field.select_options.by_name(value).try(:_id) |
| - | end |
| - | end |
| - | end |
| + | # # belongs_to fields? if so, make sure we use the _id and we deal with the ID, not the object itself |
| + | # def prepare_conditions_for_belongs_to_fields(conditions) |
| + | # self.content_type.fields.belongs_to.each do |field| |
| + | # if value = conditions[name = field.name.to_s] |
| + | # conditions.delete(name) |
| + | # conditions[name + '_id'] = value.try(:_id) |
| + | # end |
| + | # end |
| + | # end |
| + | |
| + | # # select fields? if so, use the _id of the option instead of the option name |
| + | # def prepare_conditions_for_select_fields(conditions) |
| + | # self.content_type.fields.selects.each do |field| |
| + | # if value = conditions[name = field.name.to_s] |
| + | # conditions.delete(name) |
| + | # conditions[name + '_id'] = field.select_options.by_name(value).try(:_id) |
| + | # end |
| + | # end |
| + | # end |
| def add_localized_fields_to_mapper(mapper) | |
| unless self.content_type.localized_names.blank? | |
| @@ | @@ -194,6 +204,37 @@ module Locomotive |
| end | |
| end | |
| + | class Conditions |
| + | |
| + | def initialize(conditions = {}, fields) |
| + | @conditions, @fields = conditions.try(:with_indifferent_access) || {}, fields |
| + | end |
| + | |
| + | def prepare |
| + | return {} if @conditions.blank? |
| + | |
| + | _prepare(@fields.selects) do |field, value| |
| + | field.select_options.by_name(value).try(:_id) |
| + | end |
| + | |
| + | _prepare(@fields.belongs_to) { |field, value| value.try(:_id) } |
| + | |
| + | @conditions |
| + | end |
| + | |
| + | protected |
| + | |
| + | def _prepare(fields, &block) |
| + | fields.each do |field| |
| + | if value = @conditions[name = field.name.to_s] |
| + | @conditions.delete(name) |
| + | @conditions[name + '_id'] = yield(field, value) |
| + | end |
| + | end |
| + | end |
| + | |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/repositories/content_type_field_repository.rb b/lib/locomotive/steam/repositories/content_type_field_repository.rb
+4
-0
| @@ | @@ -22,6 +22,10 @@ module Locomotive |
| query { where(type: :select) }.all | |
| end | |
| + | def belongs_to |
| + | query { where(type: :belongs_to) }.all |
| + | end |
| + | |
| def associations | |
| query { where(k(:type, :in) => %i(belongs_to has_many many_to_many)) }.all | |
| end | |
spec/unit/repositories/content_entry_repository_spec.rb
+4
-4
| @@ | @@ -4,7 +4,7 @@ require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| describe Locomotive::Steam::ContentEntryRepository do | |
| - | let(:_fields) { instance_double('Fields', selects: []) } |
| + | let(:_fields) { instance_double('Fields', selects: [], belongs_to: []) } |
| let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } | |
| let(:entries) { [{ content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }] } | |
| let(:locale) { :en } | |
| @@ | @@ -248,7 +248,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| let(:other_type) { build_content_type('Authors', _id: 2, label_field_name: :name, fields: _fields, fields_by_name: { name: instance_double('Field', name: :name, type: :string) }) } | |
| let(:other_entries) { [{ content_type_id: 2, _id: 'john-doe', name: 'John Doe' }] } | |
| - | let(:type_repository) { instance_double('ArticleBelongsToRepository', selects: []) } |
| + | let(:type_repository) { instance_double('ArticleBelongsToRepository', selects: [], belongs_to: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |
| @@ | @@ -281,7 +281,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| ] | |
| } | |
| - | let(:type_repository) { instance_double('AuthorRepository', selects: []) } |
| + | let(:type_repository) { instance_double('AuthorRepository', selects: [], belongs_to: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |
| @@ | @@ -314,7 +314,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| ] | |
| } | |
| - | let(:type_repository) { instance_double('AuthorRepository', selects: []) } |
| + | let(:type_repository) { instance_double('AuthorRepository', selects: [], belongs_to: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |