forked from fatfreecrm/fat_free_crm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended callbacks to allow a hook to be defined with a block, and th…
…en content can be appended or prepended, or replace or remove the block entirely. Is fully backwards compatible with existing view hooks
- Loading branch information
1 parent
a897de7
commit 63ac207
Showing
2 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
class TestCallback < FatFreeCRM::Callback::Base | ||
insert_before :test_before do |view, context| | ||
"BEFORE-" | ||
end | ||
insert_after :test_after do |view, context| | ||
"-AFTER" | ||
end | ||
replace :test_replace do |view, context| | ||
"REPLACE" | ||
end | ||
remove :test_remove | ||
|
||
def test_legacy(view, context={}) | ||
"LEGACY" | ||
end | ||
end | ||
|
||
include FatFreeCRM::Callback::Helper | ||
include ActionView::Helpers::CaptureHelper | ||
|
||
def test_hook(position) | ||
Haml::Engine.new(%Q^ | ||
= hook("test_#{position}".to_sym, ActionView::Base.new) do | ||
BLOCK^).render.gsub("\n",'') | ||
end | ||
|
||
describe FatFreeCRM::Callback do | ||
it "should insert content before the block" do | ||
test_hook(:before).should == "BEFORE-BLOCK" | ||
end | ||
|
||
it "should insert content after the block" do | ||
test_hook(:after).should == "BLOCK-AFTER" | ||
end | ||
|
||
it "should replace the content of the block" do | ||
test_hook(:replace).should == "REPLACE" | ||
end | ||
|
||
it "should remove the block" do | ||
test_hook(:remove).should == "" | ||
end | ||
|
||
it "should still work for legacy hooks" do | ||
Haml::Engine.new(%Q^ | ||
= hook(:test_legacy, ActionView::Base.new) | ||
^).render.gsub("\n",'').should == "LEGACY" | ||
end | ||
end |