diff --git a/app/models/person.rb b/app/models/person.rb index 276f5a062..ffa1f5b91 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -9,23 +9,27 @@ class Person xml_accessor :profile, :as => Profile - key :email, String + key :email, String, :unique => true key :url, String - key :active, Boolean, :default => false key :key_fingerprint, String + key :owner_id, ObjectId + + belongs_to :owner, :class_name => 'User' one :profile, :class_name => 'Profile' + + many :users, :class_name => 'User' many :posts, :class_name => 'Post', :foreign_key => :person_id many :albums, :class_name => 'Album', :foreign_key => :person_id + timestamps! before_validation :clean_url - validates_presence_of :email, :url, :key_fingerprint + validates_presence_of :email, :url, :key_fingerprint, :profile validates_format_of :url, :with => /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix - validates_true_for :url, :logic => lambda { self.url_unique?} after_destroy :remove_all_traces, :remove_key @@ -45,13 +49,46 @@ class Person GPGME::export(key_fingerprint, :armor => true) end - protected - - def url_unique? - same_url = Person.first(:url => self.url) - return same_url.nil? || same_url.id == self.id + + + + + ######## Posting ######## + def post(class_name, options = {}) + options[:person] = self + model_class = class_name.to_s.camelize.constantize + post = model_class.instantiate(options) end + ######## Commenting ######## + def comment(text, options = {}) + raise "must comment on something!" unless options[:on] + c = Comment.new(:person_id => self.id, :text => text, :post => options[:on]) + if c.save + if mine?(c.post) + c.push_to(c.post.people_with_permissions) # should return plucky query + else + c.push_to([c.post.person]) + end + true + end + false + end + + ##profile + def update_profile(params) + if self.update_attributes(params) + puts self.profile.class + self.profile.notify_people! + true + else + false + end + end + + + + protected def clean_url self.url ||= "http://localhost:3000/" if self.class == User if self.url diff --git a/app/models/user.rb b/app/models/user.rb index ba06815a8..0fe65b719 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,49 +1,36 @@ -class User < Person +class User + include MongoMapper::Document devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - - before_validation_on_create :assign_key - validates_presence_of :profile + + #before_validation_on_create :assign_key before_validation :do_bad_things - - - - ######## Posting ######## - def post(class_name, options = {}) - options[:person] = self - model_class = class_name.to_s.camelize.constantize - post = model_class.instantiate(options) + one :person, :class_name => 'Person', :foreign_key => :owner_id + + key :friend_ids, Array + key :pending_friend_ids, Array + + + def friends + Person.all(:id => self.friend_ids) end - ######## Commenting ######## - def comment(text, options = {}) - raise "must comment on something!" unless options[:on] - c = Comment.new(:person_id => self.id, :text => text, :post => options[:on]) - if c.save - if mine?(c.post) - c.push_to(c.post.people_with_permissions) # should return plucky query - else - c.push_to([c.post.person]) - end - true - end - false + def pending_friends + Person.all(:id => self.pending_friend_ids) + end + + + def real_name + "#{person.profile.first_name.to_s} #{person.profile.last_name.to_s}" end - ##profile - def update_profile(params) - if self.update_attributes(params) - puts self.profile.class - self.profile.notify_people! - true - else - false - end - end + + + ######### Friend Requesting def send_friend_request_to(friend_url) @@ -86,11 +73,18 @@ class User < Person end def unfriend(friend_id) - bad_friend = Person.first(:id => friend_id, :active => true) + bad_friend = self.friends.first(:id => friend_id) + self.friends.detect{|x| x.id == friend_id}.delete if bad_friend - Retraction.for(self).push_to_url(bad_friend.url) - bad_friend.destroy + + + bad + + + Retraction.for(self).push_to_url(bad_friend.url) + bad_friend.destroy if bad_friend.users.count == 0 end + self.save end def send_request(rel_hash) @@ -125,11 +119,11 @@ class User < Person protected def assign_key - keys = GPGME.list_keys(real_name, true) + keys = GPGME.list_keys(self.real_name, true) if keys.empty? generate_key end - self.key_fingerprint = GPGME.list_keys(real_name, true).first.subkeys.first.fingerprint + self.key_fingerprint = GPGME.list_keys(self.real_name, true).first.subkeys.first.fingerprint end def generate_key diff --git a/spec/factories.rb b/spec/factories.rb index 076ce1782..3a71bb2fc 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -11,21 +11,20 @@ Factory.define :profile do |p| end Factory.define :person do |p| - p.email "bob-person@aol.com" - p.active true - p.sequence(:url) {|n|"http://google-#{n}.com/"} + p.sequence(:email) {|n| "bob-person-#{n}@aol.com"} + p.sequence(:url) {|n| "http://google-#{n}.com/"} p.key_fingerprint GPGME::list_keys("Wesley").first.subkeys.first.fingerprint - p.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" ) + p.profile Factory.create(:profile) end Factory.define :user do |u| u.sequence(:email) {|n| "bob#{n}@aol.com"} u.password "bluepin7" u.password_confirmation "bluepin7" - u.url "www.example.com/" - u.key_fingerprint GPGME.list_keys("Smith", true).first.subkeys.first.fingerprint - u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" ) + + u.person Factory.create(:person) end + Factory.define :status_message do |m| m.sequence(:message) {|n| "jimmy's #{n} whales"} m.person diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 1660371b6..15a53cf80 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -1,17 +1,11 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Person do - it 'should not allow two people with the same url' do + it 'should not allow two people with the same email' do person_one = Factory.create(:person) - person_two = Factory.build(:person, :url => person_one.url) + person_two = Factory.build(:person, :url => person_one.email) person_two.valid?.should == false end - - it 'should not allow a person with the same url as the user' do - user = Factory.create(:user) - person = Factory.build(:person, :url => user.url) - person.valid?.should == false - end it 'should serialize to xml' do person = Factory.create(:person) @@ -25,15 +19,6 @@ describe Person do (xml.include? "first_name").should == true end - it 'should only return active friends' do - Factory.create(:person) - Factory.create(:person, :active => false) - Factory.create(:person, :active => false) - - Person.friends.all.count.should == 1 - end - - it 'should delete all of user except comments upon user deletion' do Factory.create(:user) @@ -57,14 +42,15 @@ describe Person do s.comments.count.should == 4 end - it 'should let a user unfriend another user' do - u = Factory.create(:user) + it 'should let a user unfriend a person' do + user = Factory.create(:user) + person = Factory.create(:person) - f = Factory.create(:person, :active => true) + user.friends << person - Person.friends.all.count.should == 1 - u.unfriend(f.id) - Person.friends.all.count.should == 0 + user.friends.count.should == 1 + user.unfriend(person.id) + user.friends.count.should == 0 Person.all.count.should == 1 end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1431b1cd5..8124f90bd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,6 +1,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe User do + it "should be a person" do n = Person.count Factory.create(:user)