Module: Decidim::AmendmentsHelper

Includes:
RichTextEditorHelper
Included in:
ApplicationHelper
Defined in:
decidim-core/app/helpers/decidim/amendments_helper.rb

Overview

A Helper to render and link amendments to resources.

Instance Method Summary collapse

Methods included from RichTextEditorHelper

included, #text_editor_for

Instance Method Details

#accept_and_reject_buttons_for(emendation) ⇒ Object

Renders the buttons to ACCEPT/REJECT an emendation


75
76
77
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 75

def accept_and_reject_buttons_for(emendation)
  cell("decidim/amendable/emendation_actions", emendation)
end

#action_button_card_for(emendation) ⇒ Object

Returns Html action button cards to ACCEPT/REJECT or to PROMOTE an emendation


69
70
71
72
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 69

def action_button_card_for(emendation)
  return accept_and_reject_buttons_for(emendation) if allowed_to_accept_and_reject?(emendation)
  return promote_button_for(emendation) if allowed_to_promote?(emendation)
end

#allowed_to_accept_and_reject?(emendation) ⇒ Boolean

Checks if the user can accept and reject the emendation

Returns:

  • (Boolean)

96
97
98
99
100
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 96

def allowed_to_accept_and_reject?(emendation)
  return unless emendation.amendment.evaluating?

  emendation.amendable.created_by?(current_user) || current_user.admin?
end

#allowed_to_promote?(emendation) ⇒ Boolean

Checks if the user can promote the emendation

Returns:

  • (Boolean)

103
104
105
106
107
108
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 103

def allowed_to_promote?(emendation)
  return unless emendation.amendment.rejected? && emendation.created_by?(current_user)
  return if emendation.amendment.promoted?

  current_component.current_settings.amendment_promotion_enabled
end

#amend_button_for(amendable) ⇒ Object

Returns Html action button card to AMEND an amendable resource


45
46
47
48
49
50
51
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 45

def amend_button_for(amendable)
  return unless amendments_enabled? && amendable.amendable?
  return unless current_component.current_settings.amendment_creation_enabled
  return unless can_participate_in_private_space?

  cell("decidim/amendable/amend_button_card", amendable)
end

#amenders_list_for(amendable) ⇒ Object

Renders the amenders list of an amendable resource


29
30
31
32
33
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 29

def amenders_list_for(amendable)
  return unless amendable.amendable?

  cell("decidim/amendable/amenders_list", amendable)
end

#amendments_enabled?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 84

def amendments_enabled?
  current_component.settings.amendments_enabled
end

#amendments_for(amendable) ⇒ Object

Renders the emendations of an amendable resource

Returns Html grid of CardM.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 11

def amendments_for(amendable)
  return unless amendable.amendable?
  return unless (emendations = amendable.visible_emendations_for(current_user)).any?

  content = (:h2, class: "section-heading", id: "amendments") do
    t("section_heading", scope: "decidim.amendments.amendable", count: emendations.count)
  end

  content << cell("decidim/collapsible_list",
                  emendations,
                  cell_options: { context: { current_user: current_user } },
                  list_class: "row small-up-1 medium-up-2 card-grid amendment-list",
                  size: 4).to_s

   :div, content.html_safe, class: "section"
end

#amendments_form_field_for(attribute, form, original_resource) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 117

def amendments_form_field_for(attribute, form, original_resource)
  options = {
    class: "js-hashtags",
    label: amendments_form_fields_label(attribute),
    value: amendments_form_fields_value(original_resource, attribute)
  }

  case attribute
  when :title
    form.text_field(:title, options)
  when :body
    text_editor_for(form, :body, options.merge(hashtaggable: true))
  end
end

#amendments_form_fields_label(attribute) ⇒ Object

Return the translated attribute name to use as label in a form. Returns a String.


112
113
114
115
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 112

def amendments_form_fields_label(attribute)
  model_name = amendable.model_name.singular_route_key
  I18n.t(attribute, scope: "activemodel.attributes.#{model_name}")
end

#amendments_form_fields_value(original_resource, attribute) ⇒ Object

Return the edited field value or presents the original attribute value in a form.

original_resource - name of the method to send to the controller (:amendable or :emendation) attribute - name of the attribute to send to the original_resource Presenter

Returns a String.


148
149
150
151
152
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 148

def amendments_form_fields_value(original_resource, attribute)
  return params[:amendment][:emendation_params][attribute] if params[:amendment].present?

  present(send(original_resource)).send(attribute)
end

#can_participate_in_private_space?Boolean

Checks if the user can participate in a participatory space based on its settings related with Decidim::HasPrivateUsers.

Returns:

  • (Boolean)

55
56
57
58
59
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 55

def can_participate_in_private_space?
  return true unless current_participatory_space.class.included_modules.include?(HasPrivateUsers)

  current_participatory_space.can_participate?(current_user)
end

#can_react_to_emendation?(emendation) ⇒ Boolean

Checks if there's a user that can react to an emendation

Returns:

  • (Boolean)

89
90
91
92
93
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 89

def can_react_to_emendation?(emendation)
  return unless current_user && emendation.emendation?

  current_component.current_settings.amendment_reaction_enabled
end

#emendation_actions_for(emendation) ⇒ Object

Returns Html action button cards for an emendation


62
63
64
65
66
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 62

def emendation_actions_for(emendation)
  return unless amendments_enabled? && can_react_to_emendation?(emendation)

  action_button_card_for(emendation)
end

#emendation_announcement_for(emendation) ⇒ Object

Renders the state of an emendation

Returns Html callout.


38
39
40
41
42
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 38

def emendation_announcement_for(emendation)
  return unless emendation.emendation?

  cell("decidim/amendable/announcement", emendation)
end

#promote_button_for(emendation) ⇒ Object

Renders the button to PROMOTE an emendation


80
81
82
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 80

def promote_button_for(emendation)
  cell("decidim/amendable/promote_button_card", emendation)
end

#render_emendation_body(emendation) ⇒ Object

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


133
134
135
136
137
138
139
140
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 133

def render_emendation_body(emendation)
  body = present(emendation).body(links: true, strip_tags: !rich_text_editor_in_public_views?)
  body = simple_format(body, {}, sanitize: false)

  return body unless rich_text_editor_in_public_views?

  decidim_sanitize(body)
end