title: Determine if method is called from a view or controller
body: <p>in my Application controller I have a method to check if the current user is authorized for the action. I use this method both from other controllers and from views. How can I determine if the method is called from a view or a controller? </p>
  
  <p>This is needed because the response is different (show alert and redirect when from controller, hide link when from view). Probably I'm doing something fundamentally wrong here and I should maybe solve it completely different?</p>
  
  <p>Application controller:</p>
  
  <pre><code>  helper_method :is_allowed
  
      # Check if current user is allowed to perform action
    def is_allowed(required_level)
      # Required level is level and all levels above (1=high, 3=low)
      # level 1: user
      # level 2: company_admin
      # level 3: admin
      case required_level
          when 'company_admin'
              unless current_user.role == 'company_admin' || current_user.role == 'admin'
                flash[:alert] = I18n.t(:not_allowed)
                redirect_to root_path
              end
          when 'admin'
              unless current_user.role == 'admin'
                flash[:alert] = I18n.t(:not_allowed)
                redirect_to root_path
              end
          end
    end
  </code></pre>
  
  <p>Call from other controller (this works):</p>
  
  <pre><code>  before_filter :only =&gt; [:destroy] do |c| c.is_allowed 'company_admin' end
  </code></pre>
  
  <p>Call from view (does work, but should not redirect, but only hide link):</p>
  
  <pre><code>&lt;% if is_allowed('company_admin') %&gt;
    &lt;td&gt;&lt;%= link_to I18n.t(:delete), relation, :method =&gt; :delete, :confirm =&gt; I18n.t(:sure) %&gt;&lt;/td&gt;
  &lt;% end %&gt;
  </code></pre>