Module: ElaineCrud::BaseHelper

Defined in:
app/helpers/elaine_crud/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_column_value(record, column) ⇒ String

Display the value of a column for a record (legacy method for backward compatibility) This method can be overridden in the host application to customize display

Parameters:

  • record (ActiveRecord::Base)

    The record to display

  • column (String)

    The column name to display

Returns:

  • (String)

    The formatted value



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/elaine_crud/base_helper.rb', line 11

def display_column_value(record, column)
  # Handle virtual fields that don't exist on the model
  unless record.respond_to?(column)
    return (:span, 'Virtual field', class: 'text-gray-400')
  end
  
  value = record.public_send(column)
  
  case value
  when nil
    (:span, '', class: 'text-gray-400')
  when true
    (:span, '', class: 'text-green-600 font-bold')
  when false
    (:span, '', class: 'text-red-600 font-bold')
  when Date, DateTime, Time
    value.strftime('%m/%d/%Y')
  else
    truncate(value.to_s, length: 50)
  end
end

#display_field_value(record, field_name, context: :index) ⇒ String

Display field value using new field configuration system

Parameters:

  • record (ActiveRecord::Base)

    The record to display

  • field_name (Symbol)

    The field name

  • context (Symbol) (defaults to: :index)

    The display context (:index or :show)

Returns:

  • (String)

    The formatted value



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/elaine_crud/base_helper.rb', line 38

def display_field_value(record, field_name, context: :index)
  config = controller.field_config_for(field_name)

  # Handle has_many relationships
  if config&.has_has_many? || is_has_many_relationship?(record, field_name)
    return display_has_many_value(record, field_name, config)
  end

  # Handle has_one relationships
  if config&.has_has_one? || is_has_one_relationship?(record, field_name)
    return display_has_one_value(record, field_name, config)
  end

  # If field has custom display configuration, use it first (allows overriding defaults)
  if config&.has_custom_display?
    return config.render_display_value(record, controller)
  # Handle has_and_belongs_to_many relationships with default display
  elsif config&.has_habtm? || is_habtm_relationship?(record, field_name)
    return display_habtm_field(record, field_name, config, context: context)
  elsif config&.has_foreign_key?
    # TODO: Implement foreign key display logic
    # Should load the related record and format it according to foreign_key config
    display_foreign_key_value(record, field_name, config)
  else
    # Fall back to default display logic
    display_column_value(record, field_name.to_s)
  end
end