the to_json method of the content entry liquid drop should return now the same output as the one in v2.5.x

did committed Oct 09, 2015
commit d04f8b6e6115a2c769b75915a47f370e68a75857
Showing 2 changed files with 39 additions and 0 deletions
locomotive/steam/liquid/drops/content_entry.rb b/lib/locomotive/steam/liquid/drops/content_entry.rb +23 -0
@@ @@ -56,8 +56,31 @@ module Locomotive
end
end
+ def to_hash
+ @_source.to_hash.tap do |hash|
+ hash['_id'] = hash['id']
+
+ @_source.content_type.fields_by_name.each do |name, field|
+ case field.type
+ when :file
+ hash[name] = hash["#{name}_url"] = file_field_to_url(hash[name.to_s]) if hash[name.to_s].present?
+ when :select
+ hash[name] = @_source.send(name) if hash["#{name}_id"].present?
+ end
+ end
+ end
+ end
+
+ def as_json(options = nil)
+ self.to_hash.as_json(options)
+ end
+
protected
+ def file_field_to_url(field)
+ field.to_liquid.tap { |drop| drop.context = @context }.url
+ end
+
def repository(entry)
repository = @context.registers[:services].repositories.content_entry
repository.with(entry.content_type)
spec/unit/liquid/drops/content_entry_spec.rb +16 -0
@@ @@ -96,4 +96,20 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do
end
+ 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(:picture_field) { Locomotive::Steam::ContentEntry::FileField.new('foo.png', 'http://assets.dev', 42) }
+
+ before do
+ allow(entry).to receive(:to_hash).and_return({ 'id' => 1, 'title' => 'Hello world', 'picture' => picture_field, 'category_id' => 42 })
+ allow(entry).to receive(:category).and_return('Test')
+ end
+
+ 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') }
+
+ end
+
end