a better implementation of the to_hash method for content_entries (WIP)
did
committed Oct 07, 2015
commit b639ef249fd7e7ea7fa665499840eeaa4c580381
Showing 2
changed files with
31 additions
and 0 deletions
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb
+16
-0
| @@ | @@ -73,6 +73,22 @@ module Locomotive::Steam |
| super.merge(content_type_id: content_type_id) | |
| end | |
| + | def to_hash |
| + | attributes.slice(:id, :_slug, :_position, :created_at, :updated_at).tap do |hash| |
| + | # _id & id |
| + | hash['_id'] = hash['id'] |
| + | |
| + | hash['_slug'] = self._slug |
| + | hash['_label'] = self._label |
| + | hash['_visible'] = self._visible |
| + | hash['content_type_slug'] = self.content_type_slug |
| + | |
| + | content_type.fields_by_name.each do |name, field| |
| + | hash[name] = _cast_value(field) |
| + | end |
| + | end |
| + | end |
| + | |
| def to_liquid | |
| Locomotive::Steam::Liquid::Drops::ContentEntry.new(self) | |
| end | |
spec/unit/entities/content_entry_spec.rb
+15
-0
| @@ | @@ -55,6 +55,21 @@ describe Locomotive::Steam::ContentEntry do |
| end | |
| + | describe '#to_hash' do |
| + | |
| + | let(:fields) { [instance_double('Field', name: :title, type: :string, required: true)] } |
| + | let(:attributes) { { id: 42, title: 'Hello world', _slug: 'hello-world', custom_fields_recipe: ['hello', 'world'], _type: 'Entry' } } |
| + | |
| + | subject { content_entry.to_hash } |
| + | |
| + | before do |
| + | allow(type).to receive(:fields_by_name).and_return({ title: fields.first }) |
| + | end |
| + | |
| + | it { expect(Set.new(subject.keys)).to eq(Set.new(['id', '_id', '_position', '_visible', '_label', '_slug', 'content_type_slug', 'title', 'created_at', 'updated_at'])) } |
| + | |
| + | end |
| + | |
| describe 'dynamic attributes' do | |
| let(:field_type) { :string } | |