Module: ElaineCrud::FieldConfigurationMethods

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

Overview

Instance methods for accessing and working with field configurations Provides runtime access to field metadata and rendering

Instance Method Summary collapse

Instance Method Details

#apply_field_defaults(record) ⇒ Object

Apply default values to a new record based on field configurations

Parameters:

  • record (ActiveRecord::Base)

    The record to apply defaults to



125
126
127
128
129
130
131
# File 'lib/elaine_crud/field_configuration_methods.rb', line 125

def apply_field_defaults(record)
  # TODO: Implement default value application
  # Should iterate through field configurations and apply default_value if present
  # Should handle both static values and proc/lambda callbacks
  # Should only apply to new records (record.new_record?)
  return record # Placeholder
end

#configured_fieldsArray<Symbol>

Get all configured field names

Returns:

  • (Array<Symbol>)

    List of configured field names



18
19
20
# File 'lib/elaine_crud/field_configuration_methods.rb', line 18

def configured_fields
  field_configurations&.keys || []
end

#debug_configurationObject

Debug method to help troubleshoot configuration issues Call this in your controller in development to see the configuration



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/elaine_crud/field_configuration_methods.rb', line 135

def debug_configuration
  return unless Rails.env.development?

  puts "\n=== ElaineCrud Configuration Debug ==="
  puts "Model: #{crud_model&.name || 'NOT SET'}"
  puts "Permitted Attributes: #{permitted_attributes&.inspect || 'NOT SET'}"
  puts "Field Configurations: #{field_configurations&.keys&.inspect || 'NONE'}"

  if crud_model
    reflections = crud_model.reflections.select { |_, r| r.is_a?(ActiveRecord::Reflection::BelongsToReflection) }
    puts "Belongs_to relationships: #{reflections.keys.inspect}"
    puts "Foreign keys: #{reflections.values.map(&:foreign_key).inspect}"
  end

  puts "====================================\n"
end

#determine_display_field_for_model(model_class) ⇒ Symbol

Instance method version of determine_display_field_for_model

Parameters:

  • model_class (Class)

    The ActiveRecord model class

Returns:

  • (Symbol)

    The field to use for display



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/elaine_crud/field_configuration_methods.rb', line 100

def determine_display_field_for_model(model_class)
  # Common field names for display, in order of preference
  display_candidates = [:name, :title, :display_name, :full_name, :label, :description]

  # Check which columns exist
  column_names = model_class.column_names.map(&:to_sym)

  # Return the first matching candidate
  display_field = display_candidates.find { |candidate| column_names.include?(candidate) }

  # If none found, use the first string/text column that's not id, created_at, updated_at
  unless display_field
    string_columns = model_class.columns.select do |col|
      [:string, :text].include?(col.type) &&
      !%w[id created_at updated_at].include?(col.name)
    end
    display_field = string_columns.first&.name&.to_sym
  end

  # Final fallback to :id
  display_field || :id
end

#field_config_for(field_name) ⇒ FieldConfiguration?

Get configuration for a specific field

Parameters:

  • field_name (Symbol)

    The field name

Returns:



12
13
14
# File 'lib/elaine_crud/field_configuration_methods.rb', line 12

def field_config_for(field_name)
  field_configurations&.dig(field_name.to_sym)
end

#field_configured?(field_name) ⇒ Boolean

Check if a field has custom configuration

Parameters:

  • field_name (Symbol)

    The field name

Returns:

  • (Boolean)

    True if field has custom configuration



25
26
27
# File 'lib/elaine_crud/field_configuration_methods.rb', line 25

def field_configured?(field_name)
  field_config_for(field_name).present?
end

#field_description(field_name) ⇒ String?

Get description for a field

Parameters:

  • field_name (Symbol)

    The field name

Returns:

  • (String, nil)

    The field description if configured



40
41
42
# File 'lib/elaine_crud/field_configuration_methods.rb', line 40

def field_description(field_name)
  field_config_for(field_name)&.description
end

#field_readonly?(field_name) ⇒ Boolean

Check if a field is readonly

Parameters:

  • field_name (Symbol)

    The field name

Returns:

  • (Boolean)

    True if field is readonly



47
48
49
# File 'lib/elaine_crud/field_configuration_methods.rb', line 47

def field_readonly?(field_name)
  field_config_for(field_name)&.readonly || false
end

#field_title(field_name) ⇒ String

Get display title for a field

Parameters:

  • field_name (Symbol)

    The field name

Returns:

  • (String)

    The display title (configured or humanized)



32
33
34
35
# File 'lib/elaine_crud/field_configuration_methods.rb', line 32

def field_title(field_name)
  config = field_config_for(field_name)
  config&.title || field_name.to_s.humanize
end

#readonly_field_display(record, field_name) ⇒ String

Render readonly field display (for readonly fields in edit forms)

Parameters:

  • record (ActiveRecord::Base)

    The record

  • field_name (Symbol)

    The field name

Returns:

  • (String)

    HTML safe string for readonly display



90
91
92
93
94
95
# File 'lib/elaine_crud/field_configuration_methods.rb', line 90

def readonly_field_display(record, field_name)
  # TODO: Implement readonly field display
  # Should render the display value but in an edit form context
  # Maybe with a different styling to indicate it's not editable
  render_field_display(record, field_name) # Placeholder
end

#render_field_display(record, field_name) ⇒ String

Render display value for a field

Parameters:

  • record (ActiveRecord::Base)

    The record

  • field_name (Symbol)

    The field name

Returns:

  • (String)

    HTML safe string for display



61
62
63
64
# File 'lib/elaine_crud/field_configuration_methods.rb', line 61

def render_field_display(record, field_name)
  # Delegate to helper which has access to view context
  helpers.display_field_value(record, field_name)
end

#render_field_edit(record, field_name, form_builder = nil) ⇒ String

Render edit field for a form

Parameters:

  • record (ActiveRecord::Base)

    The record

  • field_name (Symbol)

    The field name

  • form_builder (ActionView::Helpers::FormBuilder, nil) (defaults to: nil)

    Optional form builder

Returns:

  • (String)

    HTML safe string for form field



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/elaine_crud/field_configuration_methods.rb', line 71

def render_field_edit(record, field_name, form_builder = nil)
  config = field_config_for(field_name)

  return readonly_field_display(record, field_name) if field_readonly?(field_name)

  if config&.has_custom_edit?
    config.render_edit_field(record, self, form_builder)
  else
    # TODO: Implement fallback to default edit field logic
    # Should generate appropriate form fields based on field type
    # Should handle dropdowns for options, foreign key selects, etc.
    "<!-- TODO: Default edit field for #{field_name} -->" # Placeholder
  end
end

#turbo_disabled?Boolean

Check if Turbo frames are disabled for this controller

Returns:

  • (Boolean)

    True if Turbo frames are disabled



53
54
55
# File 'lib/elaine_crud/field_configuration_methods.rb', line 53

def turbo_disabled?
  disable_turbo_frames == true
end