Skip to content

Commit

Permalink
support callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Oct 21, 2014
1 parent a7a28c7 commit a9c98c1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
14 changes: 5 additions & 9 deletions lib/spike/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@

module Spike
class Base
#extend ActiveSupport::Concern

# ActiveModel
include ActiveModel::Conversion
extend ActiveModel::Translation
extend ActiveModel::Callbacks

# Spike
include Associations
include Attributes
include Http
include Orm

# ActiveModel
include ActiveModel::Conversion
extend ActiveModel::Translation

#included do
#extend ActiveModel::Callbacks
#end

end
end
16 changes: 11 additions & 5 deletions lib/spike/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Orm
extend ActiveSupport::Concern

included do
#define_model_callbacks :create, :update, :save
define_model_callbacks :create, :update, :save
class << self
attr_accessor :current_scope
delegate :find, :where, to: :all
Expand All @@ -32,10 +32,16 @@ def persisted?
end

def save
if persisted?
self.class.put Path.new(self.class.uri_template, id: id), to_params
else
self.class.post Path.new(self.class.uri_template), to_params
run_callbacks :save do
if persisted?
run_callbacks :update do
self.class.put Path.new(self.class.uri_template, id: id), to_params
end
else
run_callbacks :create do
self.class.post Path.new(self.class.uri_template), to_params
end
end
end
end

Expand Down
17 changes: 11 additions & 6 deletions test/callbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
module Spike
class CallbacksTest < MiniTest::Test

def setup
stub_request(:any, /.*/)
end

def test_before_save
Recipe.any_instance.expects(:before_save_callback)
Recipe.create
end

def test_before_create
skip
Recipe.any_instance.expects(:before_create_callback)
Recipe.create
end

def test_before_update

end

def test_before_save

Recipe.any_instance.expects(:before_update_callback)
Recipe.new(id: 1).save
end

end
Expand Down
4 changes: 4 additions & 0 deletions test/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Recipe < Spike::Base
scope :published, -> { where(status: 'published') }
attributes :title

before_save :before_save_callback
before_create :before_create_callback
before_update :before_update_callback

def self.page(number)
if number.present?
where(page: number)
Expand Down

0 comments on commit a9c98c1

Please sign in to comment.