Module: Decidim::AmendmentsHelper

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

Overview

A Helper to render and link amendments to resources.

Constant Summary collapse

TOTAL_STEPS =
2

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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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
# File 'app/helpers/decidim/amendments_helper.rb', line 131

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

#emendation_actions_for(emendation) ⇒ Object

Returns Html action button cards for an emendation



37
38
39
40
41
# File '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 '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 '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 '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 '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.



153
154
155
156
157
158
159
160
# File 'app/helpers/decidim/amendments_helper.rb', line 153

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

#wizard_header_titleObject

Returns the translation of the header title.



141
142
143
144
145
146
147
148
149
150
# File 'app/helpers/decidim/amendments_helper.rb', line 141

def wizard_header_title
  key = case current_step
        when 1
          :new
        when 2
          :preview_draft
        end

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