views:

26

answers:

1

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.

A: 

I don't believe this would work very well at all. In dev mode, it would probably behave, but in production the code doesn't get loaded from scratch with each request. That means the first request would load the proper module, but after that the next request might load a second module and then there would be conflict.

I don't have a good answer for you about how to avoid switch statements or if statements, but suspect that a class inheritance system would be the way to go instead of module inclusion.

Tilendor