Module: EffectiveMailchimpUser

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_mailchimp_user.rb

Overview

EffectiveMailchimpUser

Mark your user model with effective_mailchimp_user to get a few helpers And user specific point required scores

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#build_mailchimp_list_member(mailchimp_list:) ⇒ Object

Find or build



179
180
181
182
# File 'app/models/concerns/effective_mailchimp_user.rb', line 179

def build_mailchimp_list_member(mailchimp_list:)
  raise('expected a MailchimpList') unless mailchimp_list.kind_of?(Effective::MailchimpList)
  mailchimp_list_member(mailchimp_list: mailchimp_list) || mailchimp_list_members.build(mailchimp_list: mailchimp_list)
end

#build_mailchimp_list_membersObject

Used by the form to set it up for all lists



189
190
191
192
193
194
195
196
197
# File 'app/models/concerns/effective_mailchimp_user.rb', line 189

def build_mailchimp_list_members
  mailchimp_lists = Effective::MailchimpList.subscribable.sorted.to_a

  mailchimp_lists.each do |mailchimp_list|
    build_mailchimp_list_member(mailchimp_list: mailchimp_list)
  end

  mailchimp_list_members
end

#default_mailchimp_merge_fieldsObject

These are the fields we push to Mailchimp on list_add and list_update Keys can only be 10 characters long



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/concerns/effective_mailchimp_user.rb', line 97

def default_mailchimp_merge_fields
  atts = {}

  if respond_to?(:first_name) && respond_to?(:last_name)
    atts.merge!(
      'FNAME': first_name,
      'LNAME': last_name
    )
  end

  if respond_to?(:addresses)
    address = try(:billing_address) || addresses.last

    atts.merge!(
      'ADDRESS1': address&.address1,
      'ADDRESS2': address&.address2,
      'CITY': address&.city,
      'PROVINCE': address&.province,
      'COUNTRY': address&.country,
      'POSTALCODE': address&.postal_code
    )
  end

  if respond_to?(:membership)
    membership = memberships.first() # Individual or organization membership

    atts.merge!(
      'CATEGORY': membership&.categories&.to_sentence,
      'STATUS': membership&.statuses&.to_sentence,
      'NUMBER': membership&.number,
      'JOINED': membership&.joined_on&.strftime('%F'),
      'FEESPAIDTO': mailchimp_membership_fees_paid_through_period()&.strftime('%F')
    )
  end

  if self.class.respond_to?(:effective_memberships_organization_user?)
    atts.merge!(
      'COMPANY': representatives.map { |rep| rep.organization.to_s }.join(', ').presence,
      'ROLES': representatives.flat_map { |rep| rep.roles }.compact.join(', ').presence,
    )
  end

  atts
end

#mailchimp_last_synced_atObject



184
185
186
# File 'app/models/concerns/effective_mailchimp_user.rb', line 184

def mailchimp_last_synced_at
  mailchimp_list_members.map(&:last_synced_at).compact.min
end

#mailchimp_list_member(mailchimp_list:) ⇒ Object



173
174
175
176
# File 'app/models/concerns/effective_mailchimp_user.rb', line 173

def mailchimp_list_member(mailchimp_list:)
  raise('expected a MailchimpList') unless mailchimp_list.kind_of?(Effective::MailchimpList)
  mailchimp_list_members.find { |mlm| mlm.mailchimp_list_id == mailchimp_list.id }
end

#mailchimp_membership_fees_paid_through_periodObject

Returns the maximum fees_paid_through_period of the membership and any deferred membership_period fees



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/concerns/effective_mailchimp_user.rb', line 143

def mailchimp_membership_fees_paid_through_period
  periods = []

  # Find the fees_paid_through_period from any membership
  if self.class.respond_to?(:effective_memberships_user?) && memberships.present?
    membership = memberships.first() # Individual or organization membership
    periods << membership&.fees_paid_through_period
  end

  # Find the fees_paid_through_period from any deferred membership fees
  fees = if self.class.respond_to?(:effective_memberships_organization_user?) && organizations.present?
    Effective::Fee.deferred.where(owner: [self, organizations])
  else
    Effective::Fee.deferred.where(owner: self)
  end

  periods += fees.select { |fee| fee.membership_period_fee? }.map(&:fees_paid_through_period)

  # Return the maximum fees paid through period
  periods.compact.max
end

#mailchimp_merge_fieldsObject

Intended for app to extend



91
92
93
# File 'app/models/concerns/effective_mailchimp_user.rb', line 91

def mailchimp_merge_fields
  default_mailchimp_merge_fields()
end

#mailchimp_subscribe!(mailchimp_list, subscribed: true, now: false) ⇒ Object

Api method to just subscribe this user to this list right now Pass one list or an Array of lists



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/concerns/effective_mailchimp_user.rb', line 54

def mailchimp_subscribe!(mailchimp_list, subscribed: true, now: false)
  mailchimp_lists = Array(mailchimp_list)
  raise('expected a MailchimpList') unless mailchimp_lists.all? { |list| list.kind_of?(Effective::MailchimpList) }

  mailchimp_lists.each do |mailchimp_list|
    member = build_mailchimp_list_member(mailchimp_list: mailchimp_list)
    member.assign_attributes(subscribed: subscribed)
  end

  # For use in a rake task. Run the update right now
  if now
    return mailchimp_update!(only: mailchimp_lists)
  end

  # This sets up the after_commit to run the mailchimp_update! job
  assign_attributes(mailchimp_user_form_action: true)
  save!
end

#mailchimp_subscribed?(mailchimp_list) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'app/models/concerns/effective_mailchimp_user.rb', line 43

def mailchimp_subscribed?(mailchimp_list)
  raise('expected a MailchimpList') unless mailchimp_lists.all? { |list| list.kind_of?(Effective::MailchimpList) }

  member = mailchimp_list_member(mailchimp_list: mailchimp_list)
  return false if member.blank?

  member.subscribed? && member.synced?
end

#mailchimp_subscribed_interestsObject



169
170
171
# File 'app/models/concerns/effective_mailchimp_user.rb', line 169

def mailchimp_subscribed_interests
  mailchimp_list_members.select(&:subscribed?).flat_map(&:interests)
end

#mailchimp_subscribed_listsObject



165
166
167
# File 'app/models/concerns/effective_mailchimp_user.rb', line 165

def mailchimp_subscribed_lists
  mailchimp_list_members.select(&:subscribed?).map(&:mailchimp_list)
end

#mailchimp_sync!(api: EffectiveMailchimp.api) ⇒ Object

Pulls the current status from Mailchimp API into the Mailchimp List Member objects Run before the mailchimp fields are displayed Only run in the background when a user or admin clicks sync now



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/concerns/effective_mailchimp_user.rb', line 202

def mailchimp_sync!(api: EffectiveMailchimp.api)
  lists = Effective::MailchimpList.subscribable.sorted.to_a

  assign_attributes(mailchimp_user_form_action: nil)

  Timeout::timeout(lists.length * 2) do
    lists.each do |mailchimp_list|
      member = build_mailchimp_list_member(mailchimp_list: mailchimp_list)

      list_member = api.list_member(mailchimp_list, email) || {}
      member.assign_mailchimp_attributes(list_member)
    end
  end

  mailchimp_list_members.each do |member|
    list = lists.find { |list| list.id == member.mailchimp_list_id }
    member.mark_for_destruction unless list.present?
  end

  save!
end

#mailchimp_unsubscribe!(mailchimp_list) ⇒ Object

Api method to just unsubscribe this user to this list right now Pass one list or an Array of lists



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/concerns/effective_mailchimp_user.rb', line 75

def mailchimp_unsubscribe!(mailchimp_list)
  mailchimp_lists = Array(mailchimp_list)
  raise('expected a MailchimpList') unless mailchimp_lists.all? { |list| list.kind_of?(Effective::MailchimpList) }

  mailchimp_lists.each do |mailchimp_list|
    member = build_mailchimp_list_member(mailchimp_list: mailchimp_list)
    member.assign_attributes(subscribed: false)
  end

  # This sets up the after_commit to run the mailchimp_update! job
  assign_attributes(mailchimp_user_form_action: true)

  save!
end

#mailchimp_update!(api: EffectiveMailchimp.api, only: [], except: []) ⇒ Object

Pushes the current Mailchimp List Member objects to Mailchimp when needed Called in the background after a form submission that changes the user email/last_name/first_name or mailchimp subscriptions



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/concerns/effective_mailchimp_user.rb', line 226

def mailchimp_update!(api: EffectiveMailchimp.api, only: [], except: [])
  assign_attributes(mailchimp_user_form_action: nil)

  mailchimp_list_members.each do |member|
    next if only.present? && Array(only).exclude?(member.mailchimp_list)
    next if except.present? && Array(except).include?(member.mailchimp_list)

    begin
      list_member = if member.mailchimp_id.blank? && member.subscribed?
        api.list_member_add(member)
      elsif member.mailchimp_id.present?
        api.list_member_update(member)
      end

      member.assign_mailchimp_attributes(list_member) if list_member.present?
    rescue MailchimpMarketing::ApiError => e
      if e.to_s.downcase.include?("cannot be subscribed") || e.to_s.downcase.include?('deleted')
        member.assign_mailchimp_cannot_be_subscribed
      else
        raise(e)
      end
    end
  end

  save!
end