DG MS added a post model, going to put it more places than just status message now
This commit is contained in:
parent
4da68fc4b1
commit
497b477e89
8 changed files with 120 additions and 53 deletions
30
app/models/post.rb
Normal file
30
app/models/post.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
class Post
|
||||||
|
|
||||||
|
# XML accessors must always preceed mongo field tags
|
||||||
|
|
||||||
|
include Mongoid::Document
|
||||||
|
include Mongoid::Timestamps
|
||||||
|
include ROXML
|
||||||
|
|
||||||
|
xml_accessor :owner
|
||||||
|
xml_accessor :snippet
|
||||||
|
xml_accessor :source
|
||||||
|
|
||||||
|
field :owner
|
||||||
|
field :source
|
||||||
|
field :snippet
|
||||||
|
|
||||||
|
|
||||||
|
before_create :set_defaults
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def set_defaults
|
||||||
|
user_email = User.first.email
|
||||||
|
self.owner ||= user_email
|
||||||
|
self.source ||= user_email
|
||||||
|
self.snippet ||= user_email
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,18 +1,11 @@
|
||||||
class StatusMessage
|
class StatusMessage < Post
|
||||||
include Mongoid::Document
|
|
||||||
include Mongoid::Timestamps
|
|
||||||
include ROXML
|
|
||||||
|
|
||||||
xml_accessor :message
|
xml_accessor :message
|
||||||
xml_accessor :owner
|
|
||||||
|
|
||||||
|
|
||||||
field :message
|
field :message
|
||||||
field :owner
|
|
||||||
|
|
||||||
validates_presence_of :message
|
validates_presence_of :message
|
||||||
|
|
||||||
before_create :set_default_owner
|
|
||||||
|
|
||||||
def self.newest(owner_email)
|
def self.newest(owner_email)
|
||||||
StatusMessage.last(:conditions => {:owner => owner_email})
|
StatusMessage.last(:conditions => {:owner => owner_email})
|
||||||
|
|
@ -22,9 +15,5 @@ class StatusMessage
|
||||||
StatusMessage.newest(User.first.email)
|
StatusMessage.newest(User.first.email)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def set_default_owner
|
|
||||||
self.owner ||= User.first.email
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
18
lib/common.rb
Normal file
18
lib/common.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
module CommonField
|
||||||
|
|
||||||
|
def self.included(klass)
|
||||||
|
klass.class_eval do
|
||||||
|
include Mongoid::Document
|
||||||
|
include ROXML
|
||||||
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
|
xml_accessor :owner
|
||||||
|
xml_accessor :snippet
|
||||||
|
xml_accessor :source
|
||||||
|
|
||||||
|
field :owner
|
||||||
|
field :source
|
||||||
|
field :snippet
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -8,7 +8,11 @@ end
|
||||||
|
|
||||||
Factory.define :status_message do |m|
|
Factory.define :status_message do |m|
|
||||||
m.sequence(:message) {|n| "jimmy's #{n} whales"}
|
m.sequence(:message) {|n| "jimmy's #{n} whales"}
|
||||||
|
end
|
||||||
|
|
||||||
|
Factory.define :blog do |b|
|
||||||
|
b.sequence(:title) {|n| "bobby's #{n} penguins"}
|
||||||
|
b.sequence(:body) {|n| "jimmy's huge #{n} whales"}
|
||||||
end
|
end
|
||||||
|
|
||||||
Factory.define :user do |u|
|
Factory.define :user do |u|
|
||||||
|
|
@ -19,3 +23,8 @@ end
|
||||||
Factory.define :bookmark do |b|
|
Factory.define :bookmark do |b|
|
||||||
b.link "http://www.yahooligans.com/"
|
b.link "http://www.yahooligans.com/"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Factory.define :post do |p|
|
||||||
|
p.source "New York Times"
|
||||||
|
p.sequence(:snippet) {|n| "This is some information #{n}"}
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
require File.dirname(__FILE__) + '/../spec_helper'
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
describe Blog do
|
describe Blog do
|
||||||
|
before do
|
||||||
|
Factory.create(:user, :email => "bob@aol.com", :password => "diggity")
|
||||||
|
end
|
||||||
|
|
||||||
it "should have a title and body" do
|
it "should have a title and body" do
|
||||||
n = Blog.new
|
n = Blog.new
|
||||||
n.valid?.should be false
|
n.valid?.should be false
|
||||||
|
|
@ -11,51 +15,41 @@ describe Blog do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add an owner if none is present" do
|
it "should add an owner if none is present" do
|
||||||
User.create(:email => "bob@aol.com", :password => "big bux")
|
b = Factory.create(:blog)
|
||||||
n = Blog.create(:title => "kittens", :body => "puppies!")
|
b.owner.should == "bob@aol.com"
|
||||||
n.owner.should == "bob@aol.com"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
describe "newest" do
|
describe "newest" do
|
||||||
before do
|
before do
|
||||||
User.create(:email => "bob@aol.com", :password => "diggity")
|
(2..4).each { Factory.create(:blog, :owner => "some@dudes.com") }
|
||||||
Blog.create(:title => "bone dawg", :body => "wale for jimmy", :owner => "xzibit@dawgz.com")
|
(5..8).each { Factory.create(:blog) }
|
||||||
Blog.create(:title => "dawg bone", :body => "jimmy wales")
|
(9..11).each { Factory.create(:blog, :owner => "other@dudes.com") }
|
||||||
Blog.create(:title => "bone dawg", :body => "jimmy your wales", :owner => "some@dudes.com")
|
|
||||||
Blog.create(:title => "dawg bone", :body => "lions", :owner => "xzibit@dawgz.com")
|
|
||||||
Blog.create(:title => "bone dawg", :body => "bears")
|
|
||||||
Blog.create(:title => "dawg bone", :body => "sharks", :owner => "some@dudes.com")
|
|
||||||
Blog.create(:title => "bone dawg", :body => "roar")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent blog title and body from owner" do
|
it "should give the most recent blog title and body from owner" do
|
||||||
blog = Blog.my_newest
|
blog = Blog.my_newest
|
||||||
blog.title.should == "bone dawg"
|
blog.title.should == "bobby's 8 penguins"
|
||||||
blog.body.should == "roar"
|
blog.body.should == "jimmy's huge 8 whales"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent blog body for a given email" do
|
it "should give the most recent blog body for a given email" do
|
||||||
blog = Blog.newest("some@dudes.com")
|
blog = Blog.newest("some@dudes.com")
|
||||||
blog.title.should == "dawg bone"
|
blog.title.should == "bobby's 14 penguins"
|
||||||
blog.body.should == "sharks"
|
blog.body.should == "jimmy's huge 14 whales"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "XML" do
|
describe "XML" do
|
||||||
before do
|
|
||||||
@xml = "<blog>\n <title>yessir</title>\n <body>I hate WALRUSES!</body>\n <owner>Bob</owner>\n</blog>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should serialize to XML' do
|
it 'should serialize to XML' do
|
||||||
body = Blog.create(:title => "yessir", :body => "I hate WALRUSES!", :owner => "Bob")
|
body = Factory.create(:blog, :title => "yessir", :body => "penguins")
|
||||||
body.to_xml.to_s.should == @xml
|
body.to_xml.to_s.should include "<title>yessir</title>"
|
||||||
|
body.to_xml.to_s.should include "<body>penguins</body>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should marshal serialized XML to object' do
|
it 'should marshal serialized XML to object' do
|
||||||
parsed = Blog.from_xml(@xml)
|
xml = "<blog>\n <title>yessir</title>\n <body>I hate WALRUSES!</body>\n</blog>"
|
||||||
|
parsed = Blog.from_xml(xml)
|
||||||
parsed.body.should == "I hate WALRUSES!"
|
parsed.body.should == "I hate WALRUSES!"
|
||||||
parsed.owner.should == "Bob"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
describe Friend do
|
describe Friend do
|
||||||
it 'should have a diaspora username and diaspora url' do
|
it 'should have a diaspora username and diaspora url' do
|
||||||
n = Factory.build(:friend, :url => nil)
|
n = Factory.build(:friend, :url => nil)
|
||||||
#n = Friend.new(:username => 'max')
|
|
||||||
n.valid?.should be false
|
n.valid?.should be false
|
||||||
n.url = "http://max.com/"
|
n.url = "http://max.com/"
|
||||||
n.valid?.should be true
|
n.valid?.should be true
|
||||||
|
|
|
||||||
32
spec/models/post_spec.rb
Normal file
32
spec/models/post_spec.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
|
describe Post do
|
||||||
|
before(:all) do
|
||||||
|
Factory.create(:user, :email => "bob@aol.com")
|
||||||
|
@post = Factory.create(:post, :owner => nil, :source => nil, :snippet => nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'requirements' do
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'defaults' do
|
||||||
|
|
||||||
|
it "should add an owner if none is present" do
|
||||||
|
@post.owner.should == "bob@aol.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should add a source if none is present" do
|
||||||
|
@post.source.should == "bob@aol.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should add a snippet if none is present" do
|
||||||
|
@post.snippet.should == "bob@aol.com"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#question!
|
||||||
|
#STI ? do i need to call mongoid doc on child?
|
||||||
|
# validations inherit?
|
||||||
|
# type param.
|
||||||
|
# inheriting snippet builder method
|
||||||
|
|
@ -2,8 +2,9 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
describe StatusMessage do
|
describe StatusMessage do
|
||||||
before do
|
before do
|
||||||
@usr = Factory.create(:user,:email => "bob@aol.com", :password => "diggity")
|
Factory.create(:user, :email => "bob@aol.com", :password => "diggity")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have a message" do
|
it "should have a message" do
|
||||||
n = Factory.build(:status_message, :message => nil)
|
n = Factory.build(:status_message, :message => nil)
|
||||||
n.valid?.should be false
|
n.valid?.should be false
|
||||||
|
|
@ -18,12 +19,11 @@ describe StatusMessage do
|
||||||
|
|
||||||
describe "newest" do
|
describe "newest" do
|
||||||
before do
|
before do
|
||||||
(1..5).each { Factory.create(:status_message, :owner => "some@dudes.com") }
|
(1..5).each { Factory.create(:status_message, :owner => "some@dudes.com") }
|
||||||
(6..10).each { Factory.create(:status_message) }
|
(6..10).each { Factory.create(:status_message) }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should give the most recent message from owner" do
|
it "should give the most recent message from owner" do
|
||||||
#puts StatusMessage.newest("sam@cool.com")
|
|
||||||
StatusMessage.my_newest.message.should == "jimmy's 11 whales"
|
StatusMessage.my_newest.message.should == "jimmy's 11 whales"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -33,19 +33,15 @@ describe StatusMessage do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "XML" do
|
describe "XML" do
|
||||||
before do
|
|
||||||
@xml = "<statusmessage>\n <message>I hate WALRUSES!</message>\n <owner>Bob</owner>\n</statusmessage>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should serialize to XML' do
|
it 'should serialize to XML' do
|
||||||
message = Factory.create(:status_message, :message => "I hate WALRUSES!", :owner => "Bob")
|
message = Factory.create(:status_message, :message => "I hate WALRUSES!")
|
||||||
message.to_xml.to_s.should == @xml
|
message.to_xml.to_s.should include "<message>I hate WALRUSES!</message>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should marshal serialized XML to object' do
|
it 'should marshal serialized XML to object' do
|
||||||
parsed = StatusMessage.from_xml(@xml)
|
xml = "<statusmessage>\n <message>I hate WALRUSES!</message></statusmessage>"
|
||||||
|
parsed = StatusMessage.from_xml(xml)
|
||||||
parsed.message.should == "I hate WALRUSES!"
|
parsed.message.should == "I hate WALRUSES!"
|
||||||
parsed.owner.should == "Bob"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue