diaspora/app/models/aspect.rb

44 lines
999 B
Ruby

# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class Aspect
include MongoMapper::Document
key :name, String
key :request_ids, Array
key :post_ids, Array
many :people, :foreign_key => 'aspect_ids', :class_name => 'Contact'
many :requests, :in => :request_ids, :class_name => 'Request'
many :posts, :in => :post_ids, :class_name => 'Post'
belongs_to :user, :class_name => 'User'
validates_presence_of :name
validates_uniqueness_of :name, :scope => :user_id
attr_accessible :name
timestamps!
def to_s
name
end
def posts_by_person_id( id )
id = id.to_id
posts.detect{|x| x.person.id == id }
end
def as_json(opts = {})
{
:aspect => {
:name => self.name,
:people => self.people.each{|person| person.as_json},
:posts => self.posts.each {|post| post.as_json },
}
}
end
end