Module: WiserTimezone::WiserTimezoneHelper

Defined in:
app/helpers/wiser_timezone/wiser_timezone_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_timezoneObject



8
9
10
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 8

def current_timezone
  return ActiveSupport::TimeZone[offset.to_i]
end

#current_timezone_locationObject



18
19
20
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 18

def current_timezone_location
  return current_timezone.to_s.split(" ").last()
end

#current_timezone_offsetObject



22
23
24
25
26
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 22

def current_timezone_offset
  timezone_str = current_timezone.to_s
  offset_str = timezone_str[timezone_str.index('(')+1..-1].split(':').first()
  return "#{offset_str[0..3]}#{offset_str[4..-1].to_i}"
end

#current_timezone_offset_slimObject



28
29
30
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 28

def current_timezone_offset_slim
  current_timezone_offset[3..-1]
end

#current_timezone_slimObject



12
13
14
15
16
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 12

def current_timezone_slim
  location = current_timezone.to_s.split(" ").last()
  offset = current_timezone_offset
  return "(#{offset}) #{location}"
end

#ensure_timezoneObject



4
5
6
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 4

def ensure_timezone
  Time.zone = current_timezone
end

#wiser_timezone(date, date_only = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 32

def wiser_timezone(date, date_only = false)
  begin
    date_time = date.to_datetime
    if date_only
      return date_time.in_time_zone(current_timezone).beginning_of_day
    else
      return date_time.in_time_zone(current_timezone)
    end
  rescue
    raise "WiserTimezone can only accept date object."
  end
end

#wiser_timezone_initialize(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 49

def wiser_timezone_initialize(options = {})
  force = options[:force].present? && options[:force]
  if auto_set_all = options[:auto_set_all].present? && options[:auto_set_all]
    auto_set_all = true
  else
    auto_set_all = false
    auto_set_guest = options[:auto_set_guest].present? && options[:auto_set_guest]
  end

  set_link = link_to('click here', set_timezone_path, :id => 'wiser_timezone_link')
  close_link = link_to('skip', set_timezone_path(offset: 'skip'), :id => 'wiser_timezone_close', :remote => true)

  if offset.present?
    msg = "Your system's timezone does not match your current setting #{current_timezone_slim}, <span class='no_wrap'>#{set_link}</span> to update the timezone to ~TZ~.#{'<br/>' if force}Otherwise, #{close_link} setting your timezone."
  else
    msg = "You do not have timezone in your settings, <span class='no_wrap'>#{set_link}</span> to update the timezone to ~TZ~.#{'<br/>' if force}Otherwise, #{close_link} setting your timezone."
  end
  classes = "#{'force' if force} #{'no_user' if auto_set_guest} #{'auto_set' if auto_set_all}"
  space = "<div id='wiser_timezone_space' data-offset='#{offset}'>#{msg}</div>"
  cover = "<div id='wiser_timezone_cover'></div>"
  html = "<div id='wiser_timezone_container' class='#{classes}' style='display:none;' data-offset-cookie='#{cookies[:wiser_timezone_offset]}'>#{cover} #{space}</div>"
  return html.html_safe
end

#wt(date, date_only = false) ⇒ Object



45
46
47
# File 'app/helpers/wiser_timezone/wiser_timezone_helper.rb', line 45

def wt(date, date_only = false)
  wiser_timezone(date, date_only)
end