diff --git a/Gemfile b/Gemfile index fbdaf4d..77e2cd4 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,6 @@ gem 'spree_auth_devise', github: "spree/spree_auth_devise", branch: '2-3-stable' gem 'libnotify' gem 'fuubar' gem 'byebug' +gem 'pry-byebug' gemspec diff --git a/lib/spree/chimpy/controller_filters.rb b/lib/spree/chimpy/controller_filters.rb index 3082d68..dd84d09 100644 --- a/lib/spree/chimpy/controller_filters.rb +++ b/lib/spree/chimpy/controller_filters.rb @@ -2,24 +2,32 @@ module Spree::Chimpy module ControllerFilters extend ActiveSupport::Concern - included do |variable| - before_filter :find_mail_chimp_params - + included do + before_filter :set_mailchimp_params + before_filter :find_mail_chimp_params, if: :mailchimp_params? include ::Spree::Core::ControllerHelpers::Order + end private - def find_mail_chimp_params - mc_eid = params[:mc_eid] || session[:mc_eid] - mc_cid = params[:mc_cid] || session[:mc_cid] - if (mc_eid || mc_cid) && (session[:order_id] || params[:record_mc_details]) - attributes = {campaign_id: mc_cid, - email_id: mc_eid} - if current_order(create_order_if_necessary: true).source - current_order.source.update_attributes(attributes) - else - current_order.create_source(attributes) - end - end + + attr_reader :mc_eid, :mc_cid + + def set_mailchimp_params + @mc_eid = params[:mc_eid] || session[:mc_eid] + @mc_cid = params[:mc_cid] || session[:mc_cid] + end + + def mailchimp_params? + (!mc_eid.nil? || !mc_cid.nil?) && + (!session[:order_id].nil? || !params[:record_mc_details].nil?) + end + + def find_mail_chimp_params + attributes = { campaign_id: mc_cid, email_id: mc_eid } + if current_order(create_order_if_necessary: true).source + current_order.source.update_attributes(attributes) + else + current_order.create_source(attributes) end end end diff --git a/spec/controllers/controller_filters_spec.rb b/spec/controllers/controller_filters_spec.rb new file mode 100644 index 0000000..40961b1 --- /dev/null +++ b/spec/controllers/controller_filters_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe ::Spree::StoreController do + controller(::Spree::StoreController) do + def index + head :ok + end + end + + let(:user) { create(:user) } + subject { controller } + + before do + allow(subject).to receive(:try_spree_current_user).and_return(user) + subject.session[:order_id] = 'R1919' + end + + it 'sets the attributes for order if eid/cid is set in session' do + subject.session[:mc_eid] = '1234' + subject.session[:mc_cid] = 'abcd' + expect(subject).to receive(:find_mail_chimp_params) + + get :index + end + + it 'sets the attributes for the order if eid/cid is set in the params' do + expect(subject).to receive(:find_mail_chimp_params) + + get :index, mc_eid: '1234', mc_cid: 'abcd' + end + + it 'does not call find mail chimp params method if no eid/cid' do + expect(subject).to_not receive(:find_mail_chimp_params) + + get :index + end +end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index b3906f8..7c8b144 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,16 +1,15 @@ require 'spec_helper' describe Spree::Order do - let(:key) { 'e025fd58df5b66ebd5a709d3fcf6e600-us8' } let(:order) { create(:completed_order_with_totals) } - it "has a source" do + it 'has a source' do order = Spree::Order.new expect(order).to respond_to(:source) end - context "notifying mail chimp" do + context 'notifying mail chimp' do before do Spree::Chimpy::Config.key = nil @@ -21,18 +20,18 @@ subject { Spree::Chimpy } - it "doesnt update when order is not completed" do + it 'doesnt update when order is not completed' do expect(subject).to_not receive(:enqueue) @not_completed_order.update! end - it "updates when order is completed" do + it 'updates when order is completed' do new_order = create(:completed_order_with_pending_payment, state: 'confirm') expect(subject).to receive(:enqueue).with(:order, new_order) new_order.next end - it "sync when order is completed" do + it 'sync when order is completed' do expect(subject).to receive(:enqueue).with(:order, order) order.cancel! end diff --git a/spree_chimpy.gemspec b/spree_chimpy.gemspec index 9b5cd7b..85ab0eb 100644 --- a/spree_chimpy.gemspec +++ b/spree_chimpy.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'mailchimp-api', '~> 2.0.5' s.add_development_dependency 'rspec-rails', '~> 2.14' + s.add_development_dependency 'rubocop' s.add_development_dependency 'capybara', '~> 2.2.1' s.add_development_dependency 'poltergeist' s.add_development_dependency 'factory_girl', '~> 4.4'