feature/share-libs
rb 99 lines 2.62 KB
Raw
1 module ApplicationHelper
2 def current_class?(*paths)
3 paths.each do |path|
4 return 'current-item' if request.path == path
5 end
6 end
7
8 def current_class_mobile?(path)
9 request.path.start_with?(path) ? 'selected' : ''
10 end
11
12 def production?
13 request.domain == 'wwww.musik88.com' || request.domain == 'musik88.com'
14 end
15
16 def link_to_host_locale(locale, path_with_locale)
17 path = path_with_locale.split(locale.to_s)[1]
18
19 path = '' if path.nil?
20
21 host_locale = ''
22 RouteTranslator.config.host_locales.each do |locale_array|
23 host_locale = locale_array[0] if locale_array[1].to_s == locale.to_s
24
25 host_locale + ":#{request.port}"
26 end
27 host_locale + path
28 end
29
30 def current_domain_locale_url(locale_code, params)
31 domains = domains_map
32 ## Loop through all locales and get the path
33 I18n.available_locales.each do |locale|
34 path = url_for(locale: locale.to_s, only_path: true, params: params)
35 unless locale == :en || path.start_with?('/users')
36 path = path.sub('/', '').split('/')[1..].join('/').insert(0,
37 '/')
38 end
39 host = domains[locale]
40 domains[locale] = "#{host}#{path}"
41 end
42
43 domains[locale_code]
44 end
45
46 CanonicalUrl = Struct.new(:url, :locale)
47
48 def canonical_urls
49 # The canonical url is en locale in .com domain
50 domain = if Rails.env.development?
51 "http://com.musik88:#{request.port}"
52 elsif Rails.env.production?
53 'https://www.musik88.com'
54 end
55
56 urls_with_locale = []
57
58 I18n.available_locales.each do |locale|
59 canonical_url = CanonicalUrl.new
60
61 path = url_for(locale: locale.to_s, only_path: true)
62
63 canonical_url.url = domain + path
64 canonical_url.locale = locale
65
66 urls_with_locale.push(canonical_url)
67 end
68
69 urls_with_locale
70 end
71
72 def name_or_username(user)
73 user.username.nil? ? user.name : "@#{user.username}"
74 end
75
76 def profile_path_enhanced(user)
77 profile_path(user.username || user.id)
78 end
79
80 def artist_friendly_url(artist)
81 "#{artist.name.downcase.parameterize}-#{artist.id}"
82 end
83
84 def album_friendly_url(album)
85 "#{album.name.downcase.parameterize}-#{album.artists[0].name.downcase.parameterize}-#{album.id}"
86 end
87
88 private
89
90 def domains_map
91 domains = {}
92 host = Rails.env.production? ? request.host : request.host_with_port
93 I18n.available_locales.each do |locale|
94 locale_suffix = "/#{locale}" unless locale == :en || request.path.start_with?('/users')
95 domains[locale] = "#{request.protocol}#{host}#{locale_suffix}"
96 end
97 domains
98 end
99 end