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

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

View file

@ -9,7 +9,7 @@ class Item < ActiveRecord::Base
translates :name, :description, :rarity
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 :swf_assets, :through => :parent_swf_asset_relationships

View file

@ -1,7 +1,8 @@
class PetState < ActiveRecord::Base
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 :parent_swf_asset_relationships, :as => :parent,
:autosave => false

View file

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

View file

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

View file

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