Class: Kabk::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/kabk/field.rb

Overview

Represents field/column metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#accordionBoolean

Returns If true, wraps this field in an accordion section.

Returns:

  • (Boolean)

    If true, wraps this field in an accordion section.



35
36
37
# File 'lib/kabk/field.rb', line 35

def accordion
  @accordion
end

#calendarSymbol

Returns The calendar system to use if type is date/datetime (e.g., :jalali, :gregorian).

Returns:

  • (Symbol)

    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_widthInteger

Returns Bootstrap grid column width (1 to 12).

Returns:

  • (Integer)

    Bootstrap grid column width (1 to 12).



23
24
25
# File 'lib/kabk/field.rb', line 23

def col_width
  @col_width
end

#default_valueObject

Returns The default value for the field.

Returns:

  • (Object)

    The default value for the field.



41
42
43
# File 'lib/kabk/field.rb', line 41

def default_value
  @default_value
end

#depends_onHash

Returns Configures field dependency visibility (e.g., { field: "published", value: true }).

Returns:

  • (Hash)

    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_asSymbol

Returns UI hint for how to display the value (e.g., :badge, :thumbnail, :boolean_icon).

Returns:

  • (Symbol)

    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_typeSymbol

Returns The frontend input component type (e.g., :text, :switch, :date, :image_single).

Returns:

  • (Symbol)

    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_formBoolean

Returns If true, hides the field in forms.

Returns:

  • (Boolean)

    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_tableBoolean

Returns If true, hides the field in data tables.

Returns:

  • (Boolean)

    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

#labelString

Returns The human-readable label for the field.

Returns:

  • (String)

    The human-readable label for the field.



9
10
11
# File 'lib/kabk/field.rb', line 9

def label
  @label
end

#nameString

Returns The technical name of the field (e.g., "created_at").

Returns:

  • (String)

    The technical name of the field (e.g., "created_at").



7
8
9
# File 'lib/kabk/field.rb', line 7

def name
  @name
end

#nullableBoolean

Returns Indicates if this field can be null.

Returns:

  • (Boolean)

    Indicates if this field can be null.



19
20
21
# File 'lib/kabk/field.rb', line 19

def nullable
  @nullable
end

#optionsArray, Hash

Returns Hardcoded select options if applicable.

Returns:

  • (Array, Hash)

    Hardcoded select options if applicable.



39
40
41
# File 'lib/kabk/field.rb', line 39

def options
  @options
end

#orderInteger

Returns The explicit ordering weight for rendering.

Returns:

  • (Integer)

    The explicit ordering weight for rendering.



25
26
27
# File 'lib/kabk/field.rb', line 25

def order
  @order
end

#primary_keyBoolean

Returns Indicates if this field is the primary key.

Returns:

  • (Boolean)

    Indicates if this field is the primary key.



17
18
19
# File 'lib/kabk/field.rb', line 17

def primary_key
  @primary_key
end

#readonlyBoolean

Returns Indicates if this field is read-only.

Returns:

  • (Boolean)

    Indicates if this field is read-only.



29
30
31
# File 'lib/kabk/field.rb', line 29

def readonly
  @readonly
end

#relationHash

Returns Relational metadata (e.g., { resource: "user", cardinality: "many_to_one", ... }).

Returns:

  • (Hash)

    Relational metadata (e.g., { resource: "user", cardinality: "many_to_one", ... }).



47
48
49
# File 'lib/kabk/field.rb', line 47

def relation
  @relation
end

#requiredBoolean

Returns Indicates if this field is mandatory in forms.

Returns:

  • (Boolean)

    Indicates if this field is mandatory in forms.



27
28
29
# File 'lib/kabk/field.rb', line 27

def required
  @required
end

#rowsInteger

Returns For textarea form_type, specifies the number of rows.

Returns:

  • (Integer)

    For textarea form_type, specifies the number of rows.



37
38
39
# File 'lib/kabk/field.rb', line 37

def rows
  @rows
end

#typeSymbol

Returns The underlying data type (:string, :number, :boolean, :date, :datetime, :file, :relation).

Returns:

  • (Symbol)

    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_configHash

Returns Configuration for file uploads.

Returns:

  • (Hash)

    Configuration for file uploads.



49
50
51
# File 'lib/kabk/field.rb', line 49

def upload_config
  @upload_config
end

#validationHash

Returns Server-side validation rules (e.g., { min_length: 5, unique: true }).

Returns:

  • (Hash)

    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_hObject



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