class Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentType

Public Instance Methods

load(scope) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 11
def load(scope)
  super
  load_list
end

Private Instance Methods

build_field(name, attributes, position) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 34
def build_field(name, attributes, position)
  attributes.tap do |attributes|
    attributes[:_id]      = name.to_s
    attributes[:name]     = name.to_s
    attributes[:type]     = (attributes[:type] || 'string').to_s.downcase
    attributes[:position] ||= position

    if attributes[:label].blank?
      attributes[:label] = name.to_s.humanize
    end

    if %w(belongs_to has_many many_to_many).include?(attributes[:type])
      attributes[:localized] = false
    end

    if select_options = attributes.delete(:select_options)
      attributes[:select_options] = build_select_options(select_options)
    end
  end
end
build_fields(list) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 28
def build_fields(list)
  list.each_with_index.map do |attributes, index|
    build_field(attributes.keys.first, attributes.values.first, index)
  end
end
build_select_options(options) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 55
def build_select_options(options)
  if options.is_a?(Hash)
    build_select_options_from_hash(options)
  else
    build_select_options_from_array(options)
  end
end
build_select_options_from_array(options) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 77
def build_select_options_from_array(options)
  [].tap do |list|
    options.each_with_index do |name, position|
      # _id = name.is_a?(Hash) ? name.values.first : name
      list << { _id: position, name: name, position: position }
    end
  end
end
build_select_options_from_hash(options) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 63
def build_select_options_from_hash(options)
  [].tap do |list|
    options.each do |locale, values|
      values.each_with_index do |name, position|
        if (option = list.at(position)).nil?
          list << { _id: position.to_s, name: { locale => name }, position: position }
        else
          option[:name][locale] = name
        end
      end
    end
  end
end
each_file() { |filepath, slug| ... } click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 86
def each_file(&block)
  Dir.glob(File.join(path, "*.yml")).each do |filepath|
    slug = File.basename(filepath, '.yml')
    yield(filepath, slug)
  end
end
load_list() click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 18
def load_list
  [].tap do |array|
    each_file do |filepath, slug|
      attributes = _load(filepath)
      attributes[:entries_custom_fields] = build_fields(attributes.delete(:fields))
      array << { _id: slug.to_s, slug: slug }.merge(attributes)
    end
  end
end
path() click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb, line 93
def path
  File.join(site_path, 'app', 'content_types')
end