Class: RubyUIAdmin::Fields::BaseField
- Inherits:
-
Object
- Object
- RubyUIAdmin::Fields::BaseField
- Defined in:
- lib/ruby_ui_admin/fields/base_field.rb
Overview
Base class for all field types. Fields are pure data/metadata objects; the actual rendering lives in Phlex components (app/components/ruby_ui_admin/fields).
Direct Known Subclasses
AssociationField, BadgeField, BelongsToField, BooleanField, BooleanGroupField, CodeField, DateField, DateTimeField, FileField, HiddenField, IdField, KeyValueField, NumberField, PasswordField, RecordLinkField, SelectField, StatusField, TextField, TextareaField, UrlField
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
Returns the value of attribute resource.
Class Method Summary collapse
- .field_type ⇒ Object
-
.register_as(key) ⇒ Object
Registers the field under a DSL symbol, e.g.
Instance Method Summary collapse
-
#database_id ⇒ Object
The underlying attribute name on the model (overridable by subclasses, e.g. belongs_to appends
_id). -
#default_hidden_views ⇒ Object
Views where a field type is hidden unless
only_on/hide_onsay otherwise. -
#default_value(record = nil) ⇒ Object
The field's default for a new record (literal or a proc evaluated with
record,resourceandcurrent_useravailable). - #description ⇒ Object
-
#fill(record, attributes) ⇒ Object
Applies the submitted (permitted)
attributesto the record for this field. -
#fill_value(record, value) ⇒ Object
Assigns a submitted value back onto the record.
- #filterable? ⇒ Boolean
-
#formatted_value(record, view_context: nil) ⇒ Object
Value formatted for display.
- #has_default? ⇒ Boolean
- #help ⇒ Object
-
#initialize(id, resource: nil, **options, &block) ⇒ BaseField
constructor
A new instance of BaseField.
- #link_to_record? ⇒ Boolean
- #name ⇒ Object
-
#permit_param ⇒ Object
The strong-parameters fragment for this field (scalar by default; fields with hash/array values override, e.g. boolean_group -> { name => {} }, files -> { name => [] }).
-
#permit_params ⇒ Object
All strong-params fragments this field contributes.
-
#permitted_param ⇒ Object
Which form param this field reads/writes.
- #placeholder ⇒ Object
- #readonly? ⇒ Boolean
- #required? ⇒ Boolean
-
#sort_lambda ⇒ Object
Whether the field can be sorted, and the column/expression to sort by.
- #sortable? ⇒ Boolean
- #type ⇒ Object
-
#value(record, view_context: nil) ⇒ Object
Raw value read from the record (or computed via a block).
-
#visible?(view: nil, record: nil) ⇒ Boolean
Conditional visibility (
visible: -> { current_user.admin? }), evaluated withview/record/resource/current_user. - #visible_in_view?(view) ⇒ Boolean
Constructor Details
#initialize(id, resource: nil, **options, &block) ⇒ BaseField
Returns a new instance of BaseField.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 21 def initialize(id, resource: nil, **, &block) @id = id.to_sym @resource = resource @options = @block = block @name = [:name] @required = .fetch(:required, false) @readonly = .fetch(:readonly, false) @sortable = .fetch(:sortable, false) @filterable = .fetch(:filterable, false) @link_to_record = .fetch(:link_to_record, false) @format_using = [:format_using] @help = [:help] @description = [:description] @visible = [:visible] @placeholder = [:placeholder] @default = [:default] @only_on = normalize_views([:only_on]) @hide_on = normalize_views([:hide_on]) end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
8 9 10 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 8 def block @block end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 8 def id @id end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 8 def @options end |
#resource ⇒ Object
Returns the value of attribute resource.
9 10 11 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 9 def resource @resource end |
Class Method Details
.field_type ⇒ Object
17 18 19 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 17 def self.field_type @field_type end |
Instance Method Details
#database_id ⇒ Object
The underlying attribute name on the model (overridable by subclasses,
e.g. belongs_to appends _id).
67 68 69 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 67 def database_id id end |
#default_hidden_views ⇒ Object
Views where a field type is hidden unless only_on/hide_on say otherwise.
Overridden by heavy/association fields (e.g. has_many hides everywhere but show).
182 183 184 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 182 def default_hidden_views [] end |
#default_value(record = nil) ⇒ Object
The field's default for a new record (literal or a proc evaluated with
record, resource and current_user available).
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 140 def default_value(record = nil) return @default unless @default.respond_to?(:call) ExecutionContext.new( target: @default, record: record, resource: resource, current_user: resource&.user ).handle end |
#description ⇒ Object
57 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 57 def description = @description |
#fill(record, attributes) ⇒ Object
Applies the submitted (permitted) attributes to the record for this field. Default just
assigns permitted_param; fields with extra inputs override (e.g. file remove).
129 130 131 132 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 129 def fill(record, attributes) key = permitted_param.to_s fill_value(record, attributes[key]) if attributes.key?(key) end |
#fill_value(record, value) ⇒ Object
Assigns a submitted value back onto the record.
105 106 107 108 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 105 def fill_value(record, value) setter = "#{database_id}=" record.public_send(setter, value) if record.respond_to?(setter) end |
#filterable? ⇒ Boolean
62 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 62 def filterable? = !!@filterable |
#formatted_value(record, view_context: nil) ⇒ Object
Value formatted for display.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 90 def formatted_value(record, view_context: nil) raw = value(record, view_context: view_context) return raw if @format_using.nil? ExecutionContext.new( target: @format_using, value: raw, record: record, resource: resource, current_user: resource&.user, view_context: view_context ).handle end |
#has_default? ⇒ Boolean
134 135 136 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 134 def has_default? !@default.nil? end |
#help ⇒ Object
56 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 56 def help = @help |
#link_to_record? ⇒ Boolean
63 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 63 def link_to_record? = !!@link_to_record |
#name ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 47 def name return @name if @name model = resource&.model_class # Uses the model's i18n attribute name (activerecord.attributes.*), # falling back to a humanized id. model.respond_to?(:human_attribute_name) ? model.human_attribute_name(id) : id.to_s.humanize end |
#permit_param ⇒ Object
The strong-parameters fragment for this field (scalar by default; fields with hash/array values override, e.g. boolean_group -> { name => {} }, files -> { name => [] }).
117 118 119 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 117 def permit_param permitted_param end |
#permit_params ⇒ Object
All strong-params fragments this field contributes. Defaults to a single permit_param;
fields that read extra inputs (e.g. a file field's remove checkbox) return more.
123 124 125 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 123 def permit_params [permit_param] end |
#permitted_param ⇒ Object
Which form param this field reads/writes.
111 112 113 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 111 def permitted_param database_id end |
#placeholder ⇒ Object
58 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 58 def placeholder = @placeholder |
#readonly? ⇒ Boolean
60 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 60 def readonly? = !!@readonly |
#required? ⇒ Boolean
59 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 59 def required? = !!@required |
#sort_lambda ⇒ Object
Whether the field can be sorted, and the column/expression to sort by.
sortable: true sorts by the database column; sortable: ->{...} is custom.
188 189 190 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 188 def sort_lambda @sortable.respond_to?(:call) ? @sortable : nil end |
#sortable? ⇒ Boolean
61 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 61 def sortable? = !!@sortable |
#type ⇒ Object
43 44 45 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 43 def type self.class.field_type end |
#value(record, view_context: nil) ⇒ Object
Raw value read from the record (or computed via a block). When a view_context
is given, the computed block can use view/url helpers (link_to, main_app, …).
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 73 def value(record, view_context: nil) return nil if record.nil? if block ExecutionContext.new( target: block, record: record, resource: resource, current_user: resource&.user, view_context: view_context ).handle elsif record.respond_to?(id) record.public_send(id) end end |
#visible?(view: nil, record: nil) ⇒ Boolean
Conditional visibility (visible: -> { current_user.admin? }), evaluated with
view/record/resource/current_user. A literal is used as-is. Defaults to visible.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 153 def visible?(view: nil, record: nil) return true if @visible.nil? result = if @visible.respond_to?(:call) ExecutionContext.new( target: @visible, view: View.wrap(view), record: record, resource: resource, current_user: resource&.user ).handle else @visible end !!result end |
#visible_in_view?(view) ⇒ Boolean
172 173 174 175 176 177 178 |
# File 'lib/ruby_ui_admin/fields/base_field.rb', line 172 def visible_in_view?(view) view = view.to_sym return @only_on.include?(view) if @only_on return !@hide_on.include?(view) if @hide_on !default_hidden_views.include?(view) end |