From bf83d335276228cc607819de79876f8cf925c256 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 26 Dec 2012 20:45:22 +0100 Subject: [PATCH] add engine link option --- app/views/cmtool/application/_menu.html.slim | 2 + lib/cmtool/menu.rb | 41 ++++++++++++++++++++ spec/cmtool_menu/engine_link_spec.rb | 24 ++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 spec/cmtool_menu/engine_link_spec.rb diff --git a/app/views/cmtool/application/_menu.html.slim b/app/views/cmtool/application/_menu.html.slim index 75960cc..3d75e0a 100644 --- a/app/views/cmtool/application/_menu.html.slim +++ b/app/views/cmtool/application/_menu.html.slim @@ -23,6 +23,8 @@ li[ class=(controller_name == child_item.controller_name ? 'active' : '') ] = link_to child_item.title, child_item.path - elsif menu_item.resource_link? li[ class=(menu_item.controller_name == controller_name ? 'active' : '')]= link_to menu_item.title, menu_item.path + - elsif menu_item.engine_link? + li= link_to menu_item.title, menu_item.path .btn-group.pull-right a.btn.dropdown-toggle[data-toggle="dropdown" href="#"] i.icon-user diff --git a/lib/cmtool/menu.rb b/lib/cmtool/menu.rb index a79e962..3f903a2 100644 --- a/lib/cmtool/menu.rb +++ b/lib/cmtool/menu.rb @@ -48,6 +48,15 @@ module Cmtool return false if args.first.to_s.last == '?' @register.send(*args) end + + def engine_link? + false + end + + def resource_link? + false + end + end ## @@ -79,6 +88,33 @@ module Cmtool end end + ## + # Engine link, links to an engine. Root by default + ## + class EngineLink < ElementBase + attr_reader :engine + def initialize(engine, options = {}) + @engine, @options = engine, options + end + + def engine_link? + true + end + + + def title + options[:title] || engine.name.split('::').first + end + + def path + case options[:path] + when Symbol then engine.routes.url_helpers.send(:"#{options[:path]}_path") + when String then options[:path] + else engine.routes.url_helpers.root_path + end + end + end + ## # Resource link, indicates a link to standard resource. The first argument should ## @@ -143,6 +179,11 @@ module Cmtool def resource_link(resource, options = {}) @items << ResourceLink.new(resource, options) end + + def engine_link(engine, options = {}) + @items << EngineLink.new(resource, options) + end + def resource_links @items.select{|i| i.is_a?(ResourceLink)} end diff --git a/spec/cmtool_menu/engine_link_spec.rb b/spec/cmtool_menu/engine_link_spec.rb new file mode 100644 index 0000000..8b4952b --- /dev/null +++ b/spec/cmtool_menu/engine_link_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe Cmtool::Menu::EngineLink do + let(:options) { Hash.new } + let(:link){ Cmtool::Menu::EngineLink.new(Cmtool::Engine, options)} + subject{ link } + + its(:title) { should == 'Cmtool' } + + it 'Should change title when given as option' do + options[:title] = 'Engine Title' + link.title.should == 'Engine Title' + end + + it "should change the path to internal path when given as symbol option" do + options[:path] = :faqs + link.path.should == '/cmtool/faqs' + end + + it "should directly return a path specified as a string in the options" do + options[:path] = 'http://www.google.com/' + link.path.should == 'http://www.google.com/' + end +end