Module: ElaineCrud::LayoutCalculation

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
lib/elaine_crud/layout_calculation.rb

Overview

Methods for calculating and customizing layout and grid structure Handles column widths, row layouts, and field positioning

Instance Method Summary collapse

Instance Method Details

#calculate_layout(content, fields) ⇒ Array<Array<Hash>>

Calculate layout structure for a specific record

Parameters:

  • The record being displayed

  • Array of field names to include in layout

Returns:

  • Nested array where first dimension is rows, second is columns Each column hash can contain: field_name, colspan, rowspan, and future properties



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elaine_crud/layout_calculation.rb', line 14

def calculate_layout(content, fields)
  # Default implementation: single row with all fields, each taking 1 column and 1 row
  row = fields.map do |field_name|
    {
      field_name: field_name,
      colspan: 1,
      rowspan: 1
    }
  end

  [row] # Return single row
end

#calculate_layout_header(fields) ⇒ Array<Hash>

Calculate layout header structure defining column sizes and titles

Parameters:

  • Array of field names to include in layout

Returns:

  • Array of header config objects with width, field_name, and/or title Each object can contain:

    • width: CSS width (required, e.g., "minmax(100px, 1fr)" or "25%")
    • field_name: Symbol of field to display and enable sorting (optional)
    • title: Custom column title, overrides field title (optional)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elaine_crud/layout_calculation.rb', line 34

def calculate_layout_header(fields)
  # Default implementation: flexible columns that can expand to fit content
  # Using minmax() allows columns to grow beyond their base size when content requires it
  # Note: Create a new array to avoid mutating the input
  all_fields = fields + ["ROW-ACTIONS"]

  all_fields.map do |field_name|
    width = case field_name.to_s
      when 'id' then "max-content"
      when 'email' then "minmax(180px, 2fr)"
      when 'ROW-ACTIONS' then "max-content"
      else "minmax(100px, 1fr)"
    end

    {
      width: width,
      field_name: field_name
    }
  end
end