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 HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::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, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.counts_by_stateObject



62
63
64
# File 'app/models/customer_relations/organization.rb', line 62

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

.default_state_countsObject



68
69
70
71
72
# File 'app/models/customer_relations/organization.rb', line 68

def self.default_state_counts
  states.keys.index_with do |key|
    0
  end
end

.find_by_name(group_id, name) ⇒ Object



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

def self.find_by_name(group_id, name)
  where(group: group_id)
  .where('LOWER(name) = LOWER(?)', name)
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.



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

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

.search_by_state(state) ⇒ Object



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

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

.sort_by_field(field, direction) ⇒ Object



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

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

.sort_by_nameObject



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

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