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

View file

@ -46,21 +46,21 @@ class Pet < ActiveRecord::Base
}.to_query
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
if @contributor
contributables = [pet_type, @pet_state]
items.each do |item|
item.handle_assets!
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
pet_type.save!
items.each do |item|
item.handle_assets!
item.save!
end
end

View file

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