summaryrefslogtreecommitdiff
path: root/app/helpers/link_helper.rb
blob: 21e4da5972c8a9cb477b743ff8ffcf1f34c69597 (plain)
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
module LinkHelper

  def content_path_helper path_array
    url_for(
      :controller => :content,
      :action => :render_page,
      :locale => (params[:locale] || I18n.locale).to_sym == I18n.default_locale ? nil : (params[:locale] || I18n.locale),
      :page_path => path_array
    )
  end

  def content_url_helper path_array
    request.protocol + request.host_with_port + content_path_helper(path_array)
  end

  def link_to_path title, path, html_options = {}
    return "" if path.nil?

    if params[:page_path]
      page_path = params[:page_path].is_a?(Array) ? params[:page_path].join("/") : params[:page_path]
      active = (page_path == path.sub(/^\//, ""))
    end

    html_options = html_options.merge(
      :class => [html_options[:class], active ? "active" : "inactive"].compact.join(" ")
    )
    locale = (params[:locale] || I18n.locale).to_sym == I18n.default_locale ? nil : (params[:locale] || I18n.locale)

    link_to(
      title,
      content_path(path.sub(/^\//, ""), :locale => locale),
      html_options
    )
  end

  def selected? controller_name
    if params[:controller] == controller_name
      return :class => "selected"
    end
  end

  def unlock_link
    message = t("admin.common.unlock_confirm",
                :login => @node.lock_owner.login,
                :time  => @page.updated_at.to_fs(:db))
    button_to 'Unlock', unlock_node_path(@node),
      method: :put,
      form: { data: { confirm: message }, class: 'button_to state_changing' }
  end

  def content_path(page_path = nil, options = {})
    if page_path.is_a?(Hash)
      options = page_path
      page_path = options.delete(:page_path)
    end
    locale = options[:locale] || params[:locale] || I18n.locale
    options[:locale] = (locale.to_sym == I18n.default_locale) ? nil : locale
    Rails.application.routes.url_helpers.content_path(
      Array(page_path).join("/").sub(/^\//, ""),
      options
    )
  end
end