Module: ApplicationHelper

Included in:
MarkdownBody
Defined in:
app/helpers/application_helper.rb

Constant Summary collapse

ALLOW_TAGS =
%w(p br img h1 h2 h3 h4 h5 h6 blockquote pre code b i
strong em table tr td tbody th strike del u a ul ol li span hr)
ALLOW_ATTRIBUTES =
%w(href src class width height id title alt target rel data-floor)
EMPTY_STRING =
''.freeze
MOBILE_USER_AGENTS =
'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' \
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' \
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' \
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' \
'webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
LANGUAGES_LISTS =

可按需修改

{ 'Ruby' => 'ruby', 'HTML / ERB' => 'erb', 'CSS / SCSS' => 'scss', 'JavaScript' => 'js',
'YAML <i>(.yml)</i>' => 'yml', 'CoffeeScript' => 'coffee', 'Nginx / Redis <i>(.conf)</i>' => 'conf',
'Python' => 'python', 'PHP' => 'php', 'Java' => 'java', 'Erlang' => 'erlang', 'Shell / Bash' => 'shell' }

Instance Method Summary collapse

Instance Method Details

#admin?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
54
# File 'app/helpers/application_helper.rb', line 51

def admin?(user = nil)
  user ||= current_user
  user.try(:admin?)
end

#birthday_tagObject


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/application_helper.rb', line 116

def birthday_tag
  t = Time.now
  return '' unless t.month == 10 && t.day == 28
  age = t.year - 2011
  title = "Ruby China 创立 #{age} 周年纪念日"
  html = []
  html << "<div style='text-align:center;margin-bottom:20px; line-height:200%;'>"
  %w(dancers beers cake birthday crown gift crown birthday cake beers dancers).each do |name|
    html << image_tag(asset_path("assets/emojis/#{name}.png"), class: 'emoji', title: title)
  end
  html << '<br />'
  html << title
  html << '</div>'
  raw html.join(' ')
end

#cache(name = {}, options = {}, &block) ⇒ Object

Override cache helper for support multiple I18n locale


171
172
173
174
# File 'app/helpers/application_helper.rb', line 171

def cache(name = {}, options = {}, &block)
  options ||= {}
  super([I18n.locale, name], options, &block)
end

#cached_asset_path(name) ⇒ Object


164
165
166
167
168
# File 'app/helpers/application_helper.rb', line 164

def cached_asset_path(name)
  memory_cache('asset_path', name) do
    asset_path(name)
  end
end

#controller_javascript_include_tagObject


39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/application_helper.rb', line 39

def controller_javascript_include_tag
  fname = ''
  case controller_name
  when 'pages', 'topics', 'notes'
    fname = "#{controller_name}.js"
  when 'replies'
    fname = 'topics.js'
  end
  return '' if fname.blank?
  raw %(<script src="#{asset_path(fname)}" data-turbolinks-track></script>)
end

27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/application_helper.rb', line 27

def controller_stylesheet_link_tag
  fname = ''
  case controller_name
  when 'users', 'home', 'topics', 'pages', 'notes'
    fname = "#{controller_name}.css"
  when 'replies'
    fname = 'topics.css'
  end
  return '' if fname.blank?
  raw %(<link href="#{asset_path(fname)}" rel="stylesheet" data-turbolinks-track />)
end

#highlight(text) ⇒ Object


192
193
194
195
196
197
198
# File 'app/helpers/application_helper.rb', line 192

def highlight(text)
  text = escape_once(text)
  text.gsub!('[h]', '<em>')
  text.gsub!('[/h]', '</em>')
  text.gsub!(/\\n|\\r/, '')
  raw text
end

#icon_tag(name, opts = {}) ⇒ Object


138
139
140
141
142
143
144
# File 'app/helpers/application_helper.rb', line 138

def icon_tag(name, opts = {})
  label = EMPTY_STRING
  if opts[:label]
    label = %(<span>#{opts[:label]}</span>)
  end
  raw "<i class='fa fa-#{name}'></i> #{label}"
end

#insert_code_menu_items_tagObject


106
107
108
109
110
111
112
113
114
# File 'app/helpers/application_helper.rb', line 106

def insert_code_menu_items_tag
  lang_list = []
  LANGUAGES_LISTS.each do |k, l|
    lang_list << (:li) do
      link_to raw(k), '#', data: { lang: l }
    end
  end
  raw lang_list.join(EMPTY_STRING)
end

#javascript_include_tag_with_cached(name) ⇒ Object


158
159
160
161
162
# File 'app/helpers/application_helper.rb', line 158

def javascript_include_tag_with_cached(name)
  memory_cache('javascript_include_tag', name) do
    javascript_include_tag(name, 'data-turbolinks-track' => true)
  end
end

#memory_cache(*keys) ⇒ Object


146
147
148
149
150
# File 'app/helpers/application_helper.rb', line 146

def memory_cache(*keys)
  return yield unless Rails.application.config.cache_classes

  $memory_store.fetch(keys) { yield }
end

#mobile?Boolean

Returns:

  • (Boolean)

95
96
97
98
99
# File 'app/helpers/application_helper.rb', line 95

def mobile?
  agent_str = request.user_agent.to_s.downcase
  return false if agent_str =~ /ipad/
  agent_str =~ Regexp.new(MOBILE_USER_AGENTS)
end

#notice_messageObject


14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/application_helper.rb', line 14

def notice_message
  flash_messages = []

  flash.each do |type, message|
    type = :success if type.to_sym == :notice
    type = :danger if type.to_sym == :alert
    text = (:div, link_to('x', '#', :class => 'close', 'data-dismiss' => 'alert') + message, class: "alert alert-#{type}")
    flash_messages << text if message
  end

  flash_messages.join("\n").html_safe
end

#owner?(item) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
64
65
66
67
68
# File 'app/helpers/application_helper.rb', line 61

def owner?(item)
  return false if item.blank? || current_user.blank?
  if item.is_a?(User)
    item.id == current_user.id
  else
    item.user_id == current_user.id
  end
end

#random_tipsObject


132
133
134
135
136
# File 'app/helpers/application_helper.rb', line 132

def random_tips
  tips = Setting.tips
  return EMPTY_STRING if tips.blank?
  tips.split("\n").sample
end

#render_list(opts = {}) {|list| ... } ⇒ Object

Yields:

  • (list)

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/helpers/application_helper.rb', line 176

def render_list(opts = {})
  list = []
  yield(list)
  items = []
  list.each do |link|
    item_class = EMPTY_STRING
    urls = link.match(/href=(["'])(.*?)(\1)/) || []
    url = urls.length > 2 ? urls[2] : nil
    if url && current_page?(url) || (@current && @current.include?(url))
      item_class = 'active'
    end
    items << ('li', raw(link), class: item_class)
  end
  ('ul', raw(items.join(EMPTY_STRING)), opts)
end

#render_page_titleObject


76
77
78
79
80
# File 'app/helpers/application_helper.rb', line 76

def render_page_title
  site_name = Setting.app_name
  title = @page_title ? "#{@page_title} &raquo; #{site_name}" : site_name
  ('title', title, nil, false)
end

#sanitize_markdown(body) ⇒ Object


9
10
11
12
# File 'app/helpers/application_helper.rb', line 9

def sanitize_markdown(body)
  # TODO: This method slow, 3.5ms per call in topic body
  sanitize(body, tags: ALLOW_TAGS, attributes: ALLOW_ATTRIBUTES)
end

#spaceless(&block) ⇒ Object

去除区域里面的内容的换行标记


83
84
85
86
87
88
# File 'app/helpers/application_helper.rb', line 83

def spaceless(&block)
  data = with_output_buffer(&block)
  data = data.gsub(/\n\s+/, EMPTY_STRING)
  data = data.gsub(/>\s+</, '><')
  sanitize data
end

152
153
154
155
156
# File 'app/helpers/application_helper.rb', line 152

def stylesheet_link_tag_with_cached(name)
  memory_cache('stylesheets_link_tag', name) do
    stylesheet_link_tag(name, 'data-turbolinks-track' => true)
  end
end

#timeago(time, options = {}) ⇒ Object


70
71
72
73
74
# File 'app/helpers/application_helper.rb', line 70

def timeago(time, options = {})
  options[:class] = options[:class].blank? ? 'timeago' : [options[:class], 'timeago'].join(' ')
  options.merge!(title: time.iso8601)
  (:abbr, EMPTY_STRING, class: options[:class], title: time.iso8601) if time
end

#wiki_editor?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
59
# File 'app/helpers/application_helper.rb', line 56

def wiki_editor?(user = nil)
  user ||= current_user
  user.try(:wiki_editor?)
end