Custom fields

How this page is built the gem renders it — this is all you write

User-defined properties that live outside the Book table (here in a property_values store) shown as extra columns. The model is untouched — the controller loads each PropertyDefinition and turns it into a CrudComponents::DynamicColumn, then passes the set as extra_columns: — to both crud_collection and the standalone crud_filter form. Each column carries a preload: (one query per page, no N+1), a typed filter: and a sort: — so Shelf (text), Weight (number range, in g), Signed (yes/no select) and Acquired (date range) filter with the control their type wants, not a plain text box. Try the inline filter row, the sort headers, and the sidebar form.

app/controllers/custom_fields_controller.rb
@books   = Book.all
@columns = []
@columns << CrudComponents::DynamicColumn.new(
  :price_usd,
  label: 'Price (USD)',
  as: :number,
  unit: '$',
  filter: ->(scope, geq:, leq:) { scope.where('price*1.1 >= ?', geq).where('price*1.1 <= ?', leq) },
  sort: ->(scope, dir) { scope.order(Arel.sql("(price*1.1) #{dir}")) }
) { |record| record.price * 1.1 }

@columns += PropertyDefinition.order(:id).map { |defn| defn.to_crud_column(Book) }
app/models/property_definition.rb
def to_crud_column(subject_model)
  definition = self
  CrudComponents::DynamicColumn.new(
    key.to_sym, label: label, as: renderer, unit: unit.presence,
    preload: ->(records) { definition.values_by_subject(subject_model, records) },  # one query/page
    filter:  definition.filter_for(subject_model),   # filters as the as: type (see below)
    sort:    ->(scope, dir) { definition.sort_scope(scope, subject_model, dir) }
  ) { |record, loaded| definition.cast(loaded[record.id]&.value) }                   # the value resolver
end

# Just a block — its control follows the column's as: flavor (number → range,
# date → range, boolean → yes/no, string → text):
def filter_for(subject_model)
  case flavor
  when 'number'  then ->(s, geq:, leq:) { ... }
  when 'boolean' then ->(s, eq:) { ... }
  when 'date'    then ->(s, geq:, leq:) { ... }
  else                ->(s, contains:) { ... }
  end
end
app/views/custom_fields/index.html.erb
<%= crud_collection @books, fieldset: :compact, extra_columns: @columns %>
<%= crud_filter   Book, fieldset: :compact, extra_columns: @columns %>   <%# same dynamic columns in a standalone form %>

Read more: Dynamic columns  ·  Views

Reset
Title Price Price (USD) Shelf Weight Signed Acquired
Reset
The Ancient Winter 3 24.81 € 27.291 $ C2 276.0 g ✓ 2025-04-30
The Quiet Forest 8 11.98 € 13.178 $ Reserve 631.0 g ✓ 2023-09-12
The Broken Engine 9 32.99 € 36.289 $ B2 873.0 g ✓ 2022-06-19
The Distant City 10 26.91 € 29.601 $ Reserve 705.0 g ✓ 2022-12-24
The Dispossessed Station 12 16.58 € 18.238 $ C1 517.0 g ✓ 2024-12-06
The Ancient Signal 16 35.51 € 39.061 $ B2 497.0 g ✓ 2021-06-10
The Quiet Door 18 21.79 € 23.969 $ A1 910.0 g ✓ 2023-07-11
The Hidden Archive 22 23.21 € 25.531 $ B1 618.0 g ✓ 2026-04-15
The Distant Door 26 16.40 € 18.04 $ Reserve 593.0 g ✓ 2024-09-09
The Quiet City 27 39.79 € 43.769 $ C2 313.0 g ✓ 2023-04-23
The Endless Winter 28 29.97 € 32.967 $ A2 252.0 g ✓ 2023-02-13
The Dispossessed Winter 29 9.04 € 9.944 $ C2 924.0 g ✓ 2026-08-12
The Hidden Engine 31 32.85 € 36.135 $ B2 244.0 g ✓ 2023-08-30
The Hidden Winter 32 40.79 € 44.869 $ A1 445.0 g ✓ 2023-11-13
The Endless Signal 33 39.74 € 43.714 $ B1 841.0 g ✓ 2025-05-09
The Endless Door 35 24.06 € 26.466 $ A2 593.0 g ✓ 2026-02-09
The Luminous Machine 41 23.63 € 25.993 $ A2 549.0 g ✓ 2026-09-27
The Ancient Garden 43 42.67 € 46.937 $ C1 306.0 g ✓ 2021-07-21
The Luminous City 45 19.30 € 21.23 $ B1 252.0 g ✓ 2021-07-23
The Hidden Tower 47 19.92 € 21.912 $ B1 556.0 g ✓ 2026-06-03
The Broken Empire 48 30.49 € 33.539 $ A2 391.0 g ✓ 2022-03-22
The Broken Dune 49 22.90 € 25.19 $ Reserve 548.0 g ✓ 2026-06-22
The Ancient Machine 51 40.77 € 44.847 $ Reserve 916.0 g ✓ 2023-01-30
The Dispossessed Empire 54 43.50 € 47.85 $ C2 493.0 g ✓ 2025-12-24
The Luminous City 57 23.17 € 25.487 $ B1 343.0 g ✓ 2026-01-05
The Quiet Dune 62 24.14 € 26.554 $ A1 474.0 g ✓ 2021-06-19
The Distant Tower 63 18.55 € 20.405 $ B2 873.0 g ✓ 2025-02-09
The Dispossessed Forest 64 36.11 € 39.721 $ A2 747.0 g ✓ 2024-07-20
The Silent Station 70 34.46 € 37.906 $ B1 877.0 g ✓ 2023-07-10
The Endless Signal 73 19.39 € 21.329 $ Reserve 308.0 g ✓ 2024-01-28
The Ancient Ocean 74 14.70 € 16.17 $ C2 933.0 g ✓ 2023-01-11
The Hidden Tower 75 5.34 € 5.874 $ C2 217.0 g ✓ 2024-02-16
The Luminous Engine 76 34.79 € 38.269 $ C1 824.0 g ✓ 2024-12-06
The Hidden Engine 78 8.00 € 8.8 $ B1 800.0 g ✓ 2022-06-14
The Hidden Dune 79 24.25 € 26.675 $ B1 248.0 g ✓ 2026-05-08
The Ancient Engine 80 17.29 € 19.019 $ B2 623.0 g ✓ 2026-01-12
The Burning Forest 81 6.28 € 6.908 $ A2 714.0 g ✓ 2026-02-16
The Distant Engine 83 39.52 € 43.472 $ A1 928.0 g ✓ 2021-11-08
The Distant Station 86 12.86 € 14.146 $ Reserve 276.0 g ✓ 2023-07-02
The Luminous Tower 88 37.25 € 40.975 $ C1 726.0 g ✓ 2025-01-27
The Dispossessed Empire 89 11.61 € 12.771 $ A1 809.0 g ✓ 2022-12-01
The Hidden Tower 91 29.28 € 32.208 $ B1 593.0 g ✓ 2024-06-08
The Hidden Winter 92 29.93 € 32.923 $ C2 636.0 g ✓ 2023-06-06
The Ancient Forest 93 41.21 € 45.331 $ C1 778.0 g ✓ 2025-01-17
The Luminous Station 94 26.90 € 29.59 $ C2 551.0 g ✓ 2022-05-17
The Quiet Station 95 6.29 € 6.919 $ Reserve 224.0 g ✓ 2023-05-29
The Distant Winter 96 10.36 € 11.396 $ C1 629.0 g ✓ 2025-02-12
The Dispossessed Ocean 101 12.58 € 13.838 $ B1 853.0 g ✓ 2023-06-11
The Broken Forest 104 17.49 € 19.239 $ B1 297.0 g ✓ 2023-01-14
The Hidden Door 106 19.30 € 21.23 $ C1 316.0 g ✓ 2023-04-17
The Hidden Door 107 17.84 € 19.624 $ C2 660.0 g ✓ 2026-05-09
The Hidden Garden 109 22.95 € 25.245 $ C1 533.0 g ✓ 2026-07-02
The Distant Archive 111 11.59 € 12.749 $ C1 739.0 g ✓ 2026-03-20
The Quiet Forest 113 33.56 € 36.916 $ A2 196.0 g ✓ 2023-05-07
The Ancient Archive 114 44.21 € 48.631 $ C1 484.0 g ✓ 2024-01-24
The Silent Engine 115 24.38 € 26.818 $ A2 282.0 g ✓ 2026-05-11
The Silent Archive 116 10.28 € 11.308 $ A1 753.0 g ✓ 2026-06-02
The Endless Empire 117 29.30 € 32.23 $ A1 430.0 g ✓ 2023-01-09
The Burning Empire 118 17.01 € 18.711 $ Reserve 720.0 g ✓ 2025-09-30
The Silent Station 120 42.11 € 46.321 $ C2 847.0 g ✓ 2025-02-26
Filter
Title
Price
Price (USD)
Shelf
Weight
Signed
Acquired
Reset