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 url 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

.to_csvObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/geo_labels/contact.rb', line 45

def self.to_csv
  require 'csv'
  contacts = includes(:labels).all
  additional_attributes = %w[description state street subsection city department country url latitude longitude]
  CSV.generate do |csv|
    csv << %w[name labels] + additional_attributes
    contacts.each do |contact|
      csv << [contact.name, contact.labels.map(&:name).compact.sort.join('|')] +
        contact.attributes.fetch_values(*additional_attributes)
    end
  end
end

Instance Method Details

#addressObject



63
64
65
# File 'app/models/geo_labels/contact.rb', line 63

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

#address_changed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/geo_labels/contact.rb', line 67

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

#geocode_if_necessaryObject



103
104
105
106
107
108
109
110
111
# File 'app/models/geo_labels/contact.rb', line 103

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



59
60
61
# File 'app/models/geo_labels/contact.rb', line 59

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



113
114
115
116
117
118
# File 'app/models/geo_labels/contact.rb', line 113

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

#map_attributesObject



93
94
95
96
97
98
99
100
101
# File 'app/models/geo_labels/contact.rb', line 93

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



89
90
91
# File 'app/models/geo_labels/contact.rb', line 89

def map_title
  name
end