-
Notifications
You must be signed in to change notification settings - Fork 330
API: Column
A list of actions enabled for singular association columns. Removing an action from the list disables showing links to that action in lists. Possible values are :new, :edit, :show and :list. Defaults to [:new, :edit, :show]
.
# Disable :new globally
ActiveScaffold::DataStructures::Column.actions_for_association_links.delete :new
# Only shows links to show in user column
config.columns[:user].actions_for_association_links = [:show]
Whether to enable add_existing for this column when is used as a subform
The number of associated records to show. If there are more associated records, a ellipsis (“…”) will be added and the number of associated records will be shown if associated_number is enabled. Set to nil to show all associated records. Defaults to 3.
A boolean for whether shows the number of associated records when all associated records aren’t shown. Defaults to true.
For association columns, lets you specify the reverse association name in case ActiveScaffold is unable to guess itself. For more information see the API: Nested.
It allows to set any setting which is assigned with a setter (most of them) with a hash. For example:
conf.columns[:roles].attributes = {
label: 'Select your roles',
form_ui: [:select, label_method: :name_with_desc],
options: {collapsible: true}
}
Defines a calculation on the column. Anything that ActiveRecord::Calculations::ClassMethods#calculate accepts will do. Only works on real columns.
Examples:
config.columns[:price].calculate = :sum
config.columns[:price].calculate = :average
Clears out any existing link, and prevents ActiveScaffold from automatically adding a nesting link.
config.columns[:owned_by].clear_link
Defines whether the column initializes in a collapsed state. Currently collapsing is only supported for association columns on the Create/Update forms.
An extra css class to apply to this column. Currently used in List and Create/Update forms. In lists is added to TD tag, and in forms is added to DL tag, so you can set css for labels and fields. You can set a proc, which will get 2 arguments, column_value and record, but proc only will be used in List.
A short description or example text. Currently used in Update/Create.
config.columns[:name].description = "Enter the users first and last name"
If you don’t set a text, it will use the key activerecord.description.model_name.column_name to search a description translation.
There are different form_ui types which may be used for a column. Depending on the column type, it may default to nil, rendering text field, or may default to one of these types. Each type support different options, which may be set in column.options, or can be provided next to the form_ui type since v3.7, in one assignment, and these options will be used instead of column’s options, isolating the options for the form_ui from other column’s options not related to the form_ui, which may pollute the html tag.
config.columns[:roles].form_ui = :select, {label_method: :human_label, draggable_lists: true}
These options can be read with form_ui_options
method if needed: config.columns[:roles].form_ui_options
Association columns, by default renders the whole subform that lets you actually create/update associated records.
An array of associations for eager loading that are relevant for this column. For association columns, defaults to the association itself. This is especially useful with virtual columns. Consider the following example:
# last_transaction_date is a virtual column that references a joined field
active_scaffold :users do | config |
config.columns = [:name, :last_transaction_date, :status]
config.columns[:last_transaction_date] = "Last transaction date"
config.columns[:last_transaction_date].includes = [:user_transactions]
config.columns[:last_transaction_date].sort_by :sql => "user_transactions.created_at"
end
Enable in_place_editor for this column (true, :ajax or false). Defaults to false. Validation errors are reported via page.alert.
Since v2.3:
- For columns with form_ui or form override, it will copy the fields from a hidden template in the column header instead of use the default InPlaceEditor.
- If inplace_edit is set to :ajax, an AJAX request will be made to get the field, that option is needed for in place editing with some form_ui, such as :record_select or :chosen.
active_scaffold :users do | config |
config.columns[:title].inplace_edit = true # uses JS to show string field, or copy from UI from list header, same UI for each row, no AJAX request
config.columns[:title].inplace_edit = :ajax # uses JS request to render UI in the server
end
Enable updating columns, row or table after updating the column with in_place_editor. Default is nil, which will update only the column,
Possible values are:
-
:columns
to update some columns, defined withupdate_columns
attribute. If some column has update_columns, they will be updated too, avoiding circular dependencies. -
:row
to update the row. It must be used if action_links can change after updating the column, for example due to permission checks. -
:table
to update the table.
active_scaffold :users do | config |
config.columns[:status].inplace_edit_update = :row # refreshes the whole row
config.columns[:favorite].inplace_edit_update = :table # refreshes the whole table
config.columns[:approve_reason].inplace_edit_update = :columns # refreshes the columns defined in next line
config.columns[:approve_reason].update_columns = [:approved_at] # refreshes the columns defined in next line
end
The displayable name.
config.columns[:created_at].label = "How Old"
If you don’t set the label, it will use the key activerecord.attributes.model_name.column_name to search a label translation.
If a column has a special presentation you may pick an option here. Form_ui is used if list_ui is not specified and it exists such list_ui. You may pick:
:checkbox
(useful for boolean type columns)
:text
(used automatically in text type columns, accepts :truncate
in the Column#options hash to set truncate length, default is 50)
Since v3.7, the options for the list_ui can be provided next to the list_ui type, in one assignment, and these options will be used instead of column’s options, isolating the options for the list_ui from other column’s options not related to the list_ui, which may pollute the html tag.
config.columns[:description].list_ui = :text, {truncate: 30}
These options can be read with list_ui_options
method if needed: config.columns[:description].list_ui_options
If no list_ui is defined, and form_ui is defined with options, list_ui_options will return the form_ui_options too, but if list_ui is defined without options, list_ui_options will never return form_ui_options.
Html options for text input fields, and options for some form interfaces (see Column#form_ui and Column#list_ui methods).
options[:format] can be set for:
- date and time columns, and it will be used as the format argument of I18n.localize to format them.
- number columns, it can be i18n_number (set by default), currency, percentage or size. i18n_number will use number_with_delimiter, other options will use number_to_currency, number_to_percentage or number_to_human_size. Options for these helpers can be set in options[:i18n_options].
options[:collapsible] can be set to true to allow collapsing the column in Create/Update form, column is never collapsed initially.
Specifies a placeholder for HTML input
tag in create/update actions. Could replace or be used together with description.
If you don’t set a text, it will use the key activerecord.placeholder.model_name.column_name
to search a placeholder translation. Examples available on appropriate pull request page .
A boolean for whether the column is required through already-existing validation. Currently used in Update/Create. Defaults to false, but if validation reflection is available it defaults to true for columns with validates_presence_of. If it’s true, the required
attribute on HTML input
tag will be set and modern browsers won’t allow user to send form unless this column is filled.
The SQL string used when searching on this column. Defaults to the field name of the column for real columns, and association_table.primary_key
for association columns. This SQL is the left side of a condition, which means that if you want to do something with multiple fields they have to be in a function. Since 3.2.13 you can set an array with multiple left sides, and conditions will be OR’ed.
# good. this will get turned into "WHERE name = ?"
config.columns[:name].search_sql = 'name'
# bad. this will get turned into "WHERE first_name OR last_name = ?", which is invalid syntax
config.columns[:name].search_sql = 'first_name OR last_name = ?'
# good. this will get turned into "WHERE CONCAT(first_name, ' ', last_name) = ?"
config.columns[:name].search_sql = "CONCAT(first_name, ' ', last_name)"
#good. in PostgreSQL for multiple columns
config.columns[:name].search_sql = "first_name||last_name"
# good since 3.2.13. this will get turned into "WHERE first_name = ? OR last_name = ?"
config.columns[:name].search_sql = ["first_name", "last_name"]
Note: After you define the search_sql you may need to add the column to the API: Search.
When using field_search, the items in search_sql
array can be a hash too, to use a subquery for the condition, with the following structure:
-
subquery
key, required, with an array:- the first element in the array must be the model or ActiveRecord relation used for the subquery. If the relation doesn’t select one column for the subquery, the primary key will be used.
- the next elements are column names or SQL code, like normal items in
search_sql
, used for the conditions in the subquery
-
field
key, optional, the column name to match with the subquery result -
conditions
key, optional, an array with extra conditions, like the parameters forwhere
method from ActiveRecord.
For example, for a polymorphic association, in a Resume model:
conf.columns[:resume_holder].search_sql = [
{subquery: [User, 'first_name', 'last_name']},
{subquery: [Candidate, 'first_name', 'last_name']}
]
Then it would generate a condition when searching for ‘John’ like this:
resume_holder_id IN (SELECT id FROM users WHERE first_name LIKE '%John%' OR last_name LIKE '%John%') AND resume_holder_type = 'User' OR
resume_holder_id IN (SELECT id FROM candidates WHERE first_name LIKE '%John%' OR last_name LIKE '%John%') AND resume_holder_type = 'Candidate'
In that example, field
key is not needed as the foreign key for the column is used, and conditions
key is not needed because it’s generated automatically to match the foreign type of the association. However, if using a virtual column, field will be needed, and conditions may be needed too, depending on the case:
conf.columns[:parent].search_sql = [
{subquery: [User, 'first_name', 'last_name'], field: :resume_holder_id, conditions: ['resume_holder_type = ?', 'User']},
{subquery: [Candidate, 'first_name', 'last_name'], field: :resume_holder_id, conditions: ['resume_holder_type = ?', 'Candidate']}
]
In the subquery
key, scopes or where can be used, e.g. User.where(disabled: false)
, or select(:column)
to use it instead of :id
.
There are different search_ui types which may be used for a column. Form_ui is used if search_ui is not specified and it exists such search_ui. Each type support different options, which may be set in column.options, or can be provided next to the search_ui type since v3.7, in one assignment, and these options will be used instead of column’s options, isolating the options for the search_ui from other column’s options not related to the search_ui, which may pollute the html tag.
If no search_ui is defined, and form_ui is defined with options, search_ui_options will return the form_ui_options too, but if search_ui is defined without options, search_ui_options will never return form_ui_options.
config.columns[:roles].search_ui = :select, {label_method: :human_label}
These options can be read with search_ui_options
method if needed: config.columns[:roles].search_ui_options
For association columns, if search_ui and form_ui is not set, it defaults to :select
, which works with the default search_sql, then it renders a select box to search for a specific object. Also, :multi_select
or :record_select
(if recordselect gem is available) may work with the default search_sql, :multi_select
renders a collection of checkboxes to search for some specific objects. Other search_ui can be used to search in some field, setting search_sql.
WARNING Before v3.2.20 this method was named select_columns, added on v2.3
What columns load from association table when eager loading is disabled. It’s only used when includes is nil.
# Turn off eager loading
config.columns[:association_column].includes = nil
# Select only the name
config.columns[:association_column].select_associated_columns = [:name]
What columns load from main table when [API: List]#auto_select_columns is enabled. By default is the own column for real columns and foreign key for belongs_to associations. Must be an array or nil.
# Turn off column selection
config.columns[:association_column].select_columns = nil
# Select two columns
config.columns[:association_column].select_columns = ['name, surname']
Send all the form instead of single value when this column changes.
You can set options[:send_form_selector]
to filter which fields are sent.
Also, you can set to :row
and it will send only the row of the associated record instead of whole form, when column is in a subform.
Sets the action link for this column. Currently used only in List. See API: Action Link for options. This link will automatically get the id of the current row, but if you want to put any other dynamic values in the link you’re better off just using a Field Overrides .
Whether to show a blank record in subform or not. When is disabled, you must click in add new to get a blank row. Defaults to true.
# Turn off blank records in all subforms
ActiveScaffold::DataStructures::Column.show_blank_record = false
# And enable blank record to role column
config.columns[:role].show_blank_record = true
If a column has a special presentation you may pick an option here. List_ui is used if show_ui is not specified and it exists such show_ui. You may pick:
:horizontal
(only for associations, to display show columns of the associated records, instead of list values with to_label, columns are displayed horizontally, as a table)
:text
(used automatically in text type columns, display the whole value without truncating, using simple_format method from rails, options[:html_options] are passed as html_options argument, and options are passed as options argument too)
:vertical
(only for associations, to display show columns of the associated records, instead of list values with to_label, columns are displayed vertically)
Since v3.7, the options for the show_ui can be provided next to the show_ui type, in one assignment, and these options will be used instead of column’s options, isolating the options for the show_ui from other column’s options not related to the show_ui, which may pollute the html tag.
config.columns[:description].show_ui = :text, {html_options: {class: 'description'}, wrapper_tag: :div}
These options can be read with show_ui_options
method if needed: config.columns[:description].show_ui_options
If no show_ui is defined, and list_ui is defined with options, show_ui_options will return the list_ui_options too, but if show_ui is defined without options, show_ui_options will never return list_ui_options.
A boolean for whether this column is sortable or not. Defaults to false for virtual columns.
Lets you define either the SQL used for sorting (defaults to the field name) or a string for method based sorting.
# To sort by an SQL expression
columns[:name].sort_by :sql => 'concat(first_name, last_name)'
# To sort by multiple DB columns, more efficient than SQL expression becuase it can use indexes
# _(only since 3.2)_
columns[:name].sort_by :sql => ['first_name', 'last_name']
# to sort by a Ruby method (notice this is a method on the model you are scaffolding against):
columns[:name].sort_by :method => 'full_name'
# If your method sorting can return nil, then you may need to typecast:
columns[:name].sort_by :method => 'full_name or String.new'
Association columns default to using method-based sorting.
Method-based sorting is slow and resource-intensive. The entire table must be loaded and sorted, which will not scale for large data sets. Try to use sort_by :sql as much as possible.
In newer versions replaced with update_columns
The column to be updated in a form when this column changes.
The columns to be updated in a form when this column changes. If some column has update_columns, they will be updated too, avoiding circular dependencies.
Add a weight to the column to override alphabetical sorting. Columns are sorted from lowest weight to highest one, and columns with same weight are sorted alphabetically. You must assign weights before use @config.#{action}.columns, whenever you call @config.#{action}.columns, columns are sorted and copied from global config to action.