points for bulk pet adding
This commit is contained in:
parent
aa5b64fd8e
commit
7db0beec75
3 changed files with 31 additions and 21 deletions
|
@ -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?
|
||||
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
|
||||
|
|
|
@ -46,23 +46,23 @@ class Pet < ActiveRecord::Base
|
|||
}.to_query
|
||||
end
|
||||
|
||||
before_validation do
|
||||
if @contributor
|
||||
def contributables
|
||||
contributables = [pet_type, @pet_state]
|
||||
items.each do |item|
|
||||
item.handle_assets!
|
||||
contributables << item
|
||||
contributables += item.pending_swf_assets
|
||||
end
|
||||
@contributor.contribute! contributables
|
||||
else
|
||||
contributables
|
||||
end
|
||||
|
||||
before_validation do
|
||||
pet_type.save!
|
||||
items.each do |item|
|
||||
item.handle_assets!
|
||||
item.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.load(name)
|
||||
pet = Pet.find_or_initialize_by_name(name)
|
||||
|
|
|
@ -5,19 +5,25 @@ 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
|
||||
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)
|
||||
user = find_or_initialize_by_remote_id_and_auth_server_id(
|
||||
|
|
Loading…
Reference in a new issue