phew, fixed some issues with contribution saving in rails 3.2

This commit is contained in:
Emi Matchu 2013-03-05 20:51:24 -06:00
parent cf5191d33c
commit a80f70bb88
6 changed files with 15 additions and 12 deletions

View file

@ -6,8 +6,6 @@ class Contribution < ActiveRecord::Base
'PetState' => 10 'PetState' => 10
} }
attr_accessor :contributed
belongs_to :contributed, :polymorphic => true belongs_to :contributed, :polymorphic => true
belongs_to :user belongs_to :user
@ -17,7 +15,9 @@ class Contribution < ActiveRecord::Base
@@per_page = 30 @@per_page = 30
def point_value def point_value
POINT_VALUES[contributed_type] POINT_VALUES[contributed_type] ||
raise("unexpected contributed type #{contributed_type.inspect} for " +
"contributed #{contributed.inspect}")
end end
CONTRIBUTED_RELATIONSHIPS = { CONTRIBUTED_RELATIONSHIPS = {

View file

@ -9,7 +9,7 @@ class Item < ActiveRecord::Base
translates :name, :description, :rarity translates :name, :description, :rarity
has_many :closet_hangers has_many :closet_hangers
has_one :contribution, :as => :contributed has_one :contribution, :as => :contributed, :inverse_of => :contributed
has_many :parent_swf_asset_relationships, :as => :parent has_many :parent_swf_asset_relationships, :as => :parent
has_many :swf_assets, :through => :parent_swf_asset_relationships has_many :swf_assets, :through => :parent_swf_asset_relationships

View file

@ -1,7 +1,8 @@
class PetState < ActiveRecord::Base class PetState < ActiveRecord::Base
SwfAssetType = 'biology' SwfAssetType = 'biology'
has_many :contributions, :as => :contributed # in case of duplicates being merged has_many :contributions, :as => :contributed,
:inverse_of => :contributed # in case of duplicates being merged
has_many :outfits has_many :outfits
has_many :parent_swf_asset_relationships, :as => :parent, has_many :parent_swf_asset_relationships, :as => :parent,
:autosave => false :autosave => false

View file

@ -5,7 +5,7 @@ class PetType < ActiveRecord::Base
belongs_to :species belongs_to :species
belongs_to :color belongs_to :color
has_one :contribution, :as => :contributed has_one :contribution, :as => :contributed, :inverse_of => :contributed
has_many :pet_states has_many :pet_states
has_many :pets has_many :pets

View file

@ -143,7 +143,7 @@ class SwfAsset < ActiveRecord::Base
attr_accessor :item attr_accessor :item
has_one :contribution, :as => :contributed has_one :contribution, :as => :contributed, :inverse_of => :contributed
has_many :parent_swf_asset_relationships has_many :parent_swf_asset_relationships
delegate :depth, :to => :zone delegate :depth, :to => :zone

View file

@ -19,25 +19,27 @@ class User < ActiveRecord::Base
def contribute!(pet) def contribute!(pet)
new_contributions = [] new_contributions = []
new_points = 0
pet.contributables.each do |contributable| pet.contributables.each do |contributable|
if contributable.new_record? if contributable.new_record?
contribution = Contribution.new(:contributed => contributable, contribution = Contribution.new
:user => self) contribution.contributed = contributable
contribution.user = self
new_contributions << contribution new_contributions << contribution
new_points += contribution.point_value
end end
end end
self.points += new_points new_points = 0 # temp assignment for scoping
Pet.transaction do Pet.transaction do
pet.save! pet.save!
new_contributions.each do |contribution| new_contributions.each do |contribution|
Rails.logger.debug("Saving contribution of #{contribution.contributed.inspect}: #{contribution.contributed_type.inspect}, #{contribution.contributed_id.inspect}")
begin begin
contribution.save! contribution.save!
rescue ActiveRecord::RecordNotSaved => e rescue ActiveRecord::RecordNotSaved => e
raise ActiveRecord::RecordNotSaved, "#{e.message}, #{contribution.inspect}, #{contribution.valid?.inspect}, #{contribution.errors.inspect}" raise ActiveRecord::RecordNotSaved, "#{e.message}, #{contribution.inspect}, #{contribution.valid?.inspect}, #{contribution.errors.inspect}"
end end
end end
new_points = new_contributions.map(&:point_value).inject(0, &:+)
self.points += new_points
begin begin
save! save!
rescue ActiveRecord::RecordNotSaved => e rescue ActiveRecord::RecordNotSaved => e