Class: Decidim::ActivityCell

Inherits:
ViewModel
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, Cell::ViewModel::Partial, ApplicationHelper, IconHelper, SanitizeHelper
Defined in:
decidim-core/app/cells/decidim/activity_cell.rb

Overview

This cell is used to render public activities performed by users.

Each model that we want to represent should inherit from this cell and tweak the necessary methods (usually `title` is enough).

Constant Summary

Constants included from IconHelper

IconHelper::DEFAULT_RESOURCE_TYPE_ICONS

Instance Method Summary collapse

Methods included from IconHelper

#component_icon, #manifest_icon, #resource_icon, #resource_type_icon, #resource_type_icon_key

Instance Method Details

#cache_hashObject


94
95
96
97
98
99
100
101
102
103
104
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 94

def cache_hash
  hash = []
  hash << I18n.locale.to_s
  hash << model.class.name.underscore
  hash << model.cache_key_with_version
  if (author_cell = author)
    hash.push(Digest::MD5.hexdigest(author_cell.send(:cache_hash)))
  end

  hash.join(Decidim.cache_key_separator)
end

#created_atObject


75
76
77
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 75

def created_at
  t("decidim.activity.time_ago", time: time_ago_in_words(model.created_at))
end

#descriptionObject

The description to show at the card.

The card will also be displayed OK if there's no description.


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 51

def description
  resource_description = resource.try(:resource_description) || resource.try(:description)
  return if resource_description.blank?

  resource_description = case resource_description
                         when String
                           resource_description
                         when Hash
                           translated_attribute(resource_description)
                         end

  truncate(strip_tags(resource_description), length: 300)
end

#element_idObject


90
91
92
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 90

def element_id
  "action-#{model.id}"
end

#renderable?Boolean

Since activity logs could be linked to resource no longer available this method is added in order to skip rendering a cell if there's not enough data.

Returns:

  • (Boolean)

24
25
26
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 24

def renderable?
  resource.present? && participatory_space.present? && published?
end

#resourceObject

The resource linked to the activity.


29
30
31
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 29

def resource
  model.resource_lazy
end

The link to the resource linked to the activity.


66
67
68
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 66

def resource_link_path
  resource_locator(resource).path
end

The text to show as the link to the resource.


71
72
73
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 71

def resource_link_text
  decidim_html_escape(translated_attribute(resource.title))
end

#showObject


15
16
17
18
19
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 15

def show
  return unless renderable?

  render
end

#titleObject

The title to show at the card.

The card will also be displayed OK if there's no title.


36
37
38
39
40
41
42
43
44
45
46
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 36

def title
  resource_title = resource.try(:resource_title) || resource.try(:title)
  return if resource_title.blank?

  case resource_title
  when String
    resource_title
  when Hash
    translated_attribute(resource_title)
  end
end

#userObject


79
80
81
82
83
84
85
86
# File 'decidim-core/app/cells/decidim/activity_cell.rb', line 79

def user
  return resource.normalized_author if resource.respond_to?(:normalized_author)
  return resource.author if resource.respond_to?(:author)
  # As Proposals have Coauthorable concern instead of Authorable
  return resource.identities.first if resource.respond_to?(:identities)

  model.user_lazy if resource.respond_to?(:user)
end