Skip to content

Commit

Permalink
Fix proposals admin form when editing. Closes #7031 (#7042) (#7051)
Browse files Browse the repository at this point in the history
Co-authored-by: Oriol Gual <oriolgual@users.noreply.github.com>
  • Loading branch information
mrcasals and oriolgual committed Dec 24, 2020
1 parent 24f3c3b commit a53a240
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class ProposalForm < Decidim::Proposals::Admin::ProposalBaseForm
validates :title, :body, translatable_presence: true

validate :notify_missing_attachment_if_errored

def map_model(model)
super(model)
presenter = ProposalPresenter.new(model)

self.title = presenter.title(all_locales: title.is_a?(Hash))
self.body = presenter.body(all_locales: body.is_a?(Hash))
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,33 @@ def display_mention
# extras - should include extra hashtags?
#
# Returns a String.
def title(links: false, extras: true, html_escape: false)
text = translated_attribute(proposal.title)
text = decidim_html_escape(text) if html_escape
def title(links: false, extras: true, html_escape: false, all_locales: false)
return unless proposal

renderer = Decidim::ContentRenderers::HashtagRenderer.new(text)
renderer.render(links: links, extras: extras).html_safe
handle_locales(proposal.title, all_locales) do |content|
content = decidim_html_escape(content) if html_escape

renderer = Decidim::ContentRenderers::HashtagRenderer.new(content)
renderer.render(links: links, extras: extras).html_safe
end
end

def id_and_title(links: false, extras: true, html_escape: false)
"##{proposal.id} - #{title(links: links, extras: extras, html_escape: html_escape)}"
end

def body(links: false, extras: true, strip_tags: false)
text = translated_attribute(proposal.body)
def body(links: false, extras: true, strip_tags: false, all_locales: false)
return unless proposal

text = strip_tags(sanitize_text(text)) if strip_tags
handle_locales(proposal.body, all_locales) do |content|
content = strip_tags(sanitize_text(content)) if strip_tags

renderer = Decidim::ContentRenderers::HashtagRenderer.new(text)
text = renderer.render(links: links, extras: extras).html_safe
renderer = Decidim::ContentRenderers::HashtagRenderer.new(content)
content = renderer.render(links: links, extras: extras).html_safe

text = Decidim::ContentRenderers::LinkRenderer.new(text).render if links
text
content = Decidim::ContentRenderers::LinkRenderer.new(content).render if links
content
end
end

# Returns the proposal versions, hiding not published answers
Expand Down Expand Up @@ -130,6 +135,20 @@ def add_line_feeds(text)
def sanitize_text(text)
add_line_feeds(sanitize_ordered_lists(sanitize_unordered_lists(text)))
end

def handle_locales(content, all_locales, &block)
if all_locales
content.each_with_object({}) do |(key, value), parsed_content|
parsed_content[key] = if key == "machine_translations"
handle_locales(value, all_locales, &block)
else
block.call(value)
end
end
else
yield(translated_attribute(content))
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

<div class="card-section">
<div class="row column hashtags__container">
<%= form.translated :text_field, :title, class: "js-hashtags", value: form_presenter.title(extras: false).strip, hashtaggable: true %>
<%= form.translated :text_field, :title, autofocus: true, class: "js-hashtags", hashtaggable: true %>
</div>

<div class="row column hashtags__container">
<%= form.translated :editor, :body, hashtaggable: true, value: form_presenter.body(extras: false).strip %>
<%= form.translated :editor, :body, hashtaggable: true %>
</div>

<% if @form.component_automatic_hashtags.any? %>
Expand Down

0 comments on commit a53a240

Please sign in to comment.