-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
author_cell.rb
164 lines (126 loc) · 4.09 KB
/
author_cell.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# frozen_string_literal: true
module Decidim
# This cell renders the author of a resource. It is intended to be used
# below resource titles to indicate its authorship & such, and is intended
# for resources that have a single author.
class AuthorCell < Decidim::ViewModel
include CellsHelper
include ::Devise::Controllers::Helpers
include ::Devise::Controllers::UrlHelpers
include Messaging::ConversationHelper
include Decidim::FollowableHelper
include ERB::Util
LAYOUTS = [:default, :compact, :avatar].freeze
property :profile_path
property :can_be_contacted?
property :has_tooltip?
delegate :current_user, to: :controller, prefix: false
def author_name
options[:author_name_text] || model.name
end
def display_name
model.deleted? ? t("decidim.profile.deleted") : decidim_sanitize(author_name)
end
def flag_user
render unless current_user == model
end
def perform_caching?
true
end
def raw_model
model.try(:__getobj__) || model
end
def context_actions_options
return unless options.has_key?(:context_actions)
return [] if options[:context_actions].blank?
@context_actions_options ||= options[:context_actions].map(&:to_sym)
end
private
def layout
@layout ||= LAYOUTS.include?(options[:layout]) ? options[:layout] : :default
end
def show_icons?
layout != :compact
end
def context_actions
actions = [].tap do |list|
list << :date if creation_date?
list << :cancelled_on if cancelable?
list << :comments if commentable?
list << :endorsements if endorsable?
list << :withdraw if withdrawable?
end
return actions unless has_context_actions_options?
actions & context_actions_options
end
def has_context_actions_options?
context_actions_options.is_a?(Array)
end
def cache_hash
hash = []
hash.push(I18n.locale)
hash.push(model.cache_key_with_version) if model.respond_to?(:cache_key_with_version)
hash.push(from_context.cache_key_with_version) if from_context.respond_to?(:cache_key_with_version)
hash.push(current_user.try(:id))
hash.push(current_user.present?)
hash.push(commentable?)
hash.push(endorsable?)
hash.push(actionable?)
hash.push(withdrawable?)
hash.push(profile_path?)
hash.push(layout)
hash.push(context_actions.join)
hash.join(Decidim.cache_key_separator)
end
def from_context_path
resource_locator(from_context).path
end
def withdraw_path
return decidim.withdraw_amend_path(from_context.amendment) if from_context.emendation?
"#{from_context_path}/withdraw"
end
def creation_date?
return unless from_context
return unless from_context.respond_to?(:published_at) || from_context.respond_to?(:created_at)
true
end
def creation_date
date_at = from_context.try(:published_at) || from_context.try(:created_at)
l date_at, format: :decidim_short
end
def cancelled_on_date
date_at = from_context.try(:cancelled_on)
l date_at, format: :decidim_short
end
def commentable?
from_context && from_context.class.include?(Decidim::Comments::Commentable)
end
def cancelable?
from_context && from_context.respond_to?(:cancelled_on)
end
def endorsable?
from_context && from_context.class.include?(Decidim::Endorsable)
end
def actionable?
return options[:has_actions] if options.has_key?(:has_actions)
withdrawable?
end
def user_author?
"Decidim::UserPresenter".include?(model.class.to_s)
end
def profile_path?
return false if options[:skip_profile_link] == true
profile_path.present?
end
def resource_i18n_scope
@resource_i18n_scope ||= [
from_context.class.name.deconstantize.underscore.gsub("/", "."),
resource_name.pluralize,
:show
].join(".")
end
def resource_name
@resource_name ||= from_context.class.name.demodulize.underscore
end
end
end