Permalink
Browse files

Fixing bug invoving persistance, adding who.

There was a bug by which even though a new Extra was getting created, it
wasn't having its instance variables set. Whoops.

Also, Extra::Extra.who now exists.
  • Loading branch information...
1 parent afa9070 commit db869436e8bfa84e0e4722eee74beb3bde6e6175 @steveklabnik committed Sep 2, 2010
Showing with 42 additions and 13 deletions.
  1. +20 −6 lib/extra/extra.rb
  2. +21 −7 spec/extraextra_spec.rb
  3. +1 −0 spec/factories/user.rb
View
@@ -14,12 +14,26 @@ class Extra
:what,
:when
+ def initialize opts={}
+ self.category ||= opts['category']
+ self.who_id ||= opts['who_id']
+ self.who_name ||= opts['who_name']
+ self.who_class ||= opts['who_class']
+ self.what ||= opts['what']
+ self.when ||= opts['when']
+ end
+
# For now, this is just a simple sentence describing what happened.
# We'll see if something more complex makes sense later.
def to_s
"#{who_name} #{what}"
end
+ #returns the instantiated object of whodunnit
+ def who
+ Object.const_get(who_class).find(who_id)
+ end
+
class << self
attr_accessor :db, :collection
@@ -39,12 +53,12 @@ def source opts={}
def !(category, user, text)
args = {
- category: category,
- who_id: user.id,
- who_name: user.username,
- who_class: user.class.to_s,
- what: text,
- when: Time.now.to_s
+ 'category' => category,
+ 'who_id' => user.id,
+ 'who_name' => user.username,
+ 'who_class' => user.class.to_s,
+ 'what' => text,
+ 'when' => Time.now.to_s
}
collection.insert args
Extra.new args
View
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Extra::Extra do
- describe "#source" do
+ describe "self#source" do
it "should exist" do
Extra::Extra.respond_to?(:source).should == true
end
@@ -25,14 +25,14 @@
#holy setup batman
collection = mock("collection")
Extra::Extra.should_receive(:collection).and_return(collection)
- collection.should_receive(:insert).with(:category => :breaking, :who_id => 1, :who_name => "steve", :who_class => 'User', :what => "hit a home run", :when => Time.now.to_s)
+ collection.should_receive(:insert).with('category' => :breaking, 'who_id' => 1, 'who_name' => "steve", 'who_class' => 'User', 'what' => "hit a home run", 'when' => Time.now.to_s)
user = mock("User", :id => 1, :class => 'User', :username => "steve")
Extra::Extra::! :breaking, user, "hit a home run"
end
end
- describe "#read_all_about_it" do
+ describe "self#read_all_about_it" do
it "should exist" do
Extra::Extra.respond_to?(:read_all_about_it).should == true
@@ -47,18 +47,32 @@
extras.length.should == 1
ex = extras.first
ex.category.should == extra.category
- ex.who_id.should == extra.who_id
- ex.who_name.should == extra.who_name
- ex.who_class.should == extra.who_class
+ ex.who_id.should == user.id
+ ex.who_name.should == user.username
+ ex.who_class.should == user.class.to_s
ex.what.should == extra.what
ex.when.should == extra.when
ex.to_s.should == extra.to_s
end
end
- describe "#the_scoop" do
+ describe "self#the_scoop" do
it "should exist" do
Extra::Extra.respond_to?(:the_scoop).should == true
end
end
+
+ describe "#who" do
+ it "should instantiate the object" do
+ user = Factory(:user)
+ Extra::Extra.source
+ extra = Extra::Extra::! :breaking, user, "hit a home run"
+
+ u = extra.who
+ u.class.should == User
+ u.id.should == user.id
+
+ end
+
+ end
end
View
@@ -6,6 +6,7 @@ def id=(id); end
def id; 1 end
def username=(username); end
def username; "steve" end
+ def self.find(id); User.new end
end

0 comments on commit db86943

Please sign in to comment.