Module: OpenLoam::CustomFields
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/open_loam/custom_fields.rb
Overview
Runtime, migration-free fields — stored in the custom_fields json
column every generated entity table carries. Included automatically by
rails g open_loam:entity. Shape is declared at runtime via a
OpenLoam::FieldDefinition (typically through the admin "Field definitions"
screen), not in code — that's what makes it migration-free.
equipment.custom_field(:serial_number) # => "SN-123" (cast)
equipment.set_custom_field(:serial_number, "SN-123")
Reading/writing a name with no matching OpenLoam::FieldDefinition for this tenant + entity raises OpenLoam::UnknownCustomFieldError immediately — the same "fail loudly at the access site" philosophy as OpenLoam.tenant!.
Instance Method Summary collapse
- #custom_field(name) ⇒ Object
-
#custom_field_label(name) ⇒ Object
The display label for a custom field.
- #set_custom_field(name, value) ⇒ Object
Instance Method Details
#custom_field(name) ⇒ Object
36 37 38 39 |
# File 'lib/open_loam/custom_fields.rb', line 36 def custom_field(name) definition = custom_field_definition!(name) cast_custom_field(definition.field_type, custom_fields[definition.name]) end |
#custom_field_label(name) ⇒ Object
The display label for a custom field. For a dictionary-typed field this is the matching entry's label; for every other type it is just the value. A dictionary value is stored as its plain code, so read/write is unchanged — only the human-facing rendering differs.
51 52 53 54 55 56 57 |
# File 'lib/open_loam/custom_fields.rb', line 51 def custom_field_label(name) definition = custom_field_definition!(name) value = custom_field(name) return value unless definition.field_type == "dictionary" OpenLoam::Dictionaries.label_for(definition.dictionary_key, value) end |
#set_custom_field(name, value) ⇒ Object
41 42 43 44 45 |
# File 'lib/open_loam/custom_fields.rb', line 41 def set_custom_field(name, value) definition = custom_field_definition!(name) custom_fields[definition.name] = serialize_custom_field(definition.field_type, value) value end |