smailer 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -16,6 +16,11 @@ Then run `bundle install` and you should be ready to go.
16
16
 
17
17
  TODO
18
18
 
19
+ ## TODO
20
+
21
+ * Add a migration generator
22
+ * Tests, tests, tests
23
+
19
24
  ## Contribution
20
25
 
21
26
  Patches are always welcome. In case you find any issues with this code, please use the project's [Issues](http://github.com/mitio/smailer/issues) page on Github to report them. Feel free to contribute! :)
data/lib/smailer.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Smailer
2
- autoload :VERSION, 'smailer/version'
3
- autoload :Models, 'smailer/models'
4
- autoload :Tasks, 'smailer/tasks'
2
+ autoload :VERSION, 'smailer/version'
3
+ autoload :Models, 'smailer/models'
4
+ autoload :Tasks, 'smailer/tasks'
5
+ autoload :Compatibility, 'smailer/compatibility'
5
6
  end
@@ -0,0 +1,11 @@
1
+ module Smailer
2
+ module Compatibility
3
+ def self.rails_3?
4
+ Rails::VERSION::MAJOR == 3
5
+ end
6
+
7
+ def self.save_without_validation(object)
8
+ rails_3? ? object.save(:validate => false) : save(false)
9
+ end
10
+ end
11
+ end
@@ -11,9 +11,12 @@ module Smailer
11
11
  validates_presence_of :mail_campaign_id, :from, :to, :retries, :status
12
12
  validates_numericality_of :mail_campaign_id, :retries, :status, :only_integer => true, :allow_nil => true
13
13
  validates_length_of :from, :to, :subject, :last_error, :maximum => 255
14
+ validates_uniqueness_of :key, :allow_nil => true
14
15
 
15
16
  delegate :mailing_list, :to => :mail_campaign, :allow_nil => true
16
17
 
18
+ before_save :update_mail_campaign_counts
19
+
17
20
  def status_text
18
21
  Statuses.constants.each do |constant_name|
19
22
  return constant_name if Statuses.const_get(constant_name) == status
@@ -21,10 +24,15 @@ module Smailer
21
24
  nil
22
25
  end
23
26
 
27
+ def opened!
28
+ self.opened = true
29
+ Compatibility.save_without_validation self if changed?
30
+ end
31
+
24
32
  def self.add(queued_mail, status = Statuses::SENT)
25
33
  finished = self.new
26
34
 
27
- [:mail_campaign_id, :from, :to, :subject, :body_html, :body_text, :retries, :last_retry_at, :last_error].each do |field|
35
+ [:mail_campaign_id, :key, :from, :to, :subject, :body_html, :body_text, :retries, :last_retry_at, :last_error].each do |field|
28
36
  finished.send("#{field}=", queued_mail.send(field))
29
37
  end
30
38
 
@@ -34,8 +42,22 @@ module Smailer
34
42
  finished.save!
35
43
  queued_mail.destroy
36
44
 
45
+ if finished.mail_campaign
46
+ finished.mail_campaign.sent_mails_count += 1
47
+ Compatibility.save_without_validation finished.mail_campaign
48
+ end
49
+
37
50
  finished
38
51
  end
52
+
53
+ protected
54
+
55
+ def update_mail_campaign_counts
56
+ if opened_changed?
57
+ mail_campaign.opened_mails_count += opened_was ? -1 : 1
58
+ Compatibility.save_without_validation mail_campaign
59
+ end
60
+ end
39
61
  end
40
62
  end
41
63
  end
@@ -41,6 +41,11 @@ module Smailer
41
41
  "Campaign ##{id} (#{mailing_list.name})"
42
42
  end
43
43
 
44
+ def hit_rate
45
+ return nil if sent_mails_count == 0
46
+ opened_mails_count.to_f / sent_mails_count
47
+ end
48
+
44
49
  def self.unsubscribe_methods
45
50
  methods = {}
46
51
  UnsubscribeMethods.constants.map do |method_name|
@@ -1,3 +1,5 @@
1
+ require 'digest/md5'
2
+
1
3
  module Smailer
2
4
  module Models
3
5
  class QueuedMail < ActiveRecord::Base
@@ -5,6 +7,7 @@ module Smailer
5
7
 
6
8
  validates_presence_of :mail_campaign_id, :to
7
9
  validates_uniqueness_of :to, :scope => :mail_campaign_id
10
+ validates_uniqueness_of :key
8
11
  validates_numericality_of :mail_campaign_id, :retries, :only_integer => true, :allow_nil => true
9
12
  validates_length_of :to, :last_error, :maximum => 255
10
13
 
@@ -12,6 +15,9 @@ module Smailer
12
15
 
13
16
  delegate :from, :subject, :mailing_list, :to => :mail_campaign, :allow_nil => true
14
17
 
18
+ before_validation :initialize_message_key
19
+ before_save :initialize_message_key
20
+
15
21
  def body_html
16
22
  interpolate mail_campaign.body_html
17
23
  end
@@ -20,14 +26,25 @@ module Smailer
20
26
  interpolate mail_campaign.body_text
21
27
  end
22
28
 
29
+ def key
30
+ initialize_message_key
31
+ self[:key]
32
+ end
33
+
23
34
  protected
24
35
 
36
+ def initialize_message_key
37
+ self.key = Digest::MD5.hexdigest("#{mail_campaign_id}, #{to} and #{id} compose this key.")
38
+ end
39
+
25
40
  def interpolate(text)
26
41
  {
27
- :email => to,
28
- :escaped_email => lambda { ERB::Util.h(to) },
29
- :email_key => lambda { MailKey.get(to) },
30
- :mailing_list_id => lambda { mailing_list.id },
42
+ :email => to,
43
+ :escaped_email => lambda { ERB::Util.h(to) },
44
+ :email_key => lambda { MailKey.get(to) },
45
+ :mailing_list_id => lambda { mailing_list.id },
46
+ :mail_campaign_id => mail_campaign_id,
47
+ :message_key => key,
31
48
  }.each do |variable, interpolation|
32
49
  text.gsub! "%{#{variable}}" do
33
50
  interpolation.respond_to?(:call) ? interpolation.call : interpolation.to_s
@@ -1,3 +1,3 @@
1
1
  module Smailer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dimitar Dimitrov
@@ -46,6 +46,7 @@ files:
46
46
  - README.md
47
47
  - Rakefile
48
48
  - lib/smailer.rb
49
+ - lib/smailer/compatibility.rb
49
50
  - lib/smailer/models.rb
50
51
  - lib/smailer/models/finished_mail.rb
51
52
  - lib/smailer/models/mail_campaign.rb