2010-11-06 08:52:58 -07:00
|
|
|
class Contribution < ActiveRecord::Base
|
|
|
|
POINT_VALUES = {
|
|
|
|
'Item' => 3,
|
|
|
|
'SwfAsset' => 2,
|
|
|
|
'PetType' => 15,
|
|
|
|
'PetState' => 10
|
|
|
|
}
|
|
|
|
|
2010-11-06 15:08:42 -07:00
|
|
|
belongs_to :contributed, :polymorphic => true
|
2010-11-06 08:52:58 -07:00
|
|
|
belongs_to :user
|
|
|
|
|
2023-07-22 14:04:01 -07:00
|
|
|
scope :recent, -> { order('id DESC') }
|
2010-11-06 08:52:58 -07:00
|
|
|
|
|
|
|
cattr_reader :per_page
|
|
|
|
@@per_page = 30
|
|
|
|
|
|
|
|
def point_value
|
2013-03-05 18:51:24 -08:00
|
|
|
POINT_VALUES[contributed_type] ||
|
|
|
|
raise("unexpected contributed type #{contributed_type.inspect} for " +
|
|
|
|
"contributed #{contributed.inspect}")
|
2010-11-06 08:52:58 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
CONTRIBUTED_RELATIONSHIPS = {
|
|
|
|
'SwfAsset' => 'Item',
|
|
|
|
'PetState' => 'PetType'
|
|
|
|
}
|
|
|
|
CONTRIBUTED_CHILDREN = CONTRIBUTED_RELATIONSHIPS.keys
|
|
|
|
CONTRIBUTED_TYPES = CONTRIBUTED_CHILDREN + CONTRIBUTED_RELATIONSHIPS.values
|
2013-01-25 13:23:48 -08:00
|
|
|
def self.preload_contributeds_and_parents(contributions, options={})
|
|
|
|
options[:scopes] ||= {}
|
|
|
|
|
2010-11-06 08:52:58 -07:00
|
|
|
# Initialize the groups we'll be using for quick access
|
|
|
|
contributions_by_type = {}
|
|
|
|
contributed_by_type = {}
|
|
|
|
contributed_by_type_and_id = {}
|
|
|
|
needed_ids_by_type = {}
|
|
|
|
CONTRIBUTED_TYPES.each do |type|
|
|
|
|
contributions_by_type[type] = []
|
|
|
|
contributed_by_type[type] = []
|
|
|
|
contributed_by_type_and_id[type] = {}
|
|
|
|
needed_ids_by_type[type] = []
|
|
|
|
end
|
|
|
|
|
|
|
|
# Go through the contributions to sort them for future contributed
|
|
|
|
# assignment, and so we can know what immediate contributed items we'll
|
|
|
|
# need to look up
|
|
|
|
contributions.each do |contribution|
|
|
|
|
type = contribution.contributed_type
|
|
|
|
contributions_by_type[type] << contribution
|
|
|
|
needed_ids_by_type[type] << contribution.contributed_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load contributed objects without parents, prepare them for easy access
|
|
|
|
# for future assignment to contributions and looking up parents
|
|
|
|
CONTRIBUTED_CHILDREN.each do |type|
|
2013-01-25 13:23:48 -08:00
|
|
|
scope = options[:scopes][type] || type.constantize.scoped
|
|
|
|
scope.find(needed_ids_by_type[type]).each do |contributed|
|
2010-11-06 08:52:58 -07:00
|
|
|
contributed_by_type[type] << contributed
|
|
|
|
contributed_by_type_and_id[type][contributed.id] = contributed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load both parents of the children we just got, and immediately
|
|
|
|
# contributed objects of that class. all_by_ids_or_children properly
|
|
|
|
# assigns parents to children, as well
|
|
|
|
CONTRIBUTED_RELATIONSHIPS.each do |child_type, type|
|
2013-01-25 13:23:48 -08:00
|
|
|
scope = options[:scopes][type] || type.constantize.scoped
|
2010-11-06 08:52:58 -07:00
|
|
|
ids = needed_ids_by_type[type]
|
|
|
|
children = contributed_by_type[child_type]
|
2013-01-25 13:23:48 -08:00
|
|
|
scope.all_by_ids_or_children(ids, children).each do |contributed|
|
2010-11-06 08:52:58 -07:00
|
|
|
contributed_by_type_and_id[type][contributed.id] = contributed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Assign contributed objects to contributions
|
|
|
|
contributions.each do |contribution|
|
|
|
|
type = contribution.contributed_type
|
|
|
|
id = contribution.contributed_id
|
|
|
|
contribution.contributed = contributed_by_type_and_id[type][id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|