Module: DeviceTracker::ApplicationHelper

Defined in:
lib/device_tracker/helpers/application_helper.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/device_tracker/helpers/application_helper.rb', line 62

def admin?
  user = logged_in_user
  return if user.nil?
  user[:is_admin]
end

#admin_checkObject



12
13
14
15
16
17
18
19
# File 'lib/device_tracker/helpers/application_helper.rb', line 12

def admin_check
  if admin?
    true
  else
    flash_and_go_back 'warning',
                      ['You don\'t have permission to access this page.']
  end
end

#admin_email_registration(request, user) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/device_tracker/helpers/application_helper.rb', line 35

def admin_email_registration(request, user)
  @user = user
  @verification_link = "#{request.base_url}/users/#{user.id}/edit"

  body = ERB.new(
    File.read(settings.email_path + '/registration.erb')
  ).result(binding)

  send_email(
    to: [User.where(is_admin: true).map(& :email)],
    subject: 'New Registration | Device Tracker',
    body: body
  )
end

#create_flash(type, message) ⇒ Object



100
101
102
# File 'lib/device_tracker/helpers/application_helper.rb', line 100

def create_flash(type, message)
  flash[:message] = { css_class: type, message: message }
end

#flash_and_go_back(type, message) ⇒ Object



104
105
106
107
# File 'lib/device_tracker/helpers/application_helper.rb', line 104

def flash_and_go_back(type, message)
  create_flash(type, message)
  redirect back
end

#generate_activation_code(size = 6) ⇒ Object



76
77
78
79
# File 'lib/device_tracker/helpers/application_helper.rb', line 76

def generate_activation_code(size = 6)
  charset = %w(0 1 2 3 4 6 7 8 9 A C D E F G H J K M N P Q R T V W X Y Z)
  (0...size).map { charset.to_a[rand(charset.size)] }.join
end

#permission?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'lib/device_tracker/helpers/application_helper.rb', line 4

def permission?(user_id)
  if logged_in_user[:is_admin] != true &&
     logged_in_user[:id] != user_id.to_i
    return true
  end
  false
end

#protected!Object



81
82
83
84
85
86
87
# File 'lib/device_tracker/helpers/application_helper.rb', line 81

def protected!
  if logged_in_user.nil?
    create_flash 'info', ['You must be logged in to access this page.']
    redirect '/login'
  end
  true
end

#report_transaction(message, type, device = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/device_tracker/helpers/application_helper.rb', line 89

def report_transaction(message, type, device = nil)
  transaction = Transaction.new
  transaction.user_id = logged_in_user[:id] if session[:user]

  transaction.description = message
  transaction.transaction_type = type
  transaction.device_id = device.id unless device.nil?

  transaction.save!
end

#send_email(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/device_tracker/helpers/application_helper.rb', line 50

def send_email(data)
  return if Sinatra::Base.test?
  Pony.mail(
    to: data[:to],
    from: data[:from] || 'no-reply@device-tracker',
    subject: data[:subject],
    html_body: data[:body]
  )
rescue Net::OpenTimeout
  puts ">>> Net::OpenTimeout exception for [#{subject}]"
end

#valid_heartbeat?(data) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/device_tracker/helpers/application_helper.rb', line 109

def valid_heartbeat?(data)
  return false if data['heartbeat'].nil?
  heartbeat = data['heartbeat']
  return false if heartbeat['device_id'].nil? ||
                  heartbeat['longitude'].nil? || heartbeat['latitude'].nil?
  true
end

#value_for(name, object) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/device_tracker/helpers/application_helper.rb', line 68

def value_for(name, object)
  if flash[name]
    flash[name]
  elsif object
    object.send name
  end
end

#verification_email(user) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/device_tracker/helpers/application_helper.rb', line 21

def verification_email(user)
  @user = user
  @get_started_link = request.base_url + '/devices'
  body = ERB.new(
    File.read(settings.email_path + '/verification.erb')
  ).result(binding)

  send_email(
    to: @user.email,
    subject: 'Account Verified | Device Tracker',
    body: body
  )
end