Sha256: 6ed692f103e39817e1e68bdaf2691ee42adc46f2bfd122f111d7cb916f6b2763

Contents?: true

Size: 1.54 KB

Versions: 36

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Alchemy
  # Module concerning element definitions
  #
  module Element::Definitions
    extend ActiveSupport::Concern

    module ClassMethods
      # 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 definitions
        @definitions ||= read_definitions_file.map(&:with_indifferent_access)
      end

      # Returns one element definition by given name.
      #
      def definition_by_name(name)
        definitions.detect { |d| d['name'] == name }
      end

      private

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

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

    # The definition of this element.
    #
    def definition
      if definition = self.class.definitions.detect { |d| d['name'] == name }
        definition
      else
        log_warning "Could not find element definition for #{name}. Please check your elements.yml file!"
        {}
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
alchemy_cms-4.6.7 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.6 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.5 app/models/alchemy/element/definitions.rb
alchemy_cms-4.5.1 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.5 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.4 app/models/alchemy/element/definitions.rb
alchemy_cms-4.0.6 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.3 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.2 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.1 app/models/alchemy/element/definitions.rb
alchemy_cms-4.6.0 app/models/alchemy/element/definitions.rb
alchemy_cms-4.5.0 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.4 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.3 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.2 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.1 app/models/alchemy/element/definitions.rb
alchemy_cms-4.4.0 app/models/alchemy/element/definitions.rb
alchemy_cms-4.3.2 app/models/alchemy/element/definitions.rb
alchemy_cms-4.2.4 app/models/alchemy/element/definitions.rb
alchemy_cms-4.3.1 app/models/alchemy/element/definitions.rb