Class: Effective::MailchimpListMember

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

Instance Method Summary collapse

Instance Method Details

#all_mailchimp_interestsObject

From the mailchimp list



77
78
79
80
# File 'app/models/effective/mailchimp_list_member.rb', line 77

def all_mailchimp_interests
  return [] if mailchimp_list.blank?
  mailchimp_list.mailchimp_categories.flat_map(&:mailchimp_interests)
end

#assign_mailchimp_attributes(atts) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/effective/mailchimp_list_member.rb', line 82

def assign_mailchimp_attributes(atts)
  assign_attributes(
    mailchimp_id: atts['id'],
    web_id: atts['web_id'],
    email_address: atts['email_address'],
    full_name: atts['full_name'],
    subscribed: (atts['status'] == 'subscribed'),
    last_synced_at: Time.zone.now,
    interests: Hash(atts['interests']).select { |_, subscribed| subscribed == true }.keys
  )
end

#assign_mailchimp_cannot_be_subscribedObject



94
95
96
97
98
99
100
# File 'app/models/effective/mailchimp_list_member.rb', line 94

def assign_mailchimp_cannot_be_subscribed
  assign_attributes(
    subscribed: false,
    cannot_be_subscribed: true,
    last_synced_at: Time.zone.now
  )
end

#build_interests(mailchimp_interest) ⇒ Object

We use this to add the force_subscribed interests



62
63
64
65
66
67
# File 'app/models/effective/mailchimp_list_member.rb', line 62

def build_interests(mailchimp_interest)
  mailchimp_ids = Array(mailchimp_interest).map { |interest| interest.try(:mailchimp_id) || interest }
  raise('expected an array of MailchimpInterests or mailchimp_ids') unless mailchimp_ids.all? { |id| id.kind_of?(String) && id.length > 1 }

  assign_attributes(interests: interests | mailchimp_ids)
end

#emailObject



47
48
49
# File 'app/models/effective/mailchimp_list_member.rb', line 47

def email
  email_address.presence || user.email
end

#interestsObject

Array of MailchimpInterest mailchimp_ids



52
53
54
# File 'app/models/effective/mailchimp_list_member.rb', line 52

def interests
  Array(self[:interests]) - [nil, '']
end

#interests_hashObject

{“25a38426c9” => false, “9b826db370” => true }



70
71
72
73
74
# File 'app/models/effective/mailchimp_list_member.rb', line 70

def interests_hash
  all_mailchimp_interests.each_with_object({}) do |interest, hash|
    hash[interest.mailchimp_id] = interests.include?(interest.mailchimp_id)
  end
end

#mailchimp_interestsObject

Array of MailchimpInterests



57
58
59
# File 'app/models/effective/mailchimp_list_member.rb', line 57

def mailchimp_interests
  all_mailchimp_interests.select { |interest| interests.include?(interest.mailchimp_id) }
end

#synced?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/effective/mailchimp_list_member.rb', line 102

def synced?
  last_synced_at.present?
end

#to_sObject



43
44
45
# File 'app/models/effective/mailchimp_list_member.rb', line 43

def to_s
  mailchimp_list&.to_s || model_name.human
end