Class: RubyUIAdmin::Fields::FileField
- Defined in:
- lib/ruby_ui_admin/fields/file_field.rb
Overview
Single ActiveStorage attachment.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from BaseField
#block, #id, #options, #resource
Instance Method Summary collapse
- #accept ⇒ Object
- #attached?(record) ⇒ Boolean
- #attachment(record) ⇒ Object
- #default_hidden_views ⇒ Object
-
#fill(record, attributes) ⇒ Object
Purge the current attachment when "remove" is checked; otherwise assign the upload (skipping blank so an empty input doesn't wipe an existing attachment).
-
#fill_value(record, value) ⇒ Object
Skip empty uploads so we don't blow away an existing attachment.
- #multiple? ⇒ Boolean
-
#permit_params ⇒ Object
Also permit the remove checkbox alongside the upload.
-
#preview_size ⇒ Object
Max display size (W×H, px) for an image thumbnail on the show view.
-
#remove_param ⇒ Object
Form param for the "remove current file" checkbox.
Methods inherited from BaseField
#database_id, #default_value, #description, field_type, #filterable?, #formatted_value, #has_default?, #help, #initialize, #link_to_record?, #name, #permit_param, #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
#accept ⇒ Object
17 18 19 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 17 def accept [:accept] end |
#attached?(record) ⇒ Boolean
32 33 34 35 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 32 def attached?(record) att = (record) att.respond_to?(:attached?) && att.attached? end |
#attachment(record) ⇒ Object
28 29 30 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 28 def (record) record.public_send(id) if record.respond_to?(id) end |
#default_hidden_views ⇒ Object
9 10 11 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 9 def default_hidden_views %i[index] end |
#fill(record, attributes) ⇒ Object
Purge the current attachment when "remove" is checked; otherwise assign the upload (skipping blank so an empty input doesn't wipe an existing attachment).
57 58 59 60 61 62 63 64 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 57 def fill(record, attributes) if ActiveModel::Type::Boolean.new.cast(attributes[remove_param.to_s]) record.public_send(id).purge if attached?(record) return end super end |
#fill_value(record, value) ⇒ Object
Skip empty uploads so we don't blow away an existing attachment.
38 39 40 41 42 43 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 38 def fill_value(record, value) return if value.blank? setter = "#{id}=" record.public_send(setter, value) if record.respond_to?(setter) end |
#multiple? ⇒ Boolean
13 14 15 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 13 def multiple? false end |
#permit_params ⇒ Object
Also permit the remove checkbox alongside the upload.
51 52 53 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 51 def permit_params [permit_param, remove_param] end |
#preview_size ⇒ Object
Max display size (W×H, px) for an image thumbnail on the show view. Accepts an Integer
(square, e.g. preview_size: 100) or [width, height] (e.g. [120, 80]). Applied as a
max-width/max-height (the image scales down to fit, preserving aspect). Defaults to 64×64.
24 25 26 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 24 def preview_size [:preview_size] end |
#remove_param ⇒ Object
Form param for the "remove current file" checkbox.
46 47 48 |
# File 'lib/ruby_ui_admin/fields/file_field.rb', line 46 def remove_param :"#{permitted_param}_remove" end |