2010-11-10 13:59:54 -08:00
|
|
|
class Outfit < ActiveRecord::Base
|
2010-11-12 13:31:01 -08:00
|
|
|
has_many :item_outfit_relationships, :dependent => :destroy
|
2010-11-13 14:26:14 -08:00
|
|
|
has_many :worn_item_outfit_relationships, :class_name => 'ItemOutfitRelationship',
|
|
|
|
:conditions => {:is_worn => true}
|
|
|
|
has_many :worn_items, :through => :worn_item_outfit_relationships, :source => :item
|
2010-11-10 13:59:54 -08:00
|
|
|
belongs_to :pet_state
|
|
|
|
belongs_to :user
|
|
|
|
|
2011-02-10 14:50:47 -08:00
|
|
|
validates :name, :presence => {:if => :user_id}, :uniqueness => {:scope => :user_id, :if => :user_id}
|
2010-11-10 13:59:54 -08:00
|
|
|
validates :pet_state, :presence => true
|
|
|
|
|
2010-11-13 05:50:37 -08:00
|
|
|
attr_accessible :name, :pet_state_id, :starred, :worn_and_unworn_item_ids
|
2010-11-10 13:59:54 -08:00
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def as_json(more_options={})
|
|
|
|
serializable_hash :only => [:id, :name, :pet_state_id, :starred],
|
|
|
|
:methods => [:color_id, :species_id, :worn_and_unworn_item_ids]
|
|
|
|
end
|
|
|
|
|
2010-11-13 14:26:14 -08:00
|
|
|
def closet_item_ids
|
|
|
|
item_outfit_relationships.map(&:item_id)
|
|
|
|
end
|
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def color_id
|
|
|
|
pet_state.pet_type.color_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def species_id
|
|
|
|
pet_state.pet_type.species_id
|
|
|
|
end
|
|
|
|
|
2010-11-13 14:26:14 -08:00
|
|
|
def to_query
|
|
|
|
{
|
|
|
|
:closet => closet_item_ids,
|
|
|
|
:color => color_id,
|
|
|
|
:objects => worn_item_ids,
|
|
|
|
:species => species_id,
|
|
|
|
:state => pet_state_id
|
|
|
|
}.to_query
|
|
|
|
end
|
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def worn_and_unworn_item_ids
|
2010-11-10 13:59:54 -08:00
|
|
|
{:worn => [], :unworn => []}.tap do |output|
|
2010-11-11 10:43:22 -08:00
|
|
|
item_outfit_relationships.each do |rel|
|
2010-11-10 13:59:54 -08:00
|
|
|
key = rel.is_worn? ? :worn : :unworn
|
2010-11-11 10:43:22 -08:00
|
|
|
output[key] << rel.item_id
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-13 05:50:37 -08:00
|
|
|
def worn_and_unworn_item_ids=(all_item_ids)
|
|
|
|
new_rels = []
|
|
|
|
all_item_ids.each do |key, item_ids|
|
|
|
|
worn = key == 'worn'
|
2010-11-14 19:31:41 -08:00
|
|
|
unless item_ids.blank?
|
|
|
|
item_ids.each do |item_id|
|
|
|
|
rel = ItemOutfitRelationship.new
|
|
|
|
rel.item_id = item_id
|
|
|
|
rel.is_worn = worn
|
|
|
|
new_rels << rel
|
|
|
|
end
|
2010-11-13 05:50:37 -08:00
|
|
|
end
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|
2010-11-13 05:50:37 -08:00
|
|
|
self.item_outfit_relationships = new_rels
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|
2010-11-13 14:26:14 -08:00
|
|
|
|
|
|
|
def worn_item_ids
|
|
|
|
worn_and_unworn_item_ids[:worn]
|
|
|
|
end
|
2011-02-10 14:50:47 -08:00
|
|
|
|
|
|
|
def self.build_for_user(user, params)
|
|
|
|
Outfit.new.tap do |outfit|
|
|
|
|
name = params.delete(:name)
|
|
|
|
starred = params.delete(:starred)
|
|
|
|
if user
|
|
|
|
outfit.user = user
|
|
|
|
outfit.name = name
|
|
|
|
outfit.starred = starred
|
|
|
|
end
|
|
|
|
outfit.attributes = params
|
|
|
|
end
|
|
|
|
end
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|