effective_mailchimp 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/admin/mailchimp_categories_controller.rb +15 -0
  3. data/app/controllers/admin/mailchimp_controller.rb +23 -0
  4. data/app/controllers/admin/mailchimp_interests_controller.rb +15 -0
  5. data/app/controllers/admin/mailchimp_list_members_controller.rb +15 -0
  6. data/app/controllers/admin/mailchimp_lists_controller.rb +0 -10
  7. data/app/datatables/admin/effective_mailchimp_categories_datatable.rb +32 -0
  8. data/app/datatables/admin/effective_mailchimp_interests_datatable.rb +47 -0
  9. data/app/datatables/admin/effective_mailchimp_list_members_datatable.rb +39 -0
  10. data/app/datatables/admin/effective_mailchimp_lists_datatable.rb +9 -3
  11. data/app/helpers/effective_mailchimp_helper.rb +10 -0
  12. data/app/jobs/effective_mailchimp_subscribe_all_members_job.rb +8 -0
  13. data/app/jobs/effective_mailchimp_subscribe_all_users_job.rb +8 -0
  14. data/app/models/concerns/effective_mailchimp_user.rb +15 -10
  15. data/app/models/effective/mailchimp_api.rb +70 -18
  16. data/app/models/effective/mailchimp_category.rb +74 -0
  17. data/app/models/effective/mailchimp_interest.rb +93 -0
  18. data/app/models/effective/mailchimp_list.rb +62 -32
  19. data/app/models/effective/mailchimp_list_member.rb +42 -1
  20. data/app/views/admin/mailchimp/_sync.html.haml +15 -0
  21. data/app/views/admin/mailchimp/index.html.haml +79 -0
  22. data/app/views/admin/mailchimp_interests/_form.html.haml +10 -0
  23. data/app/views/admin/mailchimp_lists/_form.html.haml +1 -0
  24. data/app/views/admin/mailchimp_user/_form.html.haml +10 -4
  25. data/app/views/effective/mailchimp_user/_fields.html.haml +36 -9
  26. data/config/effective_mailchimp.rb +6 -0
  27. data/config/locales/effective_mailchimp.en.yml +12 -0
  28. data/config/routes.rb +9 -7
  29. data/db/migrate/101_create_effective_mailchimp.rb +39 -0
  30. data/lib/effective_mailchimp/version.rb +1 -1
  31. data/lib/effective_mailchimp.rb +28 -3
  32. data/lib/tasks/effective_mailchimp_tasks.rake +0 -16
  33. metadata +16 -3
  34. data/app/views/admin/mailchimp_lists/index.html.haml +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3502aae94186923697f39763042a537f6adf5426396e576b2ce17821cc892c70
4
- data.tar.gz: dac06ef19961ed0e8eab2a548f2d7a810c50fbfccdd7219d0d198247f69353b2
3
+ metadata.gz: 56caa6a3804ed54f997cb2a732022cb077c37ec186f2a42476b2433b59f80bab
4
+ data.tar.gz: 51a5b095d91881d957c227c6f13155d78d3a9178aa3703982cf2fd4b8d92cd37
5
5
  SHA512:
6
- metadata.gz: 73df6b018f47d66c0d5ba1fa27a02124c65c9544e6322e37bad2240cc21e2f73baae853bb68bb3e3c6ac8ef691ba6d627958988f2521884cf21244526b15da6f
7
- data.tar.gz: 021eca9c105c49ee522df6c0b60f3d64e608ec9a8adc3e0930a018e6ff4ef56c8965796efdee1e0c9e1b3d988bb8b4f7578e022ffcc665a49a14c087683dfbe4
6
+ metadata.gz: 246b57c7659cbb154db572309d4a886132cef6cdf9d1bd09e4cafd37fd15dfc2d109375a2c179173bf710c478fb71306b1dc69a0c57587a6d66c4286edf6b446
7
+ data.tar.gz: 578e421f5e538bc705fcc0a4ab8290cd0adc4fae51388ddf941f934bbd4ea2c069eb13530d96c789ed4c8110d2e4ca5960a8274a1ae154e58cc30971a8722f3f
@@ -0,0 +1,15 @@
1
+ module Admin
2
+ class MailchimpCategoriesController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_mailchimp) }
5
+
6
+ include Effective::CrudController
7
+
8
+ private
9
+
10
+ def permitted_params
11
+ params.require(:effective_mailchimp_category).permit!
12
+ end
13
+
14
+ end
15
+ end
@@ -3,6 +3,29 @@ module Admin
3
3
  before_action(:authenticate_user!) if defined?(Devise)
4
4
  before_action { EffectiveResources.authorize!(self, :admin, :effective_mailchimp) }
5
5
 
6
+ include Effective::CrudController
7
+
8
+ page_title 'Mailchimp'
9
+
10
+ # /admin/mailchimp
11
+ def index
12
+ end
13
+
14
+ # Sync All
15
+ def mailchimp_sync
16
+ EffectiveResources.authorize!(self, :admin, :mailchimp_sync)
17
+
18
+ api = EffectiveMailchimp.api
19
+ Effective::MailchimpList.sync!(api: api)
20
+ Effective::MailchimpCategory.sync!(api: api)
21
+ Effective::MailchimpInterest.sync!(api: api)
22
+
23
+ flash[:success] = "Successfully synced mailchimp data"
24
+
25
+ redirect_back(fallback_location: effective_mailchimp.admin_mailchimp_path)
26
+ end
27
+
28
+ # Sync one user
6
29
  def mailchimp_sync_user
7
30
  resource = current_user.class.find(params[:id])
8
31
 
@@ -0,0 +1,15 @@
1
+ module Admin
2
+ class MailchimpInterestsController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_mailchimp) }
5
+
6
+ include Effective::CrudController
7
+
8
+ private
9
+
10
+ def permitted_params
11
+ params.require(:effective_mailchimp_interest).permit!
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Admin
2
+ class MailchimpListMembersController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_mailchimp) }
5
+
6
+ include Effective::CrudController
7
+
8
+ private
9
+
10
+ def permitted_params
11
+ params.require(:effective_mailchimp_list_member).permit!
12
+ end
13
+
14
+ end
15
+ end
@@ -5,16 +5,6 @@ module Admin
5
5
 
6
6
  include Effective::CrudController
7
7
 
8
- def mailchimp_sync
9
- EffectiveResources.authorize!(self, :mailchimp_sync, Effective::MailchimpList)
10
-
11
- Effective::MailchimpList.sync!
12
-
13
- flash[:success] = "Successfully synced mailchimp lists"
14
-
15
- redirect_back(fallback_location: effective_mailchimp.admin_mailchimp_lists_path)
16
- end
17
-
18
8
  private
19
9
 
20
10
  def permitted_params
@@ -0,0 +1,32 @@
1
+ module Admin
2
+ class EffectiveMailchimpCategoriesDatatable < Effective::Datatable
3
+
4
+ datatable do
5
+ length :all
6
+
7
+ order :name
8
+
9
+ col :updated_at, visible: false
10
+ col :created_at, visible: false
11
+ col :id, visible: false
12
+
13
+ col :mailchimp_list
14
+
15
+ col :list_id, visible: false
16
+ col :mailchimp_id, visible: false
17
+
18
+ col :name
19
+ col :list_name, visible: false
20
+
21
+ col :display_type
22
+
23
+ col :mailchimp_interests
24
+
25
+ actions_col
26
+ end
27
+
28
+ collection do
29
+ Effective::MailchimpCategory.deep.all
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ module Admin
2
+ class EffectiveMailchimpInterestsDatatable < Effective::Datatable
3
+ filters do
4
+ scope :all
5
+ scope :subscribable
6
+ end
7
+
8
+ datatable do
9
+ length :all
10
+ order :display_order
11
+
12
+ col :updated_at, visible: false
13
+ col :created_at, visible: false
14
+ col :id, visible: false
15
+
16
+ col :mailchimp_list
17
+ col :mailchimp_category
18
+
19
+ col :list_id, visible: false
20
+ col :category_id, visible: false
21
+ col :mailchimp_id, visible: false
22
+
23
+ col :name
24
+ col :can_subscribe
25
+ col :force_subscribe
26
+
27
+ col :list_name, visible: false
28
+ col :category_name, visible: false
29
+ col :display_order, visible: false
30
+
31
+ col :subscriber_count
32
+
33
+ actions_col
34
+ end
35
+
36
+ collection do
37
+ scope = Effective::MailchimpInterest.deep
38
+
39
+ if attributes[:mailchimp_category_id].present?
40
+ scope = scope.sorted
41
+ end
42
+
43
+ scope
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ module Admin
2
+ class EffectiveMailchimpListMembersDatatable < Effective::Datatable
3
+ filters do
4
+ scope :all
5
+ scope :subscribed
6
+ end
7
+
8
+ datatable do
9
+ order :updated_at
10
+
11
+ col :updated_at, visible: false
12
+ col :created_at, visible: false
13
+ col :id, visible: false
14
+
15
+ col :mailchimp_list
16
+ col :user
17
+
18
+ col :mailchimp_id, visible: false
19
+ col :web_id, visible: false
20
+
21
+ col :email_address
22
+ col :full_name
23
+ col :subscribed
24
+
25
+ col :interests, visible: false
26
+ col :mailchimp_interests, search: { collection: Effective::MailchimpInterest.order(:name), fuzzy: true }
27
+
28
+ col :last_synced_at
29
+
30
+ actions_col do |member|
31
+ dropdown_link_to('Edit', "/admin/users/#{member.user.to_param}/edit#tab-mailchimp", 'data-turbolinks': false)
32
+ end
33
+ end
34
+
35
+ collection do
36
+ Effective::MailchimpListMember.deep.all
37
+ end
38
+ end
39
+ end
@@ -6,7 +6,8 @@ module Admin
6
6
  end
7
7
 
8
8
  datatable do
9
- order :updated_at
9
+ length :all
10
+ order :name
10
11
 
11
12
  col :updated_at, visible: false
12
13
  col :created_at, visible: false
@@ -21,13 +22,18 @@ module Admin
21
22
 
22
23
  col :url, label: 'Mailchimp' do |ml|
23
24
  [
24
- link_to('View Campaign', ml.url, target: '_blank'),
25
25
  link_to('View Members', ml.members_url, target: '_blank'),
26
26
  link_to('View Merge Fields', ml.merge_fields_url, target: '_blank')
27
27
  ].join('<br>').html_safe
28
28
  end
29
29
 
30
- col :merge_fields
30
+ col :merge_fields, visible: false do |ml|
31
+ ml.merge_fields.join(', ')
32
+ end
33
+
34
+ col :member_count do |list|
35
+ list.member_count.to_i
36
+ end
31
37
 
32
38
  actions_col
33
39
  end
@@ -1,2 +1,12 @@
1
1
  module EffectiveMailchimpHelper
2
+
3
+ def mailchimp_list_member_interests_collection(mailchimp_interests)
4
+ interests = mailchimp_interests.select { |interest| interest.can_subscribe? || interest.force_subscribe? }
5
+
6
+ interests.map do |interest|
7
+ label = (interest.force_subscribe? ? (interest.to_s + ' ' + content_tag(:small, 'required', class: 'text-hint')) : interest.to_s).html_safe
8
+ [label, interest.mailchimp_id, disabled: (interest.force_subscribe || !interest.can_subscribe?)]
9
+ end
10
+ end
11
+
2
12
  end
@@ -0,0 +1,8 @@
1
+ class EffectiveMailchimpSubscribeAllMembersJob < ApplicationJob
2
+
3
+ def perform(mailchimp_list)
4
+ raise('expected an Effective::MailchimpList') unless mailchimp_list.kind_of?(Effective::MailchimpList)
5
+ mailchimp_list.subscribe_all_members!(now: true)
6
+ end
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ class EffectiveMailchimpSubscribeAllUsersJob < ApplicationJob
2
+
3
+ def perform(mailchimp_list)
4
+ raise('expected an Effective::MailchimpList') unless mailchimp_list.kind_of?(Effective::MailchimpList)
5
+ mailchimp_list.subscribe_all_users!(now: true)
6
+ end
7
+
8
+ end
@@ -35,7 +35,7 @@ module EffectiveMailchimpUser
35
35
 
36
36
  # The user updated the form
37
37
  after_commit(if: -> { mailchimp_member_update_required? }) do
38
- EffectiveMailchimpUpdateJob.perform_later(self)
38
+ EffectiveMailchimpUpdateJob.perform_later(self) # This calls user.mailchimp_update! on the background
39
39
  end
40
40
  end
41
41
 
@@ -59,12 +59,13 @@ module EffectiveMailchimpUser
59
59
  member.assign_attributes(subscribed: subscribed)
60
60
  end
61
61
 
62
- # This sets up the after_commit to run the mailchimp_update! job
63
- assign_attributes(mailchimp_user_form_action: true)
64
-
65
62
  # For use in a rake task. Run the update right now
66
- mailchimp_update! if now
63
+ if now
64
+ return mailchimp_update!(only: mailchimp_lists)
65
+ end
67
66
 
67
+ # This sets up the after_commit to run the mailchimp_update! job
68
+ assign_attributes(mailchimp_user_form_action: true)
68
69
  save!
69
70
  end
70
71
 
@@ -140,6 +141,10 @@ module EffectiveMailchimpUser
140
141
  mailchimp_list_members.select(&:subscribed?).map(&:mailchimp_list)
141
142
  end
142
143
 
144
+ def mailchimp_subscribed_interests
145
+ mailchimp_list_members.select(&:subscribed?).flat_map(&:interests)
146
+ end
147
+
143
148
  def mailchimp_list_member(mailchimp_list:)
144
149
  raise('expected a MailchimpList') unless mailchimp_list.kind_of?(Effective::MailchimpList)
145
150
  mailchimp_list_members.find { |mlm| mlm.mailchimp_list_id == mailchimp_list.id }
@@ -169,8 +174,7 @@ module EffectiveMailchimpUser
169
174
  # Pulls the current status from Mailchimp API into the Mailchimp List Member objects
170
175
  # Run before the mailchimp fields are displayed
171
176
  # Only run in the background when a user or admin clicks sync now
172
- def mailchimp_sync!
173
- api = EffectiveMailchimp.api
177
+ def mailchimp_sync!(api: EffectiveMailchimp.api)
174
178
  lists = Effective::MailchimpList.subscribable.sorted.to_a
175
179
 
176
180
  assign_attributes(mailchimp_user_form_action: nil)
@@ -194,12 +198,13 @@ module EffectiveMailchimpUser
194
198
 
195
199
  # Pushes the current Mailchimp List Member objects to Mailchimp when needed
196
200
  # Called in the background after a form submission that changes the user email/last_name/first_name or mailchimp subscriptions
197
- def mailchimp_update!
198
- api = EffectiveMailchimp.api
199
-
201
+ def mailchimp_update!(api: EffectiveMailchimp.api, only: [], except: [])
200
202
  assign_attributes(mailchimp_user_form_action: nil)
201
203
 
202
204
  mailchimp_list_members.each do |member|
205
+ next if only.present? && Array(only).exclude?(member.mailchimp_list)
206
+ next if except.present? && Array(except).include?(member.mailchimp_list)
207
+
203
208
  begin
204
209
  list_member = if member.mailchimp_id.blank? && member.subscribed?
205
210
  api.list_member_add(member)
@@ -25,10 +25,30 @@ module Effective
25
25
  Rails.env.development?
26
26
  end
27
27
 
28
+ def sandbox_mode?
29
+ EffectiveMailchimp.sandbox_mode?
30
+ end
31
+
28
32
  def admin_url
29
33
  "https://#{server}.admin.mailchimp.com"
30
34
  end
31
35
 
36
+ def audience_url
37
+ "https://#{server}.admin.mailchimp.com/audience/"
38
+ end
39
+
40
+ def groups_url
41
+ "https://#{server}.admin.mailchimp.com/lists/dashboard/groups/"
42
+ end
43
+
44
+ def contacts_url
45
+ "https://#{server}.admin.mailchimp.com/audience/contacts"
46
+ end
47
+
48
+ def campaigns_url
49
+ "https://#{server}.admin.mailchimp.com/campaigns/"
50
+ end
51
+
32
52
  def public_url
33
53
  "https://mailchimp.com"
34
54
  end
@@ -40,21 +60,37 @@ module Effective
40
60
  # Returns an Array of Lists, which are each Hash
41
61
  # Like this [{ ...}, { ... }]
42
62
  def lists
43
- Rails.logger.info "[effective_mailchimp] Index Lists..." if debug?
63
+ Rails.logger.info "[effective_mailchimp] Index Lists" if debug?
44
64
 
45
65
  response = client.lists.get_all_lists(count: 250)
46
66
  Array(response['lists']) - [nil, '', {}]
47
67
  end
48
68
 
49
69
  def list(id)
50
- Rails.logger.info "[effective_mailchimp] Show List..." if debug?
70
+ Rails.logger.info "[effective_mailchimp] Get List" if debug?
51
71
 
52
72
  client.lists.get_list(id.try(:mailchimp_id) || id)
53
73
  end
54
74
 
75
+ def categories(list_id)
76
+ Rails.logger.info "[effective_mailchimp] Index Interest Categories" if debug?
77
+
78
+ response = client.lists.get_list_interest_categories(list_id.try(:mailchimp_id) || list_id)
79
+ Array(response['categories']) - [nil, '', {}]
80
+ end
81
+
82
+ def interests(list_id, category_id)
83
+ Rails.logger.info "[effective_mailchimp] Index Interest Category Interests" if debug?
84
+
85
+ response = client.lists.list_interest_category_interests(list_id, category_id)
86
+ Array(response['interests']) - [nil, '', {}]
87
+ end
88
+
55
89
  def list_member(id, email)
56
90
  raise('expected an email') unless email.to_s.include?('@')
57
91
 
92
+ Rails.logger.info "[effective_mailchimp] Get List Member" if debug?
93
+
58
94
  begin
59
95
  client.lists.get_list_member(id.try(:mailchimp_id) || id, email)
60
96
  rescue MailchimpMarketing::ApiError => e
@@ -63,18 +99,24 @@ module Effective
63
99
  end
64
100
 
65
101
  def list_merge_fields(id)
66
- response = client.lists.get_list_merge_fields(id, count: 100)
67
- Array(response['merge_fields']) - [nil, '', {}]
102
+ Rails.logger.info "[effective_mailchimp] Get List Merge Fields" if debug?
103
+
104
+ response = client.lists.get_list_merge_fields(id.try(:mailchimp_id) || id, count: 100)
105
+ Array(response['merge_fields']) - [nil, '', ' ', {}]
68
106
  end
69
107
 
70
108
  def add_merge_field(id, name:, type: :text)
71
109
  raise("invalid mailchimp merge key: #{name}. Must be 10 or fewer characters") if name.to_s.length > 10
72
110
 
111
+ return if sandbox_mode?
112
+ Rails.logger.info "[effective_mailchimp] Add List Merge Field #{name}" if debug?
113
+
73
114
  payload = { name: name.to_s.titleize, tag: name.to_s, type: type }
74
115
 
75
116
  begin
76
- client.lists.add_list_merge_field(id, payload)
117
+ client.lists.add_list_merge_field(id.try(:mailchimp_id) || id, payload)
77
118
  rescue MailchimpMarketing::ApiError => e
119
+ EffectiveLogger.error(e.message, details: name.to_s) if defined?(EffectiveLogger)
78
120
  false
79
121
  end
80
122
  end
@@ -82,34 +124,44 @@ module Effective
82
124
  def list_member_add(member)
83
125
  raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)
84
126
 
85
- existing = list_member(member.mailchimp_list, member.user.email)
86
- return existing if existing.present?
127
+ return if sandbox_mode?
128
+ Rails.logger.info "[effective_mailchimp] Add List Member" if debug?
87
129
 
88
- merge_fields = member.user.mailchimp_merge_fields
89
- raise('expected user mailchimp_merge_fields to be a Hash') unless merge_fields.kind_of?(Hash)
130
+ # See if they exist somehow
131
+ existing = list_member(member.mailchimp_list, member.user.email)
90
132
 
91
- payload = {
92
- email_address: member.user.email,
93
- status: (member.subscribed ? 'subscribed' : 'unsubscribed'),
94
- merge_fields: merge_fields.delete_if { |k, v| v.blank? }
95
- }
133
+ if existing.present?
134
+ member.assign_attributes(mailchimp_id: existing['id'])
135
+ return list_member_update(member)
136
+ end
96
137
 
138
+ # Actually add
139
+ payload = list_member_payload(member)
97
140
  client.lists.add_list_member(member.mailchimp_list.mailchimp_id, payload)
98
141
  end
99
142
 
100
143
  def list_member_update(member)
101
144
  raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)
102
145
 
146
+ return if sandbox_mode?
147
+ Rails.logger.info "[effective_mailchimp] Update List Member" if debug?
148
+
149
+ payload = list_member_payload(member)
150
+ client.lists.update_list_member(member.mailchimp_list.mailchimp_id, member.email, payload)
151
+ end
152
+
153
+ def list_member_payload(member)
154
+ raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)
155
+
103
156
  merge_fields = member.user.mailchimp_merge_fields
104
157
  raise('expected user mailchimp_merge_fields to be a Hash') unless merge_fields.kind_of?(Hash)
105
158
 
106
159
  payload = {
107
160
  email_address: member.user.email,
108
161
  status: (member.subscribed ? 'subscribed' : 'unsubscribed'),
109
- merge_fields: merge_fields.delete_if { |k, v| v.blank? }
110
- }
111
-
112
- client.lists.update_list_member(member.mailchimp_list.mailchimp_id, member.email, payload)
162
+ merge_fields: merge_fields.delete_if { |k, v| v.blank? },
163
+ interests: member.interests_hash.presence
164
+ }.compact
113
165
  end
114
166
 
115
167
  end
@@ -0,0 +1,74 @@
1
+ module Effective
2
+ class MailchimpCategory < ActiveRecord::Base
3
+ self.table_name = (EffectiveMailchimp.mailchimp_categories_table_name || :mailchimp_categories).to_s
4
+
5
+ belongs_to :mailchimp_list
6
+ has_many :mailchimp_interests
7
+
8
+ effective_resource do
9
+ mailchimp_id :string # ID of this Mailchimp InterestCategory
10
+ list_id :string # Mailchimp list ID
11
+
12
+ name :string # Title
13
+ list_name :string
14
+
15
+ display_type :string # Type
16
+
17
+ timestamps
18
+ end
19
+
20
+ validates :mailchimp_id, presence: true
21
+ validates :list_id, presence: true
22
+ validates :name, presence: true
23
+
24
+ scope :deep, -> { all }
25
+ scope :sorted, -> { order(:name) }
26
+ scope :subscribable, -> { all }
27
+
28
+ # Reads all the InterestCategories from Mailchimp and creates local MailchimpCategory records
29
+ # This is part of the Sync changes from Mailchimp button
30
+ def self.sync!(api: EffectiveMailchimp.api)
31
+ # For every mailchimp_list, get all the categories
32
+ mailchimp_lists = Effective::MailchimpList.all
33
+
34
+ mailchimp_lists.each do |mailchimp_list|
35
+ # All the Groups from Mailchimp
36
+ categories = api.categories(mailchimp_list.mailchimp_id)
37
+
38
+ # Get all our existing Effective::MailchimpCategory records
39
+ mailchimp_categories = where(mailchimp_list: mailchimp_list)
40
+
41
+ # Find or create Effective::MailchimpGroups based on existing groups
42
+ categories.each do |category|
43
+ mailchimp_id = category['id']
44
+ mailchimp_category = mailchimp_categories.find { |mc| mc.mailchimp_id == mailchimp_id } || new()
45
+
46
+ mailchimp_category.assign_attributes(
47
+ mailchimp_list: mailchimp_list,
48
+
49
+ mailchimp_id: mailchimp_id,
50
+ list_id: category['list_id'],
51
+ list_name: mailchimp_list.name,
52
+ name: (category['title'] || category['name']),
53
+ display_type: category['type']
54
+ )
55
+
56
+ mailchimp_category.save!
57
+ end
58
+
59
+ # Destroy any Effective::MailchimpGroups resources if they no longer returned by groups
60
+ mailchimp_categories.each do |mailchimp_category|
61
+ category = categories.find { |category| category['id'] == mailchimp_category.mailchimp_id }
62
+ mailchimp_category.destroy! unless category.present?
63
+ end
64
+ end
65
+
66
+ true
67
+ end
68
+
69
+ def to_s
70
+ name.presence || model_name.human
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,93 @@
1
+ module Effective
2
+ class MailchimpInterest < ActiveRecord::Base
3
+ self.table_name = (EffectiveMailchimp.mailchimp_interests_table_name || :mailchimp_interests).to_s
4
+
5
+ belongs_to :mailchimp_list
6
+ belongs_to :mailchimp_category
7
+
8
+ effective_resource do
9
+ mailchimp_id :string # ID of this interest on Mailchimp
10
+
11
+ list_id :string # Mailchimp list ID
12
+ category_id :string # Interest Category
13
+
14
+ name :string
15
+ list_name :string
16
+ category_name :string
17
+
18
+ subscriber_count :integer
19
+ display_order :integer
20
+
21
+ can_subscribe :boolean
22
+ force_subscribe :boolean
23
+
24
+ timestamps
25
+ end
26
+
27
+ validates :mailchimp_id, presence: true
28
+ validates :list_id, presence: true
29
+ validates :category_id, presence: true
30
+ validates :name, presence: true
31
+
32
+ scope :deep, -> { all }
33
+ scope :sorted, -> { order(:display_order) }
34
+ scope :subscribable, -> { where(can_subscribe: true) }
35
+
36
+ # Reads all the InterestCategoriesInterests from Mailchimp and creates local MailchimpInterest records
37
+ # This is part of the Sync changes from Mailchimp button
38
+ def self.sync!(api: EffectiveMailchimp.api)
39
+ # For every mailchimp_list, get all the interests
40
+ mailchimp_lists = Effective::MailchimpList.deep.all
41
+
42
+ mailchimp_lists.each do |mailchimp_list|
43
+ mailchimp_list.mailchimp_categories.each do |mailchimp_category|
44
+
45
+ # All the Interests from Mailchimp
46
+ interests = api.interests(mailchimp_list.mailchimp_id, mailchimp_category.mailchimp_id)
47
+
48
+ # Get all our existing Effective::MailchimpGroup records
49
+ mailchimp_interests = where(mailchimp_list: mailchimp_list, mailchimp_category: mailchimp_category)
50
+
51
+ # Find or create Effective::MailchimpInterests based on existing lists and categories
52
+ interests.each do |interest|
53
+ mailchimp_id = interest['id']
54
+ mailchimp_interest = mailchimp_interests.find { |mi| mi.mailchimp_id == mailchimp_id } || new()
55
+
56
+ mailchimp_interest.assign_attributes(
57
+ mailchimp_list: mailchimp_list,
58
+ mailchimp_category: mailchimp_category,
59
+
60
+ mailchimp_id: mailchimp_id,
61
+
62
+ list_id: interest['list_id'],
63
+ list_name: mailchimp_list.name,
64
+
65
+ category_id: interest['category_id'],
66
+ category_name: mailchimp_category.name,
67
+
68
+ name: interest['name'],
69
+ display_order: interest['display_order'],
70
+ subscriber_count: interest['subscriber_count']
71
+ )
72
+
73
+ mailchimp_interest.assign_attributes(can_subscribe: true) if mailchimp_interest.new_record?
74
+ mailchimp_interest.save!
75
+ end
76
+
77
+ # Destroy any Effective::MailchimpGroups resources if they no longer returned by interests
78
+ mailchimp_interests.each do |mailchimp_interest|
79
+ interest = interests.find { |interest| interest['id'] == mailchimp_interest.mailchimp_id }
80
+ mailchimp_interest.destroy! unless interest.present?
81
+ end
82
+ end
83
+ end
84
+
85
+ true
86
+ end
87
+
88
+ def to_s
89
+ name.presence || model_name.human
90
+ end
91
+
92
+ end
93
+ end