many_to_many relationship works even if the attribute is nil
did
committed May 12, 2016
commit 8d698af635d065531fac077ad3e3b52e101c8ddb
Showing 3
changed files with
4 additions
and 3 deletions
locomotive/steam/liquid/drops/content_entry.rb b/lib/locomotive/steam/liquid/drops/content_entry.rb
+1
-1
| @@ | @@ -65,7 +65,7 @@ module Locomotive |
| when :belongs_to | |
| hash[name] = liquify_entry(@_source.send(name))._slug | |
| when :many_to_many | |
| - | hash[name] = (@_source.send(name) || []).map { |e| liquify_entry(e)._slug }.compact |
| + | hash[name] = (@_source.send(name) || []).all.map { |e| liquify_entry(e)._slug }.compact |
| when :file | |
| hash[name] = hash["#{name}_url"] = file_field_to_url(hash[name.to_s]) if hash[name.to_s].present? | |
| when :select | |
locomotive/steam/models/associations/many_to_many.rb b/lib/locomotive/steam/models/associations/many_to_many.rb
+1
-1
| @@ | @@ -6,7 +6,7 @@ module Locomotive::Steam |
| def __load__ | |
| key = @repository.k(:_id, :in) | |
| - | @repository.local_conditions[key] = @entity[__target_key__] |
| + | @repository.local_conditions[key] = @entity[__target_key__] || [] |
| # use order_by from options as the default one for further queries | |
| @repository.local_conditions[:order_by] = @options[:order_by] unless @options[:order_by].blank? | |
spec/unit/liquid/drops/content_entry_spec.rb
+2
-1
| @@ | @@ -100,9 +100,10 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do |
| describe '#as_json' do | |
| - | let(:entry) { instance_double('Article', _id: 42, localized_attributes: {}, content_type: type, title: 'Hello world', _label: 'Hello world', _slug: 'hello-world', _translated: false, seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description', created_at: 0, updated_at: 1, author: author, authors: [author]) } |
| + | let(:entry) { instance_double('Article', _id: 42, localized_attributes: {}, content_type: type, title: 'Hello world', _label: 'Hello world', _slug: 'hello-world', _translated: false, seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description', created_at: 0, updated_at: 1, author: author, authors: authors) } |
| let(:type) { instance_double('Type', fields_by_name: { title: instance_double('StringField', type: :string ), author: instance_double('Author', type: :belongs_to), authors: instance_double('Author', type: :many_to_many), picture: instance_double('FileField', type: :file), category: instance_double('SelectField', type: :select) }) } | |
| let(:author) { instance_double('Author', _slug: 'john-doe', localized_attributes: {}) } | |
| + | let(:authors) { instance_double('Authors', all: [author]) } |
| let(:picture_field) { Locomotive::Steam::ContentEntry::FileField.new('foo.png', 'http://assets.dev', 0, 42) } | |
| before do | |