allow a default value for fields of a content entry (Filesystem adapter) (locomotivecms/engine#1014)
did
committed Jan 19, 2016
commit ea9cd181d0571fcdbd18b40c8da01dd880478db5
Showing 9
changed files with
67 additions
and 15 deletions
locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb
+27
-1
| @@ | @@ -16,13 +16,14 @@ module Locomotive::Steam |
| dataset.all.each do |entity| | |
| _apply_to_dataset(entity, dataset) | |
| end | |
| + | clean |
| end | |
| def apply_to_entity_with_dataset(entity, dataset) | |
| # Note: this statement attaches the site to the entity | |
| apply_to_entity(entity) | |
| - | # make sure it gets an unique slug and an _id |
| + | # make sure it gets an unique slug and an _id + set default values |
| _apply_to_dataset(entity, dataset) | |
| end | |
| @@ | @@ -31,6 +32,7 @@ module Locomotive::Steam |
| def _apply_to_dataset(entity, dataset) | |
| set_slug(entity, dataset) | |
| set_id(entity) | |
| + | set_default_values(entity) |
| end | |
| def add_label(entity) | |
| @@ | @@ -52,6 +54,21 @@ module Locomotive::Steam |
| end | |
| end | |
| + | def set_default_values(entity) |
| + | each_field_with_default(entity) do |field| |
| + | name = field.type == 'select' ? "#{field.name}_id" : field.name |
| + | value = field.localized? ? entity[name][default_locale] : entity[name] |
| + | |
| + | return unless value.nil? |
| + | |
| + | if field.localized? |
| + | entity[name].translations = field.default |
| + | else |
| + | entity[name] = field.default |
| + | end |
| + | end |
| + | end |
| + | |
| def set_slug(entity, dataset) | |
| if entity._label.respond_to?(:translations) # localized? | |
| entity._label.each do |locale, label| | |
| @@ | @@ -78,6 +95,15 @@ module Locomotive::Steam |
| dataset.query(locale) { where(_slug: slug, k(:_id, :ne) => id) }.first.nil? | |
| end | |
| + | def each_field_with_default(entity, &block) |
| + | @fields_with_default ||= entity.content_type.fields_with_default |
| + | @fields_with_default.each(&block) |
| + | end |
| + | |
| + | def clean |
| + | @fields_with_default = nil |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb
+1
-0
| @@ | @@ -7,6 +7,7 @@ module Locomotive::Steam |
| def_delegator :fields, :associations, :association_fields | |
| def_delegator :fields, :selects, :select_fields | |
| + | def_delegator :fields, :default, :fields_with_default |
| def initialize(attributes = {}) | |
| super({ | |
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb
+2
-1
| @@ | @@ -11,7 +11,8 @@ module Locomotive::Steam |
| type: :string, | |
| localized: false, | |
| required: false, | |
| - | unique: false |
| + | unique: false, |
| + | default: nil |
| }.merge(attributes)) | |
| end | |
locomotive/steam/repositories/content_type_field_repository.rb b/lib/locomotive/steam/repositories/content_type_field_repository.rb
+4
-0
| @@ | @@ -53,6 +53,10 @@ module Locomotive |
| query { where(localized: true) }.all.map(&:name) | |
| end | |
| + | def default |
| + | query { where(k(:default, :neq) => nil, k(:type, :in) => [:string, :text, :color, :select, :boolean, :email, :integer, :float]) }.all |
| + | end |
| + | |
| def select_options(name) | |
| if field = first { where(name: name, type: :select) } | |
| field.select_options.all | |
spec/fixtures/default/app/content_types/events.yml
+1
-0
| @@ | @@ -12,6 +12,7 @@ fields: |
| - city: | |
| type: string | |
| label: City of the event | |
| + | default: Chicago |
| - state: | |
| type: string | |
| label: State of the event | |
spec/fixtures/default/data/events.yml
+1
-2
| @@ | @@ -45,9 +45,8 @@ |
| state: Missouri | |
| - Ballydoyle's: | |
| date: 2011/06/05 | |
| - | city: Aurora |
| state: Illinois | |
| - The Belmont: | |
| date: 2011/06/04 | |
| city: Hamtramk | |
| - | state: Michigan |
| \ No newline at end of file | |
| + | state: Michigan |
spec/unit/entities/content_type_spec.rb
+10
-0
| @@ | @@ -30,6 +30,16 @@ describe Locomotive::Steam::ContentType do |
| end | |
| + | describe '#default' do |
| + | |
| + | subject { content_type.fields_with_default } |
| + | |
| + | before { expect(repository).to receive(:default).and_return([true]) } |
| + | |
| + | it { expect(subject).to eq([true]) } |
| + | |
| + | end |
| + | |
| describe '#order_by' do | |
| subject { content_type.order_by } | |
spec/unit/repositories/content_entry_repository_spec.rb
+11
-11
| @@ | @@ -5,7 +5,7 @@ require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| describe Locomotive::Steam::ContentEntryRepository do | |
| let(:_fields) { instance_double('Fields', selects: [], belongs_to: [], many_to_many: []) } | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) } |
| let(:entries) { [{ content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }] } | |
| let(:locale) { :en } | |
| let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: %i(en fr)) } | |
| @@ | @@ -108,7 +108,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| end | |
| context 'with a has_many field' do | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { articles: instance_double('Field', type: :has_many) }) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { articles: instance_double('Field', type: :has_many) }, fields_with_default: []) } |
| let(:proxy_repository) { repository.dup } | |
| let(:entry) { instance_double('Entry', articles: proxy_repository) } | |
| let(:name) { :articles } | |
| @@ | @@ -125,7 +125,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#next' do | |
| - | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) } |
| let(:entries) do | |
| [ | |
| { content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, | |
| @@ | @@ -157,7 +157,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#previous' do | |
| - | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: { _position: 'asc' }, label_field_name: :title, localized_names: [:title], fields: _fields, fields_by_name: { title: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) } |
| let(:entries) do | |
| [ | |
| { content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, | |
| @@ | @@ -204,7 +204,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| category: instance_double('SelectField', name: :category, type: :select, select_options: { en: ['cooking', 'bread'], fr: ['cuisine', 'pain'] }) | |
| } | |
| end | |
| - | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields: _fields, fields_by_name: fields) } |
| + | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields: _fields, fields_by_name: fields, fields_with_default: []) } |
| let(:name) { :category } | |
| let(:options) { | |
| @@ | @@ -243,9 +243,9 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'belongs_to' do | |
| let(:field) { instance_double('Field', name: :author, type: :belongs_to, association_options: { target_id: 2 }) } | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, association_fields: [field]) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, association_fields: [field], fields_with_default: []) } |
| let(:entries) { [{ content_type_id: 1, title: 'Hello world', author_id: 'john-doe' }] } | |
| - | let(:other_type) { build_content_type('Authors', _id: 2, label_field_name: :name, fields: _fields, fields_by_name: { name: instance_double('Field', name: :name, type: :string) }) } |
| + | let(:other_type) { build_content_type('Authors', _id: 2, label_field_name: :name, fields: _fields, fields_by_name: { name: instance_double('Field', name: :name, type: :string) }, fields_with_default: []) } |
| let(:other_entries) { [{ content_type_id: 2, _id: 'john-doe', name: 'John Doe' }] } | |
| let(:type_repository) { instance_double('ArticleBelongsToRepository', selects: [], belongs_to: [], many_to_many: []) } | |
| @@ | @@ -270,9 +270,9 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'has_many' do | |
| let(:field) { instance_double('Field', name: :articles, type: :has_many, association_options: { target_id: 2, inverse_of: :author, order_by: 'position_in_author' }) } | |
| - | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field]) } |
| + | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field], fields_with_default: []) } |
| let(:entries) { [{ content_type_id: 1, _id: 'john-doe', name: 'John Doe' }] } | |
| - | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) } |
| let(:other_entries) { | |
| [ | |
| { content_type_id: 2, _id: 'hello-world', title: 'Hello world', author_id: 'john-doe', position_in_author: 2 }, | |
| @@ | @@ -303,9 +303,9 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'many_to_many' do | |
| let(:field) { instance_double('Field', name: :articles, type: :many_to_many, association_options: { target_id: 2, inverse_of: :authors }) } | |
| - | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field], fields: _fields) } |
| + | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field], fields: _fields, fields_with_default: []) } |
| let(:entries) { [{ content_type_id: 1, _id: 1, name: 'John Doe', article_ids: ['hello-world', 'lorem-ipsum'] }] } | |
| - | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields: _fields, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }, fields_with_default: []) } |
| let(:other_entries) { | |
| [ | |
| { content_type_id: 2, _id: 'hello-world', title: 'Hello world', author_id: 'john-doe', position_in_author: 2 }, | |
spec/unit/repositories/content_type_field_repository_spec.rb
+10
-0
| @@ | @@ -48,4 +48,14 @@ describe Locomotive::Steam::ContentTypeFieldRepository do |
| end | |
| + | describe '#default' do |
| + | |
| + | let(:collection) { [{ name: 'name', type: 'string' }, { name: 'email', type: 'email', default: 'john@doe.net' }] } |
| + | |
| + | subject { repository.default } |
| + | |
| + | it { expect(subject.first.name).to eq 'email' } |
| + | |
| + | end |
| + | |
| end | |