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.

Constant Summary collapse

TOTAL_STEPS =
4

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



50
51
52
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 50

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



44
45
46
47
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 44

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)


71
72
73
74
75
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 71

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)


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

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



20
21
22
23
24
25
26
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 20

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

#amendments_enabled?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 59

def amendments_enabled?
  current_component.settings.amendments_enabled
end

#amendments_form_field_for(attribute, form, original_resource) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 92

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.



87
88
89
90
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 87

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.



123
124
125
126
127
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 123

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)


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

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 is a user that can react to an emendation

Returns:

  • (Boolean)


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

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

  current_component.current_settings.amendment_reaction_enabled
end

#current_stepObject



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

def current_step
  @current_step ||= case params[:action].to_sym
                    when :new, :create
                      1
                    when :compare_draft
                      2
                    when :edit_draft, :update_draft, :destroy_draft
                      3
                    when :preview_draft, :publish_draft
                      4
                    end
end

#emendation_actions_for(emendation) ⇒ Object

Returns Html action button cards for an emendation



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

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.



13
14
15
16
17
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 13

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



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

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.



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

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_editor(body)
end

#total_stepsObject



129
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 129

def total_steps = TOTAL_STEPS

#wizard_aside_back_url(amendable) ⇒ Object

Returns the link we want the back button to point to.



161
162
163
164
165
166
167
168
169
170
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 161

def wizard_aside_back_url(amendable)
  case current_step
  when 1
    Decidim::ResourceLocatorPresenter.new(amendable).path
  when 3
    Decidim::Core::Engine.routes.url_helpers.compare_draft_amend_path(amendable.amendment)
  when 4
    Decidim::Core::Engine.routes.url_helpers.edit_draft_amend_path(amendable.amendment)
  end
end

#wizard_header_titleObject

Returns the translation of the header title.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'decidim-core/app/helpers/decidim/amendments_helper.rb', line 145

def wizard_header_title
  key = case current_step
        when 1
          :new
        when 2
          :compare_draft
        when 3
          :edit_draft
        when 4
          :preview_draft
        end

  t("decidim.amendments.#{key}.title")
end