Skip to content
This repository has been archived by the owner on May 25, 2018. It is now read-only.

Commit

Permalink
observe namespaced model
Browse files Browse the repository at this point in the history
  • Loading branch information
karloku committed Jun 2, 2015
1 parent 456153e commit 6222c11
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/faye-rails/controller/observer_factory.rb
Expand Up @@ -9,12 +9,15 @@ module ObserverFactory
# Create
def self.define(klass, method_name, &block)
# Make a name for the callback module

klass_callbacks_name = "#{klass.name}Callbacks"

# Load the callback module if exists
unless (klass_callbacks = ObserverFactory.observer(klass_callbacks_name))
# Define callback module if one does not exist
klass_callbacks = Object.const_set(klass_callbacks_name, Module.new)
klass_parent = klass.parent
demodulized_callbacks_name = klass_callbacks_name.demodulize
klass_callbacks = klass_parent.const_set(demodulized_callbacks_name, Module.new)
end

# Add the method to the observer
Expand All @@ -27,7 +30,7 @@ def self.define(klass, method_name, &block)
end

def self.observer(module_name)
ref = Module.const_get(module_name)
ref = module_name.constantize
return ref if ref.is_a?(Module)
nil
rescue
Expand Down
46 changes: 46 additions & 0 deletions spec/observer_factory_spec.rb
Expand Up @@ -43,4 +43,50 @@
end

end

context "create new observer with namespace" do
before(:all) do
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :admin_ryans do |t|
t.string :name
t.timestamps
end

module Admin
Ryan = Class.new(ActiveRecord::Base)
Ryan.table_name = 'admin_ryans'
end
FayeRails::Controller::ObserverFactory.define(Admin::Ryan, :before_create) { |r| r.name = "will create" }
FayeRails::Controller::ObserverFactory.define(Admin::Ryan, :after_create) { |r| }
FayeRails::Controller::ObserverFactory.define(Admin::Ryan, :before_update) { |r| r.name = "will update" }
end

it { FayeRails::Controller::ObserverFactory.observer("Admin::RyanCallbacks").should_not be_nil }
it { FayeRails::Controller::ObserverFactory.observer("Admin::RyansCallbacks").should be_nil }

context "should define a module with callback methods" do
it { Admin::RyanCallbacks.should be_a_kind_of Module }
it { Admin::RyanCallbacks.method_defined?(:after_create).should be_truthy }
it { Admin::RyanCallbacks.method_defined?(:before_create).should be_truthy }
it { Admin::RyanCallbacks.method_defined?(:before_update).should be_truthy }
it { Admin::RyanCallbacks.method_defined?(:before_validation).should be_falsey }
it { Admin::RyanCallbacks.method_defined?(:after_validation).should be_falsey }
it { Admin::RyanCallbacks.method_defined?(:before_save).should be_falsey }
it { Admin::RyanCallbacks.method_defined?(:after_save).should be_falsey }
it { Admin::RyanCallbacks.method_defined?(:after_commit).should be_falsey }
end

context "callbacks should be called" do
it "should call after_create callback" do
r = Admin::Ryan.create!(:name => 'ryan')
r._create_callbacks.length.should == 2
r.name.should == "will create"

r.name = "hey"
r.save
r.name.should == "will update"
end
end

end
end

0 comments on commit 6222c11

Please sign in to comment.