Module: Decidim::Proposals::ProposalEndorsementsHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/decidim/proposals/proposal_endorsements_helper.rb

Overview

Simple helper to handle markup variations for proposal endorsements partials

Instance Method Summary collapse

Instance Method Details

#current_user_can_endorse?Boolean

Public: Checks if the current user is allowed to endorse in this step.

Returns true if the current user can endorse, false otherwise.

Returns:

  • (Boolean)


34
35
36
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 34

def current_user_can_endorse?
  current_user && endorsements_enabled? && !endorsements_blocked?
end

#endorsement_button(proposal, from_proposals_list, btn_label = nil, user_group = nil) ⇒ Object

Public: Renders a button to endorse the given proposal. To override the translation for both buttons: endorse and unendorse (use to be the name of the user/user_group).



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 58

def endorsement_button(proposal, from_proposals_list, btn_label = nil, user_group = nil)
  current_endorsement_url = proposal_proposal_endorsement_path(
    proposal_id: proposal,
    from_proposals_list: from_proposals_list,
    user_group_id: user_group&.id
  )
  endorse_label = btn_label || t("decidim.proposals.proposal_endorsements_helper.endorsement_button.endorse")
  unendorse_label = btn_label || t("decidim.proposals.proposal_endorsements_helper.endorsement_button.already_endorsed")

  render partial: "decidim/proposals/proposals/endorsement_button", locals: { proposal: proposal,
                                                                              from_proposals_list: from_proposals_list, user_group: user_group,
                                                                              current_endorsement_url: current_endorsement_url,
                                                                              endorse_label: endorse_label, unendorse_label: unendorse_label }
end

#endorsement_button_classes(from_proposals_list) ⇒ Object

Returns the css classes used for proposal endorsement button in both proposals list and show pages

from_proposals_list - A boolean to indicate if the template is rendered from the proposals list page

Returns a string with the value of the css classes.



12
13
14
15
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 12

def endorsement_button_classes(from_proposals_list)
  return "small" if from_proposals_list
  "small compact light button--sc expanded"
end

#endorsement_identity_presenter(endorsement) ⇒ Object



45
46
47
48
49
50
51
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 45

def endorsement_identity_presenter(endorsement)
  if endorsement.user_group
    Decidim::UserGroupPresenter.new(endorsement.user_group)
  else
    Decidim::UserPresenter.new(endorsement.author)
  end
end

#endorsements_blocked?Boolean

Public: Checks if endorsements are blocked in this step.

Returns true if blocked, false otherwise.

Returns:

  • (Boolean)


27
28
29
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 27

def endorsements_blocked?
  current_settings.endorsements_blocked
end

#endorsements_enabled?Boolean

Public: Checks if endorsement are enabled in this step.

Returns true if enabled, false otherwise.

Returns:

  • (Boolean)


20
21
22
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 20

def endorsements_enabled?
  current_settings.endorsements_enabled
end

#fully_endorsed?(proposal, user) ⇒ Boolean

Public: Checks if the given Proposal has been endorsed by all identities of the user.

Parameters:

  • proposal:

    The Proposal from which endorsements will be checked against.

  • user:

    The user whose identities and endorsements will be checked against.

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 79

def fully_endorsed?(proposal, user)
  return false unless user

  user_group_endorsements = Decidim::UserGroups::ManageableUserGroups.for(user).verified.all? { |user_group| proposal.endorsed_by?(user, user_group) }

  user_group_endorsements && proposal.endorsed_by?(user)
end

#render_endorsement_identity(proposal, user, user_group = nil) ⇒ Object

Public: Renders an identity for endorsement.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 92

def render_endorsement_identity(proposal, user, user_group = nil)
  current_endorsement_url = proposal_proposal_endorsement_path(
    proposal_id: proposal,
    from_proposals_list: false,
    user_group_id: user_group&.id,
    authenticity_token: form_authenticity_token
  )
  presenter = if user_group
                Decidim::UserGroupPresenter.new(user_group)
              else
                Decidim::UserPresenter.new(user)
              end
  selected = proposal.endorsed_by?(user, user_group)
  http_method = selected ? :delete : :post
  render partial: "decidim/proposals/proposal_endorsements/identity", locals:
  { identity: presenter, selected: selected, current_endorsement_url: current_endorsement_url, http_method: http_method }
end

#render_endorsements_button_card_part(proposal, fully_endorsed, html_class = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 127

def render_endorsements_button_card_part(proposal, fully_endorsed, html_class = nil)
  endorse_translated = t("decidim.proposals.proposal_endorsements_helper.render_endorsements_button_card_part.endorse")
  html_class = "card__button button" if html_class.blank?
  if current_settings.endorsements_blocked? || !current_component.participatory_space.can_participate?(current_user)
     :span, endorse_translated, class: "#{html_class} #{endorsement_button_classes(false)} disabled", disabled: true, title: endorse_translated
  elsif current_user && allowed_to?(:endorse, :proposal, proposal: proposal)
    render partial: "endorsement_identities_cabin", locals: { proposal: proposal, fully_endorsed: fully_endorsed }
  elsif current_user
    button_to(endorse_translated, proposal_path(proposal),
              data: { open: "authorizationModal", "open-url": modal_path(:endorse, proposal) },
              class: "#{html_class} #{endorsement_button_classes(false)} secondary")
  else
    action_authorized_button_to :endorse, endorse_translated, "", resource: proposal, class: "#{html_class} #{endorsement_button_classes(false)} secondary"
  end
end

#render_endorsements_count_card_part(proposal, fully_endorsed, html_class = nil) ⇒ Object

Renders the counter of endorsements that appears in card at show Propoal.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 111

def render_endorsements_count_card_part(proposal, fully_endorsed, html_class = nil)
  content = icon("bullhorn", class: "icon--small", aria_label: "Endorsements", role: "img")
  content += proposal.proposal_endorsements_count.to_s
  html_class = "button small compact light button--sc button--shadow" if html_class.blank?
  tag_params = { id: "proposal-#{proposal.id}-endorsements-count", class: "#{html_class} #{fully_endorsed ? "success" : "secondary"}" }
  if proposal.proposal_endorsements_count.positive?
    link_to "#list-of-endorsements", tag_params do
      content
    end
  else
    ("div", tag_params) do
      content
    end
  end
end

#show_endorsements_card?Boolean

Public: Checks if the card for endorsements should be rendered.

Returns true if the endorsements card should be rendered, false otherwise.

Returns:

  • (Boolean)


41
42
43
# File 'app/helpers/decidim/proposals/proposal_endorsements_helper.rb', line 41

def show_endorsements_card?
  endorsements_enabled?
end