Permalink
Browse files

Merge branch 'r3'

Conflicts:
	lib/campaign_monitor_subscriber.rb
  • Loading branch information...
2 parents 8389e2f + c2dc7fe commit cbb1330a61f5a426a9da7ed585a41eda17453b29 @garygreyling garygreyling committed Jun 18, 2012
Showing with 23 additions and 7 deletions.
  1. +18 −3 README.rdoc
  2. +5 −4 lib/campaign_monitor_subscriber.rb
View
@@ -9,19 +9,34 @@ Automatically push/delete a model's email address and custom-fields to a Campaig
== SYNOPSIS:
-Specify the model's email address field name. You can also pass additional custom fields:
+Specify the model's email address field name. You can also pass additional custom fields and list_name:
Class User < ActiveRecord::Base
- subscribe_me_using :email, {"name" => "full_name", "UserStatus" => "get_status"}
+ subscribe_me_using :email, {"name" => "full_name", "UserStatus" => "get_status"}, "users"
end
-This will call `full_name` and `get_status` methods on record which was created using `send`.
+This will call `full_name` and `get_status` methods (using `send`) on record which was created.
Set the list id & api key in config/campaign_monitor_subscriber_config.yml:
list_id: 12oeu0089oe8gf9794oe498587o5
api_key: 23rbmoe351sd123d12134hbi1234
development: false # disable in development
+If you have only one list for all models, you can leave name "list_id" in config. Otherwise you should create something like:
+ list_users: 12oeu0089oe8gf9794oe498587o5
+ list_newsletter: oEZoeu0089oe8gf9794oe498587o5
+ api_key: 23rbmoe351sd123d12134hbi1234
+
+Models for these two lists look like this:
+
+ Class User < ActiveRecord::Base
+ subscribe_me_using :email, {"name" => "full_name"}, "users"
+ end
+
+ Class NewsletterUser < ActiveRecord::Base
+ subscribe_me_using :email, {"name" => "full_name"}, "newsletter"
+ end
+
Thats it!
== REQUIREMENTS:
@@ -3,24 +3,25 @@ module CampaignMonitorSubscriber
CM_CONFIG = YAML::load_file(File.join("config/campaign_monitor_subscriber_config.yml"))
::CAMPAIGN_MONITOR_API_KEY = CM_CONFIG['api_key']
- def subscribe_me_using(email_field, custom_fields={})
+ def subscribe_me_using(email_field, custom_fields={}, list_name="id")
return if CM_CONFIG[::Rails.env] == false or %w(cucumber test).include? ::Rails.env
+
after_create do |record|
begin
custom_fields = custom_fields.inject({}) { |h, (k, v)| h[k] = record.send(v); h }
s = Campaigning::Subscriber.new(record.send(email_field), custom_fields["name"])
- s.add!(CM_CONFIG['list_id'], custom_fields)
+ s.add!(CM_CONFIG["list_#{list_name}"], custom_fields)
rescue RuntimeError
end
end
after_destroy do |record|
begin
- Campaigning::Subscriber.unsubscribe!(record.send(email_field), CM_CONFIG['list_id'])
+ Campaigning::Subscriber.unsubscribe!(record.send(email_field), CM_CONFIG["list_#{list_name}"])
rescue RuntimeError
end
end
end
end
-ActiveRecord::Base.extend(CampaignMonitorSubscriber)
+ActiveRecord::Base.extend(CampaignMonitorSubscriber)

0 comments on commit cbb1330

Please sign in to comment.