cheddargetter_client_rails 0.1.16 → 0.1.26

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -26,20 +26,19 @@ It can be very simple if you go with the default columns, ie:
26
26
 
27
27
  Then the declaration in the model is simply:
28
28
 
29
- class User < ActiveRecord::Base
30
- has_subscription
31
- end
29
+ class User < ActiveRecord::Base
30
+ has_subscription
31
+ end
32
32
 
33
33
  These are the only required columns however you can change their names locally very easily.
34
34
 
35
- class User < ActiveRecord::Base
36
- has_subscription :customerCode => :customer_code,
37
- :firstName => :name
38
- :lastName => :l_name
39
- :email => :business_email
40
- :planCode => "FREE_PLAN"
41
-
42
- end
35
+ class User < ActiveRecord::Base
36
+ has_subscription :customerCode => :customer_code,
37
+ :firstName => :name,
38
+ :lastName => :l_name,
39
+ :email => :business_email,
40
+ :planCode => "FREE_PLAN"
41
+ end
43
42
 
44
43
  Note that the plan code can also take a string.
45
44
 
@@ -56,13 +55,13 @@ The has_subscription will also takes additional key/values of items that appear
56
55
  :company
57
56
  :zip
58
57
 
59
- When the save is called on the subscription object or create is called on the user it grabs all shared attributes from your ActiveRecord record.
58
+ When the save is called on the subscription object or the user it grabs all shared attributes from your ActiveRecord record.
60
59
 
61
60
  === In the controller
62
61
 
63
62
  Make sure the subscription is always set on the user as some data may only exist there.
64
63
 
65
- class CreditCardsController < ApplicationController
64
+ class SubscriptionController < ApplicationController
66
65
  def edit
67
66
  @user = current_user
68
67
  end
@@ -70,7 +69,7 @@ Make sure the subscription is always set on the user as some data may only exist
70
69
  def update
71
70
  @user = current_user
72
71
 
73
- if @user.save_subscription(params[:cheddargetter_client_rails_subscription])
72
+ if @user.update_attributes(params[:user])
74
73
  redirect_to edit_credit_card_path, :flash => {:success => 'Billing information updated'}
75
74
  else
76
75
  render 'edit'
@@ -81,75 +80,125 @@ Make sure the subscription is always set on the user as some data may only exist
81
80
  Or in a user controller
82
81
 
83
82
  class UsersController < ApplicationController
84
- def new
85
- @user = User.new
86
- end
83
+ def new
84
+ @user = User.new
85
+ end
87
86
 
88
- def create
89
- @user = User.new
90
- @user.build_subscription(params[:cheddargetter_client_rails_subscription])
87
+ def create
88
+ @user = User.new(params[:user])
91
89
 
92
- if @user.save
93
- redirect_to after_creation_path, :flash => {:success => 'User successfully created'}
94
- else
95
- render 'new'
96
- end
97
- end
98
- end
90
+ if @user.save
91
+ redirect_to after_creation_path, :flash => {:success => 'User successfully created'}
92
+ else
93
+ render 'new'
94
+ end
95
+ end
96
+
97
+ def update
98
+ @user = User.find(params[:id])
99
+
100
+ if @user.update_attributes(params[:user])
101
+ redirect_to @user, :flash => {:success => 'User successfully updated'}
102
+ else
103
+ render 'edit'
104
+ end
105
+ end
106
+
107
+ end
99
108
 
100
109
  The user save will take care of saving the subscription to CheddarGetter.
101
110
 
102
111
  === In the view
103
112
 
104
- Do something like this, if it is a user form use fields_for @user.subscription inside the user form.
105
-
106
- <%= form_for(@user.subscription, :url => credit_cards_path) do |subscription| %>
107
- <fieldset>
108
- <%= subscription.hidden_field :planCode, :value => 'FREE_PLAN', :class => 'jq_planCodeValue' %>
109
-
113
+ In order to save through the user param it is important to nest the subscription params
114
+ in the user params with fields for. This is an example of how to set up the form properly.
115
+
116
+ <%= form_for(@user) do |f| %>
117
+ <%= @user.errors.full_messages.each do |message|%>
118
+ <%= message %>
119
+ <% end %>
120
+ <% if @user.errors.any? %>
121
+ <div id="error_explanation">
122
+ <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
123
+
124
+ <ul>
125
+ <% @user.errors.full_messages.each do |msg| %>
126
+ <li><%= msg %></li>
127
+ <% end %>
128
+ </ul>
129
+ </div>
130
+ <% end %>
131
+
132
+ <div class="field">
133
+ <%= f.label :email %><br />
134
+ <%= f.text_field :email %>
135
+ </div>
136
+ <div class="field">
137
+ <%= f.label :first_name %><br />
138
+ <%= f.text_field :first_name %>
139
+ </div>
140
+ <div class="field">
141
+ <%= f.label :last_name %><br />
142
+ <%= f.text_field :last_name %>
143
+ </div>
144
+ <div class="field">
145
+ <%= f.label :plan_code, "User Plan" %><br />
146
+ <%= f.select :plan_code, User::Plans #This is a collection of my plans, ie, [['Free Plan', 'FREE_PLAN'], ['Paid Plan'], ['PAID_PLAN']]. You will need to use your own plans here.
147
+ %>
148
+ </div>
149
+
150
+ The following fields are only required if selecting a paid plan.
151
+ <%= fields_for(:'user[subscription]') do |s| %> #THIS IS THE TRICKY WAY TO NEST THE SUBSCRIPTION PROPERLY.
110
152
  <dl>
111
- <dt>First Name:</dt>
112
- <dd>
113
- <%= subscription.text_field :ccFirstName,:autocomplete => "off" %>
114
- </dd>
115
-
116
- <dt>Last Name:</dt>
117
- <dd>
118
- <%= subscription.text_field :ccLastName, :autocomplete => "off" %>
119
- </dd>
120
-
121
- <dt>Card Number:</dt>
122
- <dd>
123
- <%= subscription.text_field :ccNumber,:autocomplete => "off" %>
124
- </dd>
125
- <dt>Expiration Date:</dt>
126
- <dd>
127
- <%= subscription.text_field :ccExpiration, :autocomplete => 'off' %>
128
- </dd>
129
- <dt>Address:</dt>
130
- <dd>
131
- <%= subscription.text_field :ccAddress, :autocomplete => 'off' %>
132
- </dd>
133
- <dt>City:</dt>
134
- <dd>
135
- <%= subscription.text_field :ccCity, :autocomplete => 'off' %>
136
- </dd>
137
- <dt>State:</dt>
138
- <dd>
139
- <%= subscription.text_field :ccState, :autocomplete => 'off' %>
140
- </dd>
141
- <dt>Zip Code:</dt>
142
- <dd>
143
- <%= subscription.text_field :zip, :autocomplete => 'off' %>
144
- </dd>
145
- <dt>Country:</dt>
146
- <dd>
147
- <%= subscription.text_field :ccCountry, :autocomplete => 'off' %>
148
- </dd>
149
- </dt
150
- </fieldset>
153
+ <dt>First Name:</dt>
154
+ <dd>
155
+ <%= s.text_field :ccFirstName,:autocomplete => "off" %>
156
+ </dd>
157
+
158
+ <dt>Last Name:</dt>
159
+ <dd>
160
+ <%= s.text_field :ccLastName, :autocomplete => "off" %>
161
+ </dd>
162
+
163
+ <dt>Card Number:</dt>
164
+ <dd>
165
+ <%= s.text_field :ccNumber,:autocomplete => "off" %>
166
+ </dd>
167
+ <dt>Expiration Date:</dt>
168
+ <dd>
169
+ <%= s.text_field :ccExpiration, :autocomplete => 'off' %>
170
+ </dd>
171
+ <dt>Address:</dt>
172
+ <dd>
173
+ <%= s.text_field :ccAddress, :autocomplete => 'off' %>
174
+ </dd>
175
+ <dt>City:</dt>
176
+ <dd>
177
+ <%= s.text_field :ccCity, :autocomplete => 'off' %>
178
+ </dd>
179
+ <dt>State:</dt>
180
+ <dd>
181
+ <%= s.text_field :ccState, :autocomplete => 'off' %>
182
+ </dd>
183
+ <dt>Zip Code:</dt>
184
+ <dd>
185
+ <%= s.text_field :zip, :autocomplete => 'off' %>
186
+ </dd>
187
+ <dt>Country:</dt>
188
+ <dd>
189
+ <%= s.text_field :ccCountry, :autocomplete => 'off' %>
190
+ </dd>
191
+ </dl>
192
+ <% end %>
193
+
194
+ <div class="actions">
195
+ <%= f.submit %>
196
+ </div>
151
197
  <% end %>
152
198
 
199
+ You'll want to user the current_subscription method to get a subscription object with current data.
200
+ CheddarGetter only stores the last 4 of the credit card. This data is gotten by calling ccLastFour
201
+ on the subscription object gotten through the current_subscription call.
153
202
 
154
203
  == Contributing to cheddargetter_client_rails
155
204
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.16
1
+ 0.1.26
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheddargetter_client_rails}
8
- s.version = "0.1.16"
8
+ s.version = "0.1.26"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brent Wooden"]
12
- s.date = %q{2011-05-15}
12
+ s.date = %q{2011-05-16}
13
13
  s.description = %q{Integrates CheddarGetter api with Active Record. Uses cheddargetter_client_ruby.}
14
14
  s.email = %q{brent.wooden@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,11 +35,13 @@ Gem::Specification.new do |s|
35
35
  "lib/generators/cheddargetter/cheddargetter_generator.rb",
36
36
  "lib/generators/cheddargetter/templates/cheddargetter.yml",
37
37
  "lib/generators/cheddargetter/templates/cheddargetter_client.rb",
38
+ "lib/rails/naming.rb",
38
39
  "lib/rails/record_identifier.rb",
39
40
  "spec/cheddargetter_client_rails/subscription_spec.rb",
40
41
  "spec/cheddargetter_client_rails_spec.rb",
41
42
  "spec/fixtures/users.yml",
42
43
  "spec/generator_spec.rb",
44
+ "spec/naming_spec.rb",
43
45
  "spec/record_identifier_spec.rb",
44
46
  "spec/spec_helper.rb"
45
47
  ]
@@ -2,6 +2,7 @@ require 'active_support'
2
2
  require 'action_controller/record_identifier'
3
3
  require 'cheddargetter_client_ruby'
4
4
  require 'rails/record_identifier'
5
+ require 'rails/naming'
5
6
 
6
7
  module CheddargetterClientRails
7
8
  autoload :Subscription, 'cheddargetter_client_rails/subscription'
@@ -26,6 +27,10 @@ module CheddargetterClientRails
26
27
  end
27
28
 
28
29
  def supplement_subscription_fields
30
+ if subscription.is_a?(ActiveSupport::HashWithIndifferentAccess)
31
+ self.subscription = CheddargetterClientRails::Subscription.new(subscription)
32
+ end
33
+
29
34
  self.class.shared_columns.each do |subscription_column, user_attribute|
30
35
  if(subscription_column == :planCode && user_attribute.is_a?(String)) #user can specify planCode as a string
31
36
  subscription.send(subscription_column.to_s + '=', user_attribute)
@@ -78,6 +83,21 @@ module CheddargetterClientRails
78
83
  build_subscription(attributes_hash)
79
84
  subscription.save
80
85
  end
86
+
87
+ def update_subscription
88
+ if !new_record?
89
+ subscription.customerCode = customer_code_column_value if subscription.customerCode.blank? and customer_code_column_value.present?
90
+ if shared_attributes_have_changed? || subscription.fields_present?
91
+ subscription.update
92
+ end
93
+ end
94
+ end
95
+
96
+ def shared_attributes_have_changed?
97
+ self.class.shared_columns.collect do |cgkey, column|
98
+ self.send(column.to_s + '_changed?')
99
+ end.include?(true)
100
+ end
81
101
  end
82
102
 
83
103
  module ClassMethods
@@ -101,7 +121,8 @@ module CheddargetterClientRails
101
121
 
102
122
  validate :validate_subscription
103
123
  after_create :create_subscription
104
- before_destroy :destroy_subscription
124
+ before_destroy :destroy_subscription
125
+ after_save :update_subscription
105
126
  end
106
127
 
107
128
  def responds_to_customer_code_column?
@@ -4,6 +4,25 @@ module CheddargetterClientRails
4
4
 
5
5
  Months = ("01".."12").freeze
6
6
  Years = (Date.current.year..Date.current.year+10).collect{|y| y.to_s }.freeze
7
+ CGKeys = [
8
+ :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
+
7
26
 
8
27
  attr_accessor :planCode,
9
28
  :company,
@@ -182,5 +201,16 @@ module CheddargetterClientRails
182
201
  :email => email
183
202
  }
184
203
  end
204
+
205
+ def to_key
206
+ nil
207
+ end
208
+
209
+ def fields_present?
210
+ CGKeys.collect do |key|
211
+ val = send(key)
212
+ val if val.present?
213
+ end.compact.present?
214
+ end
185
215
  end
186
216
  end
@@ -0,0 +1,9 @@
1
+ module ActiveModel::Naming
2
+ def self.singular(record_or_class)
3
+ model_name_from_record_or_class(record_or_class).singular.gsub('cheddargetter_client_rails_', '')
4
+ end
5
+
6
+ def self.plural(record_or_class)
7
+ model_name_from_record_or_class(record_or_class).plural.gsub('cheddargetter_client_rails_', '')
8
+ end
9
+ end
@@ -1,7 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe CheddargetterClientRails::Subscription do
4
- let(:user) { double("TestUser") }
4
+ before {
5
+ class TestUser < ActiveRecord::Base
6
+ attr_accessor :customer_code, :first_name, :last_name, :plan_code, :email
7
+
8
+ def self.column_names
9
+ []
10
+ end
11
+
12
+ has_subscription :customerCode => :customer_code,
13
+ :firstName => :first_name,
14
+ :lastName => :last_name,
15
+ :ccFirstName => :first_name,
16
+ :ccLastName => :last_name,
17
+ :planCode => :plan_code
18
+
19
+ end
20
+ }
21
+
22
+ before { ActiveRecord::Base.stub(:connection).and_return mock(:columns => []) }
23
+
24
+ let(:user) { TestUser.new }
5
25
  let(:subscription) { CheddargetterClientRails::Subscription.new }
6
26
  let(:customer_code) { 'JOHN_DOE' }
7
27
 
@@ -196,4 +216,27 @@ describe CheddargetterClientRails::Subscription do
196
216
  })
197
217
  }
198
218
  end
219
+
220
+ describe 'to_key' do
221
+ subject { subscription.to_key }
222
+ it { should eq(nil) }
223
+ end
224
+
225
+ describe 'subscription_fields_present?' do
226
+ subject { user.subscription.fields_present? }
227
+
228
+ context 'when they are not present' do
229
+ it { should be_false }
230
+ end
231
+
232
+ context 'when one is and it is nil' do
233
+ before { user.subscription.firstName = nil }
234
+ it { should be_false }
235
+ end
236
+
237
+ context 'when is and it is not nil' do
238
+ before { user.subscription.firstName = 'Joe' }
239
+ it { should be_true }
240
+ end
241
+ end
199
242
  end
@@ -338,6 +338,38 @@ describe "CheddargetterClientRails" do
338
338
  user.subscription.planCode.should == "EVERYBODYS_PLAN"
339
339
  }
340
340
  end
341
+
342
+ context 'when subscription is a ActiveSupport::HashWithIndifferentAccess' do
343
+ let!(:user) {
344
+ TestUser.new
345
+ }
346
+
347
+ before { user.subscription = ActiveSupport::HashWithIndifferentAccess.new }
348
+
349
+ before { user.class.stub(:shared_columns).and_return({
350
+ :firstName => :first_name,
351
+ :lastName => :last_name,
352
+ :ccFirstName => :first_name,
353
+ :ccLastName => :last_name,
354
+ :planCode => :plan_code
355
+ })
356
+ }
357
+
358
+ before {
359
+ user.customer_code = "FIRST_NAME"
360
+ user.first_name = "First"
361
+ user.last_name = "Last"
362
+ user.plan_code = "TEST_PLAN"
363
+ }
364
+
365
+ subject { user.supplement_subscription_fields }
366
+ specify {
367
+ subject
368
+ user.subscription.firstName.should == "First"
369
+ user.subscription.lastName.should == "Last"
370
+ user.subscription.planCode.should == "TEST_PLAN"
371
+ }
372
+ end
341
373
  end
342
374
 
343
375
  describe 'create_subscription' do
@@ -494,4 +526,60 @@ describe "CheddargetterClientRails" do
494
526
  before { user.subscription.should_receive(:save) }
495
527
  it do subject end
496
528
  end
529
+
530
+ describe 'update_subscription' do
531
+ subject { user.update_subscription }
532
+
533
+ context 'when user is a new record' do
534
+ before { user.stub(:new_record?).and_return(true) }
535
+ before { user.subscription.should_not_receive(:save) }
536
+ it do subject end
537
+ end
538
+
539
+ context 'when user is not a new record' do
540
+ before { user.stub(:new_record?).and_return(false) }
541
+ before { user.customer_code = 'coooode' }
542
+
543
+ context 'and shared_attributes have not changed' do
544
+ before { user.stub(:shared_attributes_have_changed?).and_return false }
545
+ before { user.subscription.stub(:fields_present?).and_return false }
546
+ before { user.subscription.should_not_receive(:save) }
547
+ it do subject end
548
+ end
549
+
550
+ context 'and shared_attributes have changed' do
551
+ before { user.stub(:shared_attributes_have_changed?).and_return true }
552
+
553
+ context 'but subscription fields are not present' do
554
+ before { user.subscription.stub(:fields_present?).and_return false }
555
+ before { user.subscription.should_receive(:update) }
556
+ it do subject end
557
+ end
558
+
559
+ context 'and attributes have changed and subscription fields are present' do
560
+ before { user.subscription.stub(:fields_present?).and_return true }
561
+ before { user.subscription.should_receive(:update) }
562
+ it do subject end
563
+ end
564
+ end
565
+ end
566
+ end
567
+
568
+ describe 'shared_attributes_have_changed?' do
569
+ subject { user.shared_attributes_have_changed? }
570
+ before {
571
+ user.class.shared_columns.each do |cgkey, attribute|
572
+ user.stub((attribute.to_s + '_changed?').to_sym).and_return false
573
+ end
574
+ }
575
+
576
+ context 'when attributes have not changed' do
577
+ it { should be_false }
578
+ end
579
+
580
+ context 'when an attribute has changed' do
581
+ before { user.stub((user.class.shared_columns.first[1]).to_s + '_changed?').and_return true }
582
+ it { should be_true }
583
+ end
584
+ end
497
585
  end
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RecordIdentifier" do
4
+ before {
5
+ class TestUser < ActiveRecord::Base
6
+ attr_accessor :customer_code, :first_name, :last_name, :plan_code, :email
7
+
8
+ def self.column_names
9
+ []
10
+ end
11
+
12
+ has_subscription :customerCode => :customer_code,
13
+ :firstName => :first_name,
14
+ :lastName => :last_name,
15
+ :ccFirstName => :first_name,
16
+ :ccLastName => :last_name,
17
+ :planCode => :plan_code
18
+
19
+ end
20
+ }
21
+
22
+ before { ActiveRecord::Base.stub(:connection).and_return mock(:columns => []) }
23
+
24
+ describe 'singular' do
25
+ subject { ActiveModel::Naming.singular(record_or_class) }
26
+
27
+ context 'called with class' do
28
+ let(:record_or_class) { CheddargetterClientRails::Subscription }
29
+ it { should eq('subscription') }
30
+ end
31
+
32
+ context 'called with class' do
33
+ let(:record_or_class) { CheddargetterClientRails::Subscription.new }
34
+ it { should eq('subscription') }
35
+ end
36
+
37
+ context 'with user model' do
38
+ context 'called with class' do
39
+ let(:record_or_class) { TestUser }
40
+ it { should eq('test_user') }
41
+ end
42
+
43
+ context 'called with class' do
44
+ let(:record_or_class) { TestUser.new }
45
+ it { should eq('test_user') }
46
+ end
47
+ end
48
+ end
49
+
50
+ describe 'plural' do
51
+ subject { ActiveModel::Naming.plural(record_or_class) }
52
+
53
+ context 'called with class' do
54
+ let(:record_or_class) { CheddargetterClientRails::Subscription }
55
+ it { should eq('subscriptions') }
56
+ end
57
+
58
+ context 'called with class' do
59
+ let(:record_or_class) { CheddargetterClientRails::Subscription.new }
60
+ it { should eq('subscriptions') }
61
+ end
62
+
63
+ context 'with user model' do
64
+ context 'called with class' do
65
+ let(:record_or_class) { TestUser }
66
+ it { should eq('test_users') }
67
+ end
68
+
69
+ context 'called with class' do
70
+ let(:record_or_class) { TestUser.new }
71
+ it { should eq('test_users') }
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+
78
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheddargetter_client_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 47
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 16
10
- version: 0.1.16
9
+ - 26
10
+ version: 0.1.26
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brent Wooden
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-15 00:00:00 -04:00
18
+ date: 2011-05-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -138,11 +138,13 @@ files:
138
138
  - lib/generators/cheddargetter/cheddargetter_generator.rb
139
139
  - lib/generators/cheddargetter/templates/cheddargetter.yml
140
140
  - lib/generators/cheddargetter/templates/cheddargetter_client.rb
141
+ - lib/rails/naming.rb
141
142
  - lib/rails/record_identifier.rb
142
143
  - spec/cheddargetter_client_rails/subscription_spec.rb
143
144
  - spec/cheddargetter_client_rails_spec.rb
144
145
  - spec/fixtures/users.yml
145
146
  - spec/generator_spec.rb
147
+ - spec/naming_spec.rb
146
148
  - spec/record_identifier_spec.rb
147
149
  - spec/spec_helper.rb
148
150
  has_rdoc: true