lib/alchemy/page_layout.rb
# frozen_string_literal: true
module Alchemy
class PageLayout
class << self
# Returns all page layouts.
#
# They are defined in +config/alchemy/page_layout.yml+ file.
#
def all
@definitions ||= read_definitions_file.map(&:with_indifferent_access)
end
# Add additional page definitions to collection.
#
# Useful for extending the page layouts from an Alchemy module.
#
# === Usage Example
#
# Call +Alchemy::PageLayout.add(your_definition)+ in your engine.rb file.
#
# @param [Array || Hash]
# You can pass a single layout definition as Hash, or a collection of page layouts as Array.
#
def add(page_layout)
all
if page_layout.is_a?(Array)
@definitions += page_layout
elsif page_layout.is_a?(Hash)
@definitions << page_layout
else
raise TypeError
end
end
# Returns one page definition by given name.
#
def get(name)
return {} if name.blank?
all.detect { |a| a["name"].casecmp(name).zero? }
end
def reset!
@definitions = nil
end
# The absolute +page_layouts.yml+ file path
# @return [Pathname]
def layouts_file_path
Rails.root.join("config", "alchemy", "page_layouts.yml")
end
private
# Reads the layout definitions from +config/alchemy/page_layouts.yml+.
#
def read_definitions_file
if File.exist?(layouts_file_path)
Array.wrap(
YAML.safe_load(
ERB.new(File.read(layouts_file_path)).result,
permitted_classes: YAML_PERMITTED_CLASSES,
aliases: true
) || []
)
else
raise LoadError, "Could not find page_layouts.yml file! Please run `rails generate alchemy:install`"
end
end
end
end
end