saucy 0.2.6.1 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,17 +7,21 @@
7
7
  <% @plans.each do |plan| -%>
8
8
  <%= content_tag_for :li, plan, :class => "#{plan.name.parameterize} #{'disabled' if !@account.can_change_plan_to?(plan)}" do %>
9
9
  <%= form.label :plan_id, render(plan), :value => plan.id %>
10
- <%= form.radio_button :plan_id, plan.id, :disabled => !@account.can_change_plan_to?(plan) %>
10
+ <%= form.radio_button :plan_id, plan.id, :disabled => !@account.can_change_plan_to?(plan), "data-free" => plan.free? %>
11
11
  <% end %>
12
12
  <% end -%>
13
13
  <% end %>
14
14
 
15
15
  <% if !@account.credit_card -%>
16
- <h5 class="legend">Billing Information</h5>
17
- <%= render :partial => 'billings/form', :locals => { :form => form } %>
16
+ <div class="billing_information" style="<%= 'display: none' if @account.plan.free? %>">
17
+ <h5 class="legend">Billing Information</h5>
18
+ <%= render :partial => 'billings/form', :locals => { :form => form } %>
19
+ </div>
18
20
  <% end -%>
19
21
 
20
22
  <%= form.buttons do %>
21
23
  <%= form.commit_button "Upgrade" %>
22
24
  <% end %>
23
25
  <% end %>
26
+
27
+ <%= render :partial => "shared/saucy_javascript" %>
@@ -11,6 +11,23 @@
11
11
  $(this).siblings('p.inline-hints').find('span').text(mainPart);
12
12
  $('#signup_keyword, #project_keyword').val(mainPart);
13
13
  });
14
+
15
+ $.fn.updateSelectedPlan = function() {
16
+ $(".plans-edit input:radio:not(checked)").each(function() {
17
+ $("label[for=" + $(this).attr("id") + "]").removeClass('selected');
18
+ });
19
+ $("label[for=" + $(this).attr("id") + "]").addClass('selected');
20
+ if($(this).attr("data-free") == "true") {
21
+ $(".billing_information").hide();
22
+ } else {
23
+ $(".billing_information").show();
24
+ }
25
+ };
26
+
27
+ $(".plans-edit input:radio").click(function() {
28
+ $(this).updateSelectedPlan();
29
+ });
30
+ $(".plans-edit input:radio:checked").updateSelectedPlan();
14
31
  });
15
32
  <% end %>
16
33
  <% end -%>
data/lib/saucy/account.rb CHANGED
@@ -82,21 +82,12 @@ module Saucy
82
82
 
83
83
  def save_braintree!(attributes)
84
84
  successful = true
85
- self.plan = ::Plan.find(attributes[:plan_id]) if attributes[:plan_id].present?
86
- if CUSTOMER_ATTRIBUTES.keys.any? { |attribute| attributes[attribute].present? }
87
- CUSTOMER_ATTRIBUTES.keys.each do |attribute|
88
- send("#{attribute}=", attributes[attribute]) if attributes[attribute].present?
89
- end
90
- result = Braintree::Customer.update(customer_token, customer_attributes)
91
- successful = result.success?
92
- handle_customer_result(result)
85
+ self.plan = ::Plan.find(attributes[:plan_id]) if changing_plan?(attributes)
86
+ if changing_braintree_attributes?(attributes)
87
+ successful = update_braintree_customer(attributes)
93
88
  end
94
- if successful && attributes[:plan_id].present?
95
- if subscription
96
- Braintree::Subscription.update(subscription_token, :plan_id => attributes[:plan_id])
97
- else
98
- create_subscription
99
- end
89
+ if successful && changing_plan?(attributes)
90
+ save_subscription
100
91
  flush_cache :subscription
101
92
  end
102
93
  successful && save
@@ -114,6 +105,35 @@ module Saucy
114
105
 
115
106
  private
116
107
 
108
+ def changing_plan?(attributes)
109
+ attributes[:plan_id].present?
110
+ end
111
+
112
+ def changing_braintree_attributes?(attributes)
113
+ CUSTOMER_ATTRIBUTES.keys.any? { |attribute| attributes[attribute].present? }
114
+ end
115
+
116
+ def set_braintree_attributes(attributes)
117
+ CUSTOMER_ATTRIBUTES.keys.each do |attribute|
118
+ send("#{attribute}=", attributes[attribute]) if attributes[attribute].present?
119
+ end
120
+ end
121
+
122
+ def update_braintree_customer(attributes)
123
+ set_braintree_attributes(attributes)
124
+ result = Braintree::Customer.update(customer_token, customer_attributes)
125
+ handle_customer_result(result)
126
+ result.success?
127
+ end
128
+
129
+ def save_subscription
130
+ if subscription
131
+ Braintree::Subscription.update(subscription_token, :plan_id => plan_id)
132
+ elsif plan.billed?
133
+ create_subscription
134
+ end
135
+ end
136
+
117
137
  def customer_attributes
118
138
  {
119
139
  :email => billing_email,
@@ -139,7 +159,7 @@ module Saucy
139
159
 
140
160
  def credit_card_options
141
161
  if customer && customer.credit_cards.any?
142
- { :update_existing_token => customer.credit_cards[0].token }
162
+ { :update_existing_token => credit_card.token }
143
163
  else
144
164
  {}
145
165
  end
@@ -216,7 +216,7 @@ describe Account, "with a free plan" do
216
216
  FakeBraintree.subscriptions[subject.subscription_token].should be_nil
217
217
  end
218
218
 
219
- it "creates a credit card, and subscription when the plan is changed and billing info is supplied" do
219
+ it "creates a credit card, and subscription when the plan is changed to a paid plan and the billing info is supplied" do
220
220
  new_plan = Factory(:paid_plan, :name => "New Plan")
221
221
  subject.save_braintree!(:plan_id => new_plan.id,
222
222
  :cardholder_name => "Ralph Robot",
@@ -232,6 +232,14 @@ describe Account, "with a free plan" do
232
232
  subject.credit_card.should_not be_nil
233
233
  subject.subscription.should_not be_nil
234
234
  end
235
+
236
+ it "doesn't create a credit card, and subscription when the plan is changed to a different free plan" do
237
+ new_plan = Factory(:plan, :name => "New Plan")
238
+ subject.save_braintree!(:plan_id => new_plan.id)
239
+
240
+ subject.credit_card.should be_nil
241
+ subject.subscription.should be_nil
242
+ end
235
243
  end
236
244
 
237
245
  describe Account, "with a plan and limits, and other plans" do
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saucy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 69
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 6
10
- - 1
11
- version: 0.2.6.1
9
+ - 7
10
+ version: 0.2.7
12
11
  platform: ruby
13
12
  authors:
14
13
  - thoughtbot, inc.
@@ -18,7 +17,7 @@ autorequire:
18
17
  bindir: bin
19
18
  cert_chain: []
20
19
 
21
- date: 2011-01-15 00:00:00 -05:00
20
+ date: 2011-01-18 00:00:00 -05:00
22
21
  default_executable:
23
22
  dependencies:
24
23
  - !ruby/object:Gem::Dependency