the json liquid filter when applied to content entries also returns the slug(s) of content entries of belongs_to and many_to_many relationships

did committed May 12, 2016
commit 57860e004c1a96129a9517fdc923547bdf5c9e72
Showing 4 changed files with 32 additions and 9 deletions
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb +8 -5
@@ @@ -76,19 +76,22 @@ module Locomotive::Steam
end
def to_hash
+ hash = {}
+
# default attributes
_attributes = %i(_id _slug _label _visible _position content_type_slug created_at updated_at)
# dynamic attributes
_attributes += content_type.persisted_field_names
- _attributes.inject({}) do |hash, name|
+ _attributes.each do |name|
hash[name.to_s] = send(name) rescue nil
- hash
- end.tap do |hash|
- # errors?
- hash['errors'] = self.errors.to_hash.stringify_keys unless self.errors.empty?
end
+
+ # errors?
+ hash['errors'] = self.errors.to_hash.stringify_keys unless self.errors.empty?
+
+ hash
end
def to_liquid
locomotive/steam/liquid/drops/content_entry.rb b/lib/locomotive/steam/liquid/drops/content_entry.rb +8 -0
@@ @@ -62,6 +62,10 @@ module Locomotive
@_source.content_type.fields_by_name.each do |name, field|
case field.type
+ 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
when :file
hash[name] = hash["#{name}_url"] = file_field_to_url(hash[name.to_s]) if hash[name.to_s].present?
when :select
@@ @@ -77,6 +81,10 @@ module Locomotive
protected
+ def liquify_entry(entry)
+ self.class.new(entry).tap { |drop| drop.context = @context }
+ end
+
def file_field_to_url(field)
field.to_liquid.tap { |drop| drop.context = @context }.url
end
spec/unit/liquid/drops/content_entry_spec.rb +4 -2
@@ @@ -100,7 +100,9 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do
describe '#as_json' do
- let(:type) { instance_double('Type', fields_by_name: { title: instance_double('StringField', type: :string ), picture: instance_double('FileField', type: :file), category: instance_double('SelectField', type: :select) }) }
+ 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(: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(:picture_field) { Locomotive::Steam::ContentEntry::FileField.new('foo.png', 'http://assets.dev', 0, 42) }
before do
@@ @@ -110,7 +112,7 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do
subject { drop.as_json }
- it { is_expected.to eq('id' => 1, '_id' => 1, 'title' => 'Hello world', 'picture' => 'http://assets.dev/foo.png?42', 'picture_url' => 'http://assets.dev/foo.png?42', 'category_id' => 42, 'category' => 'Test') }
+ it { is_expected.to eq('id' => 1, '_id' => 1, 'title' => 'Hello world', 'picture' => 'http://assets.dev/foo.png?42', 'picture_url' => 'http://assets.dev/foo.png?42', 'category_id' => 42, 'category' => 'Test', 'author' => 'john-doe', 'authors' => ['john-doe']) }
end
spec/unit/middlewares/site_spec.rb +12 -2
@@ @@ -107,11 +107,21 @@ describe Locomotive::Steam::Middlewares::Site do
end
- describe 'redirection to both https and the first domain' do
+ describe 'redirection to both https and the first domain' do
let(:redirect_to_https) { true }
let(:redirect_to_first_domain) { true }
- let(:url) { 'http://acme.com/foo/bar' }
+ let(:url) { 'http://acme.com/foo/bar' }
+
+ it { is_expected.to eq [301, 'https://www.acme.com/foo/bar'] }
+
+ end
+
+ describe 'redirection to the first domain' do
+
+ let(:redirect_to_https) { true }
+ let(:redirect_to_first_domain) { true }
+ let(:url) { 'https://acme.com/foo/bar' }
it { is_expected.to eq [301, 'https://www.acme.com/foo/bar'] }