|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Alchemy |
| 4 | + class ElementDefinition |
| 5 | + include ActiveModel::Model |
| 6 | + include ActiveModel::Attributes |
| 7 | + include Alchemy::Hints |
| 8 | + |
| 9 | + extend ActiveModel::Translation |
| 10 | + |
| 11 | + attribute :name, :string |
| 12 | + attribute :unique, :boolean, default: false |
| 13 | + attribute :amount, :integer, default: Float::INFINITY |
| 14 | + attribute :taggable, :boolean, default: false |
| 15 | + attribute :compact, :boolean, default: false |
| 16 | + attribute :fixed, :boolean, default: false |
| 17 | + attribute :ingredients, default: [] |
| 18 | + attribute :nestable_elements, default: [] |
| 19 | + attribute :autogenerate, default: [] |
| 20 | + attribute :deprecated |
| 21 | + attribute :message |
| 22 | + attribute :warning |
| 23 | + attribute :hint |
| 24 | + |
| 25 | + validates :name, |
| 26 | + presence: true, |
| 27 | + format: { |
| 28 | + with: /\A[a-z_-]+\z/ |
| 29 | + } |
| 30 | + |
| 31 | + delegate :blank?, to: :name |
| 32 | + |
| 33 | + class << self |
| 34 | + # Returns the definitions from elements.yml file. |
| 35 | + # |
| 36 | + # Place a +elements.yml+ file inside your apps +config/alchemy+ folder to define |
| 37 | + # your own set of elements |
| 38 | + # |
| 39 | + def all |
| 40 | + @definitions ||= read_definitions_file.map { new(**_1) } |
| 41 | + end |
| 42 | + |
| 43 | + # Add additional page definitions to collection. |
| 44 | + # |
| 45 | + # Useful for extending the elements from an Alchemy module. |
| 46 | + # |
| 47 | + # === Usage Example |
| 48 | + # |
| 49 | + # Call +Alchemy::ElementDefinition.add(your_definition)+ in your engine.rb file. |
| 50 | + # |
| 51 | + # @param [Array || Hash] |
| 52 | + # You can pass a single element definition as Hash, or a collection of elements as Array. |
| 53 | + # |
| 54 | + def add(definition) |
| 55 | + all |
| 56 | + @definitions += Array.wrap(definition).map { new(**_1) } |
| 57 | + end |
| 58 | + |
| 59 | + # Returns one element definition by given name. |
| 60 | + # |
| 61 | + def get(name) |
| 62 | + return new if name.blank? |
| 63 | + |
| 64 | + all.detect { _1.name.casecmp(name).zero? } |
| 65 | + end |
| 66 | + |
| 67 | + def reset! |
| 68 | + @definitions = nil |
| 69 | + end |
| 70 | + |
| 71 | + # The absolute +elements.yml+ file path |
| 72 | + # @return [Pathname] |
| 73 | + def definitions_file_path |
| 74 | + Rails.root.join("config", "alchemy", "elements.yml") |
| 75 | + end |
| 76 | + |
| 77 | + private |
| 78 | + |
| 79 | + def definitions_file |
| 80 | + File.read(definitions_file_path) |
| 81 | + end |
| 82 | + |
| 83 | + def definitions_file_exist? |
| 84 | + File.exist?(definitions_file_path) |
| 85 | + end |
| 86 | + |
| 87 | + # Reads the element definitions from +config/alchemy/elements.yml+. |
| 88 | + # |
| 89 | + def read_definitions_file |
| 90 | + if definitions_file_exist? |
| 91 | + YAML.safe_load( |
| 92 | + ERB.new(definitions_file).result, |
| 93 | + permitted_classes: YAML_PERMITTED_CLASSES, |
| 94 | + aliases: true |
| 95 | + ) || [] |
| 96 | + else |
| 97 | + raise LoadError, |
| 98 | + "Could not find elements.yml file! Please run `rails generate alchemy:install`" |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def attributes |
| 104 | + super.with_indifferent_access |
| 105 | + end |
| 106 | + alias_method :definition, :attributes |
| 107 | + |
| 108 | + def ingredients |
| 109 | + super.map(&:with_indifferent_access) |
| 110 | + end |
| 111 | + |
| 112 | + private |
| 113 | + |
| 114 | + def hint_translation_scope |
| 115 | + :element_hints |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments