Sha256: 97764adfca3d690c34148dbcec5ad1c7f6b860fe83d6956432e3accea61b0d57

Contents?: true

Size: 1.84 KB

Versions: 26

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Alchemy
  class ElementDefinition
    class << self
      # Returns the definitions from elements.yml file.
      #
      # Place a +elements.yml+ file inside your apps +config/alchemy+ folder to define
      # your own set of elements
      #
      def all
        @definitions ||= read_definitions_file.map(&:with_indifferent_access)
      end

      # Add additional page definitions to collection.
      #
      # Useful for extending the elements from an Alchemy module.
      #
      # === Usage Example
      #
      #   Call +Alchemy::ElementDefinition.add(your_definition)+ in your engine.rb file.
      #
      # @param [Array || Hash]
      #   You can pass a single element definition as Hash, or a collection of elements as Array.
      #
      def add(element)
        all
        if element.is_a?(Array)
          @definitions += element
        elsif element.is_a?(Hash)
          @definitions << element
        else
          raise TypeError
        end
      end

      # Returns one element definition by given name.
      #
      def get(name)
        return {} if name.blank?

        all.detect { |a| a["name"] == name }
      end

      private

      # Reads the element definitions from +config/alchemy/elements.yml+.
      #
      def read_definitions_file
        if File.exist?(definitions_file_path)
          YAML.safe_load(
            ERB.new(File.read(definitions_file_path)).result,
            permitted_classes: YAML_PERMITTED_CLASSES,
            aliases: true,
          ) || []
        else
          raise LoadError,
                "Could not find elements.yml file! Please run `rails generate alchemy:install`"
        end
      end

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
alchemy_cms-6.1.10 lib/alchemy/element_definition.rb
alchemy_cms-6.1.9 lib/alchemy/element_definition.rb
alchemy_cms-6.1.8 lib/alchemy/element_definition.rb
alchemy_cms-6.1.7 lib/alchemy/element_definition.rb
alchemy_cms-6.1.6 lib/alchemy/element_definition.rb
alchemy_cms-6.1.5 lib/alchemy/element_definition.rb
alchemy_cms-6.0.14 lib/alchemy/element_definition.rb
alchemy_cms-6.1.4 lib/alchemy/element_definition.rb
alchemy_cms-7.0.0.pre.a lib/alchemy/element_definition.rb
alchemy_cms-6.0.13 lib/alchemy/element_definition.rb
alchemy_cms-6.1.3 lib/alchemy/element_definition.rb
alchemy_cms-6.1.2 lib/alchemy/element_definition.rb
alchemy_cms-6.1.1 lib/alchemy/element_definition.rb
alchemy_cms-6.1.0 lib/alchemy/element_definition.rb
alchemy_cms-6.0.12 lib/alchemy/element_definition.rb
alchemy_cms-6.0.11 lib/alchemy/element_definition.rb
alchemy_cms-6.0.10 lib/alchemy/element_definition.rb
alchemy_cms-6.0.9 lib/alchemy/element_definition.rb
alchemy_cms-6.0.8 lib/alchemy/element_definition.rb
alchemy_cms-6.0.7 lib/alchemy/element_definition.rb