Module: Decidim::EnhancedTextwork::ApplicationHelper

Includes:
CheckBoxesTreeHelper, Comments::CommentsHelper, Decidim::EndorsableHelper, CollaborativeDraftHelper, ControlVersionHelper, MapHelper, ParagraphVotesHelper, FollowableHelper, MapHelper, RichTextEditorHelper, PaginateHelper
Included in:
CollaborativeDraftCellsHelper, ParagraphCellsHelper
Defined in:
app/helpers/decidim/enhanced_textwork/application_helper.rb

Overview

Custom helpers, scoped to the paragraphs engine.

Instance Method Summary collapse

Methods included from ControlVersionHelper

#item_name

Methods included from CollaborativeDraftHelper

#accept_request_button_label, #filter_collaborative_drafts_state_values, #reject_request_button_label

Methods included from MapHelper

#has_position?, #paragraph_data_for_map, #paragraph_preview_data_for_map, #paragraphs_data_for_map

Methods included from ParagraphVotesHelper

#can_accumulate_supports_beyond_threshold?, #current_user_can_vote?, #remaining_votes_count_for, #threshold_per_paragraph, #threshold_per_paragraph_enabled?, #vote_button_classes, #vote_limit, #vote_limit_enabled?, #votes_blocked?, #votes_count_classes, #votes_enabled?

Instance Method Details

#activity_filter_valuesObject

Options to filter Paragraphs by activity.



168
169
170
171
172
173
174
175
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 168

def activity_filter_values
  base = [
    ["all", t(".all")],
    ["my_paragraphs", t(".my_paragraphs")]
  ]
  base += [["voted", t(".voted")]] if current_settings.votes_enabled?
  base
end

#collaborative_draft_state_badge_css_class(state) ⇒ Object

Public: The css class applied based on the collaborative draft state.

state - The String state of the collaborative draft.

Returns a String.



68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 68

def collaborative_draft_state_badge_css_class(state)
  case state
  when "open"
    "success"
  when "withdrawn"
    "alert"
  when "published"
    "secondary"
  end
end

#filter_origin_valuesObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 177

def filter_origin_values
  origin_values = []
  origin_values << TreePoint.new("official", t("decidim.enhanced_textwork.application_helper.filter_origin_values.official")) if component_settings.official_paragraphs_enabled
  origin_values << TreePoint.new("citizens", t("decidim.enhanced_textwork.application_helper.filter_origin_values.citizens"))
  origin_values << TreePoint.new("user_group", t("decidim.enhanced_textwork.application_helper.filter_origin_values.user_groups")) if current_organization.user_groups_enabled?
  origin_values << TreePoint.new("meeting", t("decidim.enhanced_textwork.application_helper.filter_origin_values.meetings"))

  TreeNode.new(
    TreePoint.new("", t("decidim.enhanced_textwork.application_helper.filter_origin_values.all")),
    origin_values
  )
end

#filter_type_valuesObject



159
160
161
162
163
164
165
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 159

def filter_type_values
  [
    ["all", t("decidim.enhanced_textwork.application_helper.filter_type_values.all")],
    ["paragraphs", t("decidim.enhanced_textwork.application_helper.filter_type_values.paragraphs")],
    ["amendments", t("decidim.enhanced_textwork.application_helper.filter_type_values.amendments")]
  ]
end

#form_has_address?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 145

def form_has_address?
  @form.address.present? || @form.has_address
end

#humanize_collaborative_draft_state(state) ⇒ Object

Public: The state of a paragraph in a way a human can understand.

state - The String state of the paragraph.

Returns a String.



59
60
61
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 59

def humanize_collaborative_draft_state(state)
  I18n.t("decidim.enhanced_textwork.collaborative_drafts.states.#{state}", default: :open)
end

#humanize_paragraph_state(state) ⇒ Object

Public: The state of a paragraph in a way a human can understand.

state - The String state of the paragraph.

Returns a String.



27
28
29
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 27

def humanize_paragraph_state(state)
  I18n.t(state, scope: "decidim.enhanced_textwork.answers", default: :not_answered)
end

#minimum_votes_per_user_enabled?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 83

def minimum_votes_per_user_enabled?
  minimum_votes_per_user.positive?
end

#not_from_collaborative_draft(paragraph) ⇒ Object



87
88
89
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 87

def not_from_collaborative_draft(paragraph)
  paragraph.linked_resources(:paragraphs, "created_from_collaborative_draft").empty?
end

#not_from_participatory_text(paragraph) ⇒ Object



91
92
93
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 91

def not_from_participatory_text(paragraph)
  paragraph.participatory_text_level.nil?
end

#paragraph_limitObject



124
125
126
127
128
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 124

def paragraph_limit
  return if component_settings.paragraph_limit.zero?

  component_settings.paragraph_limit
end

#paragraph_limit_enabled?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 79

def paragraph_limit_enabled?
  paragraph_limit.present?
end

#paragraph_state_css_class(paragraph) ⇒ Object

Public: The css class applied based on the paragraph state.

paragraph - The paragraph to evaluate.

Returns a String.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 36

def paragraph_state_css_class(paragraph)
  state = paragraph.state
  state = paragraph.internal_state if paragraph.answered? && !paragraph.published_state?

  case state
  when "accepted"
    "text-success"
  when "rejected"
    "text-alert"
  when "evaluating"
    "text-warning"
  when "withdrawn"
    "text-alert"
  else
    "text-info"
  end
end

#render_paragraph_body(paragraph) ⇒ Object

If the content is safe, HTML tags are sanitized, otherwise, they are stripped.



104
105
106
107
108
109
110
111
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 104

def render_paragraph_body(paragraph)
  body = present(paragraph).body(links: true, strip_tags: !safe_content?)
  body = simple_format(body, {}, sanitize: false)

  return body unless safe_content?

  decidim_sanitize(body)
end

#safe_content?Boolean

If the paragraph is official or the rich text editor is enabled on the frontend, the paragraph body is considered as safe content; that's unless the paragraph comes from a collaborative_draft or a participatory_text.

Returns:

  • (Boolean)


98
99
100
101
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 98

def safe_content?
  rich_text_editor_in_public_views? && not_from_collaborative_draft(@paragraph) ||
    (@paragraph.official? || @paragraph.official_meeting?) && not_from_participatory_text(@paragraph)
end

#show_voting_rules?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 149

def show_voting_rules?
  return false unless votes_enabled?

  return true if vote_limit_enabled?
  return true if threshold_per_paragraph_enabled?
  return true if paragraph_limit_enabled?
  return true if can_accumulate_supports_beyond_threshold?
  return true if minimum_votes_per_user_enabled?
end

#text_editor_for_paragraph_body(form) ⇒ Object

Returns :text_area or :editor based on the organization' settings.



114
115
116
117
118
119
120
121
122
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 114

def text_editor_for_paragraph_body(form)
  options = {
    class: "js-hashtags",
    hashtaggable: true,
    value: form_presenter.body(extras: false).strip
  }

  text_editor_for(form, :body, options)
end

#vote_button_for(model, from_paragraphs_list) ⇒ Object



141
142
143
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 141

def vote_button_for(model, from_paragraphs_list)
  render partial: "decidim/enhanced_textwork/paragraphs/participatory_texts/paragraph_vote_button.html", locals: { paragraph: model, from_paragraphs_list: from_paragraphs_list }
end

#votes_count_for(model, from_paragraphs_list) ⇒ Object



137
138
139
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 137

def votes_count_for(model, from_paragraphs_list)
  render partial: "decidim/enhanced_textwork/paragraphs/participatory_texts/paragraph_votes_count.html", locals: { paragraph: model, from_paragraphs_list: from_paragraphs_list }
end

#votes_givenObject



130
131
132
133
134
135
# File 'app/helpers/decidim/enhanced_textwork/application_helper.rb', line 130

def votes_given
  @votes_given ||= ParagraphVote.where(
    paragraph: Paragraph.where(component: current_component),
    author: current_user
  ).count
end