Class: CustomerRelations::Contact

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

Constant Summary collapse

SAFE_ATTRIBUTES =
%w[
  created_at
  description
  first_name
  group_id
  id
  last_name
  organization_id
  state
  updated_at
].freeze

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

Instance 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



127
128
129
# File 'app/models/customer_relations/contact.rb', line 127

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

.exists_for_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'app/models/customer_relations/contact.rb', line 121

def self.exists_for_group?(group)
  return false unless group

  exists?(group: group)
end

.find_ids_by_emails(group, emails) ⇒ Object

Raises:

  • (ArgumentError)


115
116
117
118
119
# File 'app/models/customer_relations/contact.rb', line 115

def self.find_ids_by_emails(group, emails)
  raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK

  where(group: group).where('lower(email) in (?)', emails.map(&:downcase)).pluck(:id)
end

.reference_postfixObject



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

def self.reference_postfix
  ']'
end

.reference_prefixObject



54
55
56
# File 'app/models/customer_relations/contact.rb', line 54

def self.reference_prefix
  '[contact:'
end

.reference_prefix_quotedObject



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

def self.reference_prefix_quoted
  '["contact:'
end

.search(query) ⇒ Object

Searches for contacts with a matching first name, last name, email or description.

This method uses ILIKE on PostgreSQL

query - The search query as a String

Returns an ActiveRecord::Relation.



73
74
75
# File 'app/models/customer_relations/contact.rb', line 73

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

.search_by_state(state) ⇒ Object



77
78
79
# File 'app/models/customer_relations/contact.rb', line 77

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

.sort_by_field(field, direction) ⇒ Object



81
82
83
84
85
86
87
# File 'app/models/customer_relations/contact.rb', line 81

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

.sort_by_nameObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/customer_relations/contact.rb', line 97

def self.sort_by_name
  order(Gitlab::Pagination::Keyset::Order.build(
    [
      Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
        attribute_name: 'last_name',
        order_expression: arel_table[:last_name].asc
      ),
      Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
        attribute_name: 'first_name',
        order_expression: arel_table[:first_name].asc
      ),
      Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
        attribute_name: 'id',
        order_expression: arel_table[:id].asc
      )
    ]))
end

.sort_by_organization(direction) ⇒ Object



89
90
91
92
93
94
95
# File 'app/models/customer_relations/contact.rb', line 89

def self.sort_by_organization(direction)
  if direction == :asc
    order_by_organization_asc
  else
    order_by_organization_desc
  end
end

Instance Method Details

#hook_attrsObject



50
51
52
# File 'app/models/customer_relations/contact.rb', line 50

def hook_attrs
  attributes.slice(*SAFE_ATTRIBUTES)
end