Module: Models::EmployeeSelectOptions::ClassMethods

Defined in:
app/concerns/models/employee_select_options.rb

Overview

Class-level select-option helpers.

Instance Method Summary collapse

Instance Method Details

#activity_assignment_options_for_selectArray<Array(String, Integer)>

Builds select options of active employees for activity assignment.

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



60
61
62
# File 'app/concerns/models/employee_select_options.rb', line 60

def activity_assignment_options_for_select
  active_employees.sorted.pluck(:full_name, :id)
end

#all_active_employees_sms_numbers(rep_ids: nil, format_for_select: false) ⇒ Array<String>, Array<Array(String, String)>

Returns distinct formatted SMS numbers for active employees, optionally
scoped to specific reps and formatted as select options.

Parameters:

  • rep_ids (Array<Integer>, nil) (defaults to: nil)

    optional employee ids to scope to

  • format_for_select (Boolean) (defaults to: false)

    whether to return select-option pairs

Returns:

  • (Array<String>, Array<Array(String, String)>)

    formatted SMS numbers,
    or pairs of "number - name" label and number when format_for_select is true



162
163
164
165
166
167
168
169
170
171
# File 'app/concerns/models/employee_select_options.rb', line 162

def all_active_employees_sms_numbers(rep_ids: nil, format_for_select: false)
  employees = Employee.active_employees
  employees = employees.where(id: rep_ids) if rep_ids.present?
  contact_points = ContactPoint.joins(:party).merge(employees).company_sms_numbers.order(:detail)
  if format_for_select
    contact_points.map { |cp| ["#{cp.formatted_for_sms} - #{cp.party.full_name}", cp.formatted_for_sms] }.uniq
  else
    contact_points.map(&:formatted_for_sms).uniq
  end
end

#assigned_sales_rep_options_for_select(role_types: %i[primary_sales_rep_id secondary_sales_rep_id local_sales_rep_id],, customer_ids: nil) ⇒ Array<Array(String, Integer)>

Builds select options of employees currently assigned as a sales rep on
any customer, for the given rep role columns.

Parameters:

  • role_types (Array<Symbol>) (defaults to: %i[primary_sales_rep_id secondary_sales_rep_id local_sales_rep_id],)

    customer columns holding rep employee ids

  • customer_ids (Array<Integer>, nil) (defaults to: nil)

    optional customer ids to scope to

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



85
86
87
88
89
90
91
92
# File 'app/concerns/models/employee_select_options.rb', line 85

def assigned_sales_rep_options_for_select(role_types: %i[primary_sales_rep_id secondary_sales_rep_id local_sales_rep_id], customer_ids: nil)
  rep_ids = role_types.flat_map do |r|
    customers = Customer.where.not(r => nil)
    customers = customers.where(id: customer_ids) if customer_ids.present?
    customers.pluck("distinct #{r}")
  end.uniq
  Employee.where(id: rep_ids).order(:full_name).pluck(:full_name, :id)
end

#blog_author_options_for_selectArray<Array(String, Integer, nil)>

Builds select options of employees allowed to author blog posts,
prepended with a "No Author" blank option.

Returns:

  • (Array<Array(String, Integer, nil)>)

    pairs of full name and employee id



68
69
70
# File 'app/concerns/models/employee_select_options.rb', line 68

def blog_author_options_for_select
  [['No Author', nil]] + blog_employees.sorted.pluck(:full_name, :id)
end

#customer_service_managerEmployee?

Finds the employee holding the customer_service_manager role.

Returns:

  • (Employee, nil)

    the customer service manager, if any



151
152
153
# File 'app/concerns/models/employee_select_options.rb', line 151

def customer_service_manager
  Employee.joins(account: :roles).where(Role[:name].eq('customer_service_manager')).first
end

#engineering_rep_options_for_select(show_working_status: false) ⇒ Array<Array(String, Integer)>

Builds select options of active engineering reps.

Parameters:

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



136
137
138
# File 'app/concerns/models/employee_select_options.rb', line 136

def engineering_rep_options_for_select(show_working_status: false)
  format_for_select(engineers.active_employees.sorted, show_working_status:)
end

#for_sales_rep_assignment_queue(_store, role) ⇒ ActiveRecord::Relation<Employee>

Returns active employees whose employee record flags them for the given
role, used by the sales rep assignment queue.

Parameters:

  • _store (Object)

    unused store context

  • role (String, Symbol)

    employee_records boolean column naming the role

Returns:

  • (ActiveRecord::Relation<Employee>)

    matching active employees, sorted



53
54
55
# File 'app/concerns/models/employee_select_options.rb', line 53

def for_sales_rep_assignment_queue(_store, role)
  active_employees.joins(:employee_record).where(employee_records: { role.to_sym => true }).sorted
end

#format_for_select(reps, show_working_status: false) ⇒ Array<Array(String, Integer)>

Formats a collection of reps into select options, optionally appending
each rep's working status for the current day to the label.

Parameters:

  • reps (Enumerable<Employee>)

    the reps to format

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



100
101
102
103
104
105
106
# File 'app/concerns/models/employee_select_options.rb', line 100

def format_for_select(reps, show_working_status: false)
  reps.map do |r|
    rep_name = r.full_name
    rep_name = "#{rep_name} (#{r.work_status_on_day(Date.current).to_s.humanize})" if show_working_status
    [rep_name, r.id]
  end
end

#local_sales_rep_options_for_select(show_working_status: false) ⇒ Array<Array(String, Integer)>

Builds select options of active local sales reps.

Parameters:

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



128
129
130
# File 'app/concerns/models/employee_select_options.rb', line 128

def local_sales_rep_options_for_select(show_working_status: false)
  format_for_select(local_sales_reps.active_employees.sorted, show_working_status:)
end

#managers_select_optionsArray<Array(String, Integer)>

Builds select options of active employees eligible to be managers.

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



12
13
14
# File 'app/concerns/models/employee_select_options.rb', line 12

def managers_select_options
  active_employees.sorted.pluck(:full_name, :id)
end

#phone_employees_select_optionsArray<Array(String, Integer)>

Builds select options of active employees who have a phone status.

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



30
31
32
# File 'app/concerns/models/employee_select_options.rb', line 30

def phone_employees_select_options
  active_employees.joins(:employee_phone_status).sorted.pluck(:full_name, :id)
end

#primary_sales_rep_options_for_select(show_working_status: false) ⇒ Array<Array(String, Integer)>

Builds select options of active primary sales reps.

Parameters:

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



112
113
114
# File 'app/concerns/models/employee_select_options.rb', line 112

def primary_sales_rep_options_for_select(show_working_status: false)
  format_for_select(primary_sales_reps.active_employees.sorted, show_working_status:)
end

#sales_rep_options_for_selectArray<Array(String, Integer)>

Builds select options of active sales reps.

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



75
76
77
# File 'app/concerns/models/employee_select_options.rb', line 75

def sales_rep_options_for_select
  sales_reps.active_employees.sorted.pluck(:full_name, :id)
end

#secondary_sales_rep_options_for_select(show_working_status: false) ⇒ Array<Array(String, Integer)>

Builds select options of active secondary sales reps.

Parameters:

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



120
121
122
# File 'app/concerns/models/employee_select_options.rb', line 120

def secondary_sales_rep_options_for_select(show_working_status: false)
  format_for_select(secondary_sales_reps.active_employees.sorted, show_working_status:)
end

#select_options(exclude_ids: [], active_only: true) ⇒ Array<Array(String, Integer)>

Builds select options of employees, optionally excluding ids and
limiting to active employees.

Parameters:

  • exclude_ids (Array<Integer>) (defaults to: [])

    employee ids to exclude

  • active_only (Boolean) (defaults to: true)

    whether to limit to active employees

Returns:

  • (Array<Array(String, Integer)>)

    pairs of full name and employee id



22
23
24
25
# File 'app/concerns/models/employee_select_options.rb', line 22

def select_options(exclude_ids: [], active_only: true)
  scope = active_only ? active_employees : Employee.all
  scope.where.not(id: exclude_ids).sorted.pluck(:full_name, :id)
end

#sms_enabled_for_selectArray<Array(String, Integer)>

Builds select options of active employees with an SMS contact point,
including their formatted phone number in the label.

Returns:

  • (Array<Array(String, Integer)>)

    pairs of "name - phone" label and employee id



38
39
40
41
42
43
44
45
# File 'app/concerns/models/employee_select_options.rb', line 38

def sms_enabled_for_select
  active_employees
    .joins(:employee_phone_status)
    .where.not(employee_phone_statuses: { contact_point_id: nil })
    .includes(employee_phone_status: :contact_point)
    .sorted
    .map { |e| ["#{e.full_name} - #{PhoneNumber.parse_and_format(e.pbx_did, display_format: :crm)}", e.id] }
end

#technical_support_rep_options_for_select(show_working_status: false) ⇒ Array<Array(String, Integer)>

Builds select options of active technical support reps.

Parameters:

  • show_working_status (Boolean) (defaults to: false)

    whether to append today's work status

Returns:

  • (Array<Array(String, Integer)>)

    pairs of label and employee id



144
145
146
# File 'app/concerns/models/employee_select_options.rb', line 144

def technical_support_rep_options_for_select(show_working_status: false)
  format_for_select(technical_support_reps.active_employees.sorted, show_working_status:)
end