convert criterion of a MongoDB query into symbol operators if possible
did
committed May 25, 2015
commit 01dcee15e0abac4553ca95a794a0fe513582c82e
Showing 4
changed files with
34 additions
and 3 deletions
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb
+1
-1
| @@ | @@ -28,7 +28,7 @@ module Locomotive::Steam |
| end | |
| def key(name, operator) | |
| - | name.__send__(operator) |
| + | name.to_sym.__send__(operator.to_sym) |
| end | |
| def base_url(mapper, scope, entity = nil) | |
locomotive/steam/adapters/mongodb/query.rb b/lib/locomotive/steam/adapters/mongodb/query.rb
+17
-1
| @@ | @@ -17,7 +17,7 @@ module Locomotive::Steam |
| def where(criterion = nil) | |
| self.tap do | |
| - | @criteria.merge!(criterion) unless criterion.nil? |
| + | @criteria.merge!(decode_symbol_operators(criterion)) unless criterion.nil? |
| end | |
| end | |
| @@ | @@ -75,6 +75,22 @@ module Locomotive::Steam |
| where(site_id: @scope.site._id) if @scope.site | |
| end | |
| + | def decode_symbol_operators(criterion) |
| + | criterion.dup.tap do |_criterion| |
| + | criterion.each do |key, value| |
| + | next unless key.is_a?(String) |
| + | |
| + | _key, operator = key.split('.') |
| + | |
| + | if operator |
| + | _criterion.delete(key) |
| + | _key = _key.to_s.to_sym.public_send(operator.to_sym) |
| + | _criterion[_key] = value |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb
+4
-0
| @@ | @@ -48,6 +48,10 @@ module Locomotive::Steam |
| self[:raw_template] | |
| end | |
| + | def depth |
| + | @depth || self[:depth] |
| + | end |
| + | |
| def to_liquid | |
| Locomotive::Steam::Liquid::Drops::Page.new(self) | |
| end | |
spec/integration/repositories/page_repository_spec.rb
+12
-1
| @@ | @@ -5,6 +5,11 @@ require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| describe Locomotive::Steam::PageRepository do | |
| + | # before(:all) do |
| + | # Moped.logger = Logger.new($stdout) |
| + | # Moped.logger.level = Logger::DEBUG |
| + | # end |
| + | |
| shared_examples_for 'a repository' do | |
| let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } | |
| @@ | @@ -12,8 +17,14 @@ describe Locomotive::Steam::PageRepository do |
| let(:repository) { described_class.new(adapter, site, locale) } | |
| describe '#all' do | |
| - | subject { repository.all } |
| + | let(:conditions) { {} } |
| + | subject { repository.all(conditions) } |
| it { expect(subject.size).to eq 24 } | |
| + | |
| + | context 'with conditions' do |
| + | let(:conditions) { { fullpath: 'index', 'slug.ne' => '404' } } |
| + | it { expect(subject.size).to eq 1 } |
| + | end |
| end | |
| describe '#query' do | |