Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Fixing bug invoving persistance, adding who.
Browse files Browse the repository at this point in the history
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
steveklabnik committed Sep 2, 2010
1 parent afa9070 commit db86943
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
26 changes: 20 additions & 6 deletions lib/extra/extra.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down
28 changes: 21 additions & 7 deletions spec/extraextra_spec.rb
@@ -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
Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions spec/factories/user.rb
Expand Up @@ -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

Expand Down

0 comments on commit db86943

Please sign in to comment.