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
| Title | Price | Price (USD) | Shelf | Weight | Signed | Acquired | ||
|---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|||||
| The Hidden Forest 1 | 28.24 € | 31.064 $ | A1 | 191.0 g | ✗ | 2022-07-15 | ||
| The Endless Mirror 2 | 28.63 € | 31.493 $ | B1 | 462.0 g | ✗ | 2022-12-26 | ||
| The Dispossessed City 4 | 19.95 € | 21.945 $ | B2 | 248.0 g | ✗ | 2024-06-15 | ||
| The Quiet Mirror 5 | 32.73 € | 36.003 $ | B1 | 212.0 g | ✗ | 2026-10-01 | ||
| The Burning Winter 6 | 21.78 € | 23.958 $ | A1 | 198.0 g | ✗ | 2026-05-07 | ||
| The Dispossessed City 7 | 17.82 € | 19.602 $ | B1 | 629.0 g | ✗ | 2025-05-10 | ||
| The Endless Station 11 | 10.86 € | 11.946 $ | C2 | 656.0 g | ✗ | 2025-10-12 | ||
| The Endless Station 13 | 27.05 € | 29.755 $ | A1 | 748.0 g | ✗ | 2026-04-16 | ||
| The Hidden Mirror 14 | 22.58 € | 24.838 $ | C2 | 848.0 g | ✗ | 2026-11-03 | ||
| The Endless Forest 15 | 11.84 € | 13.024 $ | A2 | 697.0 g | ✗ | 2023-03-12 | ||
| The Endless Dune 17 | 18.69 € | 20.559 $ | A2 | 653.0 g | ✗ | 2025-09-21 | ||
| The Hidden Mirror 19 | 37.51 € | 41.261 $ | Reserve | 566.0 g | ✗ | 2026-08-14 | ||
| The Burning Tower 20 | 14.56 € | 16.016 $ | A1 | 387.0 g | ✗ | 2022-05-05 | ||
| The Endless City 21 | 36.46 € | 40.106 $ | A2 | 345.0 g | ✗ | 2026-06-19 | ||
| The Hidden Engine 23 | 40.55 € | 44.605 $ | C1 | 674.0 g | ✗ | 2026-10-31 | ||
| The Ancient Mirror 24 | 33.42 € | 36.762 $ | B1 | 732.0 g | ✗ | 2026-06-25 | ||
| The Endless Winter 25 | 41.02 € | 45.122 $ | B2 | 582.0 g | ✗ | 2022-10-05 | ||
| The Endless Winter 30 | 39.19 € | 43.109 $ | B1 | 687.0 g | ✗ | 2025-10-03 | ||
| The Distant Garden 34 | 25.55 € | 28.105 $ | C1 | 427.0 g | ✗ | 2024-07-05 | ||
| The Burning Door 36 | 31.42 € | 34.562 $ | A1 | 480.0 g | ✗ | 2024-01-24 | ||
| The Broken Mirror 37 | 7.28 € | 8.008 $ | A1 | 316.0 g | ✗ | 2026-08-29 | ||
| The Distant Forest 38 | 38.71 € | 42.581 $ | A1 | 829.0 g | ✗ | 2023-11-19 | ||
| The Silent Tower 39 | 25.60 € | 28.16 $ | C1 | 225.0 g | ✗ | 2024-05-06 | ||
| The Silent Archive 40 | 41.43 € | 45.573 $ | C2 | 355.0 g | ✗ | 2025-02-01 | ||
| The Ancient Garden 42 | 35.86 € | 39.446 $ | A2 | 598.0 g | ✗ | 2022-09-11 | ||
| The Hidden Station 44 | 12.11 € | 13.321 $ | Reserve | 450.0 g | ✗ | 2022-06-05 | ||
| The Distant Engine 46 | 21.25 € | 23.375 $ | B1 | 427.0 g | ✗ | 2021-12-07 | ||
| The Burning Station 50 | 14.43 € | 15.873 $ | Reserve | 659.0 g | ✗ | 2023-12-15 | ||
| The Distant Garden 52 | 36.80 € | 40.48 $ | C1 | 478.0 g | ✗ | 2024-09-09 | ||
| The Ancient Ocean 53 | 25.74 € | 28.314 $ | B1 | 240.0 g | ✗ | 2024-04-01 | ||
| The Endless Station 55 | 20.24 € | 22.264 $ | A2 | 785.0 g | ✗ | 2022-09-15 | ||
| The Ancient Forest 56 | 33.64 € | 37.004 $ | A2 | 517.0 g | ✗ | 2025-02-19 | ||
| The Broken Empire 58 | 34.83 € | 38.313 $ | C1 | 683.0 g | ✗ | 2025-10-29 | ||
| The Quiet Empire 59 | 39.87 € | 43.857 $ | A2 | 782.0 g | ✗ | 2021-12-16 | ||
| The Silent Station 60 | 34.96 € | 38.456 $ | B1 | 673.0 g | ✗ | 2024-11-29 | ||
| The Broken Station 61 | 18.89 € | 20.779 $ | A1 | 688.0 g | ✗ | 2022-05-22 | ||
| The Hidden Ocean 65 | 43.18 € | 47.498 $ | A2 | 624.0 g | ✗ | 2025-11-22 | ||
| The Dispossessed City 66 | 29.12 € | 32.032 $ | B1 | 367.0 g | ✗ | 2025-01-09 | ||
| The Ancient Dune 67 | 38.41 € | 42.251 $ | B2 | 805.0 g | ✗ | 2022-05-22 | ||
| The Luminous Forest 68 | 40.60 € | 44.66 $ | C1 | 514.0 g | ✗ | 2022-08-19 | ||
| The Endless City 69 | 40.98 € | 45.078 $ | A2 | 242.0 g | ✗ | 2025-06-26 | ||
| The Ancient Engine 71 | 32.68 € | 35.948 $ | B2 | 393.0 g | ✗ | 2026-07-08 | ||
| The Distant Dune 72 | 32.20 € | 35.42 $ | C1 | 267.0 g | ✗ | 2024-03-07 | ||
| The Luminous Tower 77 | 28.81 € | 31.691 $ | B2 | 200.0 g | ✗ | 2025-08-22 | ||
| The Dispossessed Archive 82 | 35.72 € | 39.292 $ | C1 | 498.0 g | ✗ | 2026-09-29 | ||
| The Luminous Mirror 84 | 19.40 € | 21.34 $ | Reserve | 568.0 g | ✗ | 2025-03-08 | ||
| The Luminous City 85 | 22.63 € | 24.893 $ | A2 | 356.0 g | ✗ | 2026-01-10 | ||
| The Quiet Door 87 | 35.06 € | 38.566 $ | C1 | 316.0 g | ✗ | 2024-02-13 | ||
| The Quiet Tower 90 | 31.01 € | 34.111 $ | Reserve | 644.0 g | ✗ | 2025-08-06 | ||
| The Endless Engine 97 | 43.50 € | 47.85 $ | B2 | 197.0 g | ✗ | 2025-01-27 | ||
| The Endless Mirror 98 | 33.32 € | 36.652 $ | C2 | 468.0 g | ✗ | 2024-11-29 | ||
| The Hidden Garden 99 | 24.11 € | 26.521 $ | C2 | 414.0 g | ✗ | 2024-04-26 | ||
| The Burning Archive 100 | 24.48 € | 26.928 $ | B1 | 813.0 g | ✗ | 2024-02-04 | ||
| The Broken City 102 | 40.54 € | 44.594 $ | B2 | 190.0 g | ✗ | 2022-03-23 | ||
| The Dispossessed Door 103 | 30.10 € | 33.11 $ | Reserve | 738.0 g | ✗ | 2023-09-26 | ||
| The Endless Ocean 105 | 34.90 € | 38.39 $ | B2 | 843.0 g | ✗ | 2025-11-22 | ||
| The Luminous Ocean 108 | 39.89 € | 43.879 $ | A2 | 354.0 g | ✗ | 2025-08-28 | ||
| The Burning Station 110 | 24.38 € | 26.818 $ | C2 | 272.0 g | ✗ | 2026-03-26 | ||
| The Dispossessed Signal 112 | 38.73 € | 42.603 $ | C1 | 474.0 g | ✗ | 2024-07-17 | ||
| The Ancient Archive 119 | 21.49 € | 23.639 $ | A2 | 559.0 g | ✗ | 2026-10-26 | ||