Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Support for pre-defined Custom Field Types #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/locomotive/steam/adapters/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion lib/locomotive/steam/adapters/filesystem/sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down