Class: Effective::MailchimpList
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::MailchimpList
- Defined in:
- app/models/effective/mailchimp_list.rb
Class Method Summary collapse
-
.sync!(api: EffectiveMailchimp.api) ⇒ Object
Reads all the Lists from Mailchimp and creates local MailchimpList records Also writes our merge fields to Mailchimp if they don’t exist This is part of the Sync changes from Mailchimp button.
Instance Method Summary collapse
- #grouped? ⇒ Boolean
- #members_url ⇒ Object
- #merge_fields ⇒ Object
- #merge_fields_url ⇒ Object
- #subscribe_all_members!(now: false) ⇒ Object
- #subscribe_all_users!(now: false) ⇒ Object
- #to_s ⇒ Object
- #ungrouped? ⇒ Boolean
Class Method Details
.sync!(api: EffectiveMailchimp.api) ⇒ Object
Reads all the Lists from Mailchimp and creates local MailchimpList records Also writes our merge fields to Mailchimp if they don’t exist This is part of the Sync changes from Mailchimp button
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/effective/mailchimp_list.rb', line 28 def self.sync!(api: EffectiveMailchimp.api) # All the Lists from Mailchimp lists = api.lists() # Get all our existing Effective::MailchimpList records mailchimp_lists = all() # Find or create Effective::Mailchimp based on existing lists lists.each do |list| mailchimp_id = list['id'] mailchimp_list = mailchimp_lists.find { |ml| ml.mailchimp_id == mailchimp_id } || new() mailchimp_list.assign_attributes( mailchimp_id: mailchimp_id, web_id: list['web_id'], name: list['name'], member_count: (list.dig('stats', 'member_count') || 0), updated_at: Time.zone.now ) mailchimp_list.assign_attributes(can_subscribe: true) if mailchimp_list.new_record? mailchimp_list.save! end # Destroy any Effective::Mailchimp resources if they no longer returned by lists mailchimp_lists.each do |mailchimp_list| list = lists.find { |list| list['id'] == mailchimp_list.mailchimp_id } mailchimp_list.destroy! unless list.present? end # Sync merge fields if (merge_fields = EffectiveMailchimp.merge_fields).present? merge_field_keys = merge_fields.keys.map(&:to_s) mailchimp_lists.reject(&:destroyed?).each do |mailchimp_list| existing = api.list_merge_fields(mailchimp_list).map { |hash| hash['tag'] } (merge_field_keys - existing).each { |name| api.add_merge_field(mailchimp_list, name: name) } end end true end |
Instance Method Details
#grouped? ⇒ Boolean
97 98 99 |
# File 'app/models/effective/mailchimp_list.rb', line 97 def grouped? mailchimp_categories.present? && mailchimp_categories.any? { |category| category.mailchimp_interests.present? } end |
#members_url ⇒ Object
105 106 107 |
# File 'app/models/effective/mailchimp_list.rb', line 105 def members_url EffectiveMailchimp.api.admin_url + "/lists/members?id=#{web_id}" end |
#merge_fields ⇒ Object
92 93 94 95 |
# File 'app/models/effective/mailchimp_list.rb', line 92 def merge_fields return [] unless mailchimp_id EffectiveMailchimp.api.list_merge_fields(mailchimp_id).map { |hash| hash['tag'] }.sort end |
#merge_fields_url ⇒ Object
109 110 111 |
# File 'app/models/effective/mailchimp_list.rb', line 109 def merge_fields_url EffectiveMailchimp.api.admin_url + "/lists/settings/merge-tags?id=#{web_id}" end |
#subscribe_all_members!(now: false) ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/models/effective/mailchimp_list.rb', line 80 def subscribe_all_members!(now: false) if now subscribe_all!(EffectiveMailchimp.User.all.members) else EffectiveMailchimpSubscribeAllMembersJob.perform_later(self) end end |
#subscribe_all_users!(now: false) ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/models/effective/mailchimp_list.rb', line 72 def subscribe_all_users!(now: false) if now subscribe_all!(EffectiveMailchimp.User.all) else EffectiveMailchimpSubscribeAllUsersJob.perform_later(self) end end |
#to_s ⇒ Object
88 89 90 |
# File 'app/models/effective/mailchimp_list.rb', line 88 def to_s name.presence || model_name.human end |
#ungrouped? ⇒ Boolean
101 102 103 |
# File 'app/models/effective/mailchimp_list.rb', line 101 def ungrouped? !grouped? end |