camaleon_mailchimp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +37 -0
  5. data/app/controllers/plugins/camaleon_mailchimp/admin_controller.rb +61 -0
  6. data/app/controllers/plugins/camaleon_mailchimp/front_controller.rb +8 -0
  7. data/app/helpers/plugins/camaleon_mailchimp/main_helper.rb +56 -0
  8. data/app/models/plugins/camaleon_mailchimp/camaleon_mailchimp.rb +12 -0
  9. data/app/views/plugins/camaleon_mailchimp/_register_form.erb +5 -0
  10. data/app/views/plugins/camaleon_mailchimp/_update_user_more_action.erb +112 -0
  11. data/app/views/plugins/camaleon_mailchimp/admin/settings.html.erb +27 -0
  12. data/config/camaleon_plugin.json +42 -0
  13. data/config/custom_models.rb +133 -0
  14. data/config/locales/translation.yml +47 -0
  15. data/config/routes.rb +29 -0
  16. data/lib/camaleon_mailchimp/engine.rb +4 -0
  17. data/lib/camaleon_mailchimp/version.rb +3 -0
  18. data/lib/camaleon_mailchimp.rb +4 -0
  19. data/lib/tasks/camaleon_mailchimp_tasks.rake +4 -0
  20. data/test/camaleon_mailchimp_test.rb +7 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/test/dummy/bin/bundle +3 -0
  29. data/test/dummy/bin/rails +4 -0
  30. data/test/dummy/bin/rake +4 -0
  31. data/test/dummy/bin/setup +29 -0
  32. data/test/dummy/config/application.rb +26 -0
  33. data/test/dummy/config/boot.rb +5 -0
  34. data/test/dummy/config/database.yml +25 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +41 -0
  37. data/test/dummy/config/environments/production.rb +79 -0
  38. data/test/dummy/config/environments/test.rb +42 -0
  39. data/test/dummy/config/initializers/assets.rb +11 -0
  40. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  42. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  43. data/test/dummy/config/initializers/inflections.rb +16 -0
  44. data/test/dummy/config/initializers/mime_types.rb +4 -0
  45. data/test/dummy/config/initializers/session_store.rb +3 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +23 -0
  48. data/test/dummy/config/routes.rb +56 -0
  49. data/test/dummy/config/secrets.yml +22 -0
  50. data/test/dummy/config.ru +4 -0
  51. data/test/dummy/public/404.html +67 -0
  52. data/test/dummy/public/422.html +67 -0
  53. data/test/dummy/public/500.html +66 -0
  54. data/test/dummy/public/favicon.ico +0 -0
  55. data/test/integration/navigation_test.rb +8 -0
  56. data/test/test_helper.rb +20 -0
  57. metadata +170 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59b9b2da65e3f63e6462246b1b7c96a53cfb90c0
4
+ data.tar.gz: 124848d87d8748172d8f8f5982aed820008be02a
5
+ SHA512:
6
+ metadata.gz: 5689a4bfbd2eef0c0871f92ffc63b4cfff67c7ec33ef843bf837673b329462fa841cd2a19ec7630b3a79773cf0dccc7f73f0ebb6770ed8d8dc1f71711f518b0f
7
+ data.tar.gz: 47f39ff6f1c94ec6ba3260201060b8b6a4c964fa82f71947161dd7ebd0904ee0d2efb2e0339b9e3d37d7168a94528f4668afd77dc805887f990fa4a817229202
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Raúl Anatol
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = CamaleonMailchimp
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CamaleonMailchimp'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,61 @@
1
+ class Plugins::CamaleonMailchimp::AdminController < Apps::PluginsAdminController
2
+ include Plugins::CamaleonMailchimp::MainHelper
3
+
4
+ def settings
5
+ @mailchimp = current_site.get_meta('mailchimp_config')
6
+ end
7
+
8
+ def save_settings
9
+ current_site.set_meta('mailchimp_config',
10
+ {
11
+ api_key: params[:mailchimp][:api_key],
12
+ list_id: params[:mailchimp][:list_id]
13
+ })
14
+ flash[:notice] = "#{t('plugin.mailchimp.messages.settings_saved')}"
15
+ redirect_to action: :settings
16
+ end
17
+
18
+
19
+ def subscribe
20
+ user = current_site.users.find(params[:user_id])
21
+ error = user.mailchimp_subscribe!
22
+ if error.nil?
23
+ result = {
24
+ message: {
25
+ title: t('plugin.mailchimp.success.subscribe.title'),
26
+ close: t('plugin.mailchimp.close'),
27
+ }
28
+ }
29
+ else
30
+ result = {
31
+ errors: {
32
+ title: error[:title],
33
+ msg: error[:message],
34
+ close: t('plugin.mailchimp.close'),
35
+ }
36
+ }
37
+ end
38
+ render json: result
39
+ end
40
+
41
+ def unsubscribe
42
+ user = current_site.users.find(params[:user_id])
43
+ if user.mailchimp_unsubscribe!
44
+ result = {
45
+ message: {
46
+ title: t('plugin.mailchimp.success.unsubscribe.title'),
47
+ close: t('plugin.mailchimp.close'),
48
+ }
49
+ }
50
+ else
51
+ result = {
52
+ errors: {
53
+ title: t('plugin.mailchimp.error.unsubscribe.title'),
54
+ msg: t('plugin.mailchimp.error.unsubscribe.message'),
55
+ close: t('plugin.mailchimp.close'),
56
+ }
57
+ }
58
+ end
59
+ render json: result
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ class Plugins::CamaleonMailchimp::FrontController < Apps::PluginsFrontController
2
+ include Plugins::CamaleonMailchimp::MainHelper
3
+ def index
4
+ # actions for frontend module
5
+ end
6
+
7
+ # add custom methods below
8
+ end
@@ -0,0 +1,56 @@
1
+ module Plugins::CamaleonMailchimp::MainHelper
2
+ def self.included(klass)
3
+ # klass.helper_method [:my_helper_method] rescue "" # here your methods accessible from views
4
+ end
5
+
6
+ # here all actions on going to active
7
+ # you can run sql commands like this:
8
+ # results = ActiveRecord::Base.connection.execute(query);
9
+ # plugin: plugin model
10
+ def camaleon_mailchimp_on_active(plugin)
11
+ generate_custom_field_newsletter
12
+
13
+ current_site.set_meta('mailchimp_config', {api_key: 'XXX', list_id: 'XXX'})
14
+ end
15
+
16
+ # here all actions on going to inactive
17
+ # plugin: plugin model
18
+ def camaleon_mailchimp_on_inactive(plugin)
19
+ end
20
+
21
+ # here all actions to upgrade for a new version
22
+ # plugin: plugin model
23
+ def camaleon_mailchimp_on_upgrade(plugin)
24
+ end
25
+
26
+ def mailchimp_user_register_form(plugin)
27
+ plugin[:html] << render('plugins/camaleon_mailchimp/register_form', f: plugin[:f])
28
+ end
29
+
30
+ def mailchimp_user_update_more_actions(plugin)
31
+ plugin[:html] << render('plugins/camaleon_mailchimp/update_user_more_action', f: plugin[:f])
32
+ end
33
+
34
+ def camaleon_mailchimp_plugin_options(arg)
35
+ arg[:links] << link_to(t('plugin.mailchimp.settings.link_name'), admin_plugins_camaleon_mailchimp_settings_path)
36
+ end
37
+
38
+ def mailchimp_user_after_register(plugin)
39
+ if params[:user][:newsletter_enabled] == '1'
40
+ user = plugin[:user]
41
+ user.mailchimp_subscribe!
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def generate_custom_field_newsletter
48
+ group = User.first.get_user_field_groups(current_site).where({slug: 'plugin_mailchimp_user_data'})
49
+ unless group.present?
50
+ new_group = group.create({name: 'Mailchimp user data', slug: 'plugin_mailchimp_user_data', description: 'Mailchimp newsletter user subscription data'})
51
+ new_group.add_field({:name => "t('plugin.mailchimp.user.newsletter_subscribed')", :slug => 'mailchimp_newsletter_subscribed'}, {field_key: 'checkbox', default_value: false})
52
+ new_group.add_field({:name => "t('plugin.mailchimp.user.newsletter_enabled_at')", :slug => 'mailchimp_newsletter_enabled_at'}, {field_key: 'date'})
53
+ new_group.add_field({:name => "t('plugin.mailchimp.user.member_id')", :slug => 'mailchimp_member_id'}, {field_key: 'text_box'})
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ # class Plugins::CamaleonMailchimp::Models::CamaleonMailchimp < ActiveRecord::Base
2
+ # attr_accessible :path, :browser_key
3
+ # belongs_to :site
4
+
5
+ # here create your models normally
6
+ # notice: your tables in database will be plugins_camaleon_mailchimp in plural (check rails documentation)
7
+ # end
8
+
9
+ # here your default models customization
10
+ # Site.class_eval do
11
+ # has_many :camaleon_mailchimp, class_name: "Plugins::CamaleonMailchimp::Models::CamaleonMailchimp"
12
+ # end
@@ -0,0 +1,5 @@
1
+ <div class="form-group">
2
+ <div class="col-md-12">
3
+ <%= f.check_box :newsletter_enabled, :class => 'form_control', :checked => true %> <span class="login-box">Subscribe newsletter</span>
4
+ </div>
5
+ </div>
@@ -0,0 +1,112 @@
1
+ <div class="col-md-3">
2
+ <div class="panel panel-default panel-lite plugin-panel-user-tags">
3
+ <div class="panel-heading">
4
+ <h3 class="panel-title">Mailchimp</h3>
5
+ <ul class="panel-controls">
6
+ <li><a href="#" class="panel-collapse"><span class="fa fa-angle-down"></span></a></li>
7
+ </ul>
8
+ </div>
9
+ <div class="panel-body ">
10
+ <% if !@user.the_newsletter_subscribed? %>
11
+ <a href="#" class="btn btn-success btn-block" data-toggle="modal" data-target="#modal_plugin_mailchimp_subscribe">
12
+ <%= t('plugin.mailchimp.form.subscribe') %>
13
+ </a>
14
+ <% else %>
15
+ <a href="#" class="btn btn-danger btn-block" data-toggle="modal" data-target="#modal_plugin_mailchimp_unsubscribe">
16
+ <%= t('plugin.mailchimp.form.unsubscribe') %>
17
+ </a>
18
+ <% end %>
19
+ </div>
20
+ </div>
21
+ </div>
22
+
23
+ <div class="modal animated fadeIn" id="modal_plugin_mailchimp_subscribe" tabindex="-1" role="dialog" aria-labelledby="smallModalHead" aria-hidden="true">
24
+ <div class="modal-dialog">
25
+ <div class="modal-content">
26
+ <div class="modal-header">
27
+ <button type="button" class="close" data-dismiss="modal">
28
+ <span aria-hidden="true">&times;</span><span class="sr-only"><%= t('plugin.mailchimp.modal.close') %></span>
29
+ </button>
30
+ <h4 class="modal-title" id="smallModalHead"><%= t('plugin.mailchimp.modal.subscribe.title') %></h4>
31
+ </div>
32
+ <div class="modal-body">
33
+ <p><%= t('plugin.mailchimp.modal.subscribe.body') %></p>
34
+ <%= form_for @user, url: @user.new_record? ? admin_users_path : admin_plugins_camaleon_mailchimp_subscribe_path(@user), html: {class: 'form-ajax-mailchimp-subscribe'} do |f| %>
35
+ <div class="modal-body form-horizontal form-group-separated">
36
+ <div class="modal-footer">
37
+ <button type="submit" class="btn btn-danger mailchimp-subscribe-btn"><%= t('plugin.mailchimp.modal.subscribe.submit') %></button>
38
+ <button type="button" class="btn btn-default" data-dismiss="modal"><%= t('plugin.mailchimp.modal.close') %></button>
39
+ </div>
40
+ </div>
41
+ <% end %>
42
+ </div>
43
+ </div>
44
+ </div>
45
+ </div>
46
+
47
+ <div class="modal animated fadeIn" id="modal_plugin_mailchimp_unsubscribe" tabindex="-1" role="dialog" aria-labelledby="smallModalHead" aria-hidden="true">
48
+ <div class="modal-dialog">
49
+ <div class="modal-content">
50
+ <div class="modal-header">
51
+ <button type="button" class="close" data-dismiss="modal">
52
+ <span aria-hidden="true">&times;</span><span class="sr-only"><%= t('plugin.mailchimp.modal.close') %></span>
53
+ </button>
54
+ <h4 class="modal-title" id="smallModalHead"><%= t('plugin.mailchimp.modal.unsubscribe.title') %></h4>
55
+ </div>
56
+ <div class="modal-body">
57
+ <p><%= t('plugin.mailchimp.modal.unsubscribe.body') %></p>
58
+ <%= form_for @user, url: @user.new_record? ? admin_users_path : admin_plugins_camaleon_mailchimp_unsubscribe_path(@user), html: {class: 'form-ajax-mailchimp-unsubscribe'} do |f| %>
59
+ <div class="modal-body form-horizontal form-group-separated">
60
+ <div class="modal-footer">
61
+ <button type="submit" class="btn btn-danger"><%= t('plugin.mailchimp.modal.unsubscribe.submit') %></button>
62
+ <button type="button" class="btn btn-default" data-dismiss="modal"><%= t('plugin.mailchimp.modal.close') %></button>
63
+ </div>
64
+ </div>
65
+ <% end %>
66
+ </div>
67
+ </div>
68
+ </div>
69
+ </div>
70
+
71
+ <% add_asset_library('form_ajax', 'cropper') %>
72
+
73
+ <script>
74
+ jQuery(function ($) {
75
+ $('.form-ajax-mailchimp-subscribe').ajaxForm({
76
+ success: function (res) {
77
+ if (res.errors) {
78
+ $.fn.alert({
79
+ type: 'error',
80
+ title: res.errors.title,
81
+ content: res.errors.msg,
82
+ icon: 'times',
83
+ close: res.errors.close
84
+ });
85
+ } else {
86
+ $.fn.alert({title: res.message.msg, close: res.message.close});
87
+ $("#modal_plugin_mailchimp_subscribe").modal('hide');
88
+ window.location.href = '<%= admin_users_path %>';
89
+ }
90
+ }
91
+ });
92
+
93
+ $('.form-ajax-mailchimp-unsubscribe').ajaxForm({
94
+ success: function (res) {
95
+ if (res.errors) {
96
+ $.fn.alert({
97
+ type: 'error',
98
+ title: res.errors.title,
99
+ content: res.errors.msg,
100
+ icon: 'times',
101
+ close: res.errors.close
102
+ });
103
+ } else {
104
+ $.fn.alert({title: res.message.msg, close: res.message.close});
105
+ $("#modal_plugin_mailchimp_unsubscribe").modal('hide');
106
+ window.location.href = '<%= admin_users_path %>';
107
+ }
108
+ }
109
+ });
110
+
111
+ });
112
+ </script>
@@ -0,0 +1,27 @@
1
+ <%= form_for(admin_plugins_camaleon_mailchimp_settings_path, html: {id: 'mailchimp_form', class: 'validate'}) do %>
2
+ <div class="panel panel-default">
3
+
4
+ <div class="panel-heading">
5
+ <h3 class="panel-title"> <%= t('plugin.mailchimp.settings.title') %></h3>
6
+ </div>
7
+ <div class="panel-body">
8
+ <div class="form-group">
9
+ <div class="input-group">
10
+ <span class="input-group-addon"><%= t('plugin.mailchimp.form.api_key') %> </span>
11
+ <%= text_field_tag("mailchimp[api_key]", @mailchimp[:api_key], class: "required form-control") %>
12
+ </div>
13
+ </div>
14
+ <div class="form-group">
15
+ <div class="input-group">
16
+ <span class="input-group-addon"><%= t('plugin.mailchimp.form.list_id') %> </span>
17
+ <%= text_field_tag("mailchimp[list_id]", @mailchimp[:list_id], class: "required form-control") %>
18
+ </div>
19
+ </div>
20
+ </div>
21
+ <div class="panel-footer">
22
+ <a class="btn btn-default" href="<%= url_for admin_plugins_path %>"><%= t('admin.button.back') %></a>
23
+ <button class="btn btn-primary pull-right" type="submit"><%= t('admin.button.submit') %></button>
24
+ </div>
25
+ </div>
26
+
27
+ <% end %>
@@ -0,0 +1,42 @@
1
+ {
2
+ "title": "Mailchimp",
3
+ "descr": "",
4
+ "version": "0.1",
5
+ "key": "camaleon_mailchimp",
6
+ "helpers": [
7
+ "Plugins::CamaleonMailchimp::MainHelper"
8
+ ],
9
+ "options": [
10
+ {
11
+ "label": "Settings",
12
+ "url": "admin_plugins_camaleon_mailchimp_settings_path",
13
+ "eval_url": true
14
+ }
15
+ ],
16
+ "hooks": {
17
+ "on_active": [
18
+ "camaleon_mailchimp_on_active"
19
+ ],
20
+ "on_inactive": [
21
+ "camaleon_mailchimp_on_inactive"
22
+ ],
23
+ "on_upgrade": [
24
+ "camaleon_mailchimp_on_upgrade"
25
+ ],
26
+ "plugin_options": [
27
+ "camaleon_mailchimp_plugin_options"
28
+ ],
29
+ "user_after_register": [
30
+ "mailchimp_user_after_register"
31
+ ],
32
+ "user_register_form": [
33
+ "mailchimp_user_register_form"
34
+ ],
35
+ "user_after_update": [
36
+ "mailchimp_user_after_update"
37
+ ],
38
+ "user_update_more_actions": [
39
+ "mailchimp_user_update_more_actions"
40
+ ]
41
+ }
42
+ }
@@ -0,0 +1,133 @@
1
+ require 'gibbon'
2
+ # Extending User Model to add newsletter attributes
3
+ User.class_eval do
4
+
5
+ def the_newsletter_subscribed?
6
+ self.get_field_value('mailchimp_newsletter_subscribed').to_s.to_bool
7
+ end
8
+
9
+ def the_newsletter_enabled_at?
10
+ self.get_field_value('mailchimp_newsletter_enabled_at').to_date
11
+ end
12
+
13
+ def the_mailchimp_member_id
14
+ self.get_field_value('mailchimp_member_id').to_s
15
+ end
16
+
17
+ def mailchimp_subscribe!
18
+ error = nil
19
+ begin
20
+ new_member = mailchimp_api_subscribe
21
+ mailchimp_update_subscription_values!(new_member) unless new_member.nil?
22
+ error
23
+ rescue Gibbon::MailChimpError => mailchimp_exception
24
+ if mailchimp_exception.title.downcase.include? 'exists'
25
+ Rails.logger.error "[Mailchimp plugin error] EXISTS: #{mailchimp_exception} title: #{mailchimp_exception.title} detail: #{mailchimp_exception.detail} body: #{mailchimp_exception.body}"
26
+ error = {
27
+ :message => I18n.t('plugin.mailchimp.error.exists_subscribe.message'),
28
+ :title => I18n.t('plugin.mailchimp.error.exists_subscribe.title')
29
+ }
30
+ else
31
+ error = {
32
+ :message => I18n.t('plugin.mailchimp.error.generic.message'),
33
+ :title => I18n.t('plugin.mailchimp.error.generic.title')
34
+ }
35
+ end
36
+ rescue Exception => exception
37
+ Rails.logger.warn "[Mailchimp plugin] Error trying to subscribe an new user: #{id} - Error: #{exception}"
38
+ error = {
39
+ :message => I18n.t('plugin.mailchimp.error.generic.message'),
40
+ :title => I18n.t('plugin.mailchimp.error.generic.title')
41
+ }
42
+ ensure
43
+ error
44
+ end
45
+ end
46
+
47
+ def mailchimp_unsubscribe!
48
+ begin
49
+ body_value = {
50
+ status: 'unsubscribed'
51
+ }
52
+ plugin_config = current_site.get_meta('mailchimp_config')
53
+ mailchimp_api_key = plugin_config[:api_key]
54
+ list_id = plugin_config[:list_id]
55
+ member_id = the_mailchimp_member_id
56
+ Rails.logger.info "[Mailchimp plugin] Start unsubscribe list: #{list_id} api: #{mailchimp_api_key} member_id: #{member_id}"
57
+ gibbon = Gibbon::Request.new(api_key: mailchimp_api_key)
58
+ gibbon.lists(list_id).members(member_id).update(body: body_value)
59
+ mailchimp_update_unsubscription_values!
60
+ rescue Gibbon::MailChimpError => exception
61
+ Rails.logger.error "[Mailchimp plugin error] exception: #{exception} title: #{exception.title} detail: #{exception.detail} body: #{exception.body}"
62
+ false
63
+ rescue Exception => exception
64
+ Rails.logger.warn "[Mailchimp plugin] Error trying to unsubscribe an new user: #{id} - Error: #{exception}"
65
+ false
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def mailchimp_api_subscribe
72
+ body_value = {
73
+ email_address: email,
74
+ status: 'subscribed',
75
+ merge_fields: {
76
+ FNAME: meta[:first_name],
77
+ LNAME: meta[:last_name]
78
+ }
79
+ }
80
+ plugin_config = current_site.get_meta('mailchimp_config')
81
+ mailchimp_api_key = plugin_config[:api_key]
82
+ list_id = plugin_config[:list_id]
83
+ gibbon = Gibbon::Request.new(api_key: mailchimp_api_key)
84
+ gibbon.lists(list_id).members.create(body: body_value)
85
+ end
86
+
87
+ #
88
+ # def mailchimp_api_update_subscription
89
+ # begin
90
+ # body_value = {
91
+ # email_address: email,
92
+ # status: 'subscribed',
93
+ # merge_fields: {
94
+ # FNAME: meta[:first_name],
95
+ # LNAME: meta[:last_name]
96
+ # }
97
+ # }
98
+ # plugin_config = current_site.get_meta('mailchimp_config')
99
+ # mailchimp_api_key = plugin_config[:api_key]
100
+ # list_id = plugin_config[:list_id]
101
+ # gibbon = Gibbon::Request.new(api_key: mailchimp_api_key)
102
+ # gibbon.lists(list_id).members(mem).update(body: body_value)
103
+ # rescue Gibbon::MailChimpError => exception
104
+ # Rails.logger.error "[Mailchimp plugin error] exception: #{exception} title: #{exception.title} detail: #{exception.detail} body: #{exception.body}"
105
+ # nil
106
+ # end
107
+ # end
108
+
109
+ def mailchimp_update_unsubscription_values!
110
+ update_mailchimp_values(0, '', '')
111
+ end
112
+
113
+ def mailchimp_update_subscription_values!(new_member)
114
+ update_mailchimp_values(1, Time.zone.now, new_member['id'])
115
+ end
116
+
117
+
118
+ def update_mailchimp_values(subscribed, enabled_at, member_id)
119
+ field_groups = self.get_user_field_groups(current_site).where({:slug => 'plugin_mailchimp_user_data'}).first
120
+ field_newsletter_subscribed = field_groups.get_field('mailchimp_newsletter_subscribed')
121
+ field_newsletter_enabled_at = field_groups.get_field('mailchimp_newsletter_enabled_at')
122
+ field_member_id = field_groups.get_field('mailchimp_member_id')
123
+
124
+ values_to_save = {
125
+ :mailchimp_newsletter_subscribed => {id: field_newsletter_subscribed.id, values: [subscribed]},
126
+ :mailchimp_newsletter_enabled_at => {id: field_newsletter_enabled_at.id, values: [enabled_at]},
127
+ :mailchimp_member_id => {id: field_member_id.id, values: [member_id]}
128
+ }
129
+
130
+ set_field_values(values_to_save)
131
+ end
132
+ end
133
+
@@ -0,0 +1,47 @@
1
+ en:
2
+ plugin:
3
+ mailchimp:
4
+ settings:
5
+ link_name: 'Settings'
6
+ title: "Mailchimp plugin"
7
+ user:
8
+ newsletter_subscribed: 'Subscribed'
9
+ newsletter_enabled_at: 'From:'
10
+ messages:
11
+ settings_saved: 'Settings saved!'
12
+ modal:
13
+ unsubscribe:
14
+ title: "Unsubscribe user"
15
+ body: "Are you sure?"
16
+ submit: "Unsubscribe"
17
+ subscribe:
18
+ submit: "Subscribe"
19
+ body: "Are you sure?"
20
+ title: "Subscribe user"
21
+ form:
22
+ api_key: "API Key"
23
+ list_id: "List ID"
24
+ subscribe: "Subscribe"
25
+ unsubscribe: "Unsubscribe"
26
+ success:
27
+ subscribe:
28
+ title: "User subscribed"
29
+ unsubscribe:
30
+ title: "User unsubscribed"
31
+ error:
32
+ subscribe:
33
+ title: "Error when subscribing to Mailchimp"
34
+ unsubscribe:
35
+ title: "Error when unsubscribing to Mailchimp"
36
+ message: "An error has occurred to do subscription"
37
+ close: "Close"
38
+ es:
39
+ plugin:
40
+ mailchimp:
41
+ settings:
42
+ link_name: 'Preferencias'
43
+ user:
44
+ newsletter_subscribed: 'Suscrito'
45
+ newsletter_enabled_at: 'Desde:'
46
+ messages:
47
+ settings_saved: '¡Preferencias guardadas!'
data/config/routes.rb ADDED
@@ -0,0 +1,29 @@
1
+ Rails.application.routes.draw do
2
+
3
+ scope '(:locale)', locale: /#{PluginRoutes.all_locales}/, :defaults => {} do
4
+ # frontend
5
+ namespace :plugins do
6
+ namespace 'camaleon_mailchimp' do
7
+ get 'index' => 'front#index'
8
+ end
9
+ end
10
+ end
11
+
12
+ #Admin Panel
13
+ scope 'admin', as: 'admin' do
14
+ namespace 'plugins' do
15
+ namespace 'camaleon_mailchimp' do
16
+ get 'settings' => 'admin#settings'
17
+ post 'settings' => 'admin#save_settings'
18
+
19
+ patch ':user_id/subscribe' => 'admin#subscribe', :as => 'subscribe'
20
+ patch ':user_id/unsubscribe' => 'admin#unsubscribe', :as => 'unsubscribe'
21
+ end
22
+ end
23
+ end
24
+
25
+ # main routes
26
+ #scope 'camaleon_mailchimp', module: 'plugins/camaleon_mailchimp/', as: 'camaleon_mailchimp' do
27
+ # Here my routes for main routes
28
+ #end
29
+ end
@@ -0,0 +1,4 @@
1
+ module CamaleonMailchimp
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module CamaleonMailchimp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "camaleon_mailchimp/engine"
2
+
3
+ module CamaleonMailchimp
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :camaleon_mailchimp do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CamaleonMailchimpTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, CamaleonMailchimp
6
+ end
7
+ end