with_scope handles correctly select fields for both the MongoDB and FS adapters
did
committed Aug 21, 2015
commit e51c5adb89ea3ec983dc6e4451ea6e03942bb4d0
Showing 4
changed files with
37 additions
and 13 deletions
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb
+15
-1
| @@ | @@ -130,7 +130,21 @@ module Locomotive |
| end | |
| def prepare_conditions(*conditions) | |
| - | super({ _visible: true }, conditions) |
| + | _conditions = conditions.first.try(:with_indifferent_access) |
| + | |
| + | prepare_conditions_for_select_fields(_conditions) if _conditions |
| + | |
| + | 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 | |
| def add_localized_fields_to_mapper(mapper) | |
locomotive/steam/repositories/content_type_field_select_option_repository.rb b/lib/locomotive/steam/repositories/content_type_field_select_option_repository.rb
+4
-0
| @@ | @@ -18,6 +18,10 @@ module Locomotive |
| query { order_by(position: :asc) }.all | |
| end | |
| + | def by_name(name) |
| + | query { where(name: name) }.first |
| + | end |
| + | |
| end | |
| end | |
| end | |
spec/integration/repositories/content_entry_repository_spec.rb
+5
-0
| @@ | @@ -50,6 +50,11 @@ describe Locomotive::Steam::ContentEntryRepository do |
| it { expect(subject.name).to eq 'Alice in Chains' } | |
| end | |
| + | describe 'filter by a select field' do |
| + | subject { repository.all(kind: 'grunge') } |
| + | it { expect(subject.map { |entry| entry[:name] }).to eq(['Alice in Chains', 'Pearl Jam']) } |
| + | end |
| + | |
| describe '#group_by_select_option' do | |
| subject { repository.group_by_select_option(:kind) } | |
| it { expect(subject.map { |h| h[:name] }).to eq(%w(grunge rock country)) } | |
spec/unit/repositories/content_entry_repository_spec.rb
+13
-12
| @@ | @@ -4,7 +4,8 @@ require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| describe Locomotive::Steam::ContentEntryRepository do | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:_fields) { instance_double('Fields', selects: []) } |
| + | 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 } | |
| let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: %i(en fr)) } | |
| @@ | @@ -107,7 +108,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| end | |
| context 'with a has_many field' do | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields_by_name: { articles: instance_double('Field', type: :has_many) }) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { articles: instance_double('Field', type: :has_many) }) } |
| let(:proxy_repository) { repository.dup } | |
| let(:entry) { instance_double('Entry', articles: proxy_repository) } | |
| let(:name) { :articles } | |
| @@ | @@ -124,7 +125,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#next' do | |
| - | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| let(:entries) do | |
| [ | |
| { 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' }, | |
| @@ | @@ -156,7 +157,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#previous' do | |
| - | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| let(:entries) do | |
| [ | |
| { 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' }, | |
| @@ | @@ -203,7 +204,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| category: instance_double('SelectField', name: :category, type: :select, select_options: { en: ['cooking', 'bread'], fr: ['cuisine', 'pain'] }) | |
| } | |
| end | |
| - | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields_by_name: fields) } |
| + | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields: _fields, fields_by_name: fields) } |
| let(:name) { :category } | |
| let(:options) { | |
| @@ | @@ -244,10 +245,10 @@ describe Locomotive::Steam::ContentEntryRepository do |
| let(:field) { instance_double('Field', name: :author, type: :belongs_to, association_options: { target_id: 2 }) } | |
| let(:type) { build_content_type('Articles', label_field_name: :title, association_fields: [field]) } | |
| let(:entries) { [{ content_type_id: 1, title: 'Hello world', author_id: 'john-doe' }] } | |
| - | let(:other_type) { build_content_type('Authors', _id: 2, label_field_name: :name, fields_by_name: { name: instance_double('Field', name: :name, type: :string) }) } |
| + | 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('ContentTypeRepository') } |
| + | let(:type_repository) { instance_double('ArticleBelongsToRepository', selects: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |
| @@ | @@ -271,7 +272,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| let(:field) { instance_double('Field', name: :articles, type: :has_many, association_options: { target_id: 2, inverse_of: :author, order_by: 'position_in_author' }) } | |
| let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field]) } | |
| let(:entries) { [{ content_type_id: 1, _id: 'john-doe', name: 'John Doe' }] } | |
| - | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| let(:other_entries) { | |
| [ | |
| { content_type_id: 2, _id: 'hello-world', title: 'Hello world', author_id: 'john-doe', position_in_author: 2 }, | |
| @@ | @@ -280,7 +281,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| ] | |
| } | |
| - | let(:type_repository) { instance_double('ContentTypeRepository') } |
| + | let(:type_repository) { instance_double('AuthorRepository', selects: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |
| @@ | @@ -302,9 +303,9 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'many_to_many' do | |
| let(:field) { instance_double('Field', name: :articles, type: :many_to_many, association_options: { target_id: 2, inverse_of: :authors }) } | |
| - | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field]) } |
| + | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field], fields: _fields) } |
| let(:entries) { [{ content_type_id: 1, _id: 1, name: 'John Doe', article_ids: ['hello-world', 'lorem-ipsum'] }] } | |
| - | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| let(:other_entries) { | |
| [ | |
| { content_type_id: 2, _id: 'hello-world', title: 'Hello world', author_id: 'john-doe', position_in_author: 2 }, | |
| @@ | @@ -313,7 +314,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| ] | |
| } | |
| - | let(:type_repository) { instance_double('ContentTypeRepository') } |
| + | let(:type_repository) { instance_double('AuthorRepository', selects: []) } |
| before do | |
| allow(type).to receive(:fields).and_return(type_repository) | |