Sha256: 4c11075375a058becccea5de4f9e87e0c9189c050c6574f8d6af925ef542ecc9

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

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
      end
      alias_method :descriptions, :definitions

    private

      # Reads the element definitions file named +elements.yml+ from +config/alchemy/+ folder.
      #
      def read_definitions_file
        if ::File.exists?(definitions_file_path)
          ::YAML.load(ERB.new(File.read(definitions_file_path)).result) || []
        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 #{self.name}. Please check your elements.yml file!"
        return {}
      end
    end
    alias_method :description, :definition

  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
alchemy_cms-3.2.1 app/models/alchemy/element/definitions.rb
lc_alchemy_cms-3.2.1 app/models/alchemy/element/definitions.rb
lc_alchemy_cms-3.2.0 app/models/alchemy/element/definitions.rb
alchemy_cms-3.2.0 app/models/alchemy/element/definitions.rb
alchemy_cms-3.2.0.rc1 app/models/alchemy/element/definitions.rb