Module: JapMagWidgetsHelper

Defined in:
app/helpers/jap_mag_widgets_helper.rb

Instance Method Summary collapse

Instance Method Details

#bootstrap_button_group(checkbox, id = nil, status = nil, default = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/jap_mag_widgets_helper.rb', line 80

def bootstrap_button_group checkbox, id = nil, status = nil, default = nil
  status ||= %w(turned_on turned_off)
  default ||= "turned_off"

   :div, "id" => id, "class" => "bootstrap-toggle" do
    buttons =  :div, "class" => "btn-group", "data-toggle" => "buttons-radio" do
      status.collect do |s|
         :button, "type" => "button", "class" => "btn btn-default #{s}#{" active" if s == default}", "data-status" => s do
          _("/page.my.pac.status.#{s}")
        end
      end.join.html_safe
    end

    buttons + checkbox
  end
end

#bootstrap_menu(links) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/jap_mag_widgets_helper.rb', line 49

def bootstrap_menu links
  links.collect do |link|
    klass = []
    klass << link[:klass] if link[:klass]
    klass << "last-child" if link == links.last

    if (link[:controller_action].is_a?(Array) and current_controller_action_in?(*link[:controller_action])) \
      or (current_controller_action_in?(link[:controller_action]))
      klass << "active"
    end

    klass = klass.empty? ? nil : klass.join(" ")

     :li, class: klass do
      link_to link[:text], link[:path], link[:html_options]
    end
  end.join.html_safe
end

#bootstrap_scope_button(scopes, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/jap_mag_widgets_helper.rb', line 68

def bootstrap_scope_button scopes, options = {}
   :ul, class: "nav nav-tabs" do
    scopes.collect do |scope|
      url = eval("#{options[:path].to_s}(scope: :#{scope[:key]})")
      current = ((params[:scope] == scope[:key].to_s) or (scope[:default] and scope[:default] == true and params[:scope].blank?))
      link = link_to("#{(:span, scope[:text])} (#{scope[:count]})".html_safe, url)

      concat (:li, link, class: (current ? :active : nil))
    end
  end
end

#bootstrap_stacked_progress_bar(percent, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/jap_mag_widgets_helper.rb', line 97

def bootstrap_stacked_progress_bar percent, options = {}
  klass = if percent < 50
            :success
          elsif percent >= 50 and percent < 80
            :warning
          else
            :danger
          end

  #percent = "#{percent * 100}%"
  options[:precision] ||= 2
  percent = number_to_percentage percent, options

  opts_for_container = {class: :progress}
  if options[:tooltip]
    placement = options[:tooltip][:placement] || "top"
    opts_for_container.merge!("data-toggle" => "tooltip", "data-placement" => placement, title: options[:tooltip][:title])
  end

   :div, opts_for_container do
     :div, class: "progress-bar progress-bar-#{klass}", role: :progressbar, style: "width: #{percent}" do
      percent
    end
  end
end

#cta(text, url, options = {}) ⇒ Object

call to action



141
142
143
144
145
146
147
# File 'app/helpers/jap_mag_widgets_helper.rb', line 141

def cta text, url, options = {}
  klass = %w(button button-rounded button-caution)
  klass << options[:class] if options[:class].present?
  options[:class] = klass.join(" ")

  link_to text, url, options
end

#cta_params(options = {}) ⇒ Object



149
150
151
# File 'app/helpers/jap_mag_widgets_helper.rb', line 149

def cta_params options = {}
  {data: {disable_with: _("/actions.wait")}, class: "button button-rounded button-caution"}.merge options
end

#get_body_classObject



7
8
9
10
11
12
13
# File 'app/helpers/jap_mag_widgets_helper.rb', line 7

def get_body_class
  c = []
  c << controller.controller_name
  c << "mobile" if is_mobile_device?

  c.collect{|e| e.titleize.gsub(/\s/, "")}.join " "
end

#get_body_idObject



15
16
17
# File 'app/helpers/jap_mag_widgets_helper.rb', line 15

def get_body_id
  content_for(:id).to_s.titleize.gsub(/\s/, "") unless content_for(:id).blank?
end

#get_wrapper_classObject



19
20
21
22
23
24
# File 'app/helpers/jap_mag_widgets_helper.rb', line 19

def get_wrapper_class
  c = []
  c << controller.action_name

  c.collect{|e| e.titleize.gsub(/\s/, "")}.join " "
end

#input_for_selection(text) ⇒ Object



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

def input_for_selection text
  ( :input, nil, value: text, size: text.length, class: "form-input for-selection").html_safe
end

#is_mobile_device?Boolean

Returns:

  • (Boolean)


2
3
4
5
# File 'app/helpers/jap_mag_widgets_helper.rb', line 2

def is_mobile_device?
  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'
  request.user_agent.to_s.downcase =~ Regexp.new(mobile_user_agents)
end

#last_deployed_atObject



44
45
46
47
# File 'app/helpers/jap_mag_widgets_helper.rb', line 44

def last_deployed_at
  file = File.join("tmp", "restart.txt")
  File.exists?(file) ? File.atime(file) : nil
end


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

def link_to_external text, link, options = {}
  options.merge!(target: :_blank)

  if options[:anonymous]
    link = "http://anonym.to/?#{link}"
    options.delete(:anonymous)
  end

  link_to "#{text}#{fa_icon "external-link-alt", type: :fas}".html_safe, link, options
end

#long_date(date) ⇒ Object



153
154
155
# File 'app/helpers/jap_mag_widgets_helper.rb', line 153

def long_date date
  I18n.l date, format: :long
end

#paginator(collections, options = {}) ⇒ Object



40
41
42
# File 'app/helpers/jap_mag_widgets_helper.rb', line 40

def paginator collections, options = {}
  will_paginate collections, options
end

#retina_image_tag(name_at_1x, options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/helpers/jap_mag_widgets_helper.rb', line 162

def retina_image_tag name_at_1x, options = {}
  # webp for Chrome
  if options[:webp]
    name_at_1x = name_at_1x.gsub /\.[a-zA-Z]+$/, '.webp'

    options.delete :webp
  end

  # retina
  name_at_2x = name_at_1x.gsub /\.\w+$/, "@2x\\0"

  # i18n
  if options[:i18n]
    name_at_1x = name_at_1x.gsub /\.[a-zA-Z]+$/, ".#{I18n.locale}\\0"
    name_at_2x = name_at_2x.gsub /\.[a-zA-Z]+$/, ".#{I18n.locale}\\0"

    options.delete :i18n
  end

  # HTML 5 specific tag attributes
  srcset = "#{asset_path(name_at_1x)} 1x, #{asset_path(name_at_2x)} 2x"
  options = options.merge(srcset: srcset)

  image_tag name_at_1x, options
end

#short_date(date) ⇒ Object



158
159
160
# File 'app/helpers/jap_mag_widgets_helper.rb', line 158

def short_date date
  I18n.l date, format: :short
end

#title(*titles) ⇒ Object



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

def title *titles
  seperator = " - "

  default_options = {sitename: _("/logo")}
  options = titles.last.is_a?(Hash) ? titles.pop : {}
  options = default_options.merge(options)
  page_title = page_title_for_return = titles.join(seperator)
  page_title = options[:sitename] + seperator + page_title_for_return if not options[:sitename].blank?

  content_for :title, page_title

  page_title_for_return
end