/
application_helper.rb
78 lines (65 loc) · 2.07 KB
/
application_helper.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
module ApplicationHelper
def home?
request.original_url == root_url
end
def import_bootstrap?
action_name == "new" and ["votes", "spokesmen"].include?(controller_name)
end
def menu_link(text, path, icon='')
content_tag :li, class: current_page_css(path) do
link_to path, class: "nav-link" do
content_tag(:span, '', class: icon) +
text
end
end
end
def nav_link(resource, path, index)
content_tag :li, class: current_page_css(path) do
link_to path, :class => "aside-nav-item name tag-#{index + 1}", :title => resource.name do
short_name(resource) +
content_tag(:span, class: "block-list__count") do
resource.proposals_count.to_s
end
end
end
end
def current_page_css(path)
current_page?(path) ? 'selected' : ''
end
def short_name(resource)
truncate(resource.name, :length => 30)
end
def hot_categories
@hot_categories ||= Category.hot
end
def hot_proposers
@proposers ||= Proposer.hot
end
# Awesome truncate
# First regex truncates to the length, plus the rest of that word, if any.
# Second regex removes any trailing whitespace or punctuation (except ;).
# Unlike the regular truncate method, this avoids the problem with cutting
# in the middle of an entity ex.: truncate("this & that",9) => "this &am..."
# though it will not be the exact length.
def awesome_truncate(text, length = 30, truncate_string = "...")
return if text.nil?
l = length - truncate_string.mb_chars.length
text.mb_chars.length > length ? text[/\A.{#{l}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
end
def humanize(choice)
t(choice)
end
def who_did_vote(proposal)
if proposal.votes.blank?
t(:be_first)
else
pluralize(proposal.votes.count, t(:one_did_it), t(:many_did_it))
end
end
def vote_or_delegation_count
users_page? ? User.sum(:represented_users_count) : Vote.count
end
def vote_or_delegation_key
users_page? ? :delegations_through_agora : :votes_through_agora
end
end