introduce the value_for method for the content_entry_repository
did
committed Mar 09, 2015
commit 261ab8504eae42ca0d2653416b68e02519528e1f
Showing 15
changed files with
102 additions
and 19 deletions
Rakefile
+2
-2
| @@ | @@ -35,8 +35,8 @@ RSpec::Core::RakeTask.new('spec:integration') do |spec| |
| end | |
| RSpec::Core::RakeTask.new('spec:unit') do |spec| | |
| - | # spec.pattern = 'spec/unit/**/*_spec.rb' |
| - | spec.pattern = 'spec/unit/{services,core_ext,middlewares,decorators,adapters,entities,models,repositories}/**/*_spec.rb' |
| + | spec.pattern = 'spec/unit/**/*_spec.rb' |
| + | # spec.pattern = 'spec/unit/{services,core_ext,middlewares,decorators,adapters,entities,models,repositories}/**/*_spec.rb' |
| end | |
| task default: ['mongodb:test:seed', :spec] | |
locomotive/steam/liquid/drops/content_entry.rb b/lib/locomotive/steam/liquid/drops/content_entry.rb
+1
-1
| @@ | @@ -50,7 +50,7 @@ module Locomotive |
| return '' if @_source.nil? | |
| if not @@forbidden_attributes.include?(meth.to_s) | |
| - | repository.value_for(meth, @_source, @context['with_scope']) |
| + | repository.with(@_source.content_type).value_for(@_source, meth, @context['with_scope']) |
| else | |
| nil | |
| end | |
locomotive/steam/models/repository.rb b/lib/locomotive/steam/models/repository.rb
+5
-0
| @@ | @@ -18,6 +18,11 @@ module Locomotive::Steam |
| @local_conditions = {} | |
| end | |
| + | def initialize_copy(source) |
| + | super |
| + | @local_conditions = source.local_conditions.dup |
| + | end |
| + | |
| def build(attributes, &block) | |
| mapper.to_entity(attributes) | |
| end | |
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb
+17
-0
| @@ | @@ -57,6 +57,23 @@ module Locomotive |
| first { where(conditions) } | |
| end | |
| + | def value_for(entry, name, conditions = {}) |
| + | return nil if entry.nil? |
| + | |
| + | if field = content_type.fields_by_name[name] |
| + | value = entry.send(name) |
| + | |
| + | if %i(has_many many_to_many).include?(field.type) |
| + | # a safe copy of the proxy repository is needed here |
| + | value = value.dup |
| + | # like this, we do not modify the original local conditions |
| + | value.local_conditions.merge!(conditions) |
| + | end |
| + | |
| + | value |
| + | end |
| + | end |
| + | |
| def next(entry) | |
| next_or_previous(entry, 'gt', 'lt') | |
| end | |
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb
+4
-0
| @@ | @@ -83,6 +83,10 @@ module Locomotive |
| Steam::TextileService.new | |
| end | |
| + | register :cache do |
| + | Steam::NoCacheService.new |
| + | end |
| + | |
| register :configuration do | |
| Locomotive::Steam.configuration | |
| end | |
locomotive/steam/services/no_cache_service.rb b/lib/locomotive/steam/services/no_cache_service.rb
+17
-0
| @@ | @@ -0,0 +1,17 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class NoCacheService |
| + | |
| + | def fetch(key, options = {}, &block) |
| + | @last_response = block.call |
| + | end |
| + | |
| + | def read(key) |
| + | nil |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | end |
spec/unit/liquid/drops/content_entry_spec.rb
+13
-8
| @@ | @@ -3,11 +3,12 @@ require 'spec_helper' |
| describe Locomotive::Steam::Liquid::Drops::ContentEntry do | |
| let(:site) { instance_double('Site', default_locale: 'en') } | |
| - | let(:entry) { instance_double('Article', _id: 42, title: 'Hello world', _label: 'Hello world', _slug: 'hello-world', _translated: false, seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description') } |
| + | let(:type) { instance_double('Type', fields_by_name: { title: instance_double('Field', type: :string ) }) } |
| + | let(:entry) { instance_double('Article', _id: 42, content_type: type, title: 'Hello world', _label: 'Hello world', _slug: 'hello-world', _translated: false, seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description') } |
| let(:assigns) { {} } | |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: 'en' }) } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::ContentEntry.new(entry).tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new(entry).tap { |d| d.context = context } } |
| subject { drop } | |
| @@ | @@ -29,14 +30,18 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do |
| describe 'relationship field' do | |
| - | let(:authors) { instance_double('Authors', all: ['john', 'jane'], association: true) } |
| - | let(:entry) { instance_double('Article', authors: authors) } |
| + | let(:authors) { instance_double('AuthorsRepository', all: ['john', 'jane']) } |
| + | let(:type) { instance_double('Type', fields_by_name: { authors: instance_double('Field', type: :has_many ) }) } |
| + | let(:entry) { instance_double('Article', content_type: type, authors: authors) } |
| - | before do |
| - | allow(services.repositories.content_entry).to receive(:association).and_return(authors.all) |
| - | end |
| + | # before do |
| + | # allow(services.repositories.content_entry).to receive(:association).and_return(authors.all) |
| + | # end |
| - | it { expect(subject.before_method(:authors).first).to eq 'john' } |
| + | it do |
| + | allow(subject.before_method(:authors)).to receive(:local_conditions).and_return({}) |
| + | expect(subject.before_method(:authors).first).to eq 'john' |
| + | end |
| end | |
spec/unit/liquid/drops/content_type_proxy_collection_spec.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ describe Locomotive::Steam::Liquid::Drops::ContentTypeProxyCollection do |
| let(:content_type) { instance_double('ContentType', slug: 'articles') } | |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::ContentTypeProxyCollection.new(content_type).tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new(content_type).tap { |d| d.context = context } } |
| describe '#public_submission_url' do | |
| it { expect(drop.public_submission_url).to eq '/entry_submissions/articles' } | |
spec/unit/liquid/drops/content_types_spec.rb
+1
-1
| @@ | @@ -4,7 +4,7 @@ describe Locomotive::Steam::Liquid::Drops::ContentTypes do |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| let(:context) { ::Liquid::Context.new({}, {}, { services: services }) } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::ContentTypes.new.tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new.tap { |d| d.context = context } } |
| before do | |
| allow(services.repositories.content_type).to receive(:by_slug).with('articles').and_return(true) | |
spec/unit/liquid/drops/current_user_spec.rb
+1
-1
| @@ | @@ -2,7 +2,7 @@ require 'spec_helper' |
| describe Locomotive::Steam::Liquid::Drops::CurrentUser do | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::CurrentUser.new(user) } |
| + | let(:drop) { described_class.new(user) } |
| subject { drop } | |
spec/unit/liquid/drops/page_spec.rb
+1
-1
| @@ | @@ -7,7 +7,7 @@ describe Locomotive::Steam::Liquid::Drops::Page do |
| let(:site) { instance_double('Site', default_locale: 'en') } | |
| let(:context) { ::Liquid::Context.new(assigns, {}, { locale: 'en', services: services, site: site }) } | |
| let(:page) { instance_double('Page', id: 42, localized_attributes: [], title: 'Index', slug: 'index', fullpath: 'index', content_type: nil, depth: 1, templatized?: false, listed?: true, published?: true, is_layout?: true, redirect?: false, seo_title: 'seo title', redirect_url: '/', handle: 'index', meta_keywords: 'keywords', meta_description: 'description') } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page).tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new(page).tap { |d| d.context = context } } |
| subject { drop } | |
spec/unit/liquid/drops/session_proxy_spec.rb
+1
-1
| @@ | @@ -4,7 +4,7 @@ describe Locomotive::Steam::Liquid::Drops::SessionProxy do |
| let(:request) { instance_double('Request', session: { answer: 42 }) } | |
| let(:context) { ::Liquid::Context.new({}, {}, { request: request }) } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::SessionProxy.new.tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new.tap { |d| d.context = context } } |
| subject { drop['answer'] } | |
spec/unit/liquid/drops/site_spec.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ describe Locomotive::Steam::Liquid::Drops::Site do |
| let(:services) { Locomotive::Steam::Services.build_instance } | |
| let(:context) { ::Liquid::Context.new({}, {}, { services: services }) } | |
| let(:site) { instance_double('Site', name: 'Locomotive', domains: ['acme.org'], seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description') } | |
| - | let(:drop) { Locomotive::Steam::Liquid::Drops::Site.new(site).tap { |d| d.context = context } } |
| + | let(:drop) { described_class.new(site).tap { |d| d.context = context } } |
| subject { drop } | |
spec/unit/liquid/filters/html_spec.rb
+2
-2
| @@ | @@ -12,7 +12,7 @@ describe Locomotive::Steam::Liquid::Filters::Html do |
| let(:theme_asset_url) { services.theme_asset_url } | |
| let(:theme_asset_repository) { services.repositories.theme_asset } | |
| - | before { services.repositories.theme_asset = EngineThemeAsset.new(site) } |
| + | before { services.repositories.theme_asset = EngineThemeAsset.new(nil, site) } |
| before { @context = context } | |
| @@ | @@ -254,7 +254,7 @@ describe Locomotive::Steam::Liquid::Filters::Html do |
| }.strip) | |
| end | |
| - | class EngineThemeAsset < Locomotive::Steam::Repositories::Filesystem::ThemeAsset |
| + | class EngineThemeAsset < Locomotive::Steam::ThemeAssetRepository |
| def url_for(path) | |
| ['', 'sites', site._id.to_s, 'theme', path].join('/') | |
| end | |
spec/unit/repositories/content_entry_repository_spec.rb
+35
-0
| @@ | @@ -87,6 +87,41 @@ describe Locomotive::Steam::ContentEntryRepository do |
| end | |
| + | describe '#value_for' do |
| + | |
| + | let(:entry) { nil } |
| + | let(:conditions) { {} } |
| + | let(:name) { :title } |
| + | subject { repository.with(type).value_for(entry, name, conditions) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | context 'existing entry' do |
| + | let(:entry) { instance_double('Entry', title: 'Hello world') } |
| + | it { is_expected.to eq 'Hello world' } |
| + | |
| + | context 'unknown field' do |
| + | let(:name) { :authors } |
| + | it { is_expected.to eq nil } |
| + | end |
| + | 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(:proxy_repository) { repository.dup } |
| + | let(:entry) { instance_double('Entry', articles: proxy_repository) } |
| + | let(:name) { :articles } |
| + | let(:conditions) { { published: true } } |
| + | |
| + | it 'does not modify the local conditions of the initial proxy repository' do |
| + | expect(subject.local_conditions).to eq(content_type_id: 1, published: true) |
| + | expect(proxy_repository.local_conditions).to eq(content_type_id: 1) |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| 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) }) } | |