Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Inherit a copy of hook definitions from the superclass, if possible

  • Loading branch information...
commit c1c2a2545057f0ef222b01535b7cf7e5eaa0d158 1 parent 2a6db54
@thomsbg thomsbg authored committed
Showing with 57 additions and 39 deletions.
  1. +11 −7 lib/her/model/hooks.rb
  2. +46 −32 spec/model/hooks_spec.rb
View
18 lib/her/model/hooks.rb
@@ -59,23 +59,27 @@ def wrap_in_hooks(resource, *hooks) # {{{
perform_after_hooks(resource, *hooks.reverse)
end # }}}
- private
# @private
def hooks # {{{
- @her_hooks
+ @her_hooks ||= begin
+ if superclass.respond_to?(:hooks)
+ superclass.hooks.dup
+ else
+ {}
+ end
+ end
end # }}}
+ private
+
# @private
def set_hook(time, name, action) # {{{
- @her_hooks ||= {}
- (@her_hooks["#{time}_#{name}".to_sym] ||= []) << action
+ (self.hooks["#{time}_#{name}".to_sym] ||= []) << action
end # }}}
# @private
def perform_hook(record, time, name) # {{{
- @her_hooks ||= {}
- hooks = @her_hooks["#{time}_#{name}".to_sym] || []
- hooks.each do |hook|
+ Array(self.hooks["#{time}_#{name}".to_sym]).each do |hook|
if hook.is_a? Symbol
record.send(hook)
else
View
78 spec/model/hooks_spec.rb
@@ -10,102 +10,116 @@
describe "method hooks" do
it "handles “before save” method hooks" do # {{{
Foo::User.before_save :set_internal_id
- Foo::User.send(:hooks)[:before_save].length.should == 1
- Foo::User.send(:hooks)[:before_save].first.class.should == Symbol
+ Foo::User.hooks[:before_save].length.should == 1
+ Foo::User.hooks[:before_save].first.class.should == Symbol
end # }}}
it "handles “before create” method hooks" do # {{{
Foo::User.before_create :set_internal_id
- Foo::User.send(:hooks)[:before_create].length.should == 1
- Foo::User.send(:hooks)[:before_create].first.class.should == Symbol
+ Foo::User.hooks[:before_create].length.should == 1
+ Foo::User.hooks[:before_create].first.class.should == Symbol
end # }}}
it "handles “before update” method hooks" do # {{{
Foo::User.before_update :set_internal_id
- Foo::User.send(:hooks)[:before_update].length.should == 1
- Foo::User.send(:hooks)[:before_update].first.class.should == Symbol
+ Foo::User.hooks[:before_update].length.should == 1
+ Foo::User.hooks[:before_update].first.class.should == Symbol
end # }}}
it "handles “before destroy” method hooks" do # {{{
Foo::User.before_destroy :set_internal_id
- Foo::User.send(:hooks)[:before_destroy].length.should == 1
- Foo::User.send(:hooks)[:before_destroy].first.class.should == Symbol
+ Foo::User.hooks[:before_destroy].length.should == 1
+ Foo::User.hooks[:before_destroy].first.class.should == Symbol
end # }}}
it "handles “after save” method hooks" do # {{{
Foo::User.after_save :set_internal_id
- Foo::User.send(:hooks)[:after_save].length.should == 1
- Foo::User.send(:hooks)[:after_save].first.class.should == Symbol
+ Foo::User.hooks[:after_save].length.should == 1
+ Foo::User.hooks[:after_save].first.class.should == Symbol
end # }}}
it "handles “after create” method hooks" do # {{{
Foo::User.after_create :set_internal_id
- Foo::User.send(:hooks)[:after_create].length.should == 1
- Foo::User.send(:hooks)[:after_create].first.class.should == Symbol
+ Foo::User.hooks[:after_create].length.should == 1
+ Foo::User.hooks[:after_create].first.class.should == Symbol
end # }}}
it "handles “after update” method hooks" do # {{{
Foo::User.after_update :set_internal_id
- Foo::User.send(:hooks)[:after_update].length.should == 1
- Foo::User.send(:hooks)[:after_update].first.class.should == Symbol
+ Foo::User.hooks[:after_update].length.should == 1
+ Foo::User.hooks[:after_update].first.class.should == Symbol
end # }}}
it "handles “after destroy” method hooks" do # {{{
Foo::User.after_destroy :set_internal_id
- Foo::User.send(:hooks)[:after_destroy].length.should == 1
- Foo::User.send(:hooks)[:after_destroy].first.class.should == Symbol
+ Foo::User.hooks[:after_destroy].length.should == 1
+ Foo::User.hooks[:after_destroy].first.class.should == Symbol
end # }}}
end
describe "block hooks" do
it "handles “before save” block hooks" do # {{{
Foo::User.before_save { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:before_save].length.should == 1
- Foo::User.send(:hooks)[:before_save].first.class.should == Proc
+ Foo::User.hooks[:before_save].length.should == 1
+ Foo::User.hooks[:before_save].first.class.should == Proc
end # }}}
it "handles “before create” block hooks" do # {{{
Foo::User.before_create { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:before_create].length.should == 1
- Foo::User.send(:hooks)[:before_create].first.class.should == Proc
+ Foo::User.hooks[:before_create].length.should == 1
+ Foo::User.hooks[:before_create].first.class.should == Proc
end # }}}
it "handles “before update” block hooks" do # {{{
Foo::User.before_update { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:before_update].length.should == 1
- Foo::User.send(:hooks)[:before_update].first.class.should == Proc
+ Foo::User.hooks[:before_update].length.should == 1
+ Foo::User.hooks[:before_update].first.class.should == Proc
end # }}}
it "handles “before destroy” block hooks" do # {{{
Foo::User.before_destroy { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:before_destroy].length.should == 1
- Foo::User.send(:hooks)[:before_destroy].first.class.should == Proc
+ Foo::User.hooks[:before_destroy].length.should == 1
+ Foo::User.hooks[:before_destroy].first.class.should == Proc
end # }}}
it "handles “after save” block hooks" do # {{{
Foo::User.after_save { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:after_save].length.should == 1
- Foo::User.send(:hooks)[:after_save].first.class.should == Proc
+ Foo::User.hooks[:after_save].length.should == 1
+ Foo::User.hooks[:after_save].first.class.should == Proc
end # }}}
it "handles “after create” block hooks" do # {{{
Foo::User.after_create { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:after_create].length.should == 1
- Foo::User.send(:hooks)[:after_create].first.class.should == Proc
+ Foo::User.hooks[:after_create].length.should == 1
+ Foo::User.hooks[:after_create].first.class.should == Proc
end # }}}
it "handles “after update” block hooks" do # {{{
Foo::User.after_update { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:after_update].length.should == 1
- Foo::User.send(:hooks)[:after_update].first.class.should == Proc
+ Foo::User.hooks[:after_update].length.should == 1
+ Foo::User.hooks[:after_update].first.class.should == Proc
end # }}}
it "handles “after destroy” block hooks" do # {{{
Foo::User.after_destroy { |record| record.internal_id = 42 }
- Foo::User.send(:hooks)[:after_destroy].length.should == 1
- Foo::User.send(:hooks)[:after_destroy].first.class.should == Proc
+ Foo::User.hooks[:after_destroy].length.should == 1
+ Foo::User.hooks[:after_destroy].first.class.should == Proc
end # }}}
end
+
+ context "inheriting hooks from a superclass" do
+ before do
+ end
+
+ it "copies hooks to the subclass" do
+ Foo::User.before_save :set_internal_id
+ Foo::User.after_create { |record| record.internal_id = 42 }
+ subclass = Class.new(Foo::User)
+ subclass.hooks.object_id.should_not == Foo::User.hooks.object_id
+ subclass.hooks[:before_save].should == [:set_internal_id]
+ subclass.hooks[:after_create].length.should == 1
+ end
+ end
end
context "perform hooks on a model" do
Please sign in to comment.
Something went wrong with that request. Please try again.