Module: Userswitch

Defined in:
lib/userswitch.rb,
lib/userswitch/engine.rb,
lib/userswitch/railtie.rb,
lib/userswitch/version.rb,
app/controllers/userswitch/switch_controller.rb

Defined Under Namespace

Classes: Engine, Railtie, SwitchController

Constant Summary collapse

VERSION =
"0.1.9"

Class Method Summary collapse

Class Method Details

.btn_color(color) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/userswitch.rb', line 37

def btn_color(color)
  case color
  when 'red'
    'btn-outline-danger'
  when 'dark-blue'
    'btn-outline-primary'
  when 'light-blue'
    'btn-outline-info'
  when 'green'
    'btn-outline-success'
  when 'yellow'
    'btn-outline-warning'
  when 'gray'
    'btn-outline-basic'
  when 'white'
    'btn-outline-default'
  else
    'btn-outline-primary'
  end
end

.create_button_from_user(current_user_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/userswitch.rb', line 14

def create_button_from_user(current_user_id)
  ceo_html = ''
  switch_users_yml = initialize_switch_users
  raise 'config/userswitch.yml is empty' unless switch_users_yml
  roles = switch_users_yml['Roles']
  users = switch_users_yml['SwitchUsers']
  raise 'UserSwitch no roles defined in config/userswitch.yml' if roles.nil?
  raise 'UserSwitch no users defined in config/userswitch.yml' if users.nil?

  users.each do |user|
    role = user.last['role']
    raise "#{user.first} has no role" if role.nil?
    color = btn_color(roles[role])
    id = user.last['id']
    raise "UserSwitch #{user.first} has no id" if id.nil?
    color.gsub!('-outline', '') if id == current_user_id
    name = user.last['name']
    raise "UserSwitch #{user.first} has no name" if name.nil?
    ceo_html += "<a href=\"/change-user/#{id}&#{role}\" class=\"btn #{id} #{color}\">#{name}</a> \n"
  end
  ceo_html
end

.initialize_switch_usersObject



6
7
8
9
10
11
12
# File 'lib/userswitch.rb', line 6

def initialize_switch_users
  # if File.directory?('config/userswitch.yml')
    YAML.load_file('config/userswitch.yml')
  # else
  #   raise 'Havent created config/userswitch.yml file try running rake userswitch:install'
  # end
end

.users(current_user) ⇒ Object



58
59
60
# File 'lib/userswitch.rb', line 58

def users(current_user)
  create_button_from_user(current_user.try(:id)).html_safe
end