Skip to content

Commit

Permalink
Merge pull request #244 from Crowdtilt/fix_reward_count_length
Browse files Browse the repository at this point in the history
Fix length -> count for some reward + payment queries
  • Loading branch information
liuhenry committed Mar 26, 2014
2 parents caca979 + c818fbe commit 960f096
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 29 deletions.
8 changes: 4 additions & 4 deletions app/controllers/admin/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,17 @@ def payments
if payment
@payments = [payment]
else
@payments = @campaign.payments_completed.order("created_at ASC")
@payments = @campaign.payments.completed.order("created_at ASC")
flash.now[:error] = "Contributor not found for " + params[:payment_id]
end
elsif params.has_key?(:email) && !params[:email].blank?
@payments = @campaign.payments_completed.where("lower(email) = ?", params[:email].downcase)
@payments = @campaign.payments.completed.where("lower(email) = ?", params[:email].downcase)
if @payments.blank?
@payments = @campaign.payments_completed.order("created_at ASC")
@payments = @campaign.payments.completed.order("created_at ASC")
flash.now[:error] = "Contributor not found for " + params[:email]
end
else
@payments = @campaign.payments_completed.order("created_at ASC")
@payments = @campaign.payments.completed.order("created_at ASC")
end

create_breadcrumb(['Payments', admin_campaigns_payments_path(@campaign)])
Expand Down
21 changes: 4 additions & 17 deletions app/models/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,19 @@ def orders
end

def rewards?
(self.payment_type != 'fixed' && self.rewards.length > 0)
(self.payment_type != 'fixed' && self.rewards.count > 0)
end

def rewards_claimed
@sum = 0
self.rewards.each do |reward|
@sum += reward.payments.length
end
return @sum
self.payments.joins(:reward).successful.count
end

def payments_completed
self.payments.where(:status => %w(authorized charged released rejected refunded offline))
end

def payments_successful
# 'rejected' is a post-tilt state, so they are included in successful payments.
self.payments.where(:status => %w(authorized charged released rejected offline))
end

def raised_amount
self.payments_successful.sum(:amount)/100.0
self.payments.successful.sum(:amount)/100.0
end

def number_of_contributions
self.payments_successful.count
self.payments.successful.count
end

def tilt_percent
Expand Down
3 changes: 3 additions & 0 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Payment < ActiveRecord::Base
belongs_to :campaign
belongs_to :reward

scope :successful, where(status: %w(authorized charged released rejected offline))
scope :completed, where(status: %w(authorized charged released rejected refunded offline))

def self.to_csv(options={})
#db_columns = %w{fullname email quantity amount user_fee_amount created_at status ct_payment_id}
csv_columns = ['Name', 'Email', 'Quantity', 'Amount', 'User Fee', 'Date', 'Reward',
Expand Down
6 changes: 5 additions & 1 deletion app/models/reward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class Reward < ActiveRecord::Base
has_many :payments

def sold_out?
!self.unlimited? && number_of_payments >= self.number
!self.unlimited? && self.number_of_successful_payments >= self.number
end

def number_of_payments
self.payments.count
end

def number_of_successful_payments
self.payments.successful.count
end

def unlimited?
self.number.nil? || self.number == 0
end
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/reward_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class RewardSerializer < ActiveModel::Serializer
attributes :id, :title, :description, :price, :delivery_date, :number, :campaign_id, :number_of_payments, :image_url, :collect_shipping_flag
end
attributes :id, :title, :description, :price, :delivery_date, :number, :campaign_id, :number_of_successful_payments, :image_url, :collect_shipping_flag
end
4 changes: 2 additions & 2 deletions app/views/admin/campaigns/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@
<% end %>
<input type="hidden" name="reward[][id]" value="<%= reward.id %>"/>
</td>
<td><%= reward.payments.length %></td>
<td><%= reward.payments.successful.count %></td>
<td>
<% if reward.payments.length > 0 %>
<% if reward.payments.successful.count > 0 %>
N/A<input type="hidden" name="reward[][delete]" value=""/>
<% else %>
<input type="checkbox" name="reward[][delete]" value="delete"/>
Expand Down
2 changes: 1 addition & 1 deletion app/views/campaigns/checkout_amount.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<div class="reward_description">
<p class="title"><%= reward.title %></p>
<p class="claimed">
<%= reward.payments.length %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed
<%= reward.payments.successful.count %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed
<% if reward.sold_out? %> (All gone!) <% end %>
</p>
<p class="description"><%= reward.description %></p>
Expand Down
4 changes: 2 additions & 2 deletions app/views/theme/views/campaign.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@
<% if @campaign.expired? %>
<% if reward.include_claimed? %>
<p class="claimed">
<%= reward.payments.length %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed <%= '(All gone!)' if reward.sold_out? %>
<%= reward.payments.successful.count %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed <%= '(All gone!)' if reward.sold_out? %>
</p>
<% end %>
<% else %>
<p class="claimed">
<%= reward.payments.length %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed <%= '(All gone!)' if reward.sold_out? %>
<%= reward.payments.successful.count %> <%= "of #{reward.number}" unless reward.unlimited? %> claimed <%= '(All gone!)' if reward.sold_out? %>
</p>
<% end %>
</a>
Expand Down

0 comments on commit 960f096

Please sign in to comment.