Class: Decidim::EndorsementButtonsCell

Inherits:
ViewModel
  • Object
show all
Includes:
CellsHelper, EndorsableHelper, LayoutHelper, ResourceHelper, SanitizeHelper
Defined in:
app/cells/decidim/endorsement_buttons_cell.rb

Overview

This cell renders the endrosement button and the endorsements count. It only supports one row of buttons per page due to current tag ids used by javascript.

Instance Method Summary collapse

Methods included from SanitizeHelper

#decidim_html_escape, #decidim_sanitize, #decidim_sanitize_admin, #decidim_sanitize_editor, #decidim_sanitize_editor_admin, #decidim_sanitize_newsletter, #decidim_url_escape, included

Methods included from ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #linked_resources_for, #resource_locator, #resource_title

Methods included from EndorsableHelper

#current_user_can_endorse?, #endorsement_buttons_cell, #endorsements_blocked?, #endorsements_enabled?, #endorsers_list_cell, #fully_endorsed?, #path_to_create_endorsement, #path_to_destroy_endorsement, #render_endorsement_identity, #show_endorsements_card?

Methods included from CellsHelper

#current_component, #flaggable?, #from_context, #index_action?, #show_action?, #user_flaggable?, #withdrawable?

Methods included from LayoutHelper

#_icon_classes, #apple_favicon, #application_path, #extended_navigation_bar, #external_icon, #favicon, #icon, #legacy_favicon, #organization_colors, #role

Methods inherited from ViewModel

#call, #current_user

Methods included from MarkupHelper

#element_id

Methods included from ReplaceButtonsHelper

#button_to, #submit_tag

Methods included from ActionAuthorizationHelper

#action_authorized_button_to, #action_authorized_link_to, #logged_button_to, #logged_link_to

Methods included from ScopesHelper

#has_visible_scopes?, #scope_name_for_picker, #scopes_picker_field, #scopes_picker_filter, #scopes_picker_tag

Methods included from TranslatableAttributes

#default_locale?

Methods included from DecidimFormHelper

#areas_for_select, #base_error_messages, #decidim_form_for, #decidim_form_slug_url, #editor_field_tag, #form_field_has_error?, #form_required_explanation, #name_with_locale, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Instance Method Details

#card_button_html_classObject



91
92
93
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 91

def card_button_html_class
  "card__button button"
end

#endorsement_button_classes(from_resourcess_list: false) ⇒ Object

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

from_resourcess_list - A boolean to indicate if the template is rendered from the list page of the resource.

Returns a string with the value of the css classes.



85
86
87
88
89
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 85

def endorsement_button_classes(from_resourcess_list: false)
  return "small" if from_resourcess_list

  "button small compact light button--sc expanded secondary"
end

#endorsement_identity_presenter(endorsement) ⇒ Object



157
158
159
160
161
162
163
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 157

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

#endorsement_path(*args) ⇒ Object

produce the path to an endorsement from the engine routes as the cell doesn’t have access to routes



153
154
155
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 153

def endorsement_path(*args)
  decidim.endorsement_path(*args)
end

#endorsements_path(*args) ⇒ Object

produce the path to endorsements from the engine routes as the cell doesn’t have access to routes



148
149
150
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 148

def endorsements_path(*args)
  decidim.endorsements_path(*args)
end

#render_disabled_endorsements_buttonObject

Renders the endorsements button but disabled. To be used to let the user know that endorsements are enabled but are blocked or cant participate.



68
69
70
71
72
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 68

def render_disabled_endorsements_button
   :span, class: "#{card_button_html_class} #{endorsement_button_classes(from_resourcess_list: false)} disabled", disabled: true, title: endorse_translated do
    endorse_translated + render_screen_reader_context_title
  end
end

#render_endorsements_buttonObject

Renders the “Endorse” button. Contains all the logic about how the button should be rendered and which actions the button must trigger.

It takes into account:

  • if endorsements are enabled

  • if users are logged in

  • if users can endorse with many identities (of their user_groups)

  • if users require verification



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 31

def render_endorsements_button
  if endorsements_blocked_or_user_can_not_participate?
    render_disabled_endorsements_button
  elsif !current_user
    
  elsif current_user_and_allowed?
    # Remove identities_cabin
    if user_has_verified_groups?
      render "select_identity_button"
    else
      render_user_identity_endorse_button
    end
  else # has current_user but is not allowed
    render_verification_modal
  end
end

#render_endorsements_countObject

Renders the counter of endorsements that appears in m-cards.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 49

def render_endorsements_count
  content = icon("bullhorn", class: "icon--small", aria_label: t("decidim.endorsable.endorsements_count"), role: "img")
  content += resource.endorsements_count.to_s
  html_class = "button small compact button--shadow secondary"
  html_class += " active" if fully_endorsed?(resource, current_user)
  tag_params = { id: "resource-#{resource.id}-endorsements-count", class: html_class }
  if resource.endorsements_count.positive?
    link_to "#list-of-endorsements", tag_params do
      content
    end
  else
    (:div, tag_params) do
      content
    end
  end
end

#render_screen_reader_context_titleObject



74
75
76
77
78
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 74

def render_screen_reader_context_title
   :span, class: "show-for-sr" do
    decidim_html_escape(resource_title(resource))
  end
end

#render_user_identity_endorse_buttonObject

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

This button may have different behaviours:

  • If the user is not logged in, the button will open the signin/signup popup.

  • If the user is logged in, and the resource has not been endorsed, the button will allow to endorse.

  • If the user is logged in, and the resource has already been endorsed, the button will allow to UNendorse.

Parameters:

resources  - The endorsable resource.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 105

def render_user_identity_endorse_button
  (:div, id: "resource-#{resource.id}-endorsement-button") do
    if !current_user
      
    elsif resource.endorsed_by?(current_user)
      unendorse_label = t("decidim.endorsement_buttons_cell.already_endorsed")
      destroy_endorsement_url = path_to_destroy_endorsement(resource)
      action_authorized_button_to(
        :endorse,
        destroy_endorsement_url,
        resource: resource,
        method: :delete,
        remote: true,
        class: "button #{endorsement_button_classes} active",
        id: "endorsement_button"
      ) do
        unendorse_label + render_screen_reader_context_title
      end
    else
      action_authorized_button_to(
        :endorse,
        path_to_create_endorsement(resource),
        resource: resource,
        remote: true,
        class: "button #{endorsement_button_classes}",
        id: "endorsement_button"
      ) do
        endorse_translated + render_screen_reader_context_title
      end
    end
  end
end

#resourceObject

The resource being un/endorsed is the Cell’s model.



139
140
141
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 139

def resource
  model
end

#reveal_identities_urlObject



143
144
145
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 143

def reveal_identities_url
  decidim.identities_endorsement_path(resource.to_gid.to_param)
end

#showObject



18
19
20
# File 'app/cells/decidim/endorsement_buttons_cell.rb', line 18

def show
  render
end