1
0
Fork 0
forked from OpenNeo/impress

points for bulk pet adding

This commit is contained in:
Emi Matchu 2010-11-06 19:07:15 -04:00
parent aa5b64fd8e
commit 7db0beec75
3 changed files with 31 additions and 21 deletions

View file

@ -13,8 +13,12 @@ class PetsController < ApplicationController
raise Pet::PetNotFound unless params[:name] raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(params[:name]) @pet = Pet.load(params[:name])
@pet.contributor = current_user if user_signed_in? if user_signed_in?
@pet.save points = current_user.contribute! @pet
else
@pet.save
points = true
end
respond_to do |format| respond_to do |format|
format.html do format.html do
destination = params[:destination] || params[:origin] destination = params[:destination] || params[:origin]
@ -25,7 +29,7 @@ class PetsController < ApplicationController
end end
format.json do format.json do
render :json => true render :json => points
end end
end end
end end

View file

@ -46,21 +46,21 @@ class Pet < ActiveRecord::Base
}.to_query }.to_query
end end
def contributables
contributables = [pet_type, @pet_state]
items.each do |item|
item.handle_assets!
contributables << item
contributables += item.pending_swf_assets
end
contributables
end
before_validation do before_validation do
if @contributor pet_type.save!
contributables = [pet_type, @pet_state] items.each do |item|
items.each do |item| item.handle_assets!
item.handle_assets! item.save!
contributables << item
contributables += item.pending_swf_assets
end
@contributor.contribute! contributables
else
pet_type.save!
items.each do |item|
item.handle_assets!
item.save!
end
end end
end end

View file

@ -5,18 +5,24 @@ class User < ActiveRecord::Base
scope :top_contributors, order('points DESC').where(arel_table[:points].gt(0)) scope :top_contributors, order('points DESC').where(arel_table[:points].gt(0))
def contribute!(contributables) def contribute!(pet)
new_contributions = [] new_contributions = []
contributables.each do |contributable| new_points = 0
pet.contributables.each do |contributable|
if contributable.new_record? if contributable.new_record?
contribution = Contribution.new(:contributed => contributable, contribution = Contribution.new(:contributed => contributable,
:user => self) :user => self)
new_contributions << contribution new_contributions << contribution
self.points += contribution.point_value new_points += contribution.point_value
end end
end end
self.contributions += new_contributions self.contributions += new_contributions
save! self.points += new_points
Pet.transaction do
pet.save!
save!
end
new_points
end end
def self.find_or_create_from_remote_auth_data(user_data) def self.find_or_create_from_remote_auth_data(user_data)