From c537064d9c9d77c6c46ec66962f6280cb8fd2b31 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Tue, 9 Jul 2019 12:39:17 -0700 Subject: [PATCH] Add Support for pre-defined Custom Setting Types --- lib/locomotive/steam/adapters/filesystem.rb | 4 +-- .../steam/adapters/filesystem/sanitizer.rb | 29 +++++++++++++++- .../adapters/filesystem/sanitizers/section.rb | 33 +++++++++++++++++++ .../adapters/filesystem/sanitizers/site.rb | 14 +++++++- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/lib/locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb index 25c3d236..97f25577 100644 --- a/lib/locomotive/steam/adapters/filesystem.rb +++ b/lib/locomotive/steam/adapters/filesystem.rb @@ -122,9 +122,9 @@ def build_yaml_loaders end def build_sanitizers - hash = Hash.new { build_klass('Sanitizers', :simple).new } + hash = Hash.new { build_klass('Sanitizers', :simple).new(site_path) } %i(sites pages content_types content_entries snippets sections).inject(hash) do |memo, name| - memo[name] = build_klass('Sanitizers', name).new + memo[name] = build_klass('Sanitizers', name).new(site_path) memo end end diff --git a/lib/locomotive/steam/adapters/filesystem/sanitizer.rb b/lib/locomotive/steam/adapters/filesystem/sanitizer.rb index 65f83fd8..7aafb20c 100644 --- a/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +++ b/lib/locomotive/steam/adapters/filesystem/sanitizer.rb @@ -8,7 +8,11 @@ module Sanitizer def_delegators :@scope, :site, :locale, :locales, :default_locale - attr_reader :scope + attr_reader :scope, :site_path + + def initialize(site_path) + @site_path = site_path + end def setup(scope) @scope = scope @@ -45,6 +49,29 @@ def attach_site_to(entity) entity[:site_id] = scope.site._id if scope.site end + def load_custom_field_types + Dir.glob(File.join(site_path, 'config', 'custom_field_types', "*.json")).map do |filepath| + if File.exists?(filepath) + json = File.read(filepath) + + begin + json = MultiJson.load(json) + rescue MultiJson::ParseError => e + raise Locomotive::Steam::JsonParsingError.new(e, filepath, json) + end + else + json = {} + end + + slug = File.basename(filepath).split('.').first + + { + slug: slug, + definition: json, + } + end + end + alias :current_locale :locale end diff --git a/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb index 8b5281b8..3e3ef9ec 100644 --- a/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +++ b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb @@ -41,6 +41,39 @@ def handle_aliases(definition) definition['default'] = default end + # Handle Custom Setting Types + custom_field_types = load_custom_field_types + + if custom_field_types.present? + if definition['settings'].present? + definition['settings'].each_with_index do |setting, i| + if setting['type'].present? + custom_field_type = custom_field_types.detect{|x| x[:slug] == setting['type']} + + if custom_field_type + definition['settings'][i] = custom_field_type[:definition].merge(setting) + end + end + end + end + + if definition['blocks'].present? + definition['blocks'].each_with_index do |block_def, i| + if block_def['settings'].present? + block_def['settings'].each_with_index do |setting, i2| + if setting['type'].present? + custom_field_type = custom_field_types.detect{|x| x[:slug] == setting['type']} + + if custom_field_type + definition['blocks'][i]['settings'][i2] = custom_field_type[:definition].merge(setting) + end + end + end + end + end + end + end + definition end diff --git a/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb index de0ef276..1ea2d8bf 100644 --- a/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb +++ b/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb @@ -30,6 +30,8 @@ def clean_metafields_schema(schema) end def parse_metafields(fields) + custom_field_types = load_custom_field_types + fields.each_with_index.map do |(name, attributes), position| name, attributes = name.to_a[0] if name.is_a?(Hash) # ordered list of fields @@ -38,7 +40,17 @@ def parse_metafields(fields) attributes[:hint] = { default: attributes[:hint] } if attributes[:hint].is_a?(String) end - { name: name.to_s, position: position }.merge(attributes || {}) + attributes ||= {} + + if custom_field_types.present? && attributes[:type].present? + custom_field_type = custom_field_types.detect{|x| x[:slug] == attributes[:type]} + + if custom_field_type + attributes = custom_field_type[:definition].merge(attributes) + end + end + + { name: name.to_s, position: position }.merge(attributes) end end