cheddargetter_client_rails 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,186 @@
1
+ module CheddargetterClientRails
2
+ class Subscription
3
+ include ActiveModel::Validations
4
+
5
+ Months = ("01".."12").freeze
6
+ Years = (Date.current.year..Date.current.year+10).collect{|y| y.to_s }.freeze
7
+
8
+ attr_accessor :planCode,
9
+ :company,
10
+ :firstName,
11
+ :lastName,
12
+ :ccFirstName,
13
+ :ccLastName,
14
+ :ccExpiration,
15
+ :ccNumber,
16
+ :ccLastFour,
17
+ :ccCountry,
18
+ :ccAddress,
19
+ :ccCity,
20
+ :ccState,
21
+ :customerCode,
22
+ :email,
23
+ :zip
24
+
25
+ validates_presence_of :firstName,
26
+ :lastName,
27
+ :email,
28
+ :planCode
29
+ #:customerCode, generally we call valid before unique identifier is called
30
+
31
+ validates_presence_of :ccNumber,
32
+ :ccExpiration,
33
+ :zip, :if => :paid_plan?
34
+
35
+ validate :unexpired
36
+
37
+ validate :validates_presence_of_humanized
38
+
39
+ def paid_plan?
40
+ !free_plan?
41
+ end
42
+
43
+ def free_plan?
44
+ planCode == 'FREE_PLAN'
45
+ end
46
+
47
+ def unexpired
48
+ if ccExpiration.present?
49
+ month, year = ccExpiration.split("/").collect{|string| string.to_i }
50
+ year = ('20' + year.to_s).to_i if year.size < 4
51
+ month = ('0' + month.to_s).to_i if month.size < 2
52
+
53
+ if Date.civil(year, month + 1) <= Date.today
54
+ errors.add(:ccExpiration, 'has been reached')
55
+ end
56
+ end
57
+ end
58
+
59
+ def validates_presence_of_humanized
60
+ if planCode.blank?
61
+ self.errors.add(:base, 'You must select a billing plan')
62
+ end
63
+ end
64
+
65
+ def initialize(hash = {})
66
+ hash.each { |k, v| send("#{k}=", v) }
67
+ end
68
+
69
+ def self.get(customer_code)
70
+ response = CGClient.get_customer(:code => customer_code)
71
+
72
+ if response.errors.blank?
73
+ build_from_response(response)
74
+ end
75
+ end
76
+
77
+ def self.build_from_response(response)
78
+ customer_subscription = response.try(:customer_subscription)
79
+ customer_plan = response.try(:customer_plan)
80
+
81
+ if customer_plan and customer_subscription
82
+ new(
83
+ :firstName => response.customer[:firstName],
84
+ :lastName => response.customer[:lastName],
85
+ :email => response.customer[:email],
86
+ :ccLastFour => customer_subscription[:ccLastFour],
87
+ :ccFirstName => customer_subscription[:ccFirstName],
88
+ :ccLastName => customer_subscription[:ccLastName],
89
+ :planCode => customer_plan[:code],
90
+ :zip => customer_subscription[:ccZip],
91
+ :ccExpiration => customer_subscription[:ccExpirationDate],
92
+ :customerCode => response.customer[:code]
93
+ )
94
+ end
95
+ end
96
+
97
+ def new_record?
98
+ !Subscription.get(customerCode)
99
+ end
100
+
101
+ def save
102
+ return false if !valid?
103
+
104
+ if new_record?
105
+ create
106
+ else
107
+ update
108
+ end
109
+ end
110
+
111
+ def create
112
+ response = CGClient.new_customer(
113
+ :code => customerCode,
114
+ :firstName => firstName,
115
+ :lastName => lastName,
116
+ :email => email,
117
+ :subscription => {
118
+ :planCode => planCode,
119
+ :ccFirstName => ccFirstName,
120
+ :ccLastName => ccLastName,
121
+ :ccNumber => ccNumber,
122
+ :ccExpiration => ccExpiration,
123
+ :ccZip => zip
124
+ }
125
+ )
126
+
127
+ add_errors_or_return_valid_response(response)
128
+ end
129
+
130
+ def add_errors_or_return_valid_response(response)
131
+ #this returns cheddargetter errors.
132
+ #hopefully most errors are handled before this in the valid? calls
133
+ #which return prettier errors, but inevitably some will not be caught.
134
+ if response.try(:errors).try(:any?)
135
+ response.errors.each do |error|
136
+ errors.add(:base, error[:text])
137
+ end
138
+
139
+ return false
140
+ else
141
+ response
142
+ end
143
+ end
144
+
145
+ def update
146
+ response = CGClient.edit_customer(
147
+ {:code => customerCode},
148
+ {
149
+ :firstName => firstName,
150
+ :lastName => lastName,
151
+ :email => email,
152
+ :subscription => {
153
+ :planCode => planCode,
154
+ :ccFirstName => ccFirstName,
155
+ :ccLastName => ccLastName,
156
+ :ccNumber => ccNumber,
157
+ :ccExpiration => ccExpiration,
158
+ :ccZip => zip
159
+ }
160
+ }
161
+ )
162
+
163
+ add_errors_or_return_valid_response(response)
164
+ end
165
+
166
+ def destroy
167
+ raise "Invalid customer code" if customerCode.blank?
168
+ response = CGClient.delete_customer({ :code => customerCode })
169
+
170
+ add_errors_or_return_valid_response(response)
171
+ end
172
+
173
+ def instance_variables_hash
174
+ {
175
+ :customerCode => customerCode,
176
+ :firstName => firstName,
177
+ :ccLastName => ccLastName,
178
+ :ccFirstName => ccFirstName,
179
+ :planCode => planCode,
180
+ :zip => zip,
181
+ :lastName => lastName,
182
+ :email => email
183
+ }
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,199 @@
1
+ require 'spec_helper'
2
+
3
+ describe CheddargetterClientRails::Subscription do
4
+ let(:user) { double("TestUser") }
5
+ let(:subscription) { CheddargetterClientRails::Subscription.new }
6
+ let(:customer_code) { 'JOHN_DOE' }
7
+
8
+ let(:valid_subscription_attributes) {
9
+ {
10
+ :planCode => 'PAID_PLAN',
11
+ :firstName => 'Joe',
12
+ :lastName => 'Collins',
13
+ :ccNumber => '4111111111111111',
14
+ :ccExpiration => '04/' + 3.years.from_now.year.to_s,
15
+ :zip => '47401',
16
+ :email => 'jim@test.com'
17
+ }
18
+ }
19
+
20
+ before {
21
+ user.should_receive(:customer_code=).with(customer_code)
22
+ user.customer_code = subscription.customerCode = customer_code
23
+ }
24
+
25
+ describe 'subscription#create' do
26
+ subject { subscription.create }
27
+ before {
28
+ subscription.stub(:add_errors_or_return_valid_response).and_return true
29
+ subscription.should_receive(:add_errors_or_return_valid_response)
30
+ CGClient.should_receive(:new_customer)
31
+ }
32
+
33
+ it { subject }
34
+ end
35
+
36
+ describe 'subscription#update' do
37
+ subject { subscription.update }
38
+ before {
39
+ subscription.stub(:add_errors_or_return_valid_response).and_return true
40
+ subscription.should_receive(:add_errors_or_return_valid_response)
41
+ CGClient.should_receive(:edit_customer)
42
+ }
43
+
44
+ it { subject }
45
+ end
46
+
47
+ describe 'subscription#new_record?' do
48
+ subject { subscription.new_record? }
49
+
50
+ context 'when a subscription already exists' do
51
+ before { CheddargetterClientRails::Subscription.stub(:get).and_return true }
52
+ it { should be(false) }
53
+ end
54
+
55
+ context 'when no subscription exists' do
56
+ before { CheddargetterClientRails::Subscription.stub(:get).and_return false }
57
+ it { should be(true) }
58
+ end
59
+ end
60
+
61
+ describe 'subscription#save' do
62
+ subject { subscription.save }
63
+
64
+ context 'when a subscription already exists' do
65
+ before { subscription.stub(:new_record?).and_return false }
66
+ before { valid_subscription_attributes.each {|attribute, value| subscription.send(attribute.to_s + '=', value )} }
67
+ before { subscription.should_receive(:update) }
68
+ it { subject }
69
+ end
70
+
71
+ context 'when a subscription already exists' do
72
+ before { CheddargetterClientRails::Subscription.stub(:new_record?).and_return true }
73
+ before { valid_subscription_attributes.each {|attribute, value| subscription.send(attribute.to_s + '=', value )} }
74
+ before { subscription.should_receive(:create) }
75
+ it { subject }
76
+ end
77
+
78
+ context 'when not valid' do
79
+ before { CheddargetterClientRails::Subscription.stub(:new_record?).and_return true }
80
+
81
+ specify { subject; (subscription.errors.length > 1).should be_true }
82
+ end
83
+ end
84
+
85
+ describe 'subscription#destroy' do
86
+ subject { subscription.destroy }
87
+ context 'without customerCode' do
88
+ before { subscription.customerCode = nil }
89
+ specify { lambda{subject}.should raise_error }
90
+ end
91
+
92
+ context 'with customerCode' do
93
+ specify { lambda{subject}.should_not raise_error }
94
+ end
95
+ end
96
+
97
+ describe 'validates_presence_of' do
98
+ # this tests validations
99
+ #
100
+ # validates_presence_of :firstName,
101
+ # :lastName,
102
+ # :email,
103
+ # :customerCode,
104
+ # :planCode
105
+ #
106
+ # validates_presence_of :ccNumber,
107
+ # :ccExpiration,
108
+ # :zip, :if => :paid_plan?
109
+
110
+ subject { subscription.valid? }
111
+ before { valid_subscription_attributes.each{ |attribute, value| subscription.send(attribute.to_s + '=', value) } }
112
+
113
+ context 'when free plan' do
114
+ before { subscription.planCode = "FREE_PLAN" }
115
+
116
+ context 'when firstName is not set' do
117
+ before { subscription.firstName = nil }
118
+ it { should be_false }
119
+ end
120
+ context 'when lastName is not set' do
121
+ before { subscription.lastName = nil }
122
+ it { should be_false }
123
+ end
124
+ context 'when email is not set' do
125
+ before { subscription.email = nil }
126
+ it { should be_false }
127
+ end
128
+ context 'when planCode is not set' do
129
+ before { subscription.planCode = nil }
130
+ it { should be_false }
131
+ end
132
+ context 'when ccNumber is not set' do
133
+ before { subscription.ccNumber = nil }
134
+ it { should be_true }
135
+ end
136
+ context 'when ccExpiration is not set' do
137
+ before { subscription.ccExpiration = nil }
138
+ it { should be_true }
139
+ end
140
+ context 'when zip is not set' do
141
+ before { subscription.zip = nil }
142
+ it { should be_true }
143
+ end
144
+ end
145
+
146
+ context 'when paid plan' do
147
+ context 'when firstName is not set' do
148
+ before { subscription.firstName = nil }
149
+ it { should be_false }
150
+ end
151
+ context 'when lastName is not set' do
152
+ before { subscription.lastName = nil }
153
+ it { should be_false }
154
+ end
155
+ context 'when email is not set' do
156
+ before { subscription.email = nil }
157
+ it { should be_false }
158
+ end
159
+ context 'when planCode is not set' do
160
+ before { subscription.planCode = nil }
161
+ it { should be_false }
162
+ end
163
+ context 'when ccNumber is not set' do
164
+ before { subscription.ccNumber = nil }
165
+ it { should be_false }
166
+ end
167
+ context 'when ccExpiration is not set' do
168
+ before { subscription.ccExpiration = nil }
169
+ it { should be_false }
170
+ end
171
+ context 'when zip is not set' do
172
+ before { subscription.zip = nil }
173
+ it { should be_false }
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'instance_variables_hash' do
179
+ before { valid_subscription_attributes.each {|attribute, value| subscription.send(attribute.to_s + '=', value )} }
180
+ before {
181
+ subscription.customerCode = "CUSTOMER_CODE"
182
+ subscription.ccFirstName = "Joe"
183
+ subscription.ccLastName = "Collins"
184
+ }
185
+ subject { subscription.instance_variables_hash }
186
+ it {
187
+ should eq({
188
+ :planCode => 'PAID_PLAN',
189
+ :firstName => 'Joe',
190
+ :lastName => 'Collins',
191
+ :ccFirstName => 'Joe',
192
+ :ccLastName => 'Collins',
193
+ :zip => '47401',
194
+ :email => 'jim@test.com',
195
+ :customerCode => 'CUSTOMER_CODE'
196
+ })
197
+ }
198
+ end
199
+ end
@@ -0,0 +1,470 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+
4
+
5
+ describe "CheddargetterClientRails" do
6
+ before {
7
+ class TestUser < ActiveRecord::Base
8
+ attr_accessor :customer_code, :first_name, :last_name, :plan_code
9
+
10
+ def self.column_names
11
+ []
12
+ end
13
+
14
+ cheddargetter_billable_on :customer_code, :shared_columns => {
15
+ :firstName => :first_name,
16
+ :lastName => :last_name,
17
+ :ccFirstName => :first_name,
18
+ :ccLastName => :last_name,
19
+ :planCode => :plan_code
20
+ }
21
+
22
+
23
+ end
24
+ }
25
+
26
+ before { TestUser.stub(:connection).and_return mock(:columns => []) }
27
+
28
+ let(:user_class) {
29
+ TestUser
30
+ }
31
+
32
+ let(:user) {
33
+ TestUser.new
34
+ }
35
+
36
+ describe 'module included?' do
37
+ subject { CheddargetterClientRails }
38
+ specify { lambda { subject }.should_not raise_error }
39
+ end
40
+
41
+ describe "inclusion of class methods" do
42
+ subject { user_class.respond_to?(:cheddargetter_billable_on) }
43
+
44
+ it { should be_true }
45
+ end
46
+
47
+ describe 'cheddargetter_billable_on' do
48
+ context 'argument errors' do
49
+ context 'without customer code column' do
50
+ subject {
51
+ class NoCustomerCodeUser < ActiveRecord::Base
52
+ cheddargetter_billable_on
53
+ end
54
+ }
55
+
56
+ specify { lambda { subject }.should raise_error(ArgumentError) }
57
+ end
58
+
59
+ context 'when record does not responsd to customer_code_column' do
60
+ subject {
61
+ class NoCustomerCodeUser < ActiveRecord::Base
62
+ cheddargetter_billable_on :id
63
+ end
64
+ }
65
+ specify { lambda { subject }.should raise_error }
66
+ end
67
+ end
68
+
69
+ context 'setting customer_code_column' do
70
+ subject { user_class.customer_code_column }
71
+ it { should eq(:customer_code) }
72
+ end
73
+
74
+ context 'setting shared_columns' do
75
+ subject { user_class.shared_columns }
76
+ it { should eq( :firstName => :first_name,
77
+ :lastName => :last_name,
78
+ :ccFirstName => :first_name,
79
+ :ccLastName => :last_name,
80
+ :planCode => :plan_code
81
+ )
82
+ }
83
+ end
84
+ end
85
+
86
+ describe 'validate' do
87
+ let!(:test_user) {
88
+ TestUser.new
89
+ }
90
+
91
+ subject { test_user.valid? }
92
+
93
+ it 'should call validate_subscription' do
94
+ test_user.should_receive(:validate_subscription)
95
+ subject
96
+ end
97
+
98
+ it 'should call supplement_subscription_fields' do
99
+ test_user.should_receive(:supplement_subscription_fields)
100
+ subject
101
+ end
102
+ end
103
+
104
+ describe 'before_create' do
105
+ let!(:test_user) {
106
+ TestUser.new
107
+ }
108
+
109
+ before { test_user.should_receive :create_subscription }
110
+ subject { test_user.run_callbacks(:save) }
111
+ it do subject end
112
+ end
113
+
114
+ describe 'subscription' do
115
+ let!(:subscription) { # use ! to set it now!
116
+ CheddargetterClientRails::Subscription.new
117
+ }
118
+
119
+ before {
120
+ CheddargetterClientRails::Subscription.stub(:new).and_return subscription
121
+ user.should_receive(:subscription).at_least(1).times.and_return(subscription)
122
+ }
123
+
124
+ context 'when not yet set' do
125
+ subject { user.subscription }
126
+ it { should eq(subscription) }
127
+ end
128
+
129
+ context 'when already set' do
130
+ before {
131
+ subscription = user.subscription
132
+ subscription.firstName = "First"
133
+ }
134
+
135
+ subject { user.subscription.firstName }
136
+ it { should eq("First") }
137
+ end
138
+ end
139
+
140
+ describe 'validate_subscription' do
141
+ let!(:user) {
142
+ TestUser.new
143
+ }
144
+
145
+ let(:subscription) {
146
+ user.subscription
147
+ }
148
+
149
+ subject { user.validate_subscription }
150
+
151
+ context 'with subscription' do
152
+ context 'that is valid' do
153
+ before {
154
+ user.should_receive(:skip_cheddargetter).and_return false
155
+ user.should_receive(:new_record?).and_return true
156
+ subscription.should_receive(:valid?).and_return true
157
+ }
158
+
159
+ it "should not add errors" do
160
+ subject
161
+ (user.errors.length < 1).should be_true
162
+ end
163
+ end
164
+
165
+ context 'that is invalid' do
166
+ before {
167
+ user.should_receive(:skip_cheddargetter).and_return false
168
+ user.should_receive(:new_record?).and_return true
169
+ subscription.should_receive(:valid?).and_return false
170
+ }
171
+
172
+ it "should add errors" do
173
+ subject
174
+ (user.errors.length < 1).should be_false
175
+ end
176
+ end
177
+
178
+ context 'with no subscription' do
179
+ before {
180
+ user.stub(:skip_cheddargetter).and_return false
181
+ user.stub(:new_record?).and_return true
182
+ }
183
+
184
+ specify { subject; user.valid?.should be_false }
185
+ end
186
+
187
+ context 'when skip_cheddargetter returns true' do
188
+ before {
189
+ user.stub(:skip_cheddargetter).and_return true
190
+ user.stub(:new_record?).and_return true
191
+ subscription.stub(:valid?).and_return true
192
+ }
193
+
194
+ specify { subject; user.valid?.should be_true }
195
+ end
196
+
197
+ context 'when record is not new' do
198
+ before {
199
+ user.stub(:skip_cheddargetter).and_return false
200
+ user.stub(:new_record?).and_return false
201
+ subscription.stub(:valid?).and_return false
202
+ }
203
+
204
+ specify { subject; user.valid?.should be_true }
205
+ end
206
+ end
207
+ end
208
+
209
+ describe 'responds_to_customer_code_column?' do
210
+ context 'when columns include message' do
211
+ before {
212
+ class MessageInColumns < ActiveRecord::Base
213
+ def self.column_names
214
+ ['customer_code']
215
+ end
216
+
217
+ cheddargetter_billable_on :customer_code
218
+ end
219
+ }
220
+
221
+ let(:record_class) {
222
+ MessageInColumns
223
+ }
224
+
225
+ before { record_class.stub(:connection).and_return mock(:columns => [])}
226
+
227
+ subject { record_class.responds_to_customer_code_column? }
228
+ it { should be_true }
229
+ end
230
+
231
+ context 'when instance methods includes message' do
232
+ before {
233
+ class MessageInMethods < ActiveRecord::Base
234
+
235
+ def self.column_names
236
+ []
237
+ end
238
+
239
+ def customer_code
240
+ 'TEST_CODE'
241
+ end
242
+
243
+ p 'Make not that if method and not column, then it must be declared before cheddargetter_billable_on'
244
+
245
+ cheddargetter_billable_on :customer_code
246
+ end
247
+ }
248
+
249
+ let(:record_class) {
250
+ MessageInMethods
251
+ }
252
+
253
+ before { record_class.stub(:connection).and_return mock(:columns => [])}
254
+
255
+ subject { record_class.responds_to_customer_code_column? }
256
+ it { should be_true }
257
+ end
258
+
259
+ context 'when instance does not respond to message' do
260
+ subject {
261
+ class MessageMissing < ActiveRecord::Base
262
+ def self.column_names
263
+ []
264
+ end
265
+
266
+ cheddargetter_billable_on :customer_code
267
+ end
268
+ }
269
+
270
+ specify { lambda { subject }.should raise_error }
271
+ end
272
+ end
273
+
274
+ describe 'supplement_subscription_fields' do
275
+ let!(:user) {
276
+ TestUser.new
277
+ }
278
+
279
+ before { user.class.stub(:shared_columns).and_return({
280
+ :firstName => :first_name,
281
+ :lastName => :last_name,
282
+ :ccFirstName => :first_name,
283
+ :ccLastName => :last_name,
284
+ :planCode => :plan_code
285
+ })
286
+ }
287
+
288
+ before {
289
+ user.customer_code = "FIRST_NAME"
290
+ user.first_name = "First"
291
+ user.last_name = "Last"
292
+ user.plan_code = "TEST_PLAN"
293
+ }
294
+
295
+ subject { user.supplement_subscription_fields }
296
+ specify {
297
+ subject
298
+ user.subscription.firstName.should == "First"
299
+ user.subscription.lastName.should == "Last"
300
+ user.subscription.planCode.should == "TEST_PLAN"
301
+ }
302
+
303
+ context 'when planCode is a string' do
304
+ let!(:user) {
305
+ TestUser.new
306
+ }
307
+
308
+ before { user.class.stub(:shared_columns).and_return({
309
+ :planCode => "EVERYBODYS_PLAN"
310
+ })
311
+ }
312
+
313
+ subject { user.supplement_subscription_fields }
314
+ specify {
315
+ subject
316
+ user.subscription.planCode.should == "EVERYBODYS_PLAN"
317
+ }
318
+ end
319
+ end
320
+
321
+ describe 'create_subscription' do
322
+ let!(:user) {
323
+ TestUser.new
324
+ }
325
+
326
+ subject { user.create_subscription }
327
+
328
+ context 'when skipping cheddargetter' do
329
+ before { user.customer_code = 'TEST_CODE' }
330
+ before { user.skip_cheddargetter = true }
331
+ before { user.subscription.should_not_receive(:create) }
332
+ it do subject end
333
+ end
334
+
335
+ context 'when not skipping cheddargetter' do
336
+ before { user.customer_code = 'TEST_CODE' }
337
+ before { user.skip_cheddargetter = false }
338
+ before { user.subscription.should_receive(:create) }
339
+ it do subject end
340
+ end
341
+
342
+ context 'when subscription customer_code is not set' do
343
+ before { user.customer_code = 'TEST_CODE' }
344
+ it do subject; user.subscription.customerCode.should eq('TEST_CODE') end
345
+ end
346
+
347
+ context 'when user customer code column is not set' do
348
+ specify { lambda { subject }.should raise_error }
349
+ end
350
+ end
351
+
352
+ describe 'current_subscription' do
353
+ let!(:user) {
354
+ TestUser.new
355
+ }
356
+
357
+ let(:subscription) { CheddargetterClientRails::Subscription.new() }
358
+ before { CheddargetterClientRails::Subscription.stub(:get).and_return subscription }
359
+
360
+ context 'when it does not exist' do
361
+ before { user.stub(:customer_code_column_value).and_return nil }
362
+ subject { user.current_subscription }
363
+ it { should be_nil }
364
+ end
365
+
366
+ context 'when it does exist' do
367
+ before { user.stub(:customer_code_column_value).and_return 'CUSTOMER_CODE' }
368
+ context 'when it has not been accessed' do
369
+ subject { user.current_subscription }
370
+ it { should eq(subscription) }
371
+ end
372
+
373
+ context 'when it has been accesssed' do
374
+ before { user.current_subscription.firstName = 'First' }
375
+ subject { user.current_subscription.firstName }
376
+ it { should eq("First") }
377
+ end
378
+ end
379
+ end
380
+
381
+ describe 'destroy_subscription' do
382
+ let!(:user) {
383
+ TestUser.new(:customer_code => 'CUSTOMER_CODE')
384
+ }
385
+
386
+ let(:subscription) { CheddargetterClientRails::Subscription.new }
387
+ before { CheddargetterClientRails::Subscription.stub(:get).and_return subscription }
388
+ before { subscription.should_receive(:destroy) }
389
+
390
+ subject { user.destroy_subscription }
391
+ it { subject }
392
+ end
393
+
394
+ describe 'build_subscription' do
395
+ let!(:current_subscription) {
396
+ CheddargetterClientRails::Subscription.new({:firstName => "First", :lastName => "Last"})
397
+ }
398
+
399
+ let(:subscription_params) {
400
+ {:lastName => 'NewLast'}
401
+ }
402
+
403
+ subject { user.build_subscription(subscription_params) }
404
+
405
+ context 'when current subscription' do
406
+ before { user.stub(:current_subscription).and_return(current_subscription) }
407
+ it 'should use data from current subscription' do
408
+ subject
409
+ user.subscription.firstName.should eq("First")
410
+ end
411
+
412
+ it 'should overwrite data from current_subscription' do
413
+ subject
414
+ user.subscription.lastName.should eq("NewLast")
415
+ end
416
+ end
417
+
418
+ context 'when no current_subscription' do
419
+ it 'should use a blank subscription object' do
420
+ subject
421
+ user.subscription.firstName.should be_nil
422
+ end
423
+
424
+ it 'should fill in new data' do
425
+ subject
426
+ user.subscription.lastName.should eq("NewLast")
427
+ end
428
+ end
429
+ end
430
+
431
+ describe 'customer_code_column_value' do
432
+ subject { user.customer_code_column_value }
433
+
434
+ context 'when customer_code_column is set and value is set' do
435
+ before { user.customer_code = 'Customer Code' }
436
+ it { should eq('Customer Code') }
437
+ end
438
+
439
+ context 'when customer_code_column is not set' do
440
+ before { user.class.stub(:customer_code_column).and_return nil }
441
+ it { should be_nil }
442
+ end
443
+
444
+ context 'when value is not set' do
445
+ it { should be_nil }
446
+ end
447
+ end
448
+
449
+ describe 'save_subscription' do
450
+ let!(:current_subscription) {
451
+ CheddargetterClientRails::Subscription.new({:firstName => "First", :lastName => "Last"})
452
+ }
453
+
454
+ let!(:subscription_params) {
455
+ {:lastName => 'NewLast'}
456
+ }
457
+
458
+ let!(:new_subscription) {
459
+ CheddargetterClientRails::Subscription.new
460
+ }
461
+
462
+ subject { user.save_subscription(subscription_params) }
463
+
464
+ before { CheddargetterClientRails::Subscription.stub(:new).and_return new_subscription }
465
+
466
+ before { user.should_receive(:build_subscription) }
467
+ before { user.subscription.should_receive(:save) }
468
+ it do subject end
469
+ end
470
+ end