Module: ApplicationHelper

Includes:
SanitizeUrl
Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#about_licenses(licenses) ⇒ Object


115
116
117
# File 'app/helpers/application_helper.rb', line 115

def about_licenses(licenses)
  licenses.compact.delete_if(&:empty?).map{|l| format_license(l) }.join(' or ')
end

#cp(path) ⇒ Object


286
287
288
# File 'app/helpers/application_helper.rb', line 286

def cp(path)
  "active" if current_page?(path)
end

#dependency_platform(platform_string) ⇒ Object


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

def dependency_platform(platform_string)
  return platform_string if platform_string.nil?
  case platform_string.downcase
  when 'rubygemslockfile'
    'rubygems'
  when 'cocoapodslockfile'
    'cocoapods'
  when 'nugetlockfile', 'nuspec'
    'nuget'
  when 'packagistlockfile'
    'packagist'
  when 'gemspec'
    'rubygems'
  when 'npmshrinkwrap'
    'npm'
  else
    platform_string.downcase
  end
end

#description(page_description) ⇒ Object


106
107
108
# File 'app/helpers/application_helper.rb', line 106

def description(page_description)
  content_for(:description) { truncate(page_description, length: 160) }
end

#documentation_url(name, platform, version = nil) ⇒ Object


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

def documentation_url(name, platform, version = nil)
  case platform
  when 'Rubygems'
    "http://www.rubydoc.info/gems/#{name}/#{version}"
  when 'Go'
    "http://godoc.org/#{name}"
  when 'Pub'
    "http://www.dartdocs.org/documentation/#{name}/#{version}"
  when 'CRAN'
    "http://cran.r-project.org/web/packages/#{name}/#{name}.pdf"
  when 'Hex'
    "http://hexdocs.pm/#{name}/#{version}"
  when 'CocoaPods'
    "http://cocoadocs.org/docsets/#{name}/#{version}"
  end
end

#download_url(name, platform, version = nil) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/application_helper.rb', line 21

def download_url(name, platform, version = nil)
  case platform
  when 'Rubygems'
    "https://rubygems.org/downloads/#{name}-#{version}.gem"
  when 'Atom'
    "https://www.atom.io/api/packages/#{name}/versions/#{version}/tarball"
  when 'Cargo'
    "https://crates.io/api/v1/crates/#{name}/#{version}/download"
  when 'CRAN'
    "https://cran.r-project.org/src/contrib/#{name}_#{version}.tar.gz"
  when 'Emacs'
    "http://melpa.org/packages/#{name}-#{version}.tar"
  when 'Hackage'
    "http://hackage.haskell.org/package/#{name}-#{version}/#{name}-#{version}.tar.gz"
  end
end

#emojify(content) ⇒ Object


152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/application_helper.rb', line 152

def emojify(content)
  h(content).to_str.gsub(/:([\w+-]+):/) do |match|
    if emoji = Emoji.find_by_alias($1)
      %(<img alt="#$1" src="#{image_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
    else
      match
    end
  end.html_safe if content.present?
rescue
  content
end

#favicon(size) ⇒ Object


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

def favicon(size)
  libicon = "https://libicons.herokuapp.com/favicon.ico"
  @color ? "#{libicon}?hex=#{URI::escape(@color)}&size=#{size}" : "/favicon-#{size}.png"
end

#feature_flag(bool, negative = nil) ⇒ Object


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

def feature_flag(bool, negative = nil)
  icon_class = bool ? 'check' : 'times'
  color = bool ? 'green' : 'red'
  tag =  :i, '', class: "fa fa-#{icon_class}", style: "color:#{color}"
  !bool && negative ? (:i, negative) : tag
end

#format_language(language) ⇒ Object


143
144
145
146
# File 'app/helpers/application_helper.rb', line 143

def format_language(language)
  return nil if language.blank?
  Languages::Language[language].try(:to_s)
end

#format_license(license) ⇒ Object


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

def format_license(license)
  return 'Unknown' if license.blank?
  Project.format_license(license)
end

#github_user_title(user) ⇒ Object


200
201
202
203
204
205
206
# File 'app/helpers/application_helper.rb', line 200

def github_user_title(user)
  if user.name.present? && user.name.downcase != user..downcase
    "#{user.name} (#{user.})"
  else
    user.
  end
end

#install_instructions(project, platform, version = nil) ⇒ Object


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
# File 'app/helpers/application_helper.rb', line 55

def install_instructions(project, platform, version = nil)
  name = project.name
  case platform
  when 'Rubygems'
    "gem install #{name}" + (version ? " -v #{version}" : "")
  when 'NPM'
    "npm install #{name}" + (version ? "@#{version}" : "")
  when 'Jam'
    "jam install #{name}" + (version ? "@#{version}" : "")
  when 'Bower'
    "bower install #{name}" + (version ? "##{version}" : "")
  when 'Dub'
    "dub fetch #{name}" + (version ? " --version #{version}" : "")
  when 'Hackage'
    "cabal install #{name}" + (version ? "-#{version}" : "")
  when 'PyPi'
    "pip install #{name}" + (version ? "==#{version}" : "")
  when 'Atom'
    "apm install #{name}" + (version ? "@#{version}" : "")
  when 'Nimble'
    "nimble install #{name}" + (version ? "@##{version}" : "")
  when 'Go'
    "go get #{name}"
  when 'NuGet'
    "Install-Package #{name}" + (version ? " -Version #{version}" : "")
  when 'Meteor'
    "meteor add #{name}" + (version ? "@=#{version}" : "")
  when 'Elm'
    "elm-package install #{name} #{version}"
  when 'PlatformIO'
    "platformio lib install #{project.pm_id}"
  when 'Inqlude'
    "inqlude install #{name}"
  when 'Homebrew'
    "brew install #{name}"
  end
end

#linked_keywords(keywords) ⇒ Object


119
120
121
# File 'app/helpers/application_helper.rb', line 119

def linked_keywords(keywords)
  keywords.compact.delete_if(&:empty?).map{|k| link_to k, "/keywords/#{k}" }.join(', ').html_safe
end

#linked_licenses(licenses) ⇒ Object


110
111
112
113
# File 'app/helpers/application_helper.rb', line 110

def linked_licenses(licenses)
  return 'Unknown' if licenses.compact.empty?
  licenses.compact.delete_if(&:empty?).map{|l| link_to format_license(l), license_path(l) }.join('/').html_safe
end

17
18
19
# File 'app/helpers/application_helper.rb', line 17

def package_link(project, version = nil)
  Repositories::Base.package_link(project, version)
end

#platform_name(platform) ⇒ Object


123
124
125
126
127
128
129
130
131
# File 'app/helpers/application_helper.rb', line 123

def platform_name(platform)
  if platform.downcase == 'npm'
    return 'npm'
  elsif platform.downcase == 'wordpress'
    return 'WordPress'
  else
    return platform
  end
end

#project_description(project, version) ⇒ Object


208
209
210
211
212
# File 'app/helpers/application_helper.rb', line 208

def project_description(project, version)
  text = project.description || project.name
  text += " - #{version}" if version
  text += " - a #{project.language} library on #{project.platform_name} - Libraries.io"
end

#rss_url(project) ⇒ Object


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

def rss_url(project)
  if project.versions.count > 0
    project_versions_url({format: "atom"}.merge(project.to_param))
  elsif project.github_repository && project.github_tags.length > 0
    project_tags_url({format: "atom"}.merge(project.to_param))
  end
end

#sort_optionsObject


6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/application_helper.rb', line 6

def sort_options
  [
    ['Relevance', nil],
    ['SourceRank', 'rank'],
    ['GitHub Stars', 'stars'],
    ['Dependents', 'dependents_count'],
    ['Latest Release', 'latest_release_published_at'],
    ['Newest', 'created_at']
  ]
end

#source_path(github_repository) ⇒ Object


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

def source_path(github_repository)
  return nil unless github_repository.fork?
  if github_repository.source.present?
    github_repository_path(github_repository.source.owner_name, github_repository.source.project_name)
  else
    github_repository.source_url
  end
end

#source_rank_badge_class(value) ⇒ Object


232
233
234
235
236
237
238
239
240
# File 'app/helpers/application_helper.rb', line 232

def source_rank_badge_class(value)
  if value > 0
    'alert-success'
  elsif value < 0
    'alert-danger'
  else
    'alert-warning'
  end
end

#source_rank_explainationsObject


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/helpers/application_helper.rb', line 266

def source_rank_explainations
  {
    basic_info_present:         'Description, homepage/repository link and keywords present?',
    versions_present:           'Has the project had more than one release?',
    follows_semver:             'Every version has a valid SemVer number',
    recent_release:             'Within the past 6 months?',
    not_brand_new:              'Existed for at least 6 months',
    is_deprecated:              'Marked as deprecated by the maintainer',
    is_unmaintained:            'Marked as unmaintained by the maintainer',
    is_removed:                 'Removed from the package manager',
    all_prereleases:            'All versions are prerelease',
    any_outdated_dependencies:  'At least one dependency is behind the latest version',
    github_stars:               'Logarithmic scale',
    dependent_projects:         'Logarithmic scale times two',
    dependent_repositories:     'Logarithmic scale',
    contributors:               'Logarithmic scale divided by two',
    subscribers:                'Logarithmic scale divided by two'
  }
end

#source_rank_titlesObject


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/helpers/application_helper.rb', line 242

def source_rank_titles
  {
    basic_info_present:         'Basic info present?',
    repository_present:         'GitHub repository present?',
    readme_present:             'Readme present?',
    license_present:            'License present?',
    versions_present:           'Has multiple versions?',
    follows_semver:             'Follows SemVer?',
    recent_release:             'Recent release?',
    not_brand_new:              'Not brand new?',
    is_deprecated:              'Deprecated?',
    is_unmaintained:            'Unmaintained?',
    is_removed:                 'Removed?',
    any_outdated_dependencies:  'Outdated dependencies?',
    one_point_oh:               '1.0.0 or greater?',
    all_prereleases:            'Prerelease?',
    github_stars:               'GitHub stars',
    dependent_projects:         'Dependent Projects',
    dependent_repositories:     'Dependent Repositories',
    contributors:               'Contributors',
    subscribers:                'Libraries.io subscribers'
  }
end

#stats_for(title, records) ⇒ Object


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

def stats_for(title, records)
  render 'table', title: title, records: records
end

#title(page_title) ⇒ Object


101
102
103
104
# File 'app/helpers/application_helper.rb', line 101

def title(page_title)
  content_for(:title) { page_title }
  page_title
end

#truncate_with_tip(text, length) ⇒ Object


214
215
216
217
218
219
220
# File 'app/helpers/application_helper.rb', line 214

def truncate_with_tip(text, length)
  if text && text.length > length
    (:span, truncate(text, length: length), class: 'tip', title: text)
  else
    text
  end
end

#will_paginate(collection_or_options = nil, options = {}) ⇒ Object


222
223
224
225
226
227
228
229
230
# File 'app/helpers/application_helper.rb', line 222

def will_paginate(collection_or_options = nil, options = {})
  if collection_or_options.is_a? Hash
    options, collection_or_options = collection_or_options, nil
  end
  unless options[:renderer]
    options = options.merge :renderer => BootstrapPagination::Rails
  end
  super *[collection_or_options, options].compact
end