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 $
The Endless Mirror 2 28.63 € 31.493 $
The Silent Tower 3 43.93 € 48.323 $
The Broken Engine 4 12.63 € 13.893 $
The Endless Empire 5 25.88 € 28.468 $
The Broken Garden 6 19.85 € 21.835 $
The Burning Machine 7 10.40 € 11.44 $
The Burning Machine 8 38.02 € 41.822 $
The Hidden Dune 9 42.43 € 46.673 $
The Broken Dune 10 13.54 € 14.894 $
The Ancient Tower 11 40.82 € 44.902 $
The Quiet Station 12 11.40 € 12.54 $
The Hidden Engine 13 33.63 € 36.993 $
The Endless Machine 14 8.37 € 9.207 $
The Endless Garden 15 29.85 € 32.835 $
The Hidden Garden 16 30.44 € 33.484 $
The Ancient Ocean 17 34.01 € 37.411 $
The Endless Forest 18 11.84 € 13.024 $
The Ancient Ocean 19 43.54 € 47.894 $
The Endless Mirror 20 21.82 € 24.002 $
The Hidden Dune 21 16.41 € 18.051 $
The Hidden Door 22 30.21 € 33.231 $
The Burning Door 23 15.15 € 16.665 $
The Dispossessed Winter 24 19.40 € 21.34 $
The Luminous Ocean 25 37.67 € 41.437 $
The Endless Dune 26 26.43 € 29.073 $
The Burning City 27 29.92 € 32.912 $
The Distant Station 28 15.62 € 17.182 $
The Silent Door 29 38.24 € 42.064 $
The Endless Tower 30 38.98 € 42.878 $
The Quiet Machine 31 21.83 € 24.013 $
The Ancient Engine 32 33.11 € 36.421 $
The Endless Mirror 33 39.79 € 43.769 $
The Ancient Door 34 32.79 € 36.069 $
The Hidden Mirror 35 27.24 € 29.964 $
The Dispossessed Engine 36 23.93 € 26.323 $
The Endless Winter 37 39.19 € 43.109 $
The Hidden Machine 38 38.74 € 42.614 $
The Endless Door 39 18.58 € 20.438 $
The Hidden Forest 40 37.54 € 41.294 $
The Endless Empire 41 30.81 € 33.891 $
The Broken Machine 42 25.55 € 28.105 $
The Broken Empire 43 20.31 € 22.341 $
The Ancient Ocean 44 25.72 € 28.292 $
The Luminous City 45 40.14 € 44.154 $
The Broken Engine 46 5.74 € 6.314 $
The Endless Station 47 26.37 € 29.007 $
The Hidden Mirror 48 21.19 € 23.309 $
The Silent Machine 49 20.70 € 22.77 $
The Endless Signal 50 26.36 € 28.996 $
The Dispossessed Door 51 36.10 € 39.71 $
The Ancient City 52 17.77 € 19.547 $
The Broken Door 53 33.91 € 37.301 $
The Ancient Tower 54 27.65 € 30.415 $
The Broken Door 55 36.00 € 39.6 $
The Ancient Mirror 56 37.08 € 40.788 $
The Silent Signal 57 24.90 € 27.39 $
The Dispossessed Station 58 22.59 € 24.849 $
The Silent Forest 59 14.46 € 15.906 $
The Silent Mirror 60 25.07 € 27.577 $
The Broken Machine 61 30.16 € 33.176 $
The Silent Forest 62 35.70 € 39.27 $
The Broken Dune 63 6.27 € 6.897 $
The Luminous Ocean 64 33.85 € 37.235 $
The Luminous Garden 65 6.67 € 7.337 $
The Hidden Station 66 40.83 € 44.913 $
The Dispossessed Ocean 67 40.62 € 44.682 $
The Broken Ocean 68 6.07 € 6.677 $
The Broken Empire 69 29.21 € 32.131 $
The Luminous Mirror 70 8.90 € 9.79 $
The Silent Signal 71 35.42 € 38.962 $
The Ancient City 72 34.41 € 37.851 $
The Dispossessed Winter 73 29.24 € 32.164 $
The Dispossessed Engine 74 39.15 € 43.065 $
The Silent City 75 44.32 € 48.752 $
The Quiet Dune 76 24.14 € 26.554 $
The Distant Station 77 38.94 € 42.834 $
The Silent Tower 78 16.99 € 18.689 $
The Endless Ocean 79 24.96 € 27.456 $
The Distant Engine 80 43.18 € 47.498 $
The Quiet Forest 81 37.94 € 41.734 $
The Hidden Winter 82 12.30 € 13.53 $
The Burning Signal 83 42.17 € 46.387 $
The Endless Winter 84 34.31 € 37.741 $
The Hidden Signal 85 20.30 € 22.33 $
The Distant Dune 86 41.33 € 45.463 $
The Burning Station 87 15.13 € 16.643 $
The Ancient Signal 88 19.15 € 21.065 $
The Luminous Tower 89 22.68 € 24.948 $
The Dispossessed Tower 90 33.21 € 36.531 $
The Quiet Signal 91 9.09 € 9.999 $
The Endless Ocean 92 42.02 € 46.222 $
The Luminous Empire 93 14.02 € 15.422 $
The Distant Station 94 19.41 € 21.351 $
The Hidden Door 95 21.22 € 23.342 $
The Broken Mirror 96 34.38 € 37.818 $
The Luminous Empire 97 13.61 € 14.971 $
The Hidden Archive 98 27.03 € 29.733 $
The Endless Garden 99 17.29 € 19.019 $
The Broken Door 100 26.98 € 29.678 $
The Ancient Engine 101 37.89 € 41.679 $
The Dispossessed Dune 102 37.91 € 41.701 $
The Burning Garden 103 38.50 € 42.35 $
The Distant Dune 104 25.09 € 27.599 $
The Luminous City 105 22.63 € 24.893 $
The Distant Station 106 12.86 € 14.146 $
The Silent Dune 107 17.86 € 19.646 $
The Broken Winter 108 32.73 € 36.003 $
The Hidden Station 109 37.73 € 41.503 $
The Hidden Dune 110 40.26 € 44.286 $
The Ancient Winter 111 11.99 € 13.189 $
The Endless City 112 23.04 € 25.344 $
The Luminous Empire 113 31.61 € 34.771 $
The Dispossessed Archive 114 8.47 € 9.317 $
The Quiet Dune 115 42.25 € 46.475 $
The Endless Station 116 7.61 € 8.371 $
The Endless Machine 117 14.47 € 15.917 $
The Distant Winter 118 16.33 € 17.963 $
The Distant Door 119 29.94 € 32.934 $
The Silent Winter 120 16.22 € 17.842 $
Filter
Title
Price
Price (USD)
Shelf
Weight
Signed
Acquired
Reset