Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the way I went with in the end.</p> <p>It turns out Rails gives you loads of handy callbacks in the dirty to do this.</p> <p>Any suggestions on how to make this code less repetitive wold be gratefully received.</p> <pre><code>def update_mailchimp(optin) # Create a Hominid object (A wrapper to the mailchimp api), and pass in a hash from the yaml file # telling which mailing list id to update with subscribe/unsubscribe notifications) @hominid = Hominid.new client_site_list_id = YAML.load(File.read(RAILS_ROOT + "/config/mailchimp.yml")) case optin when 'subscribe_newsletter' logger.debug("subscribing to newsletter...") "success!" if @hominid.subscribe(client_site_list_id['client_site_to_mailchimp_API_link'], email, {:FNAME =&gt; first_name, :LNAME =&gt; last_name}, 'html') when 'unsubscribe_newsletter' logger.debug("unsubscribing from newsletter...") "success!" if @hominid.subscribe(client_site_list_id['client_site_to_mailchimp_API_link'], email, {:FNAME =&gt; first_name, :LNAME =&gt; last_name}, 'html') when 'subscribe_monthly_update' logger.debug("subscribing to monthly update...") "success!" if @hominid.subscribe(client_site_list_id['monthly_update'], email, {:FNAME =&gt; first_name, :LNAME =&gt; last_name}, 'html') when 'unsubscribe_monthly_update' logger.debug("unsubscribing from monthly update...") "success!" if @hominid.unsubscribe(client_site_list_id['monthly_update'], email, {:FNAME =&gt; first_name, :LNAME =&gt; last_name}, 'html') end end # Keep the users in sync with mailchimp's own records - by only firing requests to the API if details on a user have changed after saving. def check_against_mailchimp logger.info("Checking if changes need to be sent to mailchimp...") if newsletter_changed? logger.info("Newsletter changed...") newsletter ? update_mailchimp('subscribe_newsletter') : update_mailchimp('unsubscribe_newsletter') end if monthly_update_changed? logger.info("update preferences changed...") monthly_update ? update_mailchimp('subscribe_monthly_update') : update_mailchimp('unsubscribe_monthly_update') end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload