Class: Plum::FieldTypeRegistry
- Inherits:
-
Object
- Object
- Plum::FieldTypeRegistry
- Defined in:
- app/services/plum/field_type_registry.rb
Defined Under Namespace
Classes: Definition
Constant Summary collapse
- COMMON_CONFIGURATION =
%w[label instructions required default placeholder].freeze
- NESTED_FIELD_TYPES =
%w[text textarea number boolean date url].freeze
- DEFINITIONS =
[ Definition.new(handle: "text", label: "Text", configuration: []), Definition.new(handle: "textarea", label: "Textarea", configuration: []), Definition.new(handle: "rich_text", label: "Rich Text", configuration: []), Definition.new(handle: "number", label: "Number", configuration: %w[number_kind min max step unit]), Definition.new(handle: "boolean", label: "Boolean", configuration: []), Definition.new(handle: "date", label: "Date / Time", configuration: %w[date_mode min max]), Definition.new(handle: "select", label: "Select", configuration: %w[options]), Definition.new(handle: "radio", label: "Radio", configuration: %w[options]), Definition.new(handle: "button_group", label: "Button Group", configuration: %w[options]), Definition.new(handle: "checkboxes", label: "Checkboxes", configuration: %w[options]), Definition.new(handle: "color", label: "Color", configuration: []), Definition.new(handle: "url", label: "URL", configuration: []), Definition.new(handle: "taxonomy", label: "Taxonomy", configuration: %w[taxonomy]), Definition.new(handle: "image", label: "Image", configuration: []), Definition.new(handle: "images", label: "Images", configuration: %w[min_items max_items]), Definition.new(handle: "relationship", label: "Relationship", configuration: %w[content_type multiple min_items max_items]), Definition.new(handle: "blocks", label: "Blocks", configuration: %w[blocks]), Definition.new(handle: "list", label: "List", configuration: %w[min_items max_items unique]), Definition.new(handle: "group", label: "Group", configuration: %w[fields]), Definition.new(handle: "repeater", label: "Repeater", configuration: %w[fields min_items max_items]), Definition.new(handle: "section", label: "Section", configuration: []) ].freeze
Class Method Summary collapse
- .all ⇒ Object
- .as_json ⇒ Object
- .find(handle) ⇒ Object
- .handles ⇒ Object
- .include?(handle) ⇒ Boolean
- .options ⇒ Object
- .register(handle:, label:, configuration: [], partial: nil, normalizer: nil, validator: nil, expander: nil) ⇒ Object
- .reset_custom! ⇒ Object
Class Method Details
.all ⇒ Object
36 37 38 |
# File 'app/services/plum/field_type_registry.rb', line 36 def all DEFINITIONS + custom_definitions.values end |
.as_json ⇒ Object
56 57 58 |
# File 'app/services/plum/field_type_registry.rb', line 56 def as_json all.map { |definition| { handle: definition.handle, label: definition.label } } end |
.find(handle) ⇒ Object
44 45 46 |
# File 'app/services/plum/field_type_registry.rb', line 44 def find(handle) all.find { |definition| definition.handle == handle.to_s } end |
.handles ⇒ Object
40 41 42 |
# File 'app/services/plum/field_type_registry.rb', line 40 def handles all.map(&:handle) end |
.include?(handle) ⇒ Boolean
48 49 50 |
# File 'app/services/plum/field_type_registry.rb', line 48 def include?(handle) find(handle).present? end |
.options ⇒ Object
52 53 54 |
# File 'app/services/plum/field_type_registry.rb', line 52 def all.map { |definition| [ definition.label, definition.handle ] } end |
.register(handle:, label:, configuration: [], partial: nil, normalizer: nil, validator: nil, expander: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/services/plum/field_type_registry.rb', line 60 def register(handle:, label:, configuration: [], partial: nil, normalizer: nil, validator: nil, expander: nil) normalized_handle = handle.to_s raise ArgumentError, "Field type handle is invalid" unless normalized_handle.match?(/\A[a-z][a-z0-9_]*\z/) raise ArgumentError, "Field type #{normalized_handle} is already registered" if include?(normalized_handle) raise ArgumentError, "Custom field types require an editor partial" if partial.blank? custom_definitions[normalized_handle] = Definition.new( handle: normalized_handle, label: label.to_s.presence || normalized_handle.titleize, configuration: Array(configuration).map(&:to_s), partial: partial.to_s, normalizer: normalizer, validator: validator, expander: ).freeze end |
.reset_custom! ⇒ Object
77 78 79 |
# File 'app/services/plum/field_type_registry.rb', line 77 def reset_custom! @custom_definitions = {} end |