Class: RubyUIAdmin::Fields::SelectField
- Defined in:
- lib/ruby_ui_admin/fields/select_field.rb
Instance Attribute Summary
Attributes inherited from BaseField
#block, #id, #options, #resource
Instance Method Summary collapse
-
#enum_options ⇒ Object
[label, value]pairs derived from an ActiveRecord enum, or nil when noenum:. -
#formatted_value(record, view_context: nil) ⇒ Object
Display label for the current value.
- #include_blank? ⇒ Boolean
-
#select_options(record = nil) ⇒ Object
Resolves the configured options into an array of [label, value] pairs.
Methods inherited from BaseField
#database_id, #default_hidden_views, #default_value, #description, field_type, #fill, #fill_value, #filterable?, #has_default?, #help, #initialize, #link_to_record?, #name, #permit_param, #permit_params, #permitted_param, #placeholder, #readonly?, register_as, #required?, #sort_lambda, #sortable?, #type, #value, #visible?, #visible_in_view?
Constructor Details
This class inherits a constructor from RubyUIAdmin::Fields::BaseField
Instance Method Details
#enum_options ⇒ Object
[label, value] pairs derived from an ActiveRecord enum, or nil when no enum:.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_ui_admin/fields/select_field.rb', line 46 def enum = [:enum] return nil if enum.nil? || enum == false mapping = if enum == true resource&.model_class&.public_send(id.to_s.pluralize) else enum end return nil unless mapping.respond_to?(:keys) mapping.keys.map { |key| [key.to_s.humanize, key.to_s] } end |
#formatted_value(record, view_context: nil) ⇒ Object
Display label for the current value.
62 63 64 65 66 67 68 |
# File 'lib/ruby_ui_admin/fields/select_field.rb', line 62 def formatted_value(record, view_context: nil) raw = value(record, view_context: view_context) return super if @format_using pair = (record).find { |(_label, val)| val.to_s == raw.to_s } pair ? pair.first : raw end |
#include_blank? ⇒ Boolean
41 42 43 |
# File 'lib/ruby_ui_admin/fields/select_field.rb', line 41 def include_blank? .fetch(:include_blank, !required?) end |
#select_options(record = nil) ⇒ Object
Resolves the configured options into an array of [label, value] pairs.
options: accepts an Array, a Hash (=> label), or a lambda.
enum: derives options from an ActiveRecord enum: pass the enum mapping
(enum: Post.statuses) or enum: true to read model_class.<id.pluralize>.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_ui_admin/fields/select_field.rb', line 12 def (record = nil) return if raw = [:options] if raw.respond_to?(:call) # The options lambda can use `record`/`resource` plus `params`/`view` (e.g. an # options list that depends on the request, like a nested-create parent id). raw = ExecutionContext.new( target: raw, record: record, resource: resource, params: resource&.params, view: View.wrap(resource&.view) ).handle end pairs = case raw when Hash then raw.map { |value, label| [label, value] } when Array then raw.map { |entry| entry.is_a?(Array) ? entry : [entry, entry] } else [] end # `display_with_value: true` -> option labels become "Label (value)". return pairs.map { |(label, value)| ["#{label} (#{value})", value] } if [:display_with_value] pairs end |