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
-
#build_mailchimp_list_member(mailchimp_list:) ⇒ Object
Find or build.
-
#build_mailchimp_list_members ⇒ Object
Used by the form to set it up for all lists.
-
#default_mailchimp_merge_fields ⇒ Object
These are the fields we push to Mailchimp on list_add and list_update Keys can only be 10 characters long.
- #mailchimp_last_synced_at ⇒ Object
- #mailchimp_list_member(mailchimp_list:) ⇒ Object
-
#mailchimp_membership_fees_paid_through_period ⇒ Object
Returns the maximum fees_paid_through_period of the membership and any deferred membership_period fees.
-
#mailchimp_merge_fields ⇒ Object
Intended for app to extend.
-
#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.
-
#mailchimp_subscribe_to_force_subscribe! ⇒ Object
Subscribe to force_subscribe lists.
- #mailchimp_subscribed?(mailchimp_list) ⇒ Boolean
- #mailchimp_subscribed_interests ⇒ Object
- #mailchimp_subscribed_lists ⇒ Object
-
#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.
-
#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.
-
#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.
- #mailchimp_update_async! ⇒ Object
Instance Method Details
#build_mailchimp_list_member(mailchimp_list:) ⇒ Object
Find or build
183 184 185 186 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 183 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_members ⇒ Object
Used by the form to set it up for all lists
193 194 195 196 197 198 199 200 201 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 193 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_fields ⇒ Object
These are the fields we push to Mailchimp on list_add and list_update Keys can only be 10 characters long
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 141 142 143 144 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 101 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_at ⇒ Object
188 189 190 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 188 def mailchimp_last_synced_at mailchimp_list_members.map(&:last_synced_at).compact.min end |
#mailchimp_list_member(mailchimp_list:) ⇒ Object
177 178 179 180 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 177 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_period ⇒ Object
Returns the maximum fees_paid_through_period of the membership and any deferred membership_period fees
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 147 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_fields ⇒ Object
Intended for app to extend
95 96 97 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 95 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 58 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_subscribe_to_force_subscribe! ⇒ Object
Subscribe to force_subscribe lists
272 273 274 275 276 277 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 272 def mailchimp_subscribe_to_force_subscribe! mailchimp_lists = Effective::MailchimpList.where(force_subscribe: true).to_a return unless mailchimp_lists.present? mailchimp_subscribe!(mailchimp_lists) end |
#mailchimp_subscribed?(mailchimp_list) ⇒ Boolean
47 48 49 50 51 52 53 54 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 47 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_interests ⇒ Object
173 174 175 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 173 def mailchimp_subscribed_interests mailchimp_list_members.select(&:subscribed?).flat_map(&:interests) end |
#mailchimp_subscribed_lists ⇒ Object
169 170 171 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 169 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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 206 def mailchimp_sync!(api: EffectiveMailchimp.api) lists = Effective::MailchimpList.sorted.to_a assign_attributes(mailchimp_user_form_action: nil) mailchimp_with_retries do Timeout::timeout(lists.length * 3) 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 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 79 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
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 237 def mailchimp_update!(api: EffectiveMailchimp.api, only: [], except: []) assign_attributes(mailchimp_user_form_action: nil) return if mailchimp_member_update_blocked? 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 = e.to_s.downcase if .include?("cannot be subscribed") || .include?("deleted") member.assign_mailchimp_cannot_be_subscribed elsif .include?("already a list member") existing = api.list_member(member.mailchimp_list, member.user.email) member.assign_mailchimp_attributes(existing) if existing.present? else raise(e) end end end save! end |
#mailchimp_update_async! ⇒ Object
230 231 232 233 |
# File 'app/models/concerns/effective_mailchimp_user.rb', line 230 def mailchimp_update_async! @mailchimp_member_update_enqueued = true EffectiveMailchimpUpdateJob.perform_later(self) end |