Class: Chimpmunk::EmailCampaign

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/chimpmunk/email_campaign.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_create_callbacksObject

Returns the value of attribute skip_create_callbacks.



4
5
6
# File 'lib/chimpmunk/email_campaign.rb', line 4

def skip_create_callbacks
  @skip_create_callbacks
end

#skip_update_callbacksObject

Returns the value of attribute skip_update_callbacks.



4
5
6
# File 'lib/chimpmunk/email_campaign.rb', line 4

def skip_update_callbacks
  @skip_update_callbacks
end

Class Method Details

.associated(association_type, *associations) ⇒ Object

create has_many associations



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
141
142
143
144
145
# File 'lib/chimpmunk/email_campaign.rb', line 99

def self.associated(association_type, *associations)
  if association_type == :many
    class << self; attr_accessor :mc_edit_has_many; end
    @mc_edit_has_many = associations
  else
    class << self; attr_accessor :mc_edit_has_one; end
    @mc_edit_has_one = associations
  end

  associations.each do |association|
    # TODO: figure out has_one attr_accessible
    # TODO: test relations in rails 4
    if association_type == :many
      if Rails::VERSION::MAJOR == 3
        attr_accessible[:default] << "#{association.to_s.singularize}_ids"

        self.class_eval <<-EOF
          after_save do
            self.email_campaign_#{association}.destroy_all
            self.#{association.to_s.singularize}_ids.each{|xid| self.email_campaign_#{association}.create(#{association.to_s.singularize}_id: xid)}
          end
        EOF
      end
    else
      if Rails::VERSION::MAJOR == 3
        singular = association.to_s.singularize
        singular_id = "#{singular}_id"
        attr_accessible[:default] << singular_id
        attr_accessor singular_id.to_sym
        self.class_eval <<-EOF
          after_save do
            if self.property_id
              e = EmailCampaign#{singular.titleize}.where(email_campaign_id: self.id).first_or_initialize
              e.#{singular_id} = self.#{singular_id}
              e.save
            end
          end
        EOF
      end
    end

    association_name = "email_campaign_#{association}".to_sym
    send("has_#{association_type}", association_name, foreign_key: 'email_campaign_id', dependent: :destroy)
    send("has_#{association_type}", association, through: association_name )
    accepts_nested_attributes_for association_name, allow_destroy: true
  end
end

.editable_areas(*mc_edit_tags) ⇒ Object

store mc:edit tag names



91
92
93
94
# File 'lib/chimpmunk/email_campaign.rb', line 91

def self.editable_areas(*mc_edit_tags)
  class << self; attr_accessor :mc_edit_editable_areas; end
  @mc_edit_editable_areas = mc_edit_tags
end

.headers(*headers) ⇒ Object

create header associations



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chimpmunk/email_campaign.rb', line 37

def self.headers(*headers)
  class << self; attr_accessor :mc_edit_headers; end
  @mc_edit_headers = headers

  headers.each do |header|
    if Rails::VERSION::MAJOR == 4
      has_one header.to_sym, -> { where(name: header.to_s) }, class_name: 'Chimpmunk::EmailCampaignHeader', foreign_key: :email_campaign_id, dependent: :destroy
    else
      attr_accessible[:default] << "#{header}_attributes"
      has_one header.to_sym, conditions: { name: header.to_s }, class_name: 'Chimpmunk::EmailCampaignHeader', foreign_key: :email_campaign_id, dependent: :destroy
    end
    accepts_nested_attributes_for header.to_sym, allow_destroy: true
  end
end

.images(*images) ⇒ Object

create image associations



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chimpmunk/email_campaign.rb', line 73

def self.images(*images)
  class << self; attr_accessor :mc_edit_images; end
  @mc_edit_images = images

  images.each do |image|
    if Rails::VERSION::MAJOR == 4
      has_one image.to_sym, -> { where(name: image.to_s) }, class_name: 'Chimpmunk::EmailCampaignImage', foreign_key: :email_campaign_id, dependent: :destroy
    else
      attr_accessible[:default] << "#{image}_attributes"
      has_one image.to_sym, conditions: { name: image.to_s }, class_name: 'Chimpmunk::EmailCampaignImage', foreign_key: :email_campaign_id, dependent: :destroy
    end
    accepts_nested_attributes_for image.to_sym, allow_destroy: true
  end
end

.refresh_email_campaign_statsObject

Refreshes all of the email campaign stats



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chimpmunk/email_campaign.rb', line 172

def self.refresh_email_campaign_stats
  Chimpmunk.mailchimp.campaigns.list({status:"sent"})["data"].each do |campaign|
    email_campaign = Chimpmunk::EmailCampaign.find_by_mailchimp_campaign_id(campaign["id"])
    if email_campaign and campaign["summary"].any?
      email_campaign.skip_update_callbacks = true
      email_campaign.update_attributes(
        status: "sent",
        emails_sent: campaign["summary"]["emails_sent"],
        opened: campaign["summary"]["unique_opens"],
        clicked: campaign["summary"]["unique_clicks"],
        hard_bounces: campaign["summary"]["hard_bounces"],
        soft_bounces: campaign["summary"]["soft_bounces"],
        unsubscribes: campaign["summary"]["unsubscribes"],
        forwards: campaign["summary"]["forwards"],
        forwards_opens: campaign["summary"]["forwards_opens"],
        opens: campaign["summary"]["opens"],
        last_open: campaign["summary"]["last_open"],
        unique_opens: campaign["summary"]["unique_opens"],
        non_unique_clicks: campaign["summary"]["non_unique_clicks"],
        users_who_clicked: campaign["summary"]["users_who_clicked"],
        unique_likes: campaign["summary"]["unique_likes"],
        facebook_likes: campaign["summary"]["facebook_likes"]
      )
    end
  end
end

.refresh_email_campaignsObject

Refreshes the list of email campaigns



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chimpmunk/email_campaign.rb', line 149

def self.refresh_email_campaigns
  Chimpmunk.mailchimp.campaigns.list()["data"].each do |campaign|
    campaign_attributes = {
      title: campaign["title"],
      campaign_kind: campaign["type"],
      mailchimp_list_id: campaign["list_id"],
      email_list_id: Chimpmunk::EmailList.find_by_mailchimp_id(campaign["list_id"]).try(:id),
      subject: campaign["subject"],
      from_email: campaign["from_email"],
      from_name: campaign["from_name"],
      email_template_id: campaign["template_id"],
      mailchimp_campaign_id: campaign["id"]
    }
    unless self.where(campaign_attributes).any?
      new_campaign = self.new(campaign_attributes)
      new_campaign.skip_create_callbacks = true
      new_campaign.save
    end
  end
end

.texts(*texts) ⇒ Object

create text associations



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chimpmunk/email_campaign.rb', line 55

def self.texts(*texts)
  class << self; attr_accessor :mc_edit_texts; end
  @mc_edit_texts = texts

  texts.each do |text|
    if Rails::VERSION::MAJOR == 4
      has_one text.to_sym, -> { where(name: text.to_s) }, class_name: 'Chimpmunk::EmailCampaignText', foreign_key: :email_campaign_id, dependent: :destroy
    else
      attr_accessible[:default] << "#{text}_attributes"
      has_one text.to_sym, conditions: { name: text.to_s }, class_name: 'Chimpmunk::EmailCampaignText', foreign_key: :email_campaign_id, dependent: :destroy
    end
    accepts_nested_attributes_for text.to_sym, allow_destroy: true
  end
end

Instance Method Details

#clicked_percentageObject

clicked percentage display as float



240
241
242
243
244
# File 'lib/chimpmunk/email_campaign.rb', line 240

def clicked_percentage
  if clicked and emails_sent
    "%2.1f %" % (clicked.to_f / emails_sent.to_f * 100)
  end
end

#opened_percentageObject

opened percentage display as float



232
233
234
235
236
# File 'lib/chimpmunk/email_campaign.rb', line 232

def opened_percentage
  if opened and emails_sent
    "%2.1f %" % (opened.to_f / emails_sent.to_f * 100)
  end
end

#schedule_campaignObject

schedule a mailchimp campaign



213
214
215
216
217
218
219
220
# File 'lib/chimpmunk/email_campaign.rb', line 213

def schedule_campaign
  response = Chimpmunk.mailchimp.campaigns.schedule({
    cid: self.mailchimp_campaign_id,
    schedule_time: self.send_date.strftime("%Y-%m-%d %H:%M:%S")
  })
  self.skip_update_callbacks = true
  response['errors'].nil? && self.update_attribute(:status, 'scheduled')
end

#send_campaignObject

send a mailchimp campaign



205
206
207
208
209
# File 'lib/chimpmunk/email_campaign.rb', line 205

def send_campaign
  response = Chimpmunk.mailchimp.campaigns.send({cid: self.mailchimp_campaign_id})
  self.skip_update_callbacks = true
  response['errors'].nil? && self.update_attribute(:status, 'sent')
end

#unschedule_campaignObject

un-schedule email campaign that has been scheduled



224
225
226
227
228
# File 'lib/chimpmunk/email_campaign.rb', line 224

def unschedule_campaign
  response = Chimpmunk.mailchimp.campaigns.unschedule({cid: self.mailchimp_campaign_id})
  self.skip_update_callbacks = true
  response['errors'].nil? && self.update_attribute(:status, 'un-scheduled')
end