Module: RoundRobinAssignment

Defined in:
lib/round_robin_assignment.rb,
lib/round_robin_assignment/model.rb,
lib/round_robin_assignment/version.rb,
lib/generators/round_robin_assignment/install/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Error, Model

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.get_next_assignee(group_name, assignee_ids) ⇒ Integer?

Returns the next assignee ID in the round-robin rotation

Parameters:

  • group_name (String)

    The assignment group identifier

  • assignee_ids (Array<Integer>)

    Array of assignee IDs to rotate through

Returns:

  • (Integer, nil)

    The next assignee ID, or nil if assignee_ids is empty



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/round_robin_assignment.rb', line 14

def get_next_assignee(group_name, assignee_ids)
  return nil if assignee_ids.nil? || assignee_ids.empty?

  # Sort for consistency
  sorted_ids = assignee_ids.sort

  # Find or create the assignment record
  assignment = Model.find_or_initialize_by(assignment_group: group_name)

  # Determine the next assignee
  next_assignee_id = if assignment.new_record?
    # First assignment - start from beginning
    sorted_ids.first
  elsif !sorted_ids.include?(assignment.last_assigned_user_id)
    # Last assignee no longer in list - find next logical assignee
    # Find the next ID that would have been after the removed one
    next_id = sorted_ids.find { |id| id > assignment.last_assigned_user_id }
    next_id || sorted_ids.first
  else
    # Get the next ID in rotation
    current_index = sorted_ids.index(assignment.last_assigned_user_id)
    next_index = (current_index + 1) % sorted_ids.length
    sorted_ids[next_index]
  end

  # Update the assignment record
  assignment.last_assigned_user_id = next_assignee_id
  assignment.last_assigned_at = Time.current
  assignment.assignment_count = (assignment.assignment_count || 0) + 1
  assignment.save!

  next_assignee_id
end

.group_stats(group_name) ⇒ Hash?

Returns statistics for an assignment group

Parameters:

  • group_name (String)

    The assignment group to get stats for

Returns:

  • (Hash, nil)

    Statistics hash or nil if group doesn’t exist



59
60
61
62
63
64
65
66
67
68
# File 'lib/round_robin_assignment.rb', line 59

def group_stats(group_name)
  assignment = Model.find_by(assignment_group: group_name)
  return nil unless assignment

  {
    last_assigned_user_id: assignment.last_assigned_user_id,
    last_assigned_at: assignment.last_assigned_at,
    total_assignments: assignment.assignment_count
  }
end

.method_missing(method, *args, &block) ⇒ Object

Delegate ActiveRecord methods to Model



71
72
73
74
75
76
77
# File 'lib/round_robin_assignment.rb', line 71

def method_missing(method, *args, &block)
  if Model.respond_to?(method)
    Model.public_send(method, *args, &block)
  else
    super
  end
end

.reset_group(group_name) ⇒ Boolean

Resets the assignment history for a group

Parameters:

  • group_name (String)

    The assignment group to reset

Returns:

  • (Boolean)

    True if the group was found and deleted



51
52
53
54
# File 'lib/round_robin_assignment.rb', line 51

def reset_group(group_name)
  Model.where(assignment_group: group_name).destroy_all
  true
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/round_robin_assignment.rb', line 79

def respond_to_missing?(method, include_private = false)
  Model.respond_to?(method) || super
end