diff --git a/app/models/person.rb b/app/models/person.rb index d0db04eb2..a7ccdbf0e 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -8,13 +8,9 @@ class Person key :email, String key :real_name, String - #key :post_ids, Array#, :typecast => ObjectId - + one :profile, :class_name => 'Profile', :foreign_key => :person_id many :posts, :class_name => 'Post', :foreign_key => :person_id - validates_presence_of :email, :real_name + validates_presence_of :email, :real_name, :profile - # def newest(type = nil) - # type.constantize.where(:person_id => id).last - # end end diff --git a/app/models/post.rb b/app/models/post.rb index 4567537c9..1366e2877 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -8,19 +8,13 @@ class Post xml_accessor :_id key :person_id, ObjectId - belongs_to :person, :class_name => 'Person' - many :comments, :class_name => 'Comment', :foreign_key => :post_id + belongs_to :person, :class_name => 'Person' timestamps! - - after_save :send_to_view - #validates_presence_of :person - - def self.stream Post.sort(:created_at.desc).all diff --git a/app/models/profile.rb b/app/models/profile.rb new file mode 100644 index 000000000..16a3d22b3 --- /dev/null +++ b/app/models/profile.rb @@ -0,0 +1,11 @@ +class Profile + include MongoMapper::Document + + key :first_name, String + key :last_name, String + + belongs_to :person, :class_name => "Person" + + validates_presence_of :first_name, :last_name, :person + +end diff --git a/spec/factories.rb b/spec/factories.rb index ded4bf247..306471d97 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,11 +1,33 @@ #For Guidance #http://github.com/thoughtbot/factory_girl -#http://railscasts.com/episodes/158-factories-not-fixtures +# http://railscasts.com/episodes/158-factories-not-fixtures + + +Factory.define :profile do |p| + p.first_name "Robert" + p.last_name "Grimm" + p.person Person.new( :email => "bob@aol.com", :real_name => "Bob" ) +end + +Factory.define :person do |p| + p.email "bob@aol.com" + p.real_name "Bob" + p.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" ) +end + +Factory.define :user do |u| + u.real_name 'Bob Smith' + u.sequence(:email) {|n| "bob#{n}@aol.com"} + u.password "bluepin7" + u.password_confirmation "bluepin7" + u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" ) +end Factory.define :friend do |f| f.real_name 'John Doe' f.email 'max@max.com' f.url 'http://max.com/' + f.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" ) end Factory.define :status_message do |m| @@ -17,12 +39,6 @@ Factory.define :blog do |b| b.sequence(:body) {|n| "jimmy's huge #{n} whales"} end -Factory.define :user do |u| - u.real_name 'Bob Smith' - u.sequence(:email) {|n| "bob#{n}@aol.com"} - u.password "bluepin7" - u.password_confirmation "bluepin7" -end Factory.define :bookmark do |b| b.link "http://www.yahooligans.com/" @@ -31,4 +47,4 @@ end Factory.define :post do |p| end -Factory.define(:comment) {} \ No newline at end of file +Factory.define(:comment) {} diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index 32ef8eaeb..5ef06a625 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -16,7 +16,6 @@ describe Friend do n.valid?.should be true end - it 'should validate its url' do friend = Factory.build(:friend) @@ -65,23 +64,4 @@ describe Friend do friend.url = "http:///www.asodij.com/" friend.valid?.should == false end - - describe "XML" do - before do - @f = Factory.build(:friend) - @xml = "\n #{@f.url}\n #{@f.email}\n #{@f.real_name}\n" - end - - it 'should serialize to XML' do - @f.to_xml.to_s.should == @xml - end - - it 'should marshal serialized XML to object' do - parsed = Friend.from_xml(@xml) - parsed.email.should == @f.email - parsed.url.should == @f.url - parsed.valid?.should be_true - end - end - end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb new file mode 100644 index 000000000..0701b8c69 --- /dev/null +++ b/spec/models/person_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe Person do + + it 'should require a profile' do + person = Factory.build(:person, :profile => nil) + person.valid?.should be false + person.profile = Factory.build(:profile) + person.valid?.should be true + end + +end diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb new file mode 100644 index 000000000..5cb57b270 --- /dev/null +++ b/spec/models/profile_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe Profile do + before do + @person = Factory.build(:person) + end + + describe 'requirements' do + it "should include a first name" do + @person.profile = Factory.build(:profile, :person => @person, :first_name => nil) + @person.profile.valid?.should be false + @person.profile.first_name = "Bob" + @person.profile.valid?.should be true + end + + it "should include a last name" do + @person.profile = Factory.build(:profile, :person => @person, :last_name => nil) + @person.profile.valid?.should be false + @person.profile.last_name = "Smith" + @person.profile.valid?.should be true + end + + it "should include a person" do + profile = Factory.build(:profile, :person => nil) + profile.valid?.should be false + profile.person = @person + profile.valid?.should be true + end + end + +end +