Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "Fix proposals admin form when editing" to v0.23 #7051

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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