Class: GeoLabels::Contact

Inherits:
ApplicationRecord show all
Extended by:
Geocoder::Model::ActiveRecord
Includes:
RatingsSupport
Defined in:
app/models/geo_labels/contact.rb

Constant Summary collapse

RANSACKABLE_ATTRIBUTES =
%w[
  city country created_at department description
  id latitude longitude name state street
  subsection updated_at
].freeze
RANSACKABLE_ASSOCIATIONS =
%w[contact_labels labels].freeze
STATE_OPTIONS =
%w[approved recommended rejected].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RatingsSupport

#rating

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes

Class Method Details

.for_label_ids(label_ids, predication: 'and', states: ['approved']) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/geo_labels/contact.rb', line 30

def self.for_label_ids(label_ids, predication: 'and', states: ['approved'])
  # predication = %w[and or].include?(predication) ? predication : 'and' #whitlelisting
  if predication == 'or'
    labels = Label.find(label_ids)
    labels_scope = labels.shift.self_and_descendants
    labels.each do |label|
      labels_scope = labels_scope.or(label.self_and_descendants)
    end
    # joins(:labels).merge(Label.descendants_for_ids(label_ids)).distinct
    joins(:labels).merge(labels_scope).where(state: states).reorder('').distinct
  else
    where(id: ContactLabel.contact_ids_for_label_ids(label_ids), state: states)
  end
end

Instance Method Details

#addressObject



49
50
51
# File 'app/models/geo_labels/contact.rb', line 49

def address
  [street, city, department, country].map(&:presence).compact.join(', ')
end

#address_changed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/geo_labels/contact.rb', line 53

def address_changed?
  street_changed? or city_changed? or department_changed? or country_changed?
end

#geocode_if_necessaryObject



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

def geocode_if_necessary
  return if new_record? and latitude.present? and longitude.present?
  return unless address_changed?

  self.latitude = nil
  self.longitude = nil
  # Geocode addresses with at least 2 commas
  geocode if address.count(',') > 1
end

#label_ids=(array_or_string) ⇒ Object



45
46
47
# File 'app/models/geo_labels/contact.rb', line 45

def label_ids=(array_or_string)
  super array_or_string.is_a?(Array) ? array_or_string : array_or_string.split(',')
end

#lat_lng=(value) ⇒ Object



99
100
101
102
103
104
# File 'app/models/geo_labels/contact.rb', line 99

def lat_lng=(value)
  case value
  when String then self.latitude, self.longitude = value.split(/,\s?/)
  when Array then self.latitude, self.longitude = value
  end
end

#map_attributesObject



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

def map_attributes
  return {} unless geocoded?

  {
    lat: latitude.try(:to_f),
    lng: longitude.try(:to_f),
    title: map_title
  }
end

#map_titleObject

ransacker :full_name do |parent|

Arel::Nodes::InfixOperation.new(
  '||',
  Arel::Nodes::InfixOperation.new('||', parent.table[:first_name], parent.table[:middle_name]),
  parent.table[:last_name]
)

end



75
76
77
# File 'app/models/geo_labels/contact.rb', line 75

def map_title
  name
end