Module: JapMag

Defined in:
lib/jap_mag.rb,
lib/jap_mag/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/jap_mag.rb', line 78

def self.included base
  base.helper_method :_
  base.helper_method :current_controller_action_in?
  base.helper_method :current_locale
  base.helper_method :current_url
  base.helper_method :current_path
end

Instance Method Details

#_(key, options = {}) ⇒ Object

i18N



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/jap_mag.rb', line 3

def _ key, options={}
  # for absolute pathes /
  if (key[0] == "/")
    key = key.sub("/", "")

  # for all other cases
  else
    key = "#{params[:controller]}.#{params[:action]}.#{key}"
  end

  t(key, options)
end

#current_controller_action_in?(*args) ⇒ Boolean

the args can be one of the following:

String: “page#index” Array: %w(page#index page#intro) multiple arguments: “page#index”, “page#intro”

Returns:

  • (Boolean)


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
# File 'lib/jap_mag.rb', line 45

def current_controller_action_in?(*args)
  controller = params["controller"]
  action = params["action"]

  if args.size == 1
    if args.first.is_a?(Array)
      arguments = args.first.split(" ").first
    elsif args.first.is_a?(String)
      if args.first.split(" ").size > 1
        arguments = args.first.split(" ")
      else
        arguments = args
      end
    end
  else
    arguments = args
  end

  #raise arguments.inspect

  arguments.each do |element|
    if element.include?("#")
      array = element.match(/([a-z\-\_\/]*)#([a-z\-\_]*)/).to_a
      c, a = array[1], array[2]
      return true if controller == c && action == a
    else
      return true if controller == element
    end
  end

  false
end

#current_localeObject



16
17
18
# File 'lib/jap_mag.rb', line 16

def current_locale
  params[:locale] || I18n.default_locale || extract_locale_from_accept_language_header
end

#current_pathObject



30
31
32
# File 'lib/jap_mag.rb', line 30

def current_path
  URI.encode(request.fullpath)
end

#current_urlObject



34
35
36
# File 'lib/jap_mag.rb', line 34

def current_url
  URI.encode(request.original_url)
end

#extract_locale_from_accept_language_headerObject



20
21
22
23
24
25
26
27
28
# File 'lib/jap_mag.rb', line 20

def extract_locale_from_accept_language_header
  if request.env and request.env['HTTP_ACCEPT_LANGUAGE']
    lang = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  else
    lang = nil
  end

  lang == "zh" ? "zh-CN" : "en"
end