Soportar un campo con una lista de valores predefinidos y elegir uno
This commit is contained in:
parent
752569c8a7
commit
1623ab73de
3 changed files with 34 additions and 0 deletions
24
app/models/metadata_predefined_value.rb
Normal file
24
app/models/metadata_predefined_value.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Un campo de texto seleccionado de una lista de valores posibles
|
||||
class MetadataPredefinedValue < MetadataString
|
||||
# Obtiene todos los valores desde el layout, en un formato compatible
|
||||
# con options_for_select.
|
||||
#
|
||||
# @return [Hash]
|
||||
def values
|
||||
@values ||= layout.dig(:metadata, name, 'values', I18n.locale.to_s)&.invert || {}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Solo permite almacenar los valores predefinidos.
|
||||
#
|
||||
# @return [String]
|
||||
def sanitize(string)
|
||||
v = super string
|
||||
return '' unless values.values.include? v
|
||||
|
||||
v
|
||||
end
|
||||
end
|
3
app/views/posts/attribute_ro/_predefined_value.haml
Normal file
3
app/views/posts/attribute_ro/_predefined_value.haml
Normal file
|
@ -0,0 +1,3 @@
|
|||
%tr{ id: attribute }
|
||||
%th= post_label_t(attribute, post: post)
|
||||
%td{ dir: dir, lang: locale }= metadata.value
|
7
app/views/posts/attributes/_predefined_value.haml
Normal file
7
app/views/posts/attributes/_predefined_value.haml
Normal file
|
@ -0,0 +1,7 @@
|
|||
.form-group
|
||||
= label_tag "#{base}_#{attribute}", post_label_t(attribute, post: post)
|
||||
= select_tag(plain_field_name_for(base, attribute),
|
||||
options_for_select(metadata.values, metadata.value),
|
||||
**field_options(attribute, metadata), include_blank: t('.empty'))
|
||||
= render 'posts/attribute_feedback',
|
||||
post: post, attribute: attribute, metadata: metadata
|
Loading…
Reference in a new issue