Class: CustomerRelations::Organization

Inherits:
ApplicationRecord show all
Includes:
Gitlab::SQL::Pattern, Sortable, StripAttribute
Defined in:
app/models/customer_relations/organization.rb

Constant Summary

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_TERM

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Methods included from StripAttribute

#strip_attributes!

Methods included from Gitlab::SQL::Pattern

split_query_to_search_terms

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.counts_by_stateObject



81
82
83
# File 'app/models/customer_relations/organization.rb', line 81

def self.counts_by_state
  default_state_counts.merge(group(:state).count)
end

.find_by_name(group_id, name) ⇒ Object



56
57
58
59
# File 'app/models/customer_relations/organization.rb', line 56

def self.find_by_name(group_id, name)
  where(group: group_id)
  .where('LOWER(name) = LOWER(?)', name)
end

.move_to_root_group(group) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/customer_relations/organization.rb', line 61

def self.move_to_root_group(group)
  update_query = <<~SQL
    UPDATE #{CustomerRelations::Contact.table_name}
    SET organization_id = new_organizations.id
    FROM #{table_name} AS existing_organizations
    JOIN #{table_name} AS new_organizations ON new_organizations.group_id = :old_group_id AND LOWER(new_organizations.name) = LOWER(existing_organizations.name)
    WHERE existing_organizations.group_id = :new_group_id AND organization_id = existing_organizations.id
  SQL
  connection.execute(sanitize_sql([update_query, old_group_id: group.root_ancestor.id, new_group_id: group.id]))

  dupes_query = <<~SQL
    DELETE FROM #{table_name} AS existing_organizations
    USING #{table_name} AS new_organizations
    WHERE existing_organizations.group_id = :new_group_id AND new_organizations.group_id = :old_group_id AND LOWER(new_organizations.name) = LOWER(existing_organizations.name)
  SQL
  connection.execute(sanitize_sql([dupes_query, old_group_id: group.root_ancestor.id, new_group_id: group.id]))

  where(group: group).update_all(group_id: group.root_ancestor.id)
end

.search(query) ⇒ Object

Searches for organizations with a matching name or description.

This method uses ILIKE on PostgreSQL

query - The search query as a String

Returns an ActiveRecord::Relation.



36
37
38
# File 'app/models/customer_relations/organization.rb', line 36

def self.search(query)
  fuzzy_search(query, [:name, :description], use_minimum_char_limit: false)
end

.search_by_state(state) ⇒ Object



40
41
42
# File 'app/models/customer_relations/organization.rb', line 40

def self.search_by_state(state)
  where(state: state)
end

.sort_by_field(field, direction) ⇒ Object



44
45
46
47
48
49
50
# File 'app/models/customer_relations/organization.rb', line 44

def self.sort_by_field(field, direction)
  if direction == :asc
    order_scope_asc(field)
  else
    order_scope_desc(field)
  end
end

.sort_by_nameObject



52
53
54
# File 'app/models/customer_relations/organization.rb', line 52

def self.sort_by_name
  order(name: :asc)
end