Class: SettingsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/settings_service.rb

Overview

app/services/settings_service.rb

Class Method Summary collapse

Class Method Details

.only_member?(user) ⇒ Boolean

This method checks if the user has no active roles other than member

Returns:

  • (Boolean)

    whether the user has no other active roles



49
50
51
52
# File 'app/services/settings_service.rb', line 49

def self.only_member?(user)
  roles = user.roles
  roles.length() == 1
end

.remove_role(user, role, game = "") ⇒ nil

This method removes a role from the users active roles

Parameters:

  • user (Integer)

    the id of the user

  • role (String)

    the role being checked

  • game (String) (defaults to: "")

    the game tied to the role (if any)

Returns:

  • (nil)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/settings_service.rb', line 28

def self.remove_role(user, role, game = "")
  settings = Settings.find_by(user_id: user.id)
  return unless settings&.active_roles.present?

  current_roles = settings.active_roles.split(",").map(&:strip)

  if !game.present?
    settings.active_roles = current_roles.reject { |r| r == role }.join(",")
  elsif game == "any"
    role_regex = /\A#{Regexp.escape(role)}-/
    settings.active_roles = current_roles.reject { |r| r.match?(role_regex) }.join(",")
  else
    role_game_pair = "#{role}-#{game}"
    settings.active_roles = current_roles.reject { |r| r == role_game_pair }.join(",")
  end

  settings.save
end

.role_exists?(user, role, game = "") ⇒ Boolean

This method checks if a user has a particular active role

Parameters:

  • user (Integer)

    the id of the user

  • role (String)

    the role being checked

  • game (String) (defaults to: "")

    the game tied to the role (if any)

Returns:

  • (Boolean)

    whether the user has the active role



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/settings_service.rb', line 8

def self.role_exists?(user, role, game = "")
  settings = Settings.find_by(user_id: user.id)
  return false unless settings&.active_roles.present?

  role_regex = if game.present?
                 /\A#{Regexp.escape(role)}-#{Regexp.escape(game.name)}\z/
  else
                 /\A#{Regexp.escape(role)}\z/
  end

  settings.active_roles.split(",").map(&:strip).any? do |active_role|
    active_role.match?(role_regex)
  end
end