Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Donation and SpotusDonation can be found from paypal params

  • Loading branch information...
commit a8bf336f98cb0ff595aef2c1e33369374deea4a1 1 parent ed0a52c
GIT_AUTHOR_NAME authored veezus committed
View
16 app/models/donation.rb
@@ -113,5 +113,21 @@ def check_donation
errors.add_to_base("Thanks for your support but we only allow donations of 20% of requested amount from one user. Please lower your donation amount and try again.")
end
end
+
+ def self.find_all_from_paypal(paypal_params)
+ strip_spotus_donations(paypal_params)
+ items = paypal_params.select{|k,v| k =~ /item_number/}
+ return [] if items.blank?
+ donation_keys = items.collect{|a| a.first}
+ donation_ids = donation_keys.map{|k| paypal_params.values_at(k)}.flatten
+ Donation.find(donation_ids)
+ end
+
+ def self.strip_spotus_donations(paypal_params)
+ if spotus_keys = paypal_params.detect{|k,v| v =~ /support spot\.us/i}
+ paypal_params.delete(spotus_keys.first.gsub(/name/, 'number'))
+ paypal_params.delete(spotus_keys.first)
+ end
+ end
end
View
6 app/models/spotus_donation.rb
@@ -31,5 +31,11 @@ def tithe
(user.unpaid_donations_sum * SPOTUS_TITHE).round
end
+ def self.find_from_paypal(paypal_params)
+ spotus_donation = if spotus_keys = paypal_params.detect{|k,v| v =~ /support spot\.us/i}
+ spotus_donation_id = paypal_params.delete(spotus_keys.first.gsub(/name/, 'number'))
+ SpotusDonation.find(spotus_donation_id)
+ end
+ end
end
View
34 spec/models/donation_spec.rb
@@ -245,5 +245,39 @@
Donation::DEFAULT_AMOUNT.should == 20
end
end
+
+ describe ".strip_spotus_donations" do
+ before do
+ @params = { "item_number2"=>"28", "item_name2"=>"Support Spot.Us", "item_number1" => "10", "item_name1" => "PITCH: Some headline"}
+ end
+ it "should remove spotus donations" do
+ Donation.strip_spotus_donations(@params)
+ @params.should_not have_key("item_number2")
+ @params.should_not have_key("item_name2")
+ end
+ it "should leave all other donations" do
+ Donation.strip_spotus_donations(@params)
+ @params.should have_key("item_number1")
+ @params.should have_key("item_name1")
+ end
+ end
+
+ describe ".find_all_from_paypal" do
+ before do
+ @params = { "item_number2"=>"28", "item_name2"=>"Support Spot.Us", "item_number1" => "10", "item_name1" => "PITCH: Some headline"}
+ end
+ subject do
+ Donation.find_all_from_paypal(@params)
+ end
+ it "finds the donations" do
+ donation = mock_model(Donation)
+ Donation.should_receive(:find).with(["10"]).and_return([donation])
+ should == [donation]
+ end
+ it "returns empty array if no donation is present" do
+ @params = { "item_number1" => "28", "item_name1" => "Support Spot.Us"}
+ should be_empty
+ end
+ end
end
View
22 spec/models/spotus_donation_spec.rb
@@ -46,4 +46,26 @@
spotus_donation.amount.should == BigDecimal.new("3.0")
end
+ describe ".find_from_paypal" do
+ before do
+ @params = { "item_number2"=>"28", "item_name2"=>"Support Spot.Us", "item_number1" => "10", "item_name1" => "PITCH: Some headline"}
+ end
+ subject do
+ SpotusDonation.find_from_paypal(@params)
+ end
+ it "finds the spotus donation" do
+ spotus_donation = mock_model(SpotusDonation)
+ SpotusDonation.should_receive(:find).with("28").and_return(spotus_donation)
+ should == spotus_donation
+ end
+ it "returns nil if no spotus donation is present" do
+ @params = { "item_number1" => "10", "item_name1" => "PITCH: Some headline"}
+ should be_nil
+ end
+ it "deletes the spotus donation params" do
+ SpotusDonation.stub!(:find)
+ subject
+ @params.should_not have_key("item_number2")
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.