@@ -155,6 +155,10 @@ module Datagrid
155
155
#
156
156
# self.default_column_options = { order: false }
157
157
#
158
+ # It can also accept a proc with the column instance as an argument:
159
+ #
160
+ # self.default_column_options = ->(column) { { order: column.name == :id } }
161
+ #
158
162
# ## Columns Visibility
159
163
#
160
164
# Columns can be dynamically shown or hidden based on the grid's `column_names` accessor.
@@ -201,15 +205,19 @@ module Datagrid
201
205
# end
202
206
module Columns
203
207
# @!method default_column_options=(value)
204
- # @param [Hash] value default options passed to {#column} method call
205
- # @return [Hash] default options passed to {#column} method call
208
+ # @param [Hash,Proc] value default options passed to {#column} method call.
209
+ # When a proc is passed, it will be called with the column instance as an argument,
210
+ # and expected to produce the options hash.
211
+ # @return [Hash,Proc] default options passed to {#column} method call, or a proc that returns them.
206
212
# @example Disable default order
207
213
# self.default_column_options = { order: false }
208
214
# @example Makes entire report HTML
209
215
# self.default_column_options = { html: true }
216
+ # @example Set the default header for all columns
217
+ # self.default_column_options = ->(column) { { header: I18n.t(column.name, scope: 'my_scope.columns') } }
210
218
211
219
# @!method default_column_options
212
- # @return [Hash] default options passed to {#column} method call
220
+ # @return [Hash,Proc ] default options passed to {#column} method call, or a proc that returns them.
213
221
# @see #default_column_options=
214
222
215
223
# @!method batch_size=(value)
@@ -368,9 +376,10 @@ def define_column(columns, name, query = nil, **options, &block)
368
376
block ||= lambda do |model |
369
377
model . public_send ( name )
370
378
end
379
+
371
380
position = Datagrid ::Utils . extract_position_from_options ( columns , options )
372
381
column = Datagrid ::Columns ::Column . new (
373
- self , name , query , default_column_options . merge ( options ) , &block
382
+ self , name , query , options , &block
374
383
)
375
384
columns . insert ( position , column )
376
385
column
0 commit comments