Class: Kabk::Field
- Inherits:
-
Object
- Object
- Kabk::Field
- Defined in:
- lib/kabk/field.rb
Overview
Represents field/column metadata
Instance Attribute Summary collapse
-
#accordion ⇒ Boolean
If true, wraps this field in an accordion section.
-
#calendar ⇒ Symbol
The calendar system to use if type is date/datetime (e.g., :jalali, :gregorian).
-
#col_width ⇒ Integer
Bootstrap grid column width (1 to 12).
-
#default_value ⇒ Object
The default value for the field.
-
#depends_on ⇒ Hash
Configures field dependency visibility (e.g., { field: "published", value: true }).
-
#display_as ⇒ Symbol
UI hint for how to display the value (e.g., :badge, :thumbnail, :boolean_icon).
-
#form_type ⇒ Symbol
The frontend input component type (e.g., :text, :switch, :date, :image_single).
-
#hidden_in_form ⇒ Boolean
If true, hides the field in forms.
-
#hidden_in_table ⇒ Boolean
If true, hides the field in data tables.
-
#label ⇒ String
The human-readable label for the field.
-
#name ⇒ String
The technical name of the field (e.g., "created_at").
-
#nullable ⇒ Boolean
Indicates if this field can be null.
-
#options ⇒ Array, Hash
Hardcoded select options if applicable.
-
#order ⇒ Integer
The explicit ordering weight for rendering.
-
#primary_key ⇒ Boolean
Indicates if this field is the primary key.
-
#readonly ⇒ Boolean
Indicates if this field is read-only.
-
#relation ⇒ Hash
Relational metadata (e.g., { resource: "user", cardinality: "many_to_one", ... }).
-
#required ⇒ Boolean
Indicates if this field is mandatory in forms.
-
#rows ⇒ Integer
For textarea form_type, specifies the number of rows.
-
#type ⇒ Symbol
The underlying data type (:string, :number, :boolean, :date, :datetime, :file, :relation).
-
#upload_config ⇒ Hash
Configuration for file uploads.
-
#validation ⇒ Hash
Server-side validation rules (e.g., { min_length: 5, unique: true }).
Instance Method Summary collapse
-
#initialize(name:, **attributes) ⇒ Field
constructor
A new instance of Field.
- #to_h ⇒ Object
Constructor Details
#initialize(name:, **attributes) ⇒ Field
Returns a new instance of Field.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kabk/field.rb', line 51 def initialize(name:, **attributes) @name = name.to_s @primary_key = false @nullable = false @col_width = 12 @required = false @readonly = false @hidden_in_table = false @hidden_in_form = false @accordion = false attributes.each do |k, v| send("#{k}=", v) if respond_to?("#{k}=") end end |
Instance Attribute Details
#accordion ⇒ Boolean
Returns If true, wraps this field in an accordion section.
35 36 37 |
# File 'lib/kabk/field.rb', line 35 def accordion @accordion end |
#calendar ⇒ Symbol
Returns The calendar system to use if type is date/datetime (e.g., :jalali, :gregorian).
15 16 17 |
# File 'lib/kabk/field.rb', line 15 def calendar @calendar end |
#col_width ⇒ Integer
Returns Bootstrap grid column width (1 to 12).
23 24 25 |
# File 'lib/kabk/field.rb', line 23 def col_width @col_width end |
#default_value ⇒ Object
Returns The default value for the field.
41 42 43 |
# File 'lib/kabk/field.rb', line 41 def default_value @default_value end |
#depends_on ⇒ Hash
Returns Configures field dependency visibility (e.g., { field: "published", value: true }).
43 44 45 |
# File 'lib/kabk/field.rb', line 43 def depends_on @depends_on end |
#display_as ⇒ Symbol
Returns UI hint for how to display the value (e.g., :badge, :thumbnail, :boolean_icon).
21 22 23 |
# File 'lib/kabk/field.rb', line 21 def display_as @display_as end |
#form_type ⇒ Symbol
Returns The frontend input component type (e.g., :text, :switch, :date, :image_single).
13 14 15 |
# File 'lib/kabk/field.rb', line 13 def form_type @form_type end |
#hidden_in_form ⇒ Boolean
Returns If true, hides the field in forms.
33 34 35 |
# File 'lib/kabk/field.rb', line 33 def hidden_in_form @hidden_in_form end |
#hidden_in_table ⇒ Boolean
Returns If true, hides the field in data tables.
31 32 33 |
# File 'lib/kabk/field.rb', line 31 def hidden_in_table @hidden_in_table end |
#label ⇒ String
Returns The human-readable label for the field.
9 10 11 |
# File 'lib/kabk/field.rb', line 9 def label @label end |
#name ⇒ String
Returns The technical name of the field (e.g., "created_at").
7 8 9 |
# File 'lib/kabk/field.rb', line 7 def name @name end |
#nullable ⇒ Boolean
Returns Indicates if this field can be null.
19 20 21 |
# File 'lib/kabk/field.rb', line 19 def nullable @nullable end |
#options ⇒ Array, Hash
Returns Hardcoded select options if applicable.
39 40 41 |
# File 'lib/kabk/field.rb', line 39 def @options end |
#order ⇒ Integer
Returns The explicit ordering weight for rendering.
25 26 27 |
# File 'lib/kabk/field.rb', line 25 def order @order end |
#primary_key ⇒ Boolean
Returns Indicates if this field is the primary key.
17 18 19 |
# File 'lib/kabk/field.rb', line 17 def primary_key @primary_key end |
#readonly ⇒ Boolean
Returns Indicates if this field is read-only.
29 30 31 |
# File 'lib/kabk/field.rb', line 29 def readonly @readonly end |
#relation ⇒ Hash
Returns Relational metadata (e.g., { resource: "user", cardinality: "many_to_one", ... }).
47 48 49 |
# File 'lib/kabk/field.rb', line 47 def relation @relation end |
#required ⇒ Boolean
Returns Indicates if this field is mandatory in forms.
27 28 29 |
# File 'lib/kabk/field.rb', line 27 def required @required end |
#rows ⇒ Integer
Returns For textarea form_type, specifies the number of rows.
37 38 39 |
# File 'lib/kabk/field.rb', line 37 def rows @rows end |
#type ⇒ Symbol
Returns The underlying data type (:string, :number, :boolean, :date, :datetime, :file, :relation).
11 12 13 |
# File 'lib/kabk/field.rb', line 11 def type @type end |
#upload_config ⇒ Hash
Returns Configuration for file uploads.
49 50 51 |
# File 'lib/kabk/field.rb', line 49 def upload_config @upload_config end |
#validation ⇒ Hash
Returns Server-side validation rules (e.g., { min_length: 5, unique: true }).
45 46 47 |
# File 'lib/kabk/field.rb', line 45 def validation @validation end |
Instance Method Details
#to_h ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kabk/field.rb', line 67 def to_h hash = { name: @name, label: @label, type: @type, form_type: @form_type } hash[:calendar] = @calendar if @calendar hash[:primary_key] = @primary_key if @primary_key hash[:nullable] = @nullable unless @nullable.nil? hash[:display_as] = @display_as if @display_as hash[:col_width] = @col_width if @col_width hash[:order] = @order if @order hash[:required] = @required if @required hash[:readonly] = @readonly if @readonly hash[:hidden_in_table] = @hidden_in_table if @hidden_in_table hash[:hidden_in_form] = @hidden_in_form if @hidden_in_form hash[:accordion] = @accordion if @accordion hash[:rows] = @rows if @rows hash[:options] = @options if @options hash[:default_value] = @default_value unless @default_value.nil? hash[:depends_on] = @depends_on if @depends_on hash[:validation] = @validation if @validation hash[:relation] = @relation if @relation hash[:upload_config] = @upload_config if @upload_config hash end |