Skip to content

Commit

Permalink
refactor the persistence to go directly to API with no session
Browse files Browse the repository at this point in the history
  • Loading branch information
dnagir committed Dec 6, 2011
1 parent 574de84 commit e3f4336
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
20 changes: 12 additions & 8 deletions lib/morpheus/acts_as_persistent.rb
Expand Up @@ -6,19 +6,19 @@ module ActsAsPersistent
extend ActiveSupport::Concern

module InstanceMethods
def _session
Morpheus.current_session
end

def persisted?
!!id
end

def api
self.class.api
end

def save_without_validation
if persisted?
_session.update(self.class.api_endpoint, id, get_properties)
api.update(id, get_properties)
else
_session.create(self.class.api_endpoint, get_properties)
api.create(get_properties)
end
end

Expand Down Expand Up @@ -56,7 +56,7 @@ def destroy

def destroy!
if persisted?
_session.delete(self.class.api_endpoint, id)
api.delete(id)
true
else
false
Expand Down Expand Up @@ -84,8 +84,12 @@ def update_attributes!(new_attributes)
end

module ClassMethods
def api
Morpheus::API.const_get(self.api_endpoint.to_s.camelize).new
end

def get(id)
Morpheus.current_session.get(self, self.api_endpoint, id)
api.get(self, id)
end
end

Expand Down
18 changes: 9 additions & 9 deletions spec/acts_as_persistent_spec.rb
Expand Up @@ -13,8 +13,8 @@ def valid?; true; end
end

subject { person_class.new }
let(:session) { double('session').as_null_object }
before { Morpheus.stub(:current_session).and_return session }
let(:api) { double('API').as_null_object }
before { Morpheus::API::Relationships.stub(:new).and_return api }

describe "module interface" do
it { should respond_to :persisted? }
Expand Down Expand Up @@ -42,7 +42,7 @@ def valid?; true; end
end

it "should queue the CREATE operation with properties" do
session.should_receive(:create).with(:relationships, {:name => 'Dima'})
api.should_receive(:create).with({:name => 'Dima'})
subject.set_property(:name, 'Dima')
subject.save_without_validation
end
Expand All @@ -54,11 +54,11 @@ def valid?; true; end

it "should queue the UPDATE operation" do
subject.set_property(:name, 'Dima')
session.should_receive(:update).with(:relationships, 123, {:name => 'Dima'})
api.should_receive(:update).with(123, {:name => 'Dima'})
subject.save_without_validation end

it "should queue the DELETE operation" do
session.should_receive(:delete).with(:relationships, 123)
api.should_receive(:delete).with(123)
subject.destroy!
end

Expand All @@ -68,7 +68,7 @@ def valid?; true; end

describe "#destroy and #destroy!" do
it "should be ignored for new object" do
session.should_not_receive(:delete)
api.should_not_receive(:delete)
subject.destroy!
subject.destroy
end
Expand All @@ -79,12 +79,12 @@ def valid?; true; end
its(:destroy!) { should == true }

it "should queue the DELETE operation" do
session.should_receive(:delete).with(:relationships, 123)
api.should_receive(:delete).with(123)
subject.destroy!
end

it "should queue the DELETE operation (non-bang)" do
session.should_receive(:delete).with(:relationships, 123)
api.should_receive(:delete).with(123)
subject.destroy
end

Expand Down Expand Up @@ -119,7 +119,7 @@ def valid?; true; end
describe ".get" do
subject { person_class }
it "should queue GET" do
session.should_receive(:get).with(person_class, :relationships, 123)
api.should_receive(:get).with(person_class, 123)
subject.get(123)
end
end
Expand Down

0 comments on commit e3f4336

Please sign in to comment.