Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request DynamoMTL#56 from DynamoMTL/feature/calm-current-o…
Browse files Browse the repository at this point in the history
…rder

Chimpy includes ControllerHelpers::Order helper everywhere
  • Loading branch information
braidn committed Apr 9, 2015
2 parents 08cacf0 + 265a49c commit 39a5209
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 23 additions & 15 deletions lib/spree/chimpy/controller_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions spec/controllers/controller_filters_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spree_chimpy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 39a5209

Please sign in to comment.