Implementing Role based Helpers

Posted by Cynics on Stack Overflow See other posts from Stack Overflow or by Cynics
Published on 2010-04-06T07:15:32Z Indexed on 2010/04/06 7:23 UTC
Read the original article Hit count: 324

Filed under:
|
|

So my question is how would you implement your handwritten Helpers based on the role of current user.

Would it be efficient to change the behaviour at request time? e.g. the Helper somehow figures out the role of user, and include the proper SubModule?

module ApplicationHelper
    module LoggedInHelper
        # Some functions
    end

    module GuestHelper
        # The Same functions
    end

    # If User is Guest then include GuestHelper
    # If User is LoggedIn then include LoggedInHelper

end

Is it efficient this way? is it rails way? I've got a whole bunch of function that act like this, and I don't want to wrap every single one of them in an if statement

def menu_actions
    if current_user.nil?
        # User is guest
        { "Log in" => link_to "Login", "/login" }
    else
        # User is Logged In
        { "Log out" => link_to "Logout", "/logout" }
    end
end

Thank you for your time and thoughts.

© Stack Overflow or respective owner

Related posts about rails

Related posts about helpers