- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathprotips_helper.rb
142 lines (118 loc) · 3.6 KB
/
protips_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
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
moduleProtipsHelper
defprotips_view_breadcrumbs
@breadcrumbs ||= begin
breadcrumbs=[["Protips",trending_path]]
iftopic_name
breadcrumbs << [
t(Category.parent(params[:topic]),scope: [:categories,:long]),
url_for(topic: Category.parent(params[:topic]),order_by: :views_count)
]ifCategory.parent(params[:topic])
breadcrumbs << [topic_name,url_for(topic: params[:topic],order_by: :views_count)]
end
ifparams[:order_by] == :created_at
breadcrumbs << ["New",url_for(topic: params[:topic],order_by: params[:order_by])]
elsifparams[:order_by] == :score
breadcrumbs << ["Hot",url_for(topic: params[:topic],order_by: params[:order_by])]
end
breadcrumbs << ["Page #{params[:page]}",nil]ifparams[:page].to_i > 1
breadcrumbs=[]ifbreadcrumbs.size <= 1
breadcrumbs
end
end
defprotip_tweet_message
attribution=@protip.user.twitter ? "by @#{@protip.user.twitter}" : "via @coderwall"
CGI.escape"#{@protip.title}#{attribution}\n\n#{protip_url(@protip)}"
end
deffirst_page?
params[:page].to_i < 2
end
defon_fresh?
params[:order_by] == :created_at
end
defon_trending?
params[:order_by] == :score
end
defprotips_title
page_name=params[:page].to_i > 1 ? "Page #{params[:page]}" : nil
"#{protips_heading} - #{protips_list_type} Tips #{page_name}"
end
defprotips_heading
default=params[:topic] ? "#{protips_list_type}#{params[:topic].titleize} Programming Tips" : "#{protips_list_type} Programming Tips"
t(params[:topic],scope: [:categories,:long],default: default).html_safe
end
deftopic_short_name
returnnilifparams[:topic].blank?
t(params[:topic],scope: [:categories,:short])
end
defprotips_list_type
caseparams[:order_by].to_sym
when:created_atthen'Newest'
when:scorethen'Trending'
when:views_countthen'Popular'
end
end
defprotips_description
t(params[:topic],scope: [:categories,:descriptions],default: '')unlesson_fresh?
end
defprotips_popular_topic_path
ifparams[:topic]
popular_topic_path(topic: params[:topic])
else
popular_path
end
end
defprotips_fresh_topic_path
ifparams[:topic]
fresh_topic_path(topic: params[:topic])
else
fresh_path
end
end
defrecently_viewed_protips
protips=Protip.visible_to(current_user).recently_most_viewed
ifparams[:topic]
protips.with_any_tagged(topic_tags)
else
protips
end
end
defrecently_created_protips
protips=Protip.visible_to(current_user).recently_created
ifparams[:topic]
protips.with_any_tagged(topic_tags)
else
protips
end
end
deftopic_tags
tags=Category.children(params[:topic])
tags.empty? ? [params[:topic]] : tags
end
deftopic_name
ifparams[:topic]
ifCategory.parent(params[:topic])
"tagged #{params[:topic]}"
else
t(params[:topic],scope: [:categories,:long])
end
end
end
defhide_on_protip_exploration
return'hide'ifprotips_view_breadcrumbs.present?
end
defprotip_summary
tags=@protip.tags
tags << 'programming'iftags.empty?
"A protip by #{@protip.user.username} about #{tags.to_sentence}."
end
defsort_expiry
caseparams[:order_by]
when:scorethen5.minutes
when:views_countthen1.hour
else15.seconds
end
end
defprotip_list_cache_key
['v2','protips#index',params[:order_by],params[:topic],params[:page],current_user]
end
end