Commit aa0c4b77b60acfc85d99e9eacaff25e34b136529

Authored by Robert Speicher
1 parent afc4a754

Add current_action? helper

app/helpers/application_helper.rb
1 1 require 'digest/md5'
  2 +
2 3 module ApplicationHelper
3 4  
4 5 # Check if a particular controller is the current one
... ... @@ -15,6 +16,20 @@ module ApplicationHelper
15 16 args.any? { |v| v.to_s.downcase == controller.controller_name }
16 17 end
17 18  
  19 + # Check if a partcular action is the current one
  20 + #
  21 + # args - One or more action names to check
  22 + #
  23 + # Examples
  24 + #
  25 + # # On Projects#new
  26 + # current_action?(:new) # => true
  27 + # current_action?(:create) # => false
  28 + # current_action?(:new, :create) # => true
  29 + def current_action?(*args)
  30 + args.any? { |v| v.to_s.downcase == action_name }
  31 + end
  32 +
18 33 def gravatar_icon(user_email = '', size = 40)
19 34 if Gitlab.config.disable_gravatar? || user_email.blank?
20 35 'no_avatar.png'
... ...
spec/helpers/application_helper_spec.rb
... ... @@ -20,6 +20,25 @@ describe ApplicationHelper do
20 20 end
21 21 end
22 22  
  23 + describe 'current_action?' do
  24 + before do
  25 + stub!(:action_name).and_return('foo')
  26 + end
  27 +
  28 + it "returns true when action matches argument" do
  29 + current_action?(:foo).should be_true
  30 + end
  31 +
  32 + it "returns false when action does not match argument" do
  33 + current_action?(:bar).should_not be_true
  34 + end
  35 +
  36 + it "should take any number of arguments" do
  37 + current_action?(:baz, :bar).should_not be_true
  38 + current_action?(:baz, :bar, :foo).should be_true
  39 + end
  40 + end
  41 +
23 42 describe "gravatar_icon" do
24 43 let(:user_email) { 'user@email.com' }
25 44  
... ...