amolnpujari

Keep it simple and standardized

DRYing if @current_user.nil?

with 13 comments

While working on one of rails applications, as I usually tend to check whether user is logged in, specially in views as follows

<% if @current_user.nil? %>
  <a href='url to login page'>
<% else %>
  <a href='some url where logged in user have access'>
<% end %>

And over some period I found this pattern repeating everywhere.

I thought of DRYing it, I wanted to have a kind of before_filter for every link that appear on the page, every link should go for login if that page or feature require user entity. and this should be handled by a common thing, similar to before_filter: login_require, on server side. I intend to achieve similar in views. A while back when I read up about html5 custom data attribute, and unobtrusive javascript etc, referring to them, I got one idea as described below, though it is not something that I am making use of custom data attribute exactly, but I found this idea useful and working. May be someone would have good suggestions here for me.

I will put any link which require user login as follows

<a href="/feature/page/" login-require >Feature</a>

And then I will have my after_load.js which will have the following event binding

jQuery('a[login-require]').bind('click', function() {
  <% if @current_user.nil? %>
    openOverlay('overlay-login');
    return false;
  <% else %>
    return true;
  <% end %>
});
About these ads

Written by amolpujari

January 7, 2012 at 9:49 am

13 Responses

Subscribe to comments with RSS.

  1. You should use a HTML 5 compatible attribute, like data-login-required=”true”

    Felipe Coury

    January 7, 2012 at 11:09 am

    • Agree, I will do it. But given me these two choices, I would prefer just to have “login-require” for reasons like it is more clear and simple, straight forward… and no need to use words like data, true. And I thought ‘something=”true”‘ is same as ‘something’.

      amolnpujari

      January 7, 2012 at 11:36 am

      • If you don’t use the ‘data’ this HTML is invalid…

        kevin

        January 7, 2012 at 4:10 pm

  2. Why not use a simple css class and make it also compatible with HTML4/XHTML?

    jQuery(‘a.login-required’).bind

    Sander

    January 7, 2012 at 2:37 pm

  3. what happen if the user disable javascript in his browser?

    aditto

    January 7, 2012 at 7:07 pm

    • User will not be able to use 99% of sites currently in internet :)

      Stanislav O. Pogrebnyak

      January 7, 2012 at 7:52 pm

    • link will work as usual, and will hit the server, server side check will take care of this

      before_filter: login_require

      amolnpujari

      January 7, 2012 at 8:04 pm

      • If the server will handle it anyway, you’re not really DRYing anything. In fact, you’re doing the opposite. You’re CRYing (Code & Repeat Yourself :D).

        Mango

        January 10, 2012 at 10:34 am

  4. Another suggestion: Unconditionally link to the restricted page, and simply redirect anonymous users to the sign-in page when they try to access a restricted page. This of course only works if you have the same link text for all users.

    Jo Liss

    January 7, 2012 at 8:00 pm

  5. I wanted to avoid server trip ….

    amolnpujari

    January 7, 2012 at 8:11 pm

    • Hm, I see. I’m not sure if it’s worth the extra hassle, to be honest. Redirects are very very fast, since no HTML is loaded. Google for instance has lots of redirects every time you log in. If it’s good enough for them, it’s good enough for me. So I guess I’d just avoid doing the `if current_user` thing in the first place.

      Jo Liss

      January 7, 2012 at 9:53 pm

  6. A nice semantic approach could be to use the rel attribute like this:

    Feature

    The rel attribute is intended to contain information about the kind of relationship to the link’s destination (http://www.w3.org/TR/html4/struct/links.html#h-12.1.2). So in my opinion this is a good use case for it.

    Just modify your jQuery selector to

    jQuery(‘a[rel~=required]‘)

    and your solution wil work with this approach!

    Clemens Helm

    January 9, 2012 at 4:12 am

    • Sorry, my code was interpreted as html. Once again:

      <a href=”/feature/page/” rel=”restricted”>Feature</a>

      Clemens Helm

      January 9, 2012 at 4:25 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s