Class: RivetCms::Field

Inherits:
ApplicationRecord show all
Includes:
OrganizationScoped, SoftDeletable
Defined in:
app/models/rivet_cms/field.rb

Constant Summary collapse

FIELD_TYPE_LABELS =

Human-readable field type labels for UI

{
  "string" => "Short text",
  "text" => "Long text",
  "rich_text" => "Rich text",
  "markdown" => "Markdown",
  "integer" => "Number",
  "decimal" => "Decimal",
  "enumeration" => "Select",
  "boolean" => "True/False",
  "image" => "Image",
  "video" => "Video",
  "file" => "File",
  "reference" => "Reference",
  "component" => "Component",
  "date" => "Date",
  "datetime" => "Date & Time"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SoftDeletable

#discard, #discard!, #discarded?, #kept?, #undiscard, #undiscard!

Class Method Details

.field_types_for_selectObject



74
75
76
# File 'app/models/rivet_cms/field.rb', line 74

def self.field_types_for_select
  FIELD_TYPE_LABELS.map { |value, label| [ label, value ] }
end

.reorder!(ordered_ids) ⇒ Object



82
83
84
85
86
87
88
# File 'app/models/rivet_cms/field.rb', line 82

def self.reorder!(ordered_ids)
  transaction do
    ordered_ids.each_with_index do |id, index|
      where(id: id).update_all(position: index)
    end
  end
end

.update_layout!(rows_config) ⇒ Object

Update layout with row structure rows_config: array of arrays, each inner array contains numeric field IDs for that row, e.g. [[1], [2, 3], [4]]. Prefixed ids do not resolve through where(id:) and would silently match nothing.



94
95
96
97
98
99
100
101
102
# File 'app/models/rivet_cms/field.rb', line 94

def self.update_layout!(rows_config)
  transaction do
    rows_config.each_with_index do |field_ids, row_index|
      field_ids.each_with_index do |field_id, position_in_row|
        where(id: field_id).update_all(row: row_index, position: position_in_row)
      end
    end
  end
end

Instance Method Details

#attachment?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/rivet_cms/field.rb', line 78

def attachment?
  image? || video? || file?
end

#field_type_labelObject



70
71
72
# File 'app/models/rivet_cms/field.rb', line 70

def field_type_label
  FIELD_TYPE_LABELS[field_type] || field_type.humanize
end

#move_to_own_row!Object

Move this field to its own new row



123
124
125
126
# File 'app/models/rivet_cms/field.rb', line 123

def move_to_own_row!
  max_row = sibling_fields.maximum(:row) || 0
  update!(row: max_row + 1, position: 0)
end

#pair_with!(other_field) ⇒ Object

Pair this field with another field on the same row



129
130
131
132
133
134
# File 'app/models/rivet_cms/field.rb', line 129

def pair_with!(other_field)
  return unless width_half? && other_field.width_half?
  return if paired? || other_field.paired?

  other_field.update!(row: row, position: 1)
end

#paired?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/rivet_cms/field.rb', line 111

def paired?
  paired_field.present?
end

#paired_fieldObject

Get the other field paired in the same row (if any)



105
106
107
108
109
# File 'app/models/rivet_cms/field.rb', line 105

def paired_field
  return nil unless width_half?

  sibling_fields.where(row: row, width: "half").where.not(id: id).first
end

#unpair!Object

Move this field to its own new row



116
117
118
119
120
# File 'app/models/rivet_cms/field.rb', line 116

def unpair!
  return unless paired?

  move_to_own_row!
end