I18nField gets now its own to_json method + refactor the to_hash method of the content entry entity
did
committed Oct 08, 2015
commit efcdcf4d054590c4a1765886af347e84b73f19dd
Showing 11
changed files with
101 additions
and 56 deletions
locomotive/steam/decorators/i18n_decorator.rb b/lib/locomotive/steam/decorators/i18n_decorator.rb
+0
-1
| @@ | @@ -86,7 +86,6 @@ module Locomotive |
| def to_hash | |
| __getobj__.to_hash.tap do |hash| | |
| - | puts @__localized_attributes__.inspect |
| @__localized_attributes__.keys.each do |name| | |
| hash[name.to_s] = __get_localized_value__(name) | |
| end | |
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb
+13
-25
| @@ | @@ -74,31 +74,15 @@ module Locomotive::Steam |
| 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| |
| - | value = send(field.name) |
| - | |
| - | # custom behaviors for some types |
| - | case field.type |
| - | when :belongs_to |
| - | # TODO |
| - | when :many_to_many |
| - | # TODO |
| - | when :file |
| - | puts "value = #{value.inspect}" |
| - | hash[name] = hash[:"#{name}_url"] = value.respond_to?(:transform!) ? value.transform!(&:url) : value.url |
| - | else |
| - | hash[name] = value |
| - | end |
| - | end |
| + | # 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| |
| + | hash[name.to_s] = send(name) |
| + | hash |
| end | |
| end | |
| @@ | @@ -184,6 +168,10 @@ module Locomotive::Steam |
| base.blank? ? filename : "#{base}/#{filename}" | |
| end | |
| + | def to_json |
| + | url |
| + | end |
| + | |
| def to_liquid | |
| Locomotive::Steam::Liquid::Drops::UploadedFile.new(self) | |
| end | |
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb
+9
-0
| @@ | @@ -31,6 +31,15 @@ module Locomotive::Steam |
| fields.localized_names + select_fields.map(&:name) | |
| end | |
| + | def persisted_field_names |
| + | [].tap do |names| |
| + | fields_by_name.each do |name, field| |
| + | _name = field.persisted_name |
| + | names << _name if _name |
| + | end |
| + | end |
| + | end |
| + | |
| def label_field_name | |
| (self[:label_field_name] || fields.first.name).to_sym | |
| end | |
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb
+9
-0
| @@ | @@ -59,6 +59,15 @@ module Locomotive::Steam |
| %i(belongs_to has_many many_to_many).include?(self.type) | |
| end | |
| + | def persisted_name |
| + | case type |
| + | when :belongs_to, :select then "#{name}_id" |
| + | when :many_to_many then "#{name.singularize}_ids" |
| + | when :has_many then nil |
| + | else name |
| + | end |
| + | end |
| + | |
| class SelectOption | |
| include Locomotive::Steam::Models::Entity | |
locomotive/steam/models/concerns/to_json.rb b/lib/locomotive/steam/models/concerns/to_json.rb
+2
-5
| @@ | @@ -10,12 +10,9 @@ module Locomotive |
| attributes.each do |key, value| | |
| next if value && value.respond_to?(:repository) # skip associations | |
| - | _attributes[key] = (case value |
| - | when Locomotive::Steam::Models::I18nField then value.to_hash |
| - | else value |
| - | end) |
| + | _attributes[key.to_s] = value |
| end | |
| - | end.stringify_keys |
| + | end |
| end | |
| def as_json(options = nil) | |
locomotive/steam/models/i18n_field.rb b/lib/locomotive/steam/models/i18n_field.rb
+2
-8
| @@ | @@ -47,14 +47,8 @@ module Locomotive::Steam |
| attributes[@name] = @translations | |
| end | |
| - | def transform |
| - | @translations.each do |locale, value| |
| - | @translations[locale] = yield(value) |
| - | end |
| - | end |
| - | |
| - | def transform!(&block) |
| - | self.dup.tap { |field| field.transform(&block) } |
| + | def to_json |
| + | to_hash.to_json |
| end | |
| end | |
spec/unit/entities/content_entry_spec.rb
+4
-3
| @@ | @@ -64,9 +64,10 @@ describe Locomotive::Steam::ContentEntry do |
| before do | |
| allow(type).to receive(:fields_by_name).and_return({ title: fields.first, picture: fields.last }) | |
| + | allow(type).to receive(:persisted_field_names).and_return([:title, :picture]) |
| end | |
| - | it { expect(Set.new(subject.keys)).to eq(Set.new(['id', '_id', '_position', '_visible', '_label', '_slug', 'content_type_slug', 'title', 'picture_url', 'picture', 'created_at', 'updated_at'])) } |
| + | it { expect(Set.new(subject.keys)).to eq(Set.new(['id', '_position', '_visible', '_label', '_slug', 'content_type_slug', 'title', 'picture', 'created_at', 'updated_at'])) } |
| context 'when decorated' do | |
| @@ | @@ -74,9 +75,9 @@ describe Locomotive::Steam::ContentEntry do |
| before { allow(content_entry).to receive(:localized_attributes).and_return({ picture: true }) } | |
| - | subject { puts decorated.picture.inspect; puts decorated.send(:picture).inspect; puts "---"; decorated.to_hash } |
| + | subject { decorated.to_hash } |
| - | it { expect(subject[:picture]).to eq '/assets/foo.png' } |
| + | it { expect(subject['picture'].url).to eq '/assets/foo.png' } |
| end | |
spec/unit/entities/content_type_field_spec.rb
+41
-0
| @@ | @@ -74,4 +74,45 @@ describe Locomotive::Steam::ContentTypeField do |
| end | |
| + | describe '#persisted_name' do |
| + | |
| + | subject { field.persisted_name } |
| + | |
| + | context 'string type' do |
| + | |
| + | let(:attributes) { { name: 'title', type: 'string' } } |
| + | it { is_expected.to eq 'title' } |
| + | |
| + | end |
| + | |
| + | context 'select type' do |
| + | |
| + | let(:attributes) { { name: 'category', type: 'select' } } |
| + | it { is_expected.to eq 'category_id' } |
| + | |
| + | end |
| + | |
| + | context 'belongs_to type' do |
| + | |
| + | let(:attributes) { { name: 'article', class_name: 'articles', type: 'belongs_to' } } |
| + | it { is_expected.to eq 'article_id' } |
| + | |
| + | end |
| + | |
| + | context 'many_to_many type' do |
| + | |
| + | let(:attributes) { { name: 'articles', class_name: 'articles', type: 'many_to_many' } } |
| + | it { is_expected.to eq 'article_ids' } |
| + | |
| + | end |
| + | |
| + | context 'has_many type' do |
| + | |
| + | let(:attributes) { { name: 'articles', class_name: 'articles', type: 'has_many', inverse_of: 'author' } } |
| + | it { is_expected.to eq nil } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| end | |
spec/unit/entities/content_type_spec.rb
+10
-0
| @@ | @@ -56,4 +56,14 @@ describe Locomotive::Steam::ContentType do |
| end | |
| + | describe '#persisted_field_names' do |
| + | |
| + | let(:fields) { [instance_double('Field1', name: :title, persisted_name: 'title'), instance_double('Field2', name: :author, persisted_name: nil)] } |
| + | |
| + | subject { content_type.persisted_field_names } |
| + | |
| + | it { is_expected.to eq(['title']) } |
| + | |
| + | end |
| + | |
| end | |
spec/unit/models/concerns/to_json_spec.rb
+11
-2
| @@ | @@ -17,8 +17,9 @@ describe Locomotive::Steam::Models::Concerns::ToJson do |
| end | |
| context 'localized attributes' do | |
| - | let(:attributes) { { title: Locomotive::Steam::Models::I18nField.new(:title, { fr: 'Bonjour', en: 'Hi' })} } |
| - | it { expect(subject).to eq('title' => { 'fr' => 'Bonjour', 'en' => 'Hi' }) } |
| + | let(:i18n_field) { Locomotive::Steam::Models::I18nField.new(:title, { fr: 'Bonjour', en: 'Hi' }) } |
| + | let(:attributes) { { title: i18n_field} } |
| + | it { expect(subject).to eq('title' => i18n_field) } |
| end | |
| context 'referenced associations' do | |
| @@ | @@ -61,6 +62,14 @@ describe Locomotive::Steam::Models::Concerns::ToJson do |
| end | |
| + | context 'with localized attributes' do |
| + | |
| + | let(:attributes) { { title: Locomotive::Steam::Models::I18nField.new(:title, { fr: 'Bonjour', en: 'Hi' })} } |
| + | |
| + | it { expect(subject).to eq(%{{"title":{"fr":"Bonjour","en":"Hi"}}}) } |
| + | |
| + | end |
| + | |
| end | |
| class SimpleEntity | |
spec/unit/models/i18n_field_spec.rb
+0
-12
| @@ | @@ -22,16 +22,4 @@ describe Locomotive::Steam::Models::I18nField do |
| end | |
| - | describe '#transform' do |
| - | |
| - | let(:translations) { { fr: 42, en: '42' } } |
| - | |
| - | before { field.transform(&:to_s) } |
| - | |
| - | subject { field.translations } |
| - | |
| - | it { is_expected.to eq('fr' => '42', 'en' => '42') } |
| - | |
| - | end |
| - | |
| end | |