1
0
Fork 0
forked from OpenNeo/impress

actually contributing things, yay

This commit is contained in:
Emi Matchu 2010-11-06 18:08:42 -04:00
parent ef004cc6cb
commit d10ab2615f
9 changed files with 66 additions and 25 deletions

View file

@ -13,6 +13,7 @@ class PetsController < ApplicationController
raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(params[:name])
@pet.contributor = current_user if user_signed_in?
@pet.save
respond_to do |format|
format.html do

View file

@ -8,6 +8,7 @@ class Contribution < ActiveRecord::Base
attr_accessor :contributed
belongs_to :contributed, :polymorphic => true
belongs_to :user
scope :recent, order('id DESC')

View file

@ -1,6 +1,7 @@
class Item < ActiveRecord::Base
SwfAssetType = 'object'
has_one :contribution, :as => :contributed
has_many :parent_swf_asset_relationships, :foreign_key => 'parent_id',
:conditions => {:swf_asset_type => SwfAssetType}
has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :object_asset
@ -111,7 +112,7 @@ class Item < ActiveRecord::Base
true
end
before_save do
def handle_assets!
if @parent_swf_asset_relationships_to_update && @current_body_id
new_swf_asset_ids = @parent_swf_asset_relationships_to_update.map(&:swf_asset_id)
rels = ParentSwfAssetRelationship.arel_table
@ -129,6 +130,7 @@ class Item < ActiveRecord::Base
where(rels[:swf_asset_id].in(ids_to_delete)).
delete_all
end
self.parent_swf_asset_relationships += @parent_swf_asset_relationships_to_update
end
end
@ -144,8 +146,13 @@ class Item < ActiveRecord::Base
end
end
def pending_swf_assets
@parent_swf_asset_relationships_to_update.inject([]) do |all_swf_assets, relationship|
all_swf_assets << relationship.swf_asset
end
end
def parent_swf_asset_relationships_to_update=(rels)
self.parent_swf_asset_relationships += rels
@parent_swf_asset_relationships_to_update = rels
end

View file

@ -8,6 +8,7 @@ class Pet < ActiveRecord::Base
belongs_to :pet_type
attr_reader :items, :pet_state
attr_accessor :contributor
def load!
require 'ostruct'
@ -45,10 +46,23 @@ class Pet < ActiveRecord::Base
}.to_query
end
before_save do
self.pet_type.save
self.items.each(&:save)
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
end
end
def self.load(name)
pet = Pet.find_or_initialize_by_name(name)

View file

@ -1,6 +1,7 @@
class PetState < ActiveRecord::Base
SwfAssetType = 'biology'
has_one :contribution, :as => :contributed
has_many :parent_swf_asset_relationships, :foreign_key => 'parent_id',
:conditions => {:swf_asset_type => SwfAssetType}
has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :biology_asset

View file

@ -2,6 +2,7 @@ class PetType < ActiveRecord::Base
IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png';
IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$};
has_one :contribution, :as => :contributed
has_many :pet_states
attr_writer :origin_pet

View file

@ -5,6 +5,7 @@ class SwfAsset < ActiveRecord::Base
attr_accessor :item
has_one :contribution, :as => :contributed
has_many :object_asset_relationships, :class_name => 'ParentSwfAssetRelationship',
:conditions => {:swf_asset_type => 'object'}

View file

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

View file

@ -1,15 +1,16 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 24) do
ActiveRecord::Schema.define(:version => 0) do
create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false
@ -20,10 +21,10 @@ ActiveRecord::Schema.define(:version => 24) do
end
create_table "contributions", :force => true do |t|
t.string "contributed_class", :limit => 0, :null => false
t.integer "contributed_id", :null => false
t.integer "user_id", :null => false
t.timestamp "created_at", :null => false
t.string "contributed_type", :limit => 8, :null => false
t.integer "contributed_id", :null => false
t.integer "user_id", :null => false
t.timestamp "created_at", :null => false
end
create_table "login_cookies", :force => true do |t|
@ -48,7 +49,7 @@ ActiveRecord::Schema.define(:version => 24) do
t.integer "weight_lbs", :limit => 2, :null => false
t.text "species_support_ids"
t.integer "sold_in_mall", :limit => 1, :null => false
t.timestamp "last_spidered", :null => false
t.timestamp "last_spidered"
end
add_index "objects", ["last_spidered"], :name => "objects_last_spidered"
@ -57,12 +58,12 @@ ActiveRecord::Schema.define(:version => 24) do
create_table "parents_swf_assets", :id => false, :force => true do |t|
t.integer "parent_id", :limit => 3, :null => false
t.integer "swf_asset_id", :limit => 3, :null => false
t.string "swf_asset_type", :limit => 0
t.string "swf_asset_type", :limit => 7, :null => false
end
add_index "parents_swf_assets", ["parent_id", "swf_asset_id", "swf_asset_type"], :name => "unique_parents_swf_assets", :unique => true
add_index "parents_swf_assets", ["parent_id"], :name => "parent_swf_assets_parent_id"
add_index "parents_swf_assets", ["swf_asset_id"], :name => "parents_swf_assets_swf_asset_id"
add_index "parents_swf_assets", ["parent_id", "swf_asset_id", "swf_asset_type"], :name => "unique_parents_swf_assets", :unique => true
create_table "pet_loads", :force => true do |t|
t.string "pet_name", :limit => 20, :null => false
@ -100,7 +101,7 @@ ActiveRecord::Schema.define(:version => 24) do
end
create_table "swf_assets", :id => false, :force => true do |t|
t.string "type", :limit => 0, :null => false
t.string "type", :limit => 7, :null => false
t.integer "id", :limit => 3, :null => false
t.text "url", :null => false
t.integer "zone_id", :limit => 1, :null => false
@ -113,10 +114,10 @@ ActiveRecord::Schema.define(:version => 24) do
add_index "swf_assets", ["zone_id"], :name => "idx_swf_assets_zone_id"
create_table "users", :force => true do |t|
t.string "name", :limit => 20, :null => false
t.integer "auth_server_id", :limit => 1, :null => false
t.integer "remote_id", :null => false
t.integer "points", :null => false
t.string "name", :limit => 20, :null => false
t.integer "auth_server_id", :limit => 1, :null => false
t.integer "remote_id", :null => false
t.integer "points", :default => 0, :null => false
end
create_table "zones", :force => true do |t|