Sha256: f4ca5e33acf17e49bb096dbc4550cb1d3eb574d59b8f71a7e1a6480f8b29b678

Contents?: true

Size: 1.81 KB

Versions: 37

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

37 entries across 37 versions & 1 rubygems

Version Path
alchemy_cms-7.1.13 lib/alchemy/page_layout.rb
alchemy_cms-7.0.16 lib/alchemy/page_layout.rb
alchemy_cms-7.1.12 lib/alchemy/page_layout.rb
alchemy_cms-7.0.15 lib/alchemy/page_layout.rb
alchemy_cms-7.1.11 lib/alchemy/page_layout.rb
alchemy_cms-7.1.10 lib/alchemy/page_layout.rb
alchemy_cms-7.1.9 lib/alchemy/page_layout.rb
alchemy_cms-7.0.14 lib/alchemy/page_layout.rb
alchemy_cms-7.0.13 lib/alchemy/page_layout.rb
alchemy_cms-7.1.8 lib/alchemy/page_layout.rb
alchemy_cms-7.1.7 lib/alchemy/page_layout.rb
alchemy_cms-7.0.12 lib/alchemy/page_layout.rb
alchemy_cms-7.0.11 lib/alchemy/page_layout.rb
alchemy_cms-7.1.6 lib/alchemy/page_layout.rb
alchemy_cms-7.1.5 lib/alchemy/page_layout.rb
alchemy_cms-7.0.10 lib/alchemy/page_layout.rb
alchemy_cms-7.1.4 lib/alchemy/page_layout.rb
alchemy_cms-7.0.9 lib/alchemy/page_layout.rb
alchemy_cms-7.1.3 lib/alchemy/page_layout.rb
alchemy_cms-7.1.2 lib/alchemy/page_layout.rb