Skip to content

Commit

Permalink
Add markdown widget for interactive app forms
Browse files Browse the repository at this point in the history
Allows inserting arbitrary Markdown into the app forms through the
form.yml files.
  • Loading branch information
robinkar committed Aug 29, 2024
1 parent 76d0137 commit 72b171d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions apps/dashboard/app/lib/smart_attributes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/app/models/batch_connect/session_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -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: {})
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/app/models/batch_connect/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ locals: {
end
%>
</div>
<% @settings.app.attributes.each do |attribute| %>
<% @settings.app.attributes.select(&:serialize?).each do |attribute| %>
<p>
<strong><%= attribute.label %>:</strong>
<span><%= @settings.values[attribute.id.to_sym] %></span>
Expand Down

0 comments on commit 72b171d

Please sign in to comment.