From d10ab2615fbc41b28dc1f7de27c77e908541afcb Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 6 Nov 2010 18:08:42 -0400 Subject: [PATCH] actually contributing things, yay --- app/controllers/pets_controller.rb | 1 + app/models/contribution.rb | 1 + app/models/item.rb | 11 +++++++-- app/models/pet.rb | 22 ++++++++++++++--- app/models/pet_state.rb | 1 + app/models/pet_type.rb | 1 + app/models/swf_asset.rb | 1 + app/models/user.rb | 14 +++++++++++ db/schema.rb | 39 +++++++++++++++--------------- 9 files changed, 66 insertions(+), 25 deletions(-) diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index 22892dde..9db7f194 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -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 diff --git a/app/models/contribution.rb b/app/models/contribution.rb index b3479764..648be077 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -8,6 +8,7 @@ class Contribution < ActiveRecord::Base attr_accessor :contributed + belongs_to :contributed, :polymorphic => true belongs_to :user scope :recent, order('id DESC') diff --git a/app/models/item.rb b/app/models/item.rb index 3952b6ba..32df9e76 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/models/pet.rb b/app/models/pet.rb index 82b3aec4..e78a45ee 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -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) diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index 8c459c5a..5e64b00d 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -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 diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index a48491b2..6456405c 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -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 diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 51bd4e43..050c12a6 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -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'} diff --git a/app/models/user.rb b/app/models/user.rb index 467784bc..9c127786 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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'], diff --git a/db/schema.rb b/db/schema.rb index 7f933bc1..ae854a73 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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|