Class: Customer

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/customer.rb

Instance Method Summary collapse

Instance Method Details

#as_jsonObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/customer.rb', line 31

def as_json 
  user = User.find_by(id: self.user_id)
  {
    authentication_token: user.authentication_token,
    email:                user.email,
    customer_id:          self.id,
    phone_number:         self.phone_number,
    phone_verified:       self.phone_verified,
    first_name:           self.first_name,
    last_name:            self.last_name,
    inital_contact:       self.inital_contact,
    language_1:           self.preferred_language_id_1,
    language_2:           self.preferred_language_id_2
  }
end

#as_json2Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/customer.rb', line 47

def as_json2 
  user = User.find_by(id: self.user_id)
  {
    authentication_token: user.authentication_token,
    email:                user.email,
    customer_id:          self.id,
    phone_number:         self.phone_number,
    phone_verified:       self.phone_verified,
    first_name:           self.first_name,
    last_name:            self.last_name,
    inital_contact:       self.inital_contact,
    language_1:           self.language1.try(:name),
    language_2:           self.language2.try(:name),
  }
end

#get_rating_information(agent_id) ⇒ Object

Get rating information



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/customer.rb', line 64

def get_rating_information(agent_id)
  ratings = Rating.where(agent_id: agent_id)
  numCustomerRates = ratings.distinct.count(:customer_id)
  avgAll = ratings.average('rate').to_f
  avgCategory = ratings.joins(:rating_question).group('category').average('rate').map{|category, rate| {category: category, rate: rate}}
  ratings = ratings.where(customer_id: self.id)
  avgQuestion = ratings.group('rating_question_id').average('rate').map{|question, rate| {question: question, rate: rate}}

  rating_date = ratings.joins(:rating_question).select('category, CAST(MAX(ratings.updated_at) as date) as date').group('category')

  {numCustomerRates: numCustomerRates, avgAllRates: avgAll, avgCategory: avgCategory, avgQuestion: avgQuestion, rating_date: rating_date}
end

#nameObject

end



25
26
27
28
29
# File 'app/models/customer.rb', line 25

def name
  if first_name.present? && last_name.present?
    "#{first_name} #{last_name}"
  end
end