Sha256: da69fb6e02e21a60a65bb8d2ff10ff324a13b6ed959cd9bef0c2fe2aa8157ec0

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 KB

Contents

# 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

      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

      # Returns the page_layouts.yml file path
      #
      def layouts_file_path
        Rails.root.join "config/alchemy/page_layouts.yml"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
alchemy_cms-6.1.10 lib/alchemy/page_layout.rb
alchemy_cms-6.1.9 lib/alchemy/page_layout.rb
alchemy_cms-6.1.8 lib/alchemy/page_layout.rb
alchemy_cms-6.1.7 lib/alchemy/page_layout.rb
alchemy_cms-6.1.6 lib/alchemy/page_layout.rb
alchemy_cms-6.1.5 lib/alchemy/page_layout.rb
alchemy_cms-6.1.4 lib/alchemy/page_layout.rb
alchemy_cms-7.0.0.pre.a lib/alchemy/page_layout.rb
alchemy_cms-6.1.3 lib/alchemy/page_layout.rb
alchemy_cms-6.1.2 lib/alchemy/page_layout.rb
alchemy_cms-6.1.1 lib/alchemy/page_layout.rb
alchemy_cms-6.1.0 lib/alchemy/page_layout.rb