From 7db0beec757e287029430273d0c2eec310192767 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 6 Nov 2010 19:07:15 -0400 Subject: [PATCH] points for bulk pet adding --- app/controllers/pets_controller.rb | 10 +++++++--- app/models/pet.rb | 28 ++++++++++++++-------------- app/models/user.rb | 14 ++++++++++---- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index 9db7f194..9800a5ed 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -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 diff --git a/app/models/pet.rb b/app/models/pet.rb index e78a45ee..390a58b9 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 9c127786..6e07f9ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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)