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?
points = current_user.contribute! @pet
else
@pet.save @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,23 +46,23 @@ class Pet < ActiveRecord::Base
}.to_query }.to_query
end end
before_validation do def contributables
if @contributor
contributables = [pet_type, @pet_state] contributables = [pet_type, @pet_state]
items.each do |item| items.each do |item|
item.handle_assets! item.handle_assets!
contributables << item contributables << item
contributables += item.pending_swf_assets contributables += item.pending_swf_assets
end end
@contributor.contribute! contributables contributables
else end
before_validation do
pet_type.save! pet_type.save!
items.each do |item| items.each do |item|
item.handle_assets! item.handle_assets!
item.save! item.save!
end end
end end
end
def self.load(name) def self.load(name)
pet = Pet.find_or_initialize_by_name(name) pet = Pet.find_or_initialize_by_name(name)

View file

@ -5,19 +5,25 @@ 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
self.points += new_points
Pet.transaction do
pet.save!
save! save!
end end
new_points
end
def self.find_or_create_from_remote_auth_data(user_data) def self.find_or_create_from_remote_auth_data(user_data)
user = find_or_initialize_by_remote_id_and_auth_server_id( user = find_or_initialize_by_remote_id_and_auth_server_id(