From 3ad931ca9211d2ca0f345f97db00193ee5533dfd Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 25 Sep 2012 19:22:44 -0400 Subject: [PATCH] Add current_controller? helper method Simplifies some of the "active tab" checks we're doing --- app/helpers/application_helper.rb | 11 +++++++++++ spec/helpers/application_helper_spec.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d90fb32f30f..505f6067c59 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,17 @@ require 'digest/md5' module ApplicationHelper + # Check if a particular controller is the current one + # + # Examples + # + # # On TreeController + # current_controller?(:tree) # => true + # current_controller?(:commits) # => false + def current_controller?(name) + controller.controller_name == name.to_s.downcase + end + def gravatar_icon(user_email = '', size = 40) if Gitlab.config.disable_gravatar? || user_email.blank? 'no_avatar.png' diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 9a2df31479c..10250c93883 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,6 +1,20 @@ require 'spec_helper' describe ApplicationHelper do + describe 'current_controller?' do + before do + controller.stub!(:controller_name).and_return('foo') + end + + it "returns true when controller matches argument" do + current_controller?(:foo).should be_true + end + + it "returns false when controller does not match argument" do + current_controller?(:bar).should_not be_true + end + end + describe "gravatar_icon" do let(:user_email) { 'user@email.com' }