From 72b171dea494badc57b14db8ef21e2c7feabcf8d Mon Sep 17 00:00:00 2001 From: Robin Karlsson Date: Thu, 29 Aug 2024 16:23:55 +0300 Subject: [PATCH 1/2] Add markdown widget for interactive app forms Allows inserting arbitrary Markdown into the app forms through the form.yml files. --- .../app/helpers/batch_connect/session_contexts_helper.rb | 2 ++ apps/dashboard/app/lib/smart_attributes/attribute.rb | 4 ++++ apps/dashboard/app/models/batch_connect/session_context.rb | 4 ++-- apps/dashboard/app/models/batch_connect/settings.rb | 4 ++-- apps/dashboard/app/views/batch_connect/settings/show.html.erb | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/dashboard/app/helpers/batch_connect/session_contexts_helper.rb b/apps/dashboard/app/helpers/batch_connect/session_contexts_helper.rb index daf23edc33..f1a4c92252 100644 --- a/apps/dashboard/app/helpers/batch_connect/session_contexts_helper.rb +++ b/apps/dashboard/app/helpers/batch_connect/session_contexts_helper.rb @@ -35,6 +35,8 @@ def create_widget(form, attrib, format: nil, hide_excludable: true, hide_fixed: end when 'file_attachments' render :partial => "batch_connect/session_contexts/file_attachments", :locals => { form: form, attrib: attrib, field_options: field_options } + when 'markdown' + OodAppkit.markdown.render(attrib.value.to_s).html_safe else form.send widget, attrib.id, all_options end diff --git a/apps/dashboard/app/lib/smart_attributes/attribute.rb b/apps/dashboard/app/lib/smart_attributes/attribute.rb index 735bcf53d0..866668f864 100644 --- a/apps/dashboard/app/lib/smart_attributes/attribute.rb +++ b/apps/dashboard/app/lib/smart_attributes/attribute.rb @@ -68,6 +68,10 @@ def widget (opts[:widget] || 'text_field').to_s end + def serialize? + !!opts.fetch(:serialize, widget != "markdown") + end + # Form label for this attribute # @param fmt [String, nil] formatting of form label # @return [String] form label diff --git a/apps/dashboard/app/models/batch_connect/session_context.rb b/apps/dashboard/app/models/batch_connect/session_context.rb index 7523bf9e94..4cb7a5a969 100644 --- a/apps/dashboard/app/models/batch_connect/session_context.rb +++ b/apps/dashboard/app/models/batch_connect/session_context.rb @@ -14,7 +14,7 @@ class SessionContext # Attributes used for serialization # @return [Hash{String => String, nil}] attributes to be serialized def attributes - @attributes.reject(&:fixed?).map { |a| [a.id.to_s, nil] }.to_h + @attributes.reject(&:fixed?).select(&:serialize?).map { |a| [a.id.to_s, nil] }.to_h end def attributes=(params = {}) @@ -69,7 +69,7 @@ def update_with_cache(cache) end def to_h - Hash[*map { |a| [a.id.to_sym, a.value] }.flatten] + Hash[*select(&:serialize?).map { |a| [a.id.to_sym, a.value] }.flatten] end def to_openstruct(addons: {}) diff --git a/apps/dashboard/app/models/batch_connect/settings.rb b/apps/dashboard/app/models/batch_connect/settings.rb index fe38be0ac8..9a992d590a 100644 --- a/apps/dashboard/app/models/batch_connect/settings.rb +++ b/apps/dashboard/app/models/batch_connect/settings.rb @@ -18,12 +18,12 @@ def app def outdated? outdated = false # CHECK IF THERE ARE NEW ATTRIBUTES NOT IN THE VALUES HASH - app.attributes.each do |attribute| + app.attributes.select(&:serialize?).each do |attribute| outdated = true unless values.key?(attribute.id.to_sym) end # CHECK IF THERE ARE OLD VALUES NO LONGER IN THE APP ATTRIBUTES STILL IN THE VALUES HASH values.each_key do |attribute_id| - outdated = true if app.attributes.select { |attribute| attribute.id.to_sym == attribute_id }.empty? + outdated = true if app.attributes.select(&:serialize?).select { |attribute| attribute.id.to_sym == attribute_id }.empty? end outdated diff --git a/apps/dashboard/app/views/batch_connect/settings/show.html.erb b/apps/dashboard/app/views/batch_connect/settings/show.html.erb index 75e1fc864a..dc6201f41d 100644 --- a/apps/dashboard/app/views/batch_connect/settings/show.html.erb +++ b/apps/dashboard/app/views/batch_connect/settings/show.html.erb @@ -68,7 +68,7 @@ locals: { end %> - <% @settings.app.attributes.each do |attribute| %> + <% @settings.app.attributes.select(&:serialize?).each do |attribute| %>

<%= attribute.label %>: <%= @settings.values[attribute.id.to_sym] %> From e90533b765b9050fa0da734353f8551f5019a2e6 Mon Sep 17 00:00:00 2001 From: Robin Karlsson Date: Mon, 2 Sep 2024 08:45:05 +0300 Subject: [PATCH 2/2] Refactor serializable SmartAttributes --- apps/dashboard/app/models/batch_connect/session_context.rb | 7 +++++++ apps/dashboard/app/models/batch_connect/settings.rb | 6 ++++-- .../views/batch_connect/session_contexts/_form.html.erb | 2 +- .../app/views/batch_connect/settings/show.html.erb | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/dashboard/app/models/batch_connect/session_context.rb b/apps/dashboard/app/models/batch_connect/session_context.rb index 4cb7a5a969..5313c469a6 100644 --- a/apps/dashboard/app/models/batch_connect/session_context.rb +++ b/apps/dashboard/app/models/batch_connect/session_context.rb @@ -40,6 +40,13 @@ def [](id) # @yield [SmartAttribute::Attribute] Gives the next attribute object in the # list def each(&block) + @attributes.select(&:serialize?).each(&block) + end + + # For a block {|attribute| ...} + # @yield [SmartAttribute::Attribute] Gives the next attribute object in the + # list. Includes non-serializable attributes. + def each_form_attribute(&block) @attributes.each(&block) end diff --git a/apps/dashboard/app/models/batch_connect/settings.rb b/apps/dashboard/app/models/batch_connect/settings.rb index 9a992d590a..26a186f8f5 100644 --- a/apps/dashboard/app/models/batch_connect/settings.rb +++ b/apps/dashboard/app/models/batch_connect/settings.rb @@ -17,13 +17,15 @@ def app def outdated? outdated = false + + attributes = app.build_session_context # CHECK IF THERE ARE NEW ATTRIBUTES NOT IN THE VALUES HASH - app.attributes.select(&:serialize?).each do |attribute| + attributes.each do |attribute| outdated = true unless values.key?(attribute.id.to_sym) end # CHECK IF THERE ARE OLD VALUES NO LONGER IN THE APP ATTRIBUTES STILL IN THE VALUES HASH values.each_key do |attribute_id| - outdated = true if app.attributes.select(&:serialize?).select { |attribute| attribute.id.to_sym == attribute_id }.empty? + outdated = true if attributes.select { |attribute| attribute.id.to_sym == attribute_id }.empty? end outdated diff --git a/apps/dashboard/app/views/batch_connect/session_contexts/_form.html.erb b/apps/dashboard/app/views/batch_connect/session_contexts/_form.html.erb index 0b5d387772..66a4b9eba1 100644 --- a/apps/dashboard/app/views/batch_connect/session_contexts/_form.html.erb +++ b/apps/dashboard/app/views/batch_connect/session_contexts/_form.html.erb @@ -2,7 +2,7 @@ <%= render "prefill_templates" if Configuration.bc_saved_settings? %> <%= bootstrap_form_for(@session_context, html: { autocomplete: "off" }) do |f| %> - <% f.object.each do |attrib| %> + <% f.object.each_form_attribute do |attrib| %> <%= create_widget(f, attrib, format: @render_format) %> <% end %> diff --git a/apps/dashboard/app/views/batch_connect/settings/show.html.erb b/apps/dashboard/app/views/batch_connect/settings/show.html.erb index dc6201f41d..53013c9597 100644 --- a/apps/dashboard/app/views/batch_connect/settings/show.html.erb +++ b/apps/dashboard/app/views/batch_connect/settings/show.html.erb @@ -68,7 +68,7 @@ locals: { end %> - <% @settings.app.attributes.select(&:serialize?).each do |attribute| %> + <% @settings.app.build_session_context.each do |attribute| %>

<%= attribute.label %>: <%= @settings.values[attribute.id.to_sym] %>