read the site metafields schema file from the filesystem
did
committed Jan 24, 2016
commit 7dca1d670ea351d0ac97c631a95716b8a387aefb
Showing 7
changed files with
139 additions
and 4 deletions
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+1
-1
| @@ | @@ -116,7 +116,7 @@ module Locomotive::Steam |
| def build_sanitizers | |
| hash = Hash.new { build_klass('Sanitizers', :simple).new } | |
| - | %i(pages content_types content_entries snippets).inject(hash) do |memo, name| |
| + | %i(site pages content_types content_entries snippets).inject(hash) do |memo, name| |
| memo[name] = build_klass('Sanitizers', name).new | |
| memo | |
| end | |
locomotive/steam/adapters/filesystem/sanitizers/site.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | module Locomotive::Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module Sanitizers |
| + | |
| + | class Site |
| + | |
| + | include Adapters::Filesystem::Sanitizer |
| + | |
| + | def apply_to_entity(entity) |
| + | entity.metafields_schema = clean_metafields_schema(entity.metafields_schema) |
| + | end |
| + | |
| + | private |
| + | |
| + | def clean_metafields_schema(schema) |
| + | return nil unless schema |
| + | |
| + | schema.map do |namespace, definitions| |
| + | { |
| + | name: { default: namespace.to_s }.merge(definitions.delete(:name) || {}), |
| + | fields: parse_metafields(definitions.delete(:fields)) |
| + | }.merge(definitions) |
| + | end |
| + | end |
| + | |
| + | def parse_metafields(fields) |
| + | fields.map do |name, attributes| |
| + | if attributes # Hash |
| + | { name: { default: name.to_s }.merge(attributes.delete(:name)) }.merge(attributes) |
| + | else # Array |
| + | { name: { default: name.to_s } } |
| + | end |
| + | end |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
locomotive/steam/adapters/filesystem/yaml_loaders/site.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/site.rb
+8
-0
| @@ | @@ -15,9 +15,17 @@ module Locomotive |
| attributes[:picture] = File.expand_path(File.join(site_path, 'icon.png')) | |
| + | attributes[:metafields_schema] = load_metafields_schema |
| + | |
| [attributes] | |
| end | |
| + | private |
| + | |
| + | def load_metafields_schema |
| + | schema = _load(File.join(site_path, 'config', 'metafields_schema.yml')) |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/entities/site.rb b/lib/locomotive/steam/entities/site.rb
+3
-1
| @@ | @@ -15,7 +15,9 @@ module Locomotive::Steam |
| redirect_to_first_domain: false, | |
| url_redirections: [], | |
| private_access: false, | |
| - | password: nil |
| + | password: nil, |
| + | metafields_schema: {}, |
| + | metafields: nil |
| }.merge(attributes)) | |
| end | |
spec/fixtures/default/config/metafields_schema.yml
+26
-0
| @@ | @@ -0,0 +1,26 @@ |
| + | Social: |
| + | name: |
| + | fr: Social (FR) |
| + | position: 1 |
| + | fields: |
| + | - Facebook ID |
| + | - Google ID |
| + | |
| + | Github: |
| + | position: 0 |
| + | fields: |
| + | "API url": |
| + | name: |
| + | fr: "Url de l'API" |
| + | type: string |
| + | default: https://api.github.com/repos/locomotivecms/engine/issues?state=opened |
| + | "Expire in": |
| + | name: |
| + | fr: 'Expire dans' |
| + | hint: |
| + | en: 'Cache - In milliseconds' |
| + | fr: 'Cache - En millisecondes' |
| + | type: integer |
| + | min: 0 |
| + | max: 3600 |
| + | |
spec/unit/adapters/filesystem/sanitizers/site_spec.rb
+48
-0
| @@ | @@ -0,0 +1,48 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/sanitizer.rb' |
| + | require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb' |
| + | |
| + | describe Locomotive::Steam::Adapters::Filesystem::Sanitizers::Site do |
| + | |
| + | let(:schema) { nil } |
| + | let(:entity) { instance_double('SiteEntity', metafields_schema: schema) } |
| + | let(:sanitizer) { described_class.new } |
| + | |
| + | describe '#apply_to_entity' do |
| + | |
| + | subject { sanitizer.apply_to_entity(entity) } |
| + | |
| + | it { expect(entity).to receive(:metafields_schema=).with(nil); subject } |
| + | |
| + | end |
| + | |
| + | describe '#clean_metafields_schema' do |
| + | |
| + | subject { sanitizer.send(:clean_metafields_schema, schema) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | |
| + | describe 'with a schema' do |
| + | |
| + | # see the metafields_schema.yml in the fixtures folder |
| + | let(:schema) { {:Social=>{:name=>{:fr=>"Social (FR)"}, :position=>1, :fields=>["Facebook ID", "Google ID"]}, :Github=>{:position=>0, :fields=>{:"API url"=>{:name=>{:fr=>"Url de l'API"}, :type=>"string", :default=>"https://api.github.com/repos/locomotivecms/engine/issues?state=opened"}, :"Expire in"=>{:name=>{:fr=>"Expire dans"}, :hint=>{:en=>"Cache - In milliseconds", :fr=>"Cache - En millisecondes"}, :type=>"integer", :min=>0, :max=>3600}}}} } |
| + | |
| + | it 'loads the full schema' do |
| + | # First namespace |
| + | expect(subject[0][:name]).to eq(default: 'Social', fr: 'Social (FR)') |
| + | expect(subject[0][:position]).to eq 1 |
| + | expect(subject[0][:fields]).to eq([{ name: { default: 'Facebook ID' } }, { name: { default: 'Google ID' } }]) |
| + | |
| + | # Second namespace |
| + | expect(subject[1][:name]).to eq(default: 'Github') |
| + | expect(subject[1][:position]).to eq 0 |
| + | expect(subject[1][:fields].count).to eq 2 |
| + | expect(subject[1][:fields][0]).to eq(name: { default: 'API url', fr: "Url de l'API" }, type: 'string', default: 'https://api.github.com/repos/locomotivecms/engine/issues?state=opened') |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/adapters/filesystem/yaml_loaders/site_spec.rb
+12
-2
| @@ | @@ -10,9 +10,19 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Site do |
| describe '#load' do | |
| - | subject { loader.load(nil) } |
| + | subject { loader.load(nil).first } |
| - | it { expect(subject.first[:name]).to eq 'Sample site' } |
| + | it { expect(subject[:name]).to eq 'Sample site' } |
| + | |
| + | describe '#metafields_schema' do |
| + | |
| + | subject { loader.load(nil).first[:metafields_schema] } |
| + | |
| + | it 'loads the full schema' do |
| + | expect(subject.count).to eq 2 |
| + | end |
| + | |
| + | end |
| end | |