Skip to content

Commit

Permalink
attempt to fix #66; url is converted to a string on Item construction…
Browse files Browse the repository at this point in the history
… with adapter-specific url_for call
  • Loading branch information
mjtko committed Aug 22, 2011
1 parent 1d9b5c5 commit 4d26251
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/simple_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module SimpleNavigation
class << self
extend Forwardable

def_delegators :adapter, :request, :request_uri, :request_path, :context_for_eval, :current_page?
def_delegators :adapter, :request, :request_uri, :request_path, :context_for_eval, :current_page?, :url_for
def_delegators :adapter_class, :register

# Sets the root path and current environment as specified. Also sets the default config_file_path.
Expand Down
6 changes: 5 additions & 1 deletion lib/simple_navigation/adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def context_for_eval; end
# Used to determine if an item should be autohighlighted.
def current_page?(url); end

# Returns a URL as a string representation for the specified object.
# Used to translate objects to URL strings for autohighlight matching.
def url_for(object); end

# Returns a link with the specified name, url and options.
# Used for rendering.
def link_to(name, url, options={}); end
Expand All @@ -34,4 +38,4 @@ def content_tag(type, content, options={}); end

end
end
end
end
6 changes: 5 additions & 1 deletion lib/simple_navigation/adapters/padrino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def self.register
SimpleNavigation.set_env(PADRINO_ROOT, PADRINO_ENV)
::Padrino::Application.send(:helpers, SimpleNavigation::Helpers)
end

def url_for(object)
context.url_for object
end

def link_to(name, url, options={})
context.link_to name, url, options
Expand All @@ -17,4 +21,4 @@ def content_tag(type, content, options={})

end
end
end
end
4 changes: 4 additions & 0 deletions lib/simple_navigation/adapters/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def current_page?(url)
template.current_page?(url) if template
end

def url_for(object)
template.url_for(object) if template
end

def link_to(name, url, options={})
template.link_to(html_safe(name), url, options) if template
end
Expand Down
4 changes: 4 additions & 0 deletions lib/simple_navigation/adapters/sinatra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def current_page?(url)
end
end

def url_for(object)
object.to_s
end

def link_to(name, url, options={})
"<a href='#{url}' #{to_attributes(options)}>#{name}</a>"
end
Expand Down
9 changes: 8 additions & 1 deletion lib/simple_navigation/core/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ def initialize(container, key, name, url, options, items=nil, &sub_nav_block)
@key = key
@method = options.delete(:method)
@name = name
@url = url.instance_of?(Proc) ? url.call : url
@url = case url
when Proc
url.call
when String
url
else
SimpleNavigation.url_for(url)
end
@highlights_on = options.delete(:highlights_on)
@html_options = options
if sub_nav_block || items
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/simple_navigation/adapters/padrino_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def create_adapter
@adapter = create_adapter
end

describe 'url_for' do
it "should delegate to context" do
@context.should_receive(:url_for).with(:object)
@adapter.url_for(:object)
end
end

describe 'link_to' do
it "should delegate to context" do
@context.should_receive(:link_to).with('name', 'url', :my_option => true)
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/simple_navigation/adapters/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ def create_adapter
end
end

describe 'url_for' do
context 'template is set' do
before(:each) do
@adapter.stub!(:template => @template)
end
it "should delegate the call to the template" do
@template.should_receive(:url_for).with(:object)
@adapter.url_for(:object)
end
end
context 'template is not set' do
before(:each) do
@adapter.stub!(:template => nil)
end
it {@adapter.url_for(:object).should be_nil}
end
end

describe 'link_to' do
context 'template is set' do
before(:each) do
Expand Down
8 changes: 7 additions & 1 deletion spec/lib/simple_navigation/adapters/sinatra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def create_adapter
end
end

describe 'url_for' do
it "should return a string" do
@adapter.url_for(:object).should == 'object'
end
end

describe 'link_to' do
it "should return a link" do
@adapter.link_to('link', 'url', :class => 'clazz', :id => 'id').should == "<a href='url' class='clazz' id='id'>link</a>"
Expand All @@ -77,4 +83,4 @@ def create_adapter
end
end

end
end
11 changes: 10 additions & 1 deletion spec/lib/simple_navigation/core/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
before(:each) do
@item_container = stub(:item_container, :level => 1).as_null_object
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
@adapter = stub(:adapter)
@adapter = mock(:adapter)
@adapter.stub!(:url_for) { |o| o.as_url }
@object = mock(:object)
@object.stub!(:as_url) { 'my_url' }
SimpleNavigation.stub!(:adapter => @adapter)
end

Expand Down Expand Up @@ -114,6 +117,12 @@
end
it {@item.url.should == 'my_url'}
end
context 'url is an object' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', @object, {})
end
it {@item.url.should == 'my_url'}
end
end

end
Expand Down

0 comments on commit 4d26251

Please sign in to comment.