Module: RubyUiScaffold::AttributeHelpers

Included in:
Generators::ScaffoldControllerGenerator, Generators::ScaffoldGenerator
Defined in:
lib/ruby_ui_scaffold/attribute_helpers.rb

Overview

Shared helpers used by both the scaffold_controller and views generators to identify which attributes participate in sort and search.

Constant Summary collapse

EXCLUDED_FROM_SORT =
%i[text rich_text json jsonb binary attachment attachments].freeze

Instance Method Summary collapse

Instance Method Details

#reference_associationsObject

Non-polymorphic belongs_to attributes — used to:

1. Eager-load via `scope.includes(*reference_associations)` in the controller
2. Display friendly labels (assoc.name vs assoc_id) in index/show


31
32
33
34
35
36
# File 'lib/ruby_ui_scaffold/attribute_helpers.rb', line 31

def reference_associations
  attributes.select do |a|
    a.respond_to?(:reference?) && a.reference? &&
      !(a.respond_to?(:polymorphic?) && a.polymorphic?)
  end.map { |a| a.name.to_sym }
end

#searchable_columnsObject

Only string columns are searchable via a LIKE clause. Excludes password_digest. Boolean/integer/date columns aren’t useful with LIKE.



21
22
23
24
25
26
# File 'lib/ruby_ui_scaffold/attribute_helpers.rb', line 21

def searchable_columns
  attributes.select { |a|
    a.type == :string &&
      !(a.respond_to?(:password_digest?) && a.password_digest?)
  }.map(&:column_name)
end

#sortable_columnsObject

Columns we’ll allowlist for sorting. Excludes large/blob types and password digests, since sorting on them makes no sense.



11
12
13
14
15
16
17
# File 'lib/ruby_ui_scaffold/attribute_helpers.rb', line 11

def sortable_columns
  attributes.reject { |a|
    EXCLUDED_FROM_SORT.include?(a.type) ||
      (a.respond_to?(:password_digest?) && a.password_digest?) ||
      (a.respond_to?(:attachment?) && (a.attachment? || a.attachments?))
  }.map(&:column_name)
end